From 3968e77f9e15c149d8cafa1dac37411777bd58bd Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 17 Jun 2012 19:49:05 +0200 Subject: New function to convert bbcode to markdown. --- include/bb2diaspora.php | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index d86ba4543..0617ecc5f 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -1,10 +1,11 @@ ", ">", $Text); // If we find any event code, turn it into an event. - // After we're finished processing the bbcode we'll + // After we're finished processing the bbcode we'll // replace all of the event code with a reformatted version. - if($preserve_nl) $Text = str_replace(array("\n","\r"), array('',''),$Text); + // Convert it to HTML + $Text = bbcode($Text); + + // Now convert HTML to Markdown + $md = new Markdownify(false, false, false); + $Text = $md->parseString($Text); + + // Remove all unconverted tags + $Text = strip_tags($Text); + +/* // Set up the parameters for a URL search string $URLSearchString = "^\[\]"; // Set up the parameters for a MAIL search string @@ -98,7 +109,7 @@ function bb2diaspora($Text,$preserve_nl = false) { // 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 - $Text = preg_replace("/\[url\=([$URLSearchString]*)\]\[img\](.*?)\[\/img\]\[\/url\]/ism", + $Text = preg_replace("/\[url\=([$URLSearchString]*)\]\[img\](.*?)\[\/img\]\[\/url\]/ism", '![' . t('image/photo') . '](' . '$2' . ')' . "\n" . '[' . t('link') . '](' . '$1' . ')', $Text); $Text = preg_replace("/\[bookmark\]([$URLSearchString]*)\[\/bookmark\]/ism", '[$1]($1)', $Text); @@ -115,7 +126,7 @@ function bb2diaspora($Text,$preserve_nl = false) { // Perform MAIL Search $Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '[$1](mailto:$1)', $Text); $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '[$2](mailto:$1)', $Text); - + $Text = str_replace('*', '\\*', $Text); $Text = str_replace('_', '\\_', $Text); @@ -158,7 +169,7 @@ function bb2diaspora($Text,$preserve_nl = false) { // $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/s", '$1
' ,$Text); // $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/s", '$1
' ,$Text); - + // $Text = str_replace("[*]", "
  • ", $Text); // Check for font change text @@ -174,10 +185,10 @@ function bb2diaspora($Text,$preserve_nl = false) { // Declare the format for [quote] layout - // $QuoteLayout = '
    $1
    '; + // $QuoteLayout = '
    $1
    '; // Check for [quote] text $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/is",">$1\n\n", $Text); - + // Images // html5 video and audio @@ -187,7 +198,7 @@ function bb2diaspora($Text,$preserve_nl = false) { $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/", '$1', $Text); // $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/", '', $Text); - + // [img=widthxheight]image source[/img] // $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/", '', $Text); @@ -211,7 +222,7 @@ function bb2diaspora($Text,$preserve_nl = false) { if(x($ev,'desc') && x($ev,'start')) { $sub = format_event_diaspora($ev); - + $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/is",$sub,$Text); $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/is",'',$Text); $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/is",'',$Text); @@ -222,7 +233,7 @@ function bb2diaspora($Text,$preserve_nl = false) { $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text); $Text = preg_replace_callback('/\[(.*?)\]\((.*?)\)/ism','unescape_underscores_in_links',$Text); - +*/ call_hooks('bb2diaspora',$Text); return $Text; -- cgit v1.2.3 From cc56f33b80bdd871b8a1245e3c4bd3a31ed49330 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Mon, 18 Jun 2012 01:29:08 +0200 Subject: New option in the bbcode parser to disable oembed. --- include/bb2diaspora.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index 0617ecc5f..7b4caec46 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -70,6 +70,17 @@ function stripdcode_br_cb($s) { function bb2diaspora($Text,$preserve_nl = false) { + // Convert it to HTML - don't try oembed + $Text = bbcode($Text, $preserve_nl, false); + + // Now convert HTML to Markdown + $md = new Markdownify(false, false, false); + $Text = $md->parseString($Text); + + // Remove all unconverted tags + $Text = strip_tags($Text); + +/* $ev = bbtoevent($Text); // Replace any html brackets with HTML Entities to prevent executing HTML or script @@ -85,17 +96,7 @@ function bb2diaspora($Text,$preserve_nl = false) { if($preserve_nl) $Text = str_replace(array("\n","\r"), array('',''),$Text); - // Convert it to HTML - $Text = bbcode($Text); - // Now convert HTML to Markdown - $md = new Markdownify(false, false, false); - $Text = $md->parseString($Text); - - // Remove all unconverted tags - $Text = strip_tags($Text); - -/* // Set up the parameters for a URL search string $URLSearchString = "^\[\]"; // Set up the parameters for a MAIL search string -- cgit v1.2.3 From 7b219649a41198c3bc6f289b68ddc0cfd8b88c63 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Thu, 21 Jun 2012 21:31:32 -0600 Subject: expand support for BB code-to-Diaspora conversion --- include/bb2diaspora.php | 62 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 20 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index d86ba4543..d7324a4ad 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -66,6 +66,20 @@ function stripdcode_br_cb($s) { } +function diaspora_ul($s) { + return str_replace('[\\*]', "* ", $s[1]); +} + + +function diaspora_ol($s) { + // A hack: Diaspora will create a properly-numbered ordered list even + // if you use '1.' for each element of the list, like: + // 1. First element + // 1. Second element + // 1. Third element + return str_replace('[\\*]', "1. ", $s[1]); +} + function bb2diaspora($Text,$preserve_nl = false) { @@ -124,38 +138,45 @@ function bb2diaspora($Text,$preserve_nl = false) { // Check for bold text $Text = preg_replace("(\[b\](.*?)\[\/b\])is",'**$1**',$Text); - // Check for Italics text + // Check for italics text $Text = preg_replace("(\[i\](.*?)\[\/i\])is",'_$1_',$Text); - // Check for Underline text -// $Text = preg_replace("(\[u\](.*?)\[\/u\])is",'$1',$Text); + // Check for underline text + // Replace with italics since Diaspora doesn't have underline + $Text = preg_replace("(\[u\](.*?)\[\/u\])is",'_$1_',$Text); // Check for strike-through text -// $Text = preg_replace("(\[s\](.*?)\[\/s\])is",'$1',$Text); + $Text = preg_replace("(\[s\](.*?)\[\/s\])is",'**[strike]**$1**[/strike]**',$Text); // Check for over-line text // $Text = preg_replace("(\[o\](.*?)\[\/o\])is",'$1',$Text); // Check for colored text -// $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])is","$2",$Text); + // Remove color since Diaspora doesn't support it + $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])is","$2",$Text); // Check for sized text -// $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])is","$2",$Text); + // Remove it since Diaspora doesn't support sizes very well + $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])is","$2",$Text); // Check for list text -// $Text = preg_replace("/\[list\](.*?)\[\/list\]/is", '
      $1
    ' ,$Text); -// $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/is", '
      $1
    ' ,$Text); -// $Text = preg_replace("/\[list=i\](.*?)\[\/list\]/s",'
      $1
    ' ,$Text); -// $Text = preg_replace("/\[list=I\](.*?)\[\/list\]/s", '
      $1
    ' ,$Text); -// $Text = preg_replace("/\[list=a\](.*?)\[\/list\]/s", '
      $1
    ' ,$Text); -// $Text = preg_replace("/\[list=A\](.*?)\[\/list\]/s", '
      $1
    ' ,$Text); + $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text); + $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text); + $Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); // $Text = preg_replace("/\[li\](.*?)\[\/li\]/s", '
  • $1
  • ' ,$Text); -// $Text = preg_replace("/\[td\](.*?)\[\/td\]/s", '$1' ,$Text); -// $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/s", '$1' ,$Text); -// $Text = preg_replace("/\[table\](.*?)\[\/table\]/s", '$1
    ' ,$Text); + // Just get rid of table tags since Diaspora doesn't support tables + $Text = preg_replace("/\[th\](.*?)\[\/th\]/s", '$1' ,$Text); + $Text = preg_replace("/\[td\](.*?)\[\/td\]/s", '$1' ,$Text); + $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/s", '$1' ,$Text); + $Text = preg_replace("/\[table\](.*?)\[\/table\]/s", '$1' ,$Text); -// $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/s", '$1
    ' ,$Text); + $Text = preg_replace("/\[table border=(.*?)\](.*?)\[\/table\]/s", '$2' ,$Text); // $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/s", '$1
    ' ,$Text); @@ -165,7 +186,7 @@ function bb2diaspora($Text,$preserve_nl = false) { // $Text = preg_replace("(\[font=(.*?)\](.*?)\[\/font\])","$2",$Text); - $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/is",'stripdcode_br_cb',$Text); + $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/is",'stripdcode_br_cb',$Text); // Check for [code] text $Text = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/is","\t$2\n", $Text); @@ -177,6 +198,7 @@ function bb2diaspora($Text,$preserve_nl = false) { // $QuoteLayout = '
    $1
    '; // Check for [quote] text $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/is",">$1\n\n", $Text); + $Text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/is",">$2\n\n", $Text); // Images @@ -191,9 +213,9 @@ function bb2diaspora($Text,$preserve_nl = false) { // [img=widthxheight]image source[/img] // $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/", '', $Text); - $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); - $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); - $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); + $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); + $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); + $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'http://www.youtube.com/watch?v=$1',$Text); $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", 'http://www.youtube.com/watch?v=$1', $Text); $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'http://vimeo.com/$1',$Text); -- cgit v1.2.3 From 9c9e72704215aad390d440825a6fa7ba0f468e7e Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Fri, 22 Jun 2012 18:38:32 -0600 Subject: cleanup lists and more comment signature stuff --- include/bb2diaspora.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index d7324a4ad..6f8f92514 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -67,7 +67,9 @@ function stripdcode_br_cb($s) { function diaspora_ul($s) { - return str_replace('[\\*]', "* ", $s[1]); + // Replace "[\\*]" followed by any number (including zero) of + // spaces by "* " to match Diaspora's list format + return preg_replace("/\[\\\\\*\]( *)/", "* ", $s[1]); } @@ -77,7 +79,7 @@ function diaspora_ol($s) { // 1. First element // 1. Second element // 1. Third element - return str_replace('[\\*]', "1. ", $s[1]); + return preg_replace("/\[\\\\\*\]( *)/", "1. ", $s[1]); } @@ -98,6 +100,11 @@ function bb2diaspora($Text,$preserve_nl = false) { if($preserve_nl) $Text = str_replace(array("\n","\r"), array('',''),$Text); + else + // Remove the "return" character, as Diaspora uses only the "newline" + // character, so having the "return" character can cause signature + // failures + $Text = str_replace("\r", "", $Text); // Set up the parameters for a URL search string $URLSearchString = "^\[\]"; @@ -244,6 +251,10 @@ function bb2diaspora($Text,$preserve_nl = false) { $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text); $Text = preg_replace_callback('/\[(.*?)\]\((.*?)\)/ism','unescape_underscores_in_links',$Text); + + // Remove any leading or trailing whitespace, as this will mess up + // the Diaspora signature verification and cause the item to disappear + $Text = trim($Text); call_hooks('bb2diaspora',$Text); -- cgit v1.2.3 From 4ef33ac6ebbf7418742a9ee7a4a549808754eb1f Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sat, 23 Jun 2012 19:57:59 -0600 Subject: fix BB-to-Diaspora list formatting --- include/bb2diaspora.php | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index 96cc735bd..d509a2e31 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -68,9 +68,14 @@ function stripdcode_br_cb($s) { function diaspora_ul($s) { - // Replace "[\\*]" followed by any number (including zero) of + // Replace "[*]" followed by any number (including zero) of // spaces by "* " to match Diaspora's list format - return preg_replace("/\[\\\\\*\]( *)/", "* ", $s[1]); + if( strpos($s[0], "[list]") === 0 ) + return '
      ' . preg_replace("/\[\*\]( *)/", "* ", $s[1]) . '
    '; + elseif( strpos($s[0], "[ul]") === 0 ) + return '
      ' . preg_replace("/\[\*\]( *)/", "* ", $s[1]) . '
    '; + else + return $s[0]; } @@ -80,12 +85,47 @@ function diaspora_ol($s) { // 1. First element // 1. Second element // 1. Third element - return preg_replace("/\[\\\\\*\]( *)/", "1. ", $s[1]); + if( strpos($s[0], "[list=1]") === 0 ) + return '
      ' . preg_replace("/\[\*\]( *)/", "1. ", $s[1]) . '
    '; + elseif( strpos($s[0], "[list=i]") === 0 ) + return '
      ' . preg_replace("/\[\*\]( *)/", "1. ", $s[1]) . '
    '; + elseif( strpos($s[0], "[list=I]") === 0 ) + return '
      ' . preg_replace("/\[\*\]( *)/", "1. ", $s[1]) . '
    '; + elseif( strpos($s[0], "[list=a]") === 0 ) + return '
      ' . preg_replace("/\[\*\]( *)/", "1. ", $s[1]) . '
    '; + elseif( strpos($s[0], "[list=A]") === 0 ) + return '
      ' . preg_replace("/\[\*\]( *)/", "1. ", $s[1]) . '
    '; + elseif( strpos($s[0], "[ol]") === 0 ) + return '
      ' . preg_replace("/\[\*\]( *)/", "1. ", $s[1]) . '
    '; + else + return $s[0]; } function bb2diaspora($Text,$preserve_nl = false) { + // bbcode() will convert "[*]" into "
  • " with no closing "
  • " + // Markdownify() is unable to handle these, as it makes each new + // "
  • " into a deeper nested element until it crashes. So pre-format + // the lists as Diaspora lists before sending the $Text to bbcode() + // + // Note that regular expressions are really not suitable for parsing + // text with opening and closing tags, so nested lists may make things + // wonky + $endlessloop = 0; + while ((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false) && + (strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false) && + (strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false) && (++$endlessloop < 20)) { + $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text); + $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text); + $Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); + } + // Convert it to HTML - don't try oembed $Text = bbcode($Text, $preserve_nl, false); -- cgit v1.2.3 From 1f968396749f8cac31aaeee770ae7fc5aff48f38 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sat, 23 Jun 2012 20:01:29 -0600 Subject: small optimization --- include/bb2diaspora.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index d509a2e31..75ba57dff 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -113,18 +113,16 @@ function bb2diaspora($Text,$preserve_nl = false) { // text with opening and closing tags, so nested lists may make things // wonky $endlessloop = 0; - while ((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false) && - (strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false) && - (strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false) && (++$endlessloop < 20)) { + while ((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false) && (++$endlessloop < 20)) { $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text); - $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text); $Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); - $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); } + $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text); + $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); // Convert it to HTML - don't try oembed $Text = bbcode($Text, $preserve_nl, false); -- cgit v1.2.3 From 5d2eed35177da3debacc6db6db2f7a74bc4b64c7 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sat, 23 Jun 2012 22:02:08 -0600 Subject: allow bare URLS to make it through to Diaspora --- include/bb2diaspora.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index 75ba57dff..ac693127b 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -131,6 +131,12 @@ function bb2diaspora($Text,$preserve_nl = false) { $md = new Markdownify(false, false, false); $Text = $md->parseString($Text); + // 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: , which gets removed by strip_tags(). + // So take off the angle brackets of any such URL + $Text = preg_replace("//is", "http$1", $Text); + // Remove all unconverted tags $Text = strip_tags($Text); -- cgit v1.2.3 From 4293de75546249256e438b87f869a99d864117d6 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sun, 24 Jun 2012 09:37:31 -0600 Subject: allow nested [ol] and [ul] lists too --- include/bb2diaspora.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index ac693127b..a0d114a37 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -109,20 +109,22 @@ function bb2diaspora($Text,$preserve_nl = false) { // "
  • " into a deeper nested element until it crashes. So pre-format // the lists as Diaspora lists before sending the $Text to bbcode() // - // Note that regular expressions are really not suitable for parsing - // text with opening and closing tags, so nested lists may make things - // wonky + // Note that to get nested lists to work for Diaspora, we would need + // to define the closing tag for the list elements. So nested lists + // are going to be flattened out in Diaspora for now $endlessloop = 0; - while ((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false) && (++$endlessloop < 20)) { + while ((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false) + (strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false) && + (strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false) && (++$endlessloop < 20)) { $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text); $Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text); + $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); } - $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text); - $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); // Convert it to HTML - don't try oembed $Text = bbcode($Text, $preserve_nl, false); -- cgit v1.2.3 From b044cd922d389b5bb9367ce1004d00a711562cf9 Mon Sep 17 00:00:00 2001 From: friendica Date: Mon, 25 Jun 2012 16:03:46 -0700 Subject: typos in bbcode, add service class restrictions to jot uploads --- include/bb2diaspora.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index a0d114a37..25edb28d7 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -113,7 +113,7 @@ function bb2diaspora($Text,$preserve_nl = false) { // to define the closing tag for the list elements. So nested lists // are going to be flattened out in Diaspora for now $endlessloop = 0; - while ((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false) + while ((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false) && (strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false) && (strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false) && (++$endlessloop < 20)) { $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text); -- cgit v1.2.3 From cd9ce0ecb3162a9d1a24cf1a1398891aa035becb Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Wed, 27 Jun 2012 21:21:26 -0600 Subject: fix logic for nested lists--should be OR, not AND --- include/bb2diaspora.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index 25edb28d7..77a5f5c2a 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -113,9 +113,9 @@ function bb2diaspora($Text,$preserve_nl = false) { // to define the closing tag for the list elements. So nested lists // are going to be flattened out in Diaspora for now $endlessloop = 0; - while ((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false) && - (strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false) && - (strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false) && (++$endlessloop < 20)) { + while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) || + ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) || + ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false))) && (++$endlessloop < 20)) { $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text); $Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text); $Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text); -- cgit v1.2.3 From 315784444af3145e6afba210731f0104d21f9db3 Mon Sep 17 00:00:00 2001 From: friendica Date: Mon, 2 Jul 2012 01:11:19 -0700 Subject: try and get unadulterated bb2md2html2bb for simple stuff, then we'll work up to harder stuff --- include/bb2diaspora.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index 77a5f5c2a..f17e7b549 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -38,8 +38,8 @@ function diaspora2bb($s) { $s = Markdown($s); $s = str_replace('#','#',$s); - - $s = str_replace("\n",'
    ',$s); +// we seem to have double linebreaks +// $s = str_replace("\n",'
    ',$s); $s = html2bbcode($s); // $s = str_replace('*','*',$s); -- cgit v1.2.3 From 0990269555acc46abe646a24ee8330b9d2a8b124 Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 4 Jul 2012 19:12:09 -0700 Subject: restore translation of bbcode videos, events, bookmarks, etc. to diaspora --- include/bb2diaspora.php | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index f17e7b549..d756b922d 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -126,23 +126,39 @@ function bb2diaspora($Text,$preserve_nl = false) { $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); } + + +////////////////////// +// An attempt was made to convert bbcode to html and then to markdown +// consisting of the following lines. +// I'm undoing this as we have a lot of bbcode constructs which +// were simply getting lost, for instance bookmark, vimeo, video, youtube, events, etc. +// We can try this again, but need a very good test sequence to verify +// all the major bbcode constructs that we use are getting through. +////////////////////// + // Convert it to HTML - don't try oembed - $Text = bbcode($Text, $preserve_nl, false); +// $Text = bbcode($Text, $preserve_nl, false); // Now convert HTML to Markdown - $md = new Markdownify(false, false, false); - $Text = $md->parseString($Text); +// $md = new Markdownify(false, false, false); +// $Text = $md->parseString($Text); // 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: , which gets removed by strip_tags(). // So take off the angle brackets of any such URL - $Text = preg_replace("//is", "http$1", $Text); +// $Text = preg_replace("//is", "http$1", $Text); // Remove all unconverted tags - $Text = strip_tags($Text); +// $Text = strip_tags($Text); + +////// +// end of bb->html->md conversion attempt +////// + + -/* $ev = bbtoevent($Text); // Replace any html brackets with HTML Entities to prevent executing HTML or script @@ -299,8 +315,9 @@ function bb2diaspora($Text,$preserve_nl = false) { $sub = format_event_diaspora($ev); - $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/is",$sub,$Text); - $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/is",'',$Text); + $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/is",'',$Text); + $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/is",'',$Text); + $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/is",$sub,$Text); $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/is",'',$Text); $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/is",'',$Text); $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",'',$Text); @@ -309,7 +326,7 @@ function bb2diaspora($Text,$preserve_nl = false) { $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text); $Text = preg_replace_callback('/\[(.*?)\]\((.*?)\)/ism','unescape_underscores_in_links',$Text); -*/ + // Remove any leading or trailing whitespace, as this will mess up // the Diaspora signature verification and cause the item to disappear @@ -336,7 +353,7 @@ function format_event_diaspora($ev) { $o = 'Friendica event notification:' . "\n"; - $o .= '**' . bb2diaspora($ev['desc']) . '**' . "\n"; + $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) . '**' . "\n"; $o .= t('Starts:') . ' ' . '[' . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', -- cgit v1.2.3 From 360c35e438f4ecb6a60cefde8ab8b0e3f6f0c57f Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 4 Jul 2012 23:24:59 -0700 Subject: recycle symbol woes with multiple html entity encode/decode cycles going on --- include/bb2diaspora.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index d756b922d..c93b60d0d 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -44,6 +44,9 @@ function diaspora2bb($s) { $s = html2bbcode($s); // $s = str_replace('*','*',$s); + // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands + $s = str_replace('♲',html_entities_decode('♲',ENT_QUOTES,'UTF-8'),$s); + // Convert everything that looks like a link to a link $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s); -- cgit v1.2.3 From 041aeaf4a159e277b828d8a386c0968657afb723 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Thu, 5 Jul 2012 12:52:20 -0600 Subject: fix small bug in lists and revert Diaspora list processing to non-Markdownify version --- include/bb2diaspora.php | 70 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 19 deletions(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index c93b60d0d..75dadb84c 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -70,6 +70,12 @@ function stripdcode_br_cb($s) { } +////////////////////// +// The following "diaspora_ul" and "diaspora_ol" are only appropriate for the +// pre-Markdownify conversion. If Markdownify isn't used, use the non-Markdownify +// versions below +////////////////////// +/* function diaspora_ul($s) { // Replace "[*]" followed by any number (including zero) of // spaces by "* " to match Diaspora's list format @@ -103,10 +109,38 @@ function diaspora_ol($s) { else return $s[0]; } +*/ + +////////////////////// +// Non-Markdownify versions of "diaspora_ol" and "diaspora_ul" +////////////////////// +function diaspora_ul($s) { + // Replace "[\\*]" followed by any number (including zero) of + // spaces by "* " to match Diaspora's list format + return preg_replace("/\[\\\\\*\]( *)/", "* ", $s[1]); +} + +function diaspora_ol($s) { + // A hack: Diaspora will create a properly-numbered ordered list even + // if you use '1.' for each element of the list, like: + // 1. First element + // 1. Second element + // 1. Third element + return preg_replace("/\[\\\\\*\]( *)/", "1. ", $s[1]); +} function bb2diaspora($Text,$preserve_nl = false) { +////////////////////// +// An attempt was made to convert bbcode to html and then to markdown +// consisting of the following lines. +// I'm undoing this as we have a lot of bbcode constructs which +// were simply getting lost, for instance bookmark, vimeo, video, youtube, events, etc. +// We can try this again, but need a very good test sequence to verify +// all the major bbcode constructs that we use are getting through. +////////////////////// +/* // bbcode() will convert "[*]" into "
  • " with no closing "
  • " // Markdownify() is unable to handle these, as it makes each new // "
  • " into a deeper nested element until it crashes. So pre-format @@ -115,6 +149,7 @@ function bb2diaspora($Text,$preserve_nl = false) { // Note that to get nested lists to work for Diaspora, we would need // to define the closing tag for the list elements. So nested lists // are going to be flattened out in Diaspora for now + $endlessloop = 0; while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) || ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) || @@ -129,16 +164,7 @@ function bb2diaspora($Text,$preserve_nl = false) { $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); } - - -////////////////////// -// An attempt was made to convert bbcode to html and then to markdown -// consisting of the following lines. -// I'm undoing this as we have a lot of bbcode constructs which -// were simply getting lost, for instance bookmark, vimeo, video, youtube, events, etc. -// We can try this again, but need a very good test sequence to verify -// all the major bbcode constructs that we use are getting through. -////////////////////// +*/ // Convert it to HTML - don't try oembed // $Text = bbcode($Text, $preserve_nl, false); @@ -244,15 +270,21 @@ function bb2diaspora($Text,$preserve_nl = false) { $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])is","$2",$Text); // Check for list text - $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text); - $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text); - $Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text); - $Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text); - $Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); - $Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); - $Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); - $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); -// $Text = preg_replace("/\[li\](.*?)\[\/li\]/s", '
  • $1
  • ' ,$Text); + $endlessloop = 0; + while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) || + ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) || + ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) || + ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) { + $Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text); + $Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text); + $Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text); + $Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text); + $Text = preg_replace("/\[li\]( *)(.*?)\[\/li\]/s", '* $2' ,$Text); + } // Just get rid of table tags since Diaspora doesn't support tables $Text = preg_replace("/\[th\](.*?)\[\/th\]/s", '$1' ,$Text); -- cgit v1.2.3 From 47c7fd89ba76732c779cc79648a2c7ab2e914041 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Thu, 5 Jul 2012 20:39:22 -0600 Subject: my php thinks this is a typo --- include/bb2diaspora.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/bb2diaspora.php') diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index 75dadb84c..042177fd9 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -45,7 +45,7 @@ function diaspora2bb($s) { // $s = str_replace('*','*',$s); // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands - $s = str_replace('♲',html_entities_decode('♲',ENT_QUOTES,'UTF-8'),$s); + $s = str_replace('♲',html_entity_decode('♲',ENT_QUOTES,'UTF-8'),$s); // Convert everything that looks like a link to a link $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s); -- cgit v1.2.3