From 3e285a690ae1b170e2849cbd3e91ab5c5fd23397 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 26 Mar 2017 19:00:04 -0400 Subject: [PATCH] Fix Diaspora code blocks being mangled - Remove whitespace removal code from `diaspora2bb()` - Add code block skipping the HTML transforms removing whitespace in `htm2bbcode()` --- include/bb2diaspora.php | 9 --------- include/html2bbcode.php | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index e50999923..03eff5a6b 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -59,15 +59,6 @@ function diaspora2bb($s) { $s = str_replace('#', '#', $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", '
', $s); - $s = html2bbcode($s); // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands diff --git a/include/html2bbcode.php b/include/html2bbcode.php index 189ba91f1..c14629b89 100644 --- a/include/html2bbcode.php +++ b/include/html2bbcode.php @@ -2,7 +2,7 @@ /** * @file include/html2bbcode.php * @brief Converter for HTML to BBCode - * + * * Made by: ike@piratenpartei.de * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom * https://github.com/annando/Syncom @@ -79,16 +79,25 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb) return($replace); } -function _replace_code_cb($m){ - return "".str_replace("\n","
\n",$m[1]). "
"; -} - function html2bbcode($message) { $message = str_replace("\r", "", $message); - $message = preg_replace_callback("|
([^<]*)
|ism", "_replace_code_cb", $message); + // Removing code blocks before the whitespace removal processing below + $codeblocks = []; + $message = preg_replace_callback('#
(.*)
#iUs', + function ($matches) use (&$codeblocks) { + $return = '[codeblock-' . count($codeblocks) . ']'; + + $prefix = '[code]'; + if ($matches[1] != '') { + $prefix = '[code=' . $matches[1] . ']'; + } + $codeblocks[] = $prefix . $matches[2] . '[/code]'; + return $return; + } + , $message); $message = str_replace(array( "
  • ", @@ -232,7 +241,6 @@ function html2bbcode($message) node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]'); node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]'); - node2bbcode($doc, 'code', array(), '[code]', '[/code]'); node2bbcode($doc, 'key', array(), '[code]', '[/code]'); $message = $doc->saveHTML(); @@ -302,6 +310,19 @@ function html2bbcode($message) // Handling Yahoo style of mails $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message); - return(trim($message)); + // Restore code blocks + $message = preg_replace_callback('#\[codeblock-([0-9]+)\]#iU', + function ($matches) use ($codeblocks) { + $return = ''; + if (isset($codeblocks[intval($matches[1])])) { + $return = $codeblocks[$matches[1]]; + } + return $return; + } + , $message); + + $message = trim($message); + + return $message; } ?>