diff options
Diffstat (limited to 'include/markdown.php')
-rw-r--r-- | include/markdown.php | 132 |
1 files changed, 127 insertions, 5 deletions
diff --git a/include/markdown.php b/include/markdown.php index de9862801..d2148811c 100644 --- a/include/markdown.php +++ b/include/markdown.php @@ -5,7 +5,11 @@ */ use Michelf\MarkdownExtra; + use League\HTMLToMarkdown\HtmlConverter; +use League\HTMLToMarkdown\Environment; +use League\HTMLToMarkdown\Converter\ConverterInterface; +use League\HTMLToMarkdown\ElementInterface; require_once("include/oembed.php"); require_once("include/event.php"); @@ -74,11 +78,14 @@ function markdown_to_bb($s, $use_zrl = false, $options = []) { // Convert everything that looks like a link to a link if($use_zrl) { - $s = str_replace(['[img', '/img]'], ['[zmg', '/zmg]'], $s); - $s = preg_replace("/([^\]\=\{]|^)(https?\:\/\/)([a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@\(\)]+)/ismu", '$1[zrl=$2$3]$2$3[/zrl]',$s); + if (strpos($s,'[/img]') !== false) { + $s = preg_replace_callback("/\[img\](.*?)\[\/img\]/ism", 'use_zrl_cb_img', $s); + $s = preg_replace_callback("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", 'use_zrl_cb_img_x', $s); + } + $s = preg_replace_callback("/([^\]\=\{\/]|^)(https?\:\/\/)([a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@\(\)]+)/ismu", 'use_zrl_cb_link',$s); } else { - $s = preg_replace("/([^\]\=\{]|^)(https?\:\/\/)([a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@\(\)]+)/ismu", '$1[url=$2$3]$2$3[/url]',$s); + $s = preg_replace("/([^\]\=\{\/]|^)(https?\:\/\/)([a-zA-Z0-9\pL\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@\(\)]+)/ismu", '$1[url=$2$3]$2$3[/url]',$s); } // remove duplicate adjacent code tags @@ -96,6 +103,41 @@ function markdown_to_bb($s, $use_zrl = false, $options = []) { return $s; } +function use_zrl_cb_link($match) { + $res = ''; + $is_zid = is_matrix_url(trim($match[0])); + + if($is_zid) + $res = $match[1] . '[zrl=' . $match[2] . $match[3] . ']' . $match[2] . $match[3] . '[/zrl]'; + else + $res = $match[1] . '[url=' . $match[2] . $match[3] . ']' . $match[2] . $match[3] . '[/url]'; + + return $res; +} + +function use_zrl_cb_img($match) { + $res = ''; + $is_zid = is_matrix_url(trim($match[1])); + + if($is_zid) + $res = '[zmg]' . $match[1] . '[/zmg]'; + else + $res = $match[0]; + + return $res; +} + +function use_zrl_cb_img_x($match) { + $res = ''; + $is_zid = is_matrix_url(trim($match[3])); + + if($is_zid) + $res = '[zmg=' . $match[1] . 'x' . $match[2] . ']' . $match[3] . '[/zmg]'; + else + $res = $match[0]; + + return $res; +} /** * @brief @@ -213,6 +255,9 @@ function bb_to_markdown($Text, $options = []) { $Text = html2markdown($Text); + //html2markdown adds backslashes infront of hashes after a new line. remove them + $Text = str_replace("\n\#", "\n#", $Text); + // It also 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); @@ -248,9 +293,14 @@ function bb_to_markdown($Text, $options = []) { * @param string $html The HTML code to convert * @return string Markdown representation of the given HTML text, empty on error */ -function html2markdown($html) { +function html2markdown($html,$options = []) { $markdown = ''; - $converter = new HtmlConverter(); + + $internal_errors = libxml_use_internal_errors(true); + + $environment = Environment::createDefaultEnvironment($options); + $environment->addConverter(new TableConverter()); + $converter = new HtmlConverter($environment); try { $markdown = $converter->convert($html); @@ -258,5 +308,77 @@ function html2markdown($html) { logger("Invalid HTML. HTMLToMarkdown library threw an exception."); } + libxml_use_internal_errors($internal_errors); + return $markdown; } + +// Tables are not an official part of the markdown specification. +// This interface was suggested as a workaround. +// author: Mark Hamstra +// https://github.com/Mark-H/Docs + + +class TableConverter implements ConverterInterface +{ + /** + * @param ElementInterface $element + * + * @return string + */ + public function convert(ElementInterface $element) + { + switch ($element->getTagName()) { + case 'tr': + $line = []; + $i = 1; + foreach ($element->getChildren() as $td) { + $i++; + $v = $td->getValue(); + $v = trim($v); + if ($i % 2 === 0 || $v !== '') { + $line[] = $v; + } + } + return '| ' . implode(' | ', $line) . " |\n"; + case 'td': + case 'th': + return trim($element->getValue()); + case 'tbody': + return trim($element->getValue()); + case 'thead': + $headerLine = reset($element->getChildren())->getValue(); + $headers = explode(' | ', trim(trim($headerLine, "\n"), '|')); + $hr = []; + foreach ($headers as $td) { + $length = strlen(trim($td)) + 2; + $hr[] = str_repeat('-', $length > 3 ? $length : 3); + } + $hr = '|' . implode('|', $hr) . '|'; + return $headerLine . $hr . "\n"; + case 'table': + $inner = $element->getValue(); + if (strpos($inner, '-----') === false) { + $inner = explode("\n", $inner); + $single = explode(' | ', trim($inner[0], '|')); + $hr = []; + foreach ($single as $td) { + $length = strlen(trim($td)) + 2; + $hr[] = str_repeat('-', $length > 3 ? $length : 3); + } + $hr = '|' . implode('|', $hr) . '|'; + array_splice($inner, 1, 0, $hr); + $inner = implode("\n", $inner); + } + return trim($inner) . "\n\n"; + } + return $element->getValue(); + } + /** + * @return string[] + */ + public function getSupportedTags() + { + return array('table', 'tr', 'thead', 'td', 'tbody'); + } +} |