diff options
Diffstat (limited to 'vendor/league/html-to-markdown/src/Converter/CodeConverter.php')
-rw-r--r-- | vendor/league/html-to-markdown/src/Converter/CodeConverter.php | 45 |
1 files changed, 17 insertions, 28 deletions
diff --git a/vendor/league/html-to-markdown/src/Converter/CodeConverter.php b/vendor/league/html-to-markdown/src/Converter/CodeConverter.php index 39e6a7bc4..40eb7f85a 100644 --- a/vendor/league/html-to-markdown/src/Converter/CodeConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/CodeConverter.php @@ -1,17 +1,14 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; class CodeConverter implements ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { $language = ''; @@ -20,23 +17,24 @@ class CodeConverter implements ConverterInterface if ($classes) { // Since tags can have more than one class, we need to find the one that starts with 'language-' - $classes = explode(' ', $classes); + $classes = \explode(' ', $classes); foreach ($classes as $class) { - if (strpos($class, 'language-') !== false) { + if (\strpos($class, 'language-') !== false) { // Found one, save it as the selected language and stop looping over the classes. - $language = str_replace('language-', '', $class); + $language = \str_replace('language-', '', $class); break; } } } $markdown = ''; - $code = html_entity_decode($element->getChildrenAsString()); + $code = \html_entity_decode($element->getChildrenAsString()); // In order to remove the code tags we need to search for them and, in the case of the opening tag // use a regular expression to find the tag and the other attributes it might have - $code = preg_replace('/<code\b[^>]*>/', '', $code); - $code = str_replace('</code>', '', $code); + $code = \preg_replace('/<code\b[^>]*>/', '', $code); + \assert($code !== null); + $code = \str_replace('</code>', '', $code); // Checking if it's a code block or span if ($this->shouldBeBlock($element, $code)) { @@ -44,7 +42,7 @@ class CodeConverter implements ConverterInterface $markdown .= '```' . $language . "\n" . $code . "\n" . '```'; } else { // One line of code, wrapping it on one backtick, removing new lines - $markdown .= '`' . preg_replace('/\r\n|\r|\n/', '', $code) . '`'; + $markdown .= '`' . \preg_replace('/\r\n|\r|\n/', '', $code) . '`'; } return $markdown; @@ -53,27 +51,18 @@ class CodeConverter implements ConverterInterface /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('code'); + return ['code']; } - /** - * @param ElementInterface $element - * @param string $code - * - * @return bool - */ - private function shouldBeBlock(ElementInterface $element, $code) + private function shouldBeBlock(ElementInterface $element, string $code): bool { - if ($element->getParent()->getTagName() == 'pre') { - return true; - } - - if (preg_match('/[^\s]` `/', $code)) { + $parent = $element->getParent(); + if ($parent !== null && $parent->getTagName() === 'pre') { return true; } - return false; + return \preg_match('/[^\s]` `/', $code) === 1; } } |