aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzotlabs <mike@macgirvin.com>2018-08-12 16:25:14 -0700
committerzotlabs <mike@macgirvin.com>2018-08-12 16:25:14 -0700
commitdb1a546abaa82472bd9c8b402db6752e2a3869d0 (patch)
treeb20307d3f611db63d3cf039e8188132eb0c04e5a
parent2d29095348eb8ab73bac7c22b4f87bfed7ea06c0 (diff)
downloadvolse-hubzilla-db1a546abaa82472bd9c8b402db6752e2a3869d0.tar.gz
volse-hubzilla-db1a546abaa82472bd9c8b402db6752e2a3869d0.tar.bz2
volse-hubzilla-db1a546abaa82472bd9c8b402db6752e2a3869d0.zip
add table support to markdown
-rw-r--r--include/markdown.php81
1 files changed, 79 insertions, 2 deletions
diff --git a/include/markdown.php b/include/markdown.php
index 058b79909..1b3538306 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");
@@ -286,9 +290,12 @@ 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();
+
+ $environment = Environment::createDefaultEnvironment($options);
+ $environment->addConverter(new TableConverter());
+ $converter = new HtmlConverter($environment);
try {
$markdown = $converter->convert($html);
@@ -298,3 +305,73 @@ function html2markdown($html) {
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');
+ }
+} \ No newline at end of file