From 3e285a690ae1b170e2849cbd3e91ab5c5fd23397 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 26 Mar 2017 19:00:04 -0400 Subject: [PATCH 1/2] 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 e509999236..03eff5a6b7 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 189ba91f19..c14629b895 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; } ?> From 55e4bab00e6b0a19a998d3e8906856dbf5a4f60e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 26 Mar 2017 19:06:22 -0400 Subject: [PATCH 2/2] Improve babel page display for diaspora2bb mode - Fix formatting --- mod/babel.php | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/mod/babel.php b/mod/babel.php index e8ea11c94e..f71863bded 100644 --- a/mod/babel.php +++ b/mod/babel.php @@ -1,78 +1,73 @@ ', $s); + return str_replace("\n", '
    ', $s); } function babel_content(App $a) { - $o .= '

    Babel Diagnostic

    '; $o .= '
    '; - $o .= t('Source (bbcode) text:') . EOL . '' . EOL; + $o .= t('Source (bbcode) text:') . EOL; + $o .= '' . EOL; $o .= '
    '; $o .= '

    '; $o .= '
    '; - $o .= t('Source (Diaspora) text to convert to BBcode:') . EOL . '' . EOL; + $o .= t('Source (Diaspora) text to convert to BBcode:') . EOL; + $o .= '' . EOL; $o .= '
    '; $o .= '

    '; - if(x($_REQUEST,'text')) { - + if (x($_REQUEST, 'text')) { $text = trim($_REQUEST['text']); - $o .= "

    " . t("Source input: ") . "

    " . EOL. EOL; + $o .= '

    ' . t('Source input: ') . '

    ' . EOL. EOL; $o .= visible_lf($text) . EOL. EOL; $html = bbcode($text); - $o .= "

    " . t("bb2html (raw HTML): ") . "

    " . EOL. EOL; + $o .= '

    ' . t('bb2html (raw HTML): ') . '

    ' . EOL. EOL; $o .= htmlspecialchars($html). EOL. EOL; //$html = bbcode($text); - $o .= "

    " . t("bb2html: ") . "

    " . EOL. EOL; + $o .= '

    ' . t('bb2html: ') . '

    ' . EOL. EOL; $o .= $html. EOL. EOL; $bbcode = html2bbcode($html); - $o .= "

    " . t("bb2html2bb: ") . "

    " . EOL. EOL; + $o .= '

    ' . t('bb2html2bb: ') . '

    ' . EOL. EOL; $o .= visible_lf($bbcode) . EOL. EOL; $diaspora = bb2diaspora($text); - $o .= "

    " . t("bb2md: ") . "

    " . EOL. EOL; + $o .= '

    ' . t('bb2md: ') . '

    ' . EOL. EOL; $o .= visible_lf($diaspora) . EOL. EOL; $html = Markdown($diaspora); - $o .= "

    " . t("bb2md2html: ") . "

    " . EOL. EOL; + $o .= '

    ' . t('bb2md2html: ') . '

    ' . EOL. EOL; $o .= $html. EOL. EOL; $bbcode = diaspora2bb($diaspora); - $o .= "

    " . t("bb2dia2bb: ") . "

    " . EOL. EOL; + $o .= '

    ' . t('bb2dia2bb: ') . '

    ' . EOL. EOL; $o .= visible_lf($bbcode) . EOL. EOL; $bbcode = html2bbcode($html); - $o .= "

    " . t("bb2md2html2bb: ") . "

    " . EOL. EOL; + $o .= '

    ' . t('bb2md2html2bb: ') . '

    ' . EOL. EOL; $o .= visible_lf($bbcode) . EOL. EOL; - - - } - if(x($_REQUEST,'d2bbtext')) { - + if (x($_REQUEST, 'd2bbtext')) { $d2bbtext = trim($_REQUEST['d2bbtext']); - $o .= "

    " . t("Source input (Diaspora format): ") . "

    " . EOL. EOL; - $o .= visible_lf($d2bbtext) . EOL. EOL; - + $o .= '

    ' . t('Source input (Diaspora format): ') . '

    ' . EOL. EOL; + $o .= '
    ' . $d2bbtext . '
    ' . EOL. EOL; $bb = diaspora2bb($d2bbtext); - $o .= "

    " . t("diaspora2bb: ") . "

    " . EOL. EOL; - $o .= visible_lf($bb) . EOL. EOL; + $o .= '

    ' . t('diaspora2bb: ') . '

    ' . EOL. EOL; + $o .= '
    ' . $bb . '
    ' . EOL. EOL; } return $o;