Fix Diaspora code blocks being mangled

- Remove whitespace removal code from `diaspora2bb()`
- Add code block skipping the HTML transforms removing whitespace in
`htm2bbcode()`
This commit is contained in:
Hypolite Petovan 2017-03-26 19:00:04 -04:00
parent b952b22735
commit 3e285a690a
2 changed files with 29 additions and 17 deletions

View file

@ -59,15 +59,6 @@ function diaspora2bb($s) {
$s = str_replace('#', '#', $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", '<br>', $s);
$s = html2bbcode($s); $s = html2bbcode($s);
// protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands

View file

@ -2,7 +2,7 @@
/** /**
* @file include/html2bbcode.php * @file include/html2bbcode.php
* @brief Converter for HTML to BBCode * @brief Converter for HTML to BBCode
* *
* Made by: ike@piratenpartei.de * Made by: ike@piratenpartei.de
* Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
* https://github.com/annando/Syncom * https://github.com/annando/Syncom
@ -79,16 +79,25 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb)
return($replace); return($replace);
} }
function _replace_code_cb($m){
return "<code>".str_replace("\n","<br>\n",$m[1]). "</code>";
}
function html2bbcode($message) function html2bbcode($message)
{ {
$message = str_replace("\r", "", $message); $message = str_replace("\r", "", $message);
$message = preg_replace_callback("|<pre><code>([^<]*)</code></pre>|ism", "_replace_code_cb", $message); // Removing code blocks before the whitespace removal processing below
$codeblocks = [];
$message = preg_replace_callback('#<pre><code(?: class="([^"]*)")?>(.*)</code></pre>#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( $message = str_replace(array(
"<li><p>", "<li><p>",
@ -232,7 +241,6 @@ function html2bbcode($message)
node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]'); node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]');
node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]'); node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]');
node2bbcode($doc, 'code', array(), '[code]', '[/code]');
node2bbcode($doc, 'key', array(), '[code]', '[/code]'); node2bbcode($doc, 'key', array(), '[code]', '[/code]');
$message = $doc->saveHTML(); $message = $doc->saveHTML();
@ -302,6 +310,19 @@ function html2bbcode($message)
// Handling Yahoo style of mails // Handling Yahoo style of mails
$message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message); $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;
} }
?> ?>