From 0e5d824bff18acfcaceecd8bd87664c7f1546ef7 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Tue, 2 May 2017 22:10:53 -0700 Subject: finish the red conversion of the markdown utilities considering their relation to protocol plugins. --- include/markdown.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/include/markdown.php b/include/markdown.php index 841540357..90266afc9 100644 --- a/include/markdown.php +++ b/include/markdown.php @@ -151,3 +151,55 @@ function markdown_to_bb($s, $use_zrl = false, $options = []) { } + +function bb_to_markdown($Text) { + + /* + * Transform #tags, strip off the [url] and replace spaces with underscore + */ + + $Text = preg_replace_callback('/#\[([zu])rl\=(\w+.*?)\](\w+.*?)\[\/[(zu)]rl\]/i', + create_function('$match', 'return \'#\'. str_replace(\' \', \'_\', $match[3]);'), $Text); + + $Text = preg_replace('/#\^\[([zu])rl\=(\w+.*?)\](\w+.*?)\[\/([zu])rl\]/i', '[$1rl=$2]$3[/$4rl]', $Text); + + // Converting images with size parameters to simple images. Markdown doesn't know it. + $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text); + + + call_hooks('bb_to_markdown_bb',$Text); + + // Convert it to HTML - don't try oembed + $Text = bbcode($Text, $preserve_nl, false); + + // Markdownify does not preserve previously escaped html entities such as <> and &. + + $Text = str_replace(array('<','>','&'),array('&_lt_;','&_gt_;','&_amp_;'),$Text); + + // Now convert HTML to Markdown + $md = new Converter(Converter::LINK_AFTER_CONTENT, false, false); + $Text = $md->parseString($Text); + + // The converter adds backslashes to our attempt at getting around the html entity preservation for some weird reason. + + $Text = str_replace(array('&\\_lt\\_;','&\\_gt\\_;','&\\_amp\\_;'),array('<','>','&'),$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 empty zrl links + $Text = preg_replace("/\[zrl\=\].*?\[\/zrl\]/is", "", $Text); + + // escape all unconverted tags + $Text = escape_tags($Text); + + $Text = trim($Text); + + call_hooks('bb_to_markdown', $Text); + + return $Text; + +} \ No newline at end of file -- cgit v1.2.3