From 4ef33ac6ebbf7418742a9ee7a4a549808754eb1f Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sat, 23 Jun 2012 19:57:59 -0600 Subject: [PATCH 1/2] fix BB-to-Diaspora list formatting --- include/bb2diaspora.php | 46 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index 96cc735bdb..d509a2e31c 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 ''; + elseif( strpos($s[0], "[ul]") === 0 ) + return ''; + 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 ''; + elseif( strpos($s[0], "[list=i]") === 0 ) + return ''; + elseif( strpos($s[0], "[list=I]") === 0 ) + return ''; + elseif( strpos($s[0], "[list=a]") === 0 ) + return ''; + elseif( strpos($s[0], "[list=A]") === 0 ) + return ''; + elseif( strpos($s[0], "[ol]") === 0 ) + return ''; + 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); From 1f968396749f8cac31aaeee770ae7fc5aff48f38 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sat, 23 Jun 2012 20:01:29 -0600 Subject: [PATCH 2/2] small optimization --- include/bb2diaspora.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index d509a2e31c..75ba57dff8 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);