]> git.mxchange.org Git - friendica.git/blobdiff - include/bb2diaspora.php
Merge pull request #3281 from annando/issue-3206-2957
[friendica.git] / include / bb2diaspora.php
index 53e9ebfd04451a958bf6b226787e5ce0e7b5d2aa..03eff5a6b73a5bd2c398d11fe8c4d448110a1497 100644 (file)
@@ -7,6 +7,27 @@ require_once("include/html2bbcode.php");
 require_once("include/bbcode.php");
 require_once("library/html-to-markdown/HTML_To_Markdown.php");
 
+/**
+ * @brief Callback function to replace a Diaspora style mention in a mention for Friendica
+ *
+ * @param array $match Matching values for the callback
+ * @return string Replaced mention
+ */
+function diaspora_mention2bb($match) {
+       if ($match[2] == '') {
+               return;
+       }
+
+       $data = get_contact_details_by_addr($match[2]);
+
+       $name = $match[1];
+
+       if ($name == '') {
+               $name = $data['name'];
+       }
+
+       return '@[url='.$data['url'].']'.$name.'[/url]';
+}
 
 // we don't want to support a bbcode specific markdown interpreter
 // and the markdown library we have is pretty good, but provides HTML output.
@@ -18,9 +39,9 @@ function diaspora2bb($s) {
        $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
 
        // Handles single newlines
-       $s = str_replace("\r", '<br>', $s);
-
+       $s = str_replace("\r\n", "\n", $s);
        $s = str_replace("\n", " \n", $s);
+       $s = str_replace("\r", " \n", $s);
 
        // Replace lonely stars in lines not starting with it with literal stars
        $s = preg_replace('/^([^\*]+)\*([^\*]*)$/im', '$1\*$2', $s);
@@ -33,19 +54,11 @@ function diaspora2bb($s) {
 
        $s = Markdown($s);
 
-       $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/', '@[url=https://$3/u/$2]$1[/url]', $s);
+       $regexp = "/@\{(?:([^\}]+?); )?([^\} ]+)\}/";
+       $s = preg_replace_callback($regexp, 'diaspora_mention2bb', $s);
 
        $s = str_replace('&#35;', '#', $s);
 
-       $search = array(" \n", "\n ");
-       $replace = array("\n", "\n");
-       do {
-               $oldtext = $s;
-               $s = str_replace($search, $replace, $s);
-       } while ($oldtext != $s);
-
-       $s = str_replace("\n\n", '<br>', $s);
-
        $s = html2bbcode($s);
 
        // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
@@ -73,7 +86,7 @@ function diaspora2bb($s) {
  * @brief Callback function to replace a Friendica style mention in a mention for Diaspora
  *
  * @param array $match Matching values for the callback
- * @return text Replaced mention
+ * @return string Replaced mention
  */
 function diaspora_mentions($match) {
 
@@ -156,7 +169,7 @@ function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
 
        if ($fordiaspora) {
                $URLSearchString = "^\[\]";
-               $Text = preg_replace_callback("/([@]\[(.*?)\])\(([$URLSearchString]*)\)/ism", 'diaspora_mentions', $Text);
+               $Text = preg_replace_callback("/([@]\[(.*?)\])\(([$URLSearchString]*?)\)/ism", 'diaspora_mentions', $Text);
        }
 
        call_hooks('bb2diaspora',$Text);