From 14ab5801d2007165c0eaacd3b8c7f657efac213a Mon Sep 17 00:00:00 2001 From: Mario Date: Sat, 5 Jun 2021 08:55:32 +0000 Subject: league/html-to-markdown suports tables now --- include/markdown.php | 74 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 72 deletions(-) (limited to 'include/markdown.php') diff --git a/include/markdown.php b/include/markdown.php index 013d57c29..b75613351 100644 --- a/include/markdown.php +++ b/include/markdown.php @@ -250,7 +250,7 @@ function bb_to_markdown($Text, $options = []) { call_hooks('bb_to_markdown_bb', $x); $Text = $x['bbcode']; - + // Replace spoiler tag before BBcode conversion $Text = preg_replace("/\[\/?spoiler\]/is", "\n--- " .t('spoiler') . " ---\n", $Text); @@ -271,7 +271,7 @@ function bb_to_markdown($Text, $options = []) { // Remove empty zrl links $Text = preg_replace("/\[zrl\=\].*?\[\/zrl\]/is", "", $Text); - + // Replace unprocessed
in code $Text = str_replace("

", "\n", $Text); @@ -325,73 +325,3 @@ function html2markdown($html, $options = []) { 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'); - } -} -- cgit v1.2.3