diff options
author | Mario Vavti <mario@mariovavti.com> | 2018-10-30 12:12:07 +0100 |
---|---|---|
committer | Mario Vavti <mario@mariovavti.com> | 2018-10-30 12:12:07 +0100 |
commit | 2192ea6fdf21a00ccf44d626a01b756d194d6cbe (patch) | |
tree | 153d4b4ffb2bf40edf8aae66b82a274dac37c208 /vendor/league/html-to-markdown/src/Converter/CodeConverter.php | |
parent | 0f19627407ccd373afe1ce2d2347ea240f3195c1 (diff) | |
download | volse-hubzilla-2192ea6fdf21a00ccf44d626a01b756d194d6cbe.tar.gz volse-hubzilla-2192ea6fdf21a00ccf44d626a01b756d194d6cbe.tar.bz2 volse-hubzilla-2192ea6fdf21a00ccf44d626a01b756d194d6cbe.zip |
update html_to_markdown, smarty and sabredav libs via composer
Diffstat (limited to 'vendor/league/html-to-markdown/src/Converter/CodeConverter.php')
-rw-r--r-- | vendor/league/html-to-markdown/src/Converter/CodeConverter.php | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/vendor/league/html-to-markdown/src/Converter/CodeConverter.php b/vendor/league/html-to-markdown/src/Converter/CodeConverter.php index e536362ee..39e6a7bc4 100644 --- a/vendor/league/html-to-markdown/src/Converter/CodeConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/CodeConverter.php @@ -13,7 +13,7 @@ class CodeConverter implements ConverterInterface */ public function convert(ElementInterface $element) { - $language = null; + $language = ''; // Checking for language class on the code block $classes = $element->getAttribute('class'); @@ -24,8 +24,7 @@ class CodeConverter implements ConverterInterface foreach ($classes as $class) { if (strpos($class, 'language-') !== false) { // Found one, save it as the selected language and stop looping over the classes. - // The space after the language avoids gluing the actual code with the language tag - $language = str_replace('language-', '', $class) . ' '; + $language = str_replace('language-', '', $class); break; } } @@ -39,14 +38,13 @@ class CodeConverter implements ConverterInterface $code = preg_replace('/<code\b[^>]*>/', '', $code); $code = str_replace('</code>', '', $code); - // Checking if the code has multiple lines - $lines = preg_split('/\r\n|\r|\n/', $code); - if (count($lines) > 1) { - // Multiple lines detected, adding three backticks and newlines - $markdown .= '```' . $language . "\n" . $code . "\n" . '```' . "\n\n"; + // Checking if it's a code block or span + if ($this->shouldBeBlock($element, $code)) { + // Code block detected, newlines will be added in parent + $markdown .= '```' . $language . "\n" . $code . "\n" . '```'; } else { - // One line of code, wrapping it on one backtick. - $markdown .= '`' . $language . $code . '`'; + // One line of code, wrapping it on one backtick, removing new lines + $markdown .= '`' . preg_replace('/\r\n|\r|\n/', '', $code) . '`'; } return $markdown; @@ -59,4 +57,23 @@ class CodeConverter implements ConverterInterface { return array('code'); } + + /** + * @param ElementInterface $element + * @param string $code + * + * @return bool + */ + private function shouldBeBlock(ElementInterface $element, $code) + { + if ($element->getParent()->getTagName() == 'pre') { + return true; + } + + if (preg_match('/[^\s]` `/', $code)) { + return true; + } + + return false; + } } |