From fd8a5ff4c49fc1361f9928ea4d33e6d24d43a3a5 Mon Sep 17 00:00:00 2001 From: Mario Date: Sat, 5 Jun 2021 08:47:24 +0000 Subject: composer update league/html-to-markdown --- .../src/Converter/BlockquoteConverter.php | 22 ++-- .../src/Converter/CodeConverter.php | 45 ++++---- .../src/Converter/CommentConverter.php | 39 +++---- .../src/Converter/ConverterInterface.php | 11 +- .../src/Converter/DefaultConverter.php | 27 ++--- .../src/Converter/DivConverter.php | 24 ++--- .../src/Converter/EmphasisConverter.php | 57 +++++++---- .../src/Converter/HardBreakConverter.php | 30 ++---- .../src/Converter/HeaderConverter.php | 56 ++++------ .../src/Converter/HorizontalRuleConverter.php | 13 +-- .../src/Converter/ImageConverter.php | 17 ++-- .../src/Converter/LinkConverter.php | 64 ++++++------ .../src/Converter/ListBlockConverter.php | 13 +-- .../src/Converter/ListItemConverter.php | 49 ++++----- .../src/Converter/ParagraphConverter.php | 85 +++++++--------- .../src/Converter/PreformattedConverter.php | 36 ++++--- .../src/Converter/TableConverter.php | 113 +++++++++++++++++++++ .../src/Converter/TextConverter.php | 30 +++--- 18 files changed, 373 insertions(+), 358 deletions(-) create mode 100644 vendor/league/html-to-markdown/src/Converter/TableConverter.php (limited to 'vendor/league/html-to-markdown/src/Converter') diff --git a/vendor/league/html-to-markdown/src/Converter/BlockquoteConverter.php b/vendor/league/html-to-markdown/src/Converter/BlockquoteConverter.php index eb2d09d17..65034db12 100644 --- a/vendor/league/html-to-markdown/src/Converter/BlockquoteConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/BlockquoteConverter.php @@ -1,32 +1,30 @@ ' symbols to each line. $markdown = ''; - $quote_content = trim($element->getValue()); + $quoteContent = \trim($element->getValue()); - $lines = preg_split('/\r\n|\r|\n/', $quote_content); + $lines = \preg_split('/\r\n|\r|\n/', $quoteContent); + \assert(\is_array($lines)); - $total_lines = count($lines); + $totalLines = \count($lines); foreach ($lines as $i => $line) { $markdown .= '> ' . $line . "\n"; - if ($i + 1 === $total_lines) { + if ($i + 1 === $totalLines) { $markdown .= "\n"; } } @@ -37,8 +35,8 @@ class BlockquoteConverter implements ConverterInterface /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('blockquote'); + return ['blockquote']; } } 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 @@ 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); - $code = str_replace('', '', $code); + $code = \preg_replace('/]*>/', '', $code); + \assert($code !== null); + $code = \str_replace('', '', $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; } } diff --git a/vendor/league/html-to-markdown/src/Converter/CommentConverter.php b/vendor/league/html-to-markdown/src/Converter/CommentConverter.php index 959381d1b..c69dea551 100644 --- a/vendor/league/html-to-markdown/src/Converter/CommentConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/CommentConverter.php @@ -1,5 +1,7 @@ config = $config; } - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { if ($this->shouldPreserve($element)) { return ''; } + return ''; } /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('#comment'); + return ['#comment']; } - /** - * @param ElementInterface $element - * - * @return bool - */ - private function shouldPreserve(ElementInterface $element) + private function shouldPreserve(ElementInterface $element): bool { $preserve = $this->config->getOption('preserve_comments'); if ($preserve === true) { return true; } - if (is_array($preserve)) { - $value = trim($element->getValue()); - return in_array($value, $preserve); + + if (\is_array($preserve)) { + $value = \trim($element->getValue()); + + return \in_array($value, $preserve, true); } + return false; } } diff --git a/vendor/league/html-to-markdown/src/Converter/ConverterInterface.php b/vendor/league/html-to-markdown/src/Converter/ConverterInterface.php index 8530559a0..f10498578 100644 --- a/vendor/league/html-to-markdown/src/Converter/ConverterInterface.php +++ b/vendor/league/html-to-markdown/src/Converter/ConverterInterface.php @@ -1,20 +1,17 @@ config = $config; } - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { // If strip_tags is false (the default), preserve tags that don't have Markdown equivalents, // such as nodes on their own. C14N() canonicalizes the node to a string. @@ -37,8 +29,9 @@ class DefaultConverter implements ConverterInterface, ConfigurationAwareInterfac return $element->getValue(); } - $markdown = html_entity_decode($element->getChildrenAsString()); + $markdown = \html_entity_decode($element->getChildrenAsString()); + // Tables are only handled here if TableConverter is not used if ($element->getTagName() === 'table') { $markdown .= "\n\n"; } @@ -49,8 +42,8 @@ class DefaultConverter implements ConverterInterface, ConfigurationAwareInterfac /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array(self::DEFAULT_CONVERTER); + return [self::DEFAULT_CONVERTER]; } } diff --git a/vendor/league/html-to-markdown/src/Converter/DivConverter.php b/vendor/league/html-to-markdown/src/Converter/DivConverter.php index 656a0ba4d..6453a2a27 100644 --- a/vendor/league/html-to-markdown/src/Converter/DivConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/DivConverter.php @@ -1,5 +1,7 @@ config = $config; } - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { if ($this->config->getOption('strip_tags', false)) { return $element->getValue() . "\n\n"; } - return html_entity_decode($element->getChildrenAsString()); + return \html_entity_decode($element->getChildrenAsString()); } /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('div'); + return ['div']; } } diff --git a/vendor/league/html-to-markdown/src/Converter/EmphasisConverter.php b/vendor/league/html-to-markdown/src/Converter/EmphasisConverter.php index 8fd4dd6e2..a122f4052 100644 --- a/vendor/league/html-to-markdown/src/Converter/EmphasisConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/EmphasisConverter.php @@ -1,5 +1,7 @@ isText()) { + $tag = $element->getTagName(); + if ($tag === 'i' || $tag === 'em') { + return 'em'; + } + + if ($tag === 'b' || $tag === 'strong') { + return 'strong'; + } + } + + return ''; + } + + public function setConfig(Configuration $config): void { $this->config = $config; } - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { - $tag = $element->getTagName(); + $tag = $this->getNormTag($element); $value = $element->getValue(); - if (!trim($value)) { + if (! \trim($value)) { return $value; } - if ($tag === 'i' || $tag === 'em') { + if ($tag === 'em') { $style = $this->config->getOption('italic_style'); } else { $style = $this->config->getOption('bold_style'); } - $prefix = ltrim($value) !== $value ? ' ' : ''; - $suffix = rtrim($value) !== $value ? ' ' : ''; + $prefix = \ltrim($value) !== $value ? ' ' : ''; + $suffix = \rtrim($value) !== $value ? ' ' : ''; + + /* If this node is immediately preceded or followed by one of the same type don't emit + * the start or end $style, respectively. This prevents foobar from + * being converted to *foo**bar* which is incorrect. We want *foobar* instead. + */ + $preStyle = $this->getNormTag($element->getPreviousSibling()) === $tag ? '' : $style; + $postStyle = $this->getNormTag($element->getNextSibling()) === $tag ? '' : $style; - return $prefix . $style . trim($value) . $style . $suffix; + return $prefix . $preStyle . \trim($value) . $postStyle . $suffix; } /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('em', 'i', 'strong', 'b'); + return ['em', 'i', 'strong', 'b']; } } diff --git a/vendor/league/html-to-markdown/src/Converter/HardBreakConverter.php b/vendor/league/html-to-markdown/src/Converter/HardBreakConverter.php index 1be10bd63..45e89682e 100644 --- a/vendor/league/html-to-markdown/src/Converter/HardBreakConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/HardBreakConverter.php @@ -1,5 +1,7 @@ config = $config; } - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { $return = $this->config->getOption('hard_break') ? "\n" : " \n"; $next = $element->getNext(); if ($next) { - $next_value = $next->getValue(); - if ($next_value) { - if (in_array(substr($next_value, 0, 2), array('- ', '* ', '+ '))) { + $nextValue = $next->getValue(); + if ($nextValue) { + if (\in_array(\substr($nextValue, 0, 2), ['- ', '* ', '+ '], true)) { $parent = $element->getParent(); - if ($parent && $parent->getTagName() == 'li') { + if ($parent && $parent->getTagName() === 'li') { $return .= '\\'; } } @@ -49,8 +41,8 @@ class HardBreakConverter implements ConverterInterface, ConfigurationAwareInterf /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('br'); + return ['br']; } } diff --git a/vendor/league/html-to-markdown/src/Converter/HeaderConverter.php b/vendor/league/html-to-markdown/src/Converter/HeaderConverter.php index 353833263..e99dfa0f4 100644 --- a/vendor/league/html-to-markdown/src/Converter/HeaderConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/HeaderConverter.php @@ -1,5 +1,7 @@ config = $config; } - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { - $level = (int) substr($element->getTagName(), 1, 1); + $level = (int) \substr($element->getTagName(), 1, 1); $style = $this->config->getOption('header_style', self::STYLE_SETEXT); - if (strlen($element->getValue()) === 0) { + if (\strlen($element->getValue()) === 0) { return "\n"; } - if (($level === 1 || $level === 2) && !$element->isDescendantOf('blockquote') && $style === self::STYLE_SETEXT) { + if (($level === 1 || $level === 2) && ! $element->isDescendantOf('blockquote') && $style === self::STYLE_SETEXT) { return $this->createSetextHeader($level, $element->getValue()); } @@ -48,34 +40,22 @@ class HeaderConverter implements ConverterInterface, ConfigurationAwareInterface /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'); + return ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; } - /** - * @param int $level - * @param string $content - * - * @return string - */ - private function createSetextHeader($level, $content) + private function createSetextHeader(int $level, string $content): string { - $length = function_exists('mb_strlen') ? mb_strlen($content, 'utf-8') : strlen($content); - $underline = ($level === 1) ? '=' : '-'; + $length = \function_exists('mb_strlen') ? \mb_strlen($content, 'utf-8') : \strlen($content); + $underline = $level === 1 ? '=' : '-'; - return $content . "\n" . str_repeat($underline, $length) . "\n\n"; + return $content . "\n" . \str_repeat($underline, $length) . "\n\n"; } - /** - * @param int $level - * @param string $content - * - * @return string - */ - private function createAtxHeader($level, $content) + private function createAtxHeader(int $level, string $content): string { - $prefix = str_repeat('#', $level) . ' '; + $prefix = \str_repeat('#', $level) . ' '; return $prefix . $content . "\n\n"; } diff --git a/vendor/league/html-to-markdown/src/Converter/HorizontalRuleConverter.php b/vendor/league/html-to-markdown/src/Converter/HorizontalRuleConverter.php index 8f54f9397..ce280cc79 100644 --- a/vendor/league/html-to-markdown/src/Converter/HorizontalRuleConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/HorizontalRuleConverter.php @@ -1,17 +1,14 @@ getAttribute('src'); - $alt = $element->getAttribute('alt'); + $src = $element->getAttribute('src'); + $alt = $element->getAttribute('alt'); $title = $element->getAttribute('title'); if ($title !== '') { @@ -28,8 +25,8 @@ class ImageConverter implements ConverterInterface /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('img'); + return ['img']; } } diff --git a/vendor/league/html-to-markdown/src/Converter/LinkConverter.php b/vendor/league/html-to-markdown/src/Converter/LinkConverter.php index ed52619d2..25a3540fe 100644 --- a/vendor/league/html-to-markdown/src/Converter/LinkConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/LinkConverter.php @@ -1,5 +1,7 @@ config = $config; } - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { - $href = $element->getAttribute('href'); + $href = $element->getAttribute('href'); $title = $element->getAttribute('title'); - $text = trim($element->getValue(), "\t\n\r\0\x0B"); + $text = \trim($element->getValue(), "\t\n\r\0\x0B"); if ($title !== '') { $markdown = '[' . $text . '](' . $href . ' "' . $title . '")'; @@ -38,14 +31,19 @@ class LinkConverter implements ConverterInterface, ConfigurationAwareInterface } elseif ($href === 'mailto:' . $text && $this->isValidEmail($text)) { $markdown = '<' . $text . '>'; } else { - if (stristr($href, ' ')) { - $href = '<'.$href.'>'; + if (\stristr($href, ' ')) { + $href = '<' . $href . '>'; } + $markdown = '[' . $text . '](' . $href . ')'; } - if (!$href) { - $markdown = html_entity_decode($element->getChildrenAsString()); + if (! $href) { + if ($this->shouldStrip()) { + $markdown = $text; + } else { + $markdown = \html_entity_decode($element->getChildrenAsString()); + } } return $markdown; @@ -54,30 +52,26 @@ class LinkConverter implements ConverterInterface, ConfigurationAwareInterface /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('a'); + return ['a']; } - /** - * @param string $href - * - * @return bool - */ - private function isValidAutolink($href) + private function isValidAutolink(string $href): bool { $useAutolinks = $this->config->getOption('use_autolinks'); - return $useAutolinks && (preg_match('/^[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*/i', $href) === 1); + + return $useAutolinks && (\preg_match('/^[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*/i', $href) === 1); } - /** - * @param string $email - * - * @return bool - */ - private function isValidEmail($email) + private function isValidEmail(string $email): bool { // Email validation is messy business, but this should cover most cases - return filter_var($email, FILTER_VALIDATE_EMAIL); + return \filter_var($email, FILTER_VALIDATE_EMAIL) !== false; + } + + private function shouldStrip(): bool + { + return $this->config->getOption('strip_placeholder_links') ?? false; } } diff --git a/vendor/league/html-to-markdown/src/Converter/ListBlockConverter.php b/vendor/league/html-to-markdown/src/Converter/ListBlockConverter.php index 07a4c85a9..ce7b94654 100644 --- a/vendor/league/html-to-markdown/src/Converter/ListBlockConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/ListBlockConverter.php @@ -1,17 +1,14 @@ getValue() . "\n"; } @@ -19,8 +16,8 @@ class ListBlockConverter implements ConverterInterface /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('ol', 'ul'); + return ['ol', 'ul']; } } diff --git a/vendor/league/html-to-markdown/src/Converter/ListItemConverter.php b/vendor/league/html-to-markdown/src/Converter/ListItemConverter.php index c56ab89cd..91b3b5dbe 100644 --- a/vendor/league/html-to-markdown/src/Converter/ListItemConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/ListItemConverter.php @@ -1,5 +1,7 @@ config = $config; } - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { // If parent is an ol, use numbers, otherwise, use dashes - $list_type = $element->getParent()->getTagName(); + $listType = ($parent = $element->getParent()) ? $parent->getTagName() : 'ul'; // Add spaces to start for nested list items - $level = $element->getListItemLevel($element); + $level = $element->getListItemLevel(); - $prefixForParagraph = str_repeat(' ', $level + 1); - $value = trim(implode("\n" . $prefixForParagraph, explode("\n", trim($element->getValue())))); + $value = \trim(\implode("\n" . ' ', \explode("\n", \trim($element->getValue())))); // If list item is the first in a nested list, add a newline before it $prefix = ''; @@ -48,21 +37,21 @@ class ListItemConverter implements ConverterInterface, ConfigurationAwareInterfa $prefix = "\n"; } - if ($list_type === 'ul') { - $list_item_style = $this->config->getOption('list_item_style', '-'); - $list_item_style_alternate = $this->config->getOption('list_item_style_alternate'); - if (!isset($this->listItemStyle)) { - $this->listItemStyle = $list_item_style_alternate ? $list_item_style_alternate : $list_item_style; + if ($listType === 'ul') { + $listItemStyle = $this->config->getOption('list_item_style', '-'); + $listItemStyleAlternate = $this->config->getOption('list_item_style_alternate'); + if (! isset($this->listItemStyle)) { + $this->listItemStyle = $listItemStyleAlternate ?: $listItemStyle; } - if ($list_item_style_alternate && $level == 0 && $element->getSiblingPosition() === 1) { - $this->listItemStyle = $this->listItemStyle == $list_item_style ? $list_item_style_alternate : $list_item_style; + if ($listItemStyleAlternate && $level === 0 && $element->getSiblingPosition() === 1) { + $this->listItemStyle = $this->listItemStyle === $listItemStyle ? $listItemStyleAlternate : $listItemStyle; } return $prefix . $this->listItemStyle . ' ' . $value . "\n"; } - if ($list_type === 'ol' && $start = $element->getParent()->getAttribute('start')) { + if ($listType === 'ol' && ($parent = $element->getParent()) && ($start = \intval($parent->getAttribute('start')))) { $number = $start + $element->getSiblingPosition() - 1; } else { $number = $element->getSiblingPosition(); @@ -74,8 +63,8 @@ class ListItemConverter implements ConverterInterface, ConfigurationAwareInterfa /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('li'); + return ['li']; } } diff --git a/vendor/league/html-to-markdown/src/Converter/ParagraphConverter.php b/vendor/league/html-to-markdown/src/Converter/ParagraphConverter.php index 7207b81a6..65b37a4db 100644 --- a/vendor/league/html-to-markdown/src/Converter/ParagraphConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/ParagraphConverter.php @@ -1,23 +1,22 @@ getValue(); $markdown = ''; - $lines = preg_split('/\r\n|\r|\n/', $value); + $lines = \preg_split('/\r\n|\r|\n/', $value); + \assert($lines !== false); + foreach ($lines as $line) { /* * Some special characters need to be escaped based on the position that they appear @@ -27,23 +26,18 @@ class ParagraphConverter implements ConverterInterface $markdown .= "\n"; } - return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : ''; + return \trim($markdown) !== '' ? \rtrim($markdown) . "\n\n" : ''; } /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('p'); + return ['p']; } - /** - * @param string $line - * - * @return string - */ - private function escapeSpecialCharacters($line) + private function escapeSpecialCharacters(string $line): string { $line = $this->escapeFirstCharacters($line); $line = $this->escapeOtherCharacters($line); @@ -52,72 +46,61 @@ class ParagraphConverter implements ConverterInterface return $line; } - /** - * @param string $line - * - * @return string - */ - private function escapeFirstCharacters($line) + private function escapeFirstCharacters(string $line): string { - $escapable = array( + $escapable = [ '>', '- ', '+ ', '--', '~~~', '---', - '- - -' - ); + '- - -', + ]; foreach ($escapable as $i) { - if (strpos(ltrim($line), $i) === 0) { + if (\strpos(\ltrim($line), $i) === 0) { // Found a character that must be escaped, adding a backslash before - return '\\' . ltrim($line); + return '\\' . \ltrim($line); } } return $line; } - /** - * @param string $line - * - * @return string - */ - private function escapeOtherCharacters($line) + private function escapeOtherCharacters(string $line): string { - $escapable = array( - '