]> git.mxchange.org Git - friendica.git/blobdiff - include/bb2diaspora.php
Diaspora: The HTML paragraph element was treated wrong when importing items from...
[friendica.git] / include / bb2diaspora.php
index 436412dbd487bc700465314ab812d5670f417df8..5b990bf0dfd40ed61fe71835d7b2ded5c3844957 100644 (file)
@@ -8,65 +8,6 @@ require_once("include/bbcode.php");
 require_once("include/markdownify/markdownify.php");
 
 
-function get_bb_tag_pos($s, $name, $occurance = 1) {
-
-       if($occurance < 1)
-               $occurance = 1;
-
-       $start_open = -1;
-       for($i = 1; $i <= $occurance; $i++) {
-               if( $start_open !== false)
-                       $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
-       }
-
-       if( $start_open === false)
-               return false;
-
-       $start_equal = strpos($s, '=', $start_open);
-       $start_close = strpos($s, ']', $start_open);
-
-       if( $start_close === false)
-               return false;
-
-       $start_close++;
-
-       $end_open = strpos($s, '[/' . $name . ']', $start_close);
-
-       if( $end_open === false)
-               return false;
-
-       $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
-                     'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
-       if( $start_equal !== false)
-               $res['start']['equal'] = $start_equal + 1;
-
-       return $res;
-}
-
-function bb_tag_preg_replace($pattern, $replace, $name, $s) {
-
-       $string = $s;
-
-       $occurance = 1;
-       $pos = get_bb_tag_pos($string, $name, $occurance);
-       while($pos !== false && $occurance < 1000) {
-
-               $start = substr($string, 0, $pos['start']['open']);
-               $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
-               $end = substr($string, $pos['end']['close']);
-               if($end === false)
-                       $end = '';
-
-               $subject = preg_replace($pattern, $replace, $subject);
-               $string = $start . $subject . $end;
-
-               $occurance++;
-               $pos = get_bb_tag_pos($string, $name, $occurance);
-       }
-
-       return $string;
-}
-
 // we don't want to support a bbcode specific markdown interpreter
 // and the markdown library we have is pretty good, but provides HTML output.
 // So we'll use that to convert to HTML, then convert the HTML back to bbcode,
@@ -86,7 +27,9 @@ function diaspora2bb($s) {
        $s = str_replace("\r","",$s);
 
        // <br/> is invalid. Replace it with the valid expression
-       $s = str_replace("<br/>","<br />",$s);
+       logger("diaspora2bb: 1: ".$s);
+       $s = str_replace(array("<br/>", "</p>", "<p>"),array("<br />", "<br />", "<br />"),$s);
+       logger("diaspora2bb: 2: ".$s);
 
        $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
 
@@ -113,7 +56,7 @@ function diaspora2bb($s) {
        //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
        $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]','url',$s);
        $s = bb_tag_preg_replace("/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism",'[youtube]$1[/youtube]','url',$s);
-       $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/     \/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]','url',$s);
+       $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]','url',$s);
        $s = bb_tag_preg_replace("/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism",'[vimeo]$1[/vimeo]','url',$s);
        // remove duplicate adjacent code tags
        $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
@@ -190,19 +133,63 @@ function diaspora_ol($s) {
 }
 
 
-function bb2diaspora($Text,$preserve_nl = false) {
+function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
+
+       // Since Diaspora is creating a summary for links, this function removes them before posting
+       if ($fordiaspora)
+               $Text = bb_remove_share_information($Text);
 
        // Re-enabling the converter again.
        // The bbcode parser now handles youtube-links (and the other stuff) correctly.
        // Additionally the html code is now fixed so that lists are now working.
 
+       /**
+        * Transform #tags, strip off the [url] and replace spaces with underscore
+        */
+       $Text = preg_replace_callback('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', create_function('$match',
+               'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
+       ), $Text);
+
+
+       // Converting images with size parameters to simple images. Markdown doesn't know it.
+       $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
+
+       // the following was added on 10-January-2012 due to an inability of Diaspora's
+       // new javascript markdown processor to handle links with images as the link "text"
+       // It is not optimal and may be removed if this ability is restored in the future
+       //if ($fordiaspora)
+       //      $Text = preg_replace("/\[url\=([^\[\]]*)\]\s*\[img\](.*?)\[\/img\]\s*\[\/url\]/ism",
+       //                              "[url]$1[/url]\n[img]$2[/img]", $Text);
+
+       // Remove the avatar picture since that isn't looking good on the other side
+       //$Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text);
+
        // Convert it to HTML - don't try oembed
-       $Text = bbcode($Text, $preserve_nl, false);
+       if ($fordiaspora)
+               $Text = bbcode($Text, $preserve_nl, false, 3);
+       else
+               $Text = bbcode($Text, $preserve_nl, false, 4);
 
        // Now convert HTML to Markdown
        $md = new Markdownify(false, false, false);
        $Text = $md->parseString($Text);
 
+       // The Markdownify converter converts underscores '_' in URLs to '\_', which
+       // messes up the URL. Manually fix these
+       $count = 1;
+       $pos = bb_find_open_close($Text, '[', ']', $count);
+       while($pos !== false) {
+               $start = substr($Text, 0, $pos['start']);
+               $subject = substr($Text, $pos['start'], $pos['end'] - $pos['start'] + 1);
+               $end = substr($Text, $pos['end'] + 1);
+
+               $subject = str_replace('\_', '_', $subject);
+               $Text = $start . $subject . $end;
+
+               $count++;
+               $pos = bb_find_open_close($Text, '[', ']', $count);
+       }
+
        // If the text going into bbcode() has a plain URL in it, i.e.
        // with no [url] tags around it, it will come out of parseString()
        // looking like: <http://url.com>, which gets removed by strip_tags().