diff options
author | Mario Vavti <mario@mariovavti.com> | 2017-08-16 10:32:35 +0200 |
---|---|---|
committer | Mario Vavti <mario@mariovavti.com> | 2017-08-16 10:32:35 +0200 |
commit | 4a7384bc0ce1893a432bf4b7d67bca23796fe9db (patch) | |
tree | 5623c66a3f66445284529d6207e4ab4a2edb2810 /vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php | |
parent | c664a4bdcd1bd578f5ec3c2884f7c97e9f68d2d7 (diff) | |
parent | 90bc21f2d560d879d7eaf05a85af6d6dca53ebac (diff) | |
download | volse-hubzilla-4a7384bc0ce1893a432bf4b7d67bca23796fe9db.tar.gz volse-hubzilla-4a7384bc0ce1893a432bf4b7d67bca23796fe9db.tar.bz2 volse-hubzilla-4a7384bc0ce1893a432bf4b7d67bca23796fe9db.zip |
Merge branch '2.6RC'2.6
Diffstat (limited to 'vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php')
-rw-r--r-- | vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php b/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php new file mode 100644 index 000000000..7a4ec3357 --- /dev/null +++ b/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php @@ -0,0 +1,59 @@ +<?php + +namespace League\HTMLToMarkdown\Converter; + +use League\HTMLToMarkdown\ElementInterface; + +class PreformattedConverter implements ConverterInterface +{ + /** + * @param ElementInterface $element + * + * @return string + */ + public function convert(ElementInterface $element) + { + $markdown = ''; + + $pre_content = html_entity_decode($element->getChildrenAsString()); + $pre_content = str_replace(array('<pre>', '</pre>'), '', $pre_content); + + /* + * Checking for the code tag. + * Usually pre tags are used along with code tags. This conditional will check for already converted code tags, + * which use backticks, and if those backticks are at the beginning and at the end of the string it means + * there's no more information to convert. + */ + + $firstBacktick = strpos(trim($pre_content), '`'); + $lastBacktick = strrpos(trim($pre_content), '`'); + if ($firstBacktick === 0 && $lastBacktick === strlen(trim($pre_content)) - 1) { + return $pre_content; + } + + // If the execution reaches this point it means it's just a pre tag, with no code tag nested + + // Normalizing new lines + $pre_content = preg_replace('/\r\n|\r|\n/', PHP_EOL, $pre_content); + + // Checking if the string has multiple lines + $lines = preg_split('/\r\n|\r|\n/', $pre_content); + if (count($lines) > 1) { + // Multiple lines detected, adding three backticks and newlines + $markdown .= '```' . "\n" . $pre_content . "\n" . '```'; + } else { + // One line of code, wrapping it on one backtick. + $markdown .= '`' . $pre_content . '`'; + } + + return $markdown; + } + + /** + * @return string[] + */ + public function getSupportedTags() + { + return array('pre'); + } +} |