diff options
author | nobody <nobody@zotlabs.com> | 2021-06-17 14:57:47 -0700 |
---|---|---|
committer | nobody <nobody@zotlabs.com> | 2021-06-17 14:57:47 -0700 |
commit | efda8aac1d7d90fd7eda4a449332eedf74342951 (patch) | |
tree | 3373c0579168776cccda5224b6b33ce59fa9b274 /vendor/league/html-to-markdown/src/Converter | |
parent | 686530c1873f98d724355bf3f456243b1b7fdadd (diff) | |
parent | a84cec4acddf6804a88fcda52e4437c91785dfb2 (diff) | |
download | volse-hubzilla-efda8aac1d7d90fd7eda4a449332eedf74342951.tar.gz volse-hubzilla-efda8aac1d7d90fd7eda4a449332eedf74342951.tar.bz2 volse-hubzilla-efda8aac1d7d90fd7eda4a449332eedf74342951.zip |
Merge branch 'dev' of https://framagit.org/hubzilla/core into dev
Diffstat (limited to 'vendor/league/html-to-markdown/src/Converter')
18 files changed, 373 insertions, 358 deletions
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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; class BlockquoteConverter implements ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { // Contents should have already been converted to Markdown by this point, // so we just need to add '>' 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 @@ <?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; } } 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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\Configuration; @@ -8,55 +10,44 @@ use League\HTMLToMarkdown\ElementInterface; class CommentConverter implements ConverterInterface, ConfigurationAwareInterface { - /** - * @var Configuration - */ + /** @var Configuration */ protected $config; - /** - * @param Configuration $config - */ - public function setConfig(Configuration $config) + 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 { if ($this->shouldPreserve($element)) { return '<!--' . $element->getValue() . '-->'; } + 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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; interface ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element); + public function convert(ElementInterface $element): string; /** * @return string[] */ - public function getSupportedTags(); + public function getSupportedTags(): array; } diff --git a/vendor/league/html-to-markdown/src/Converter/DefaultConverter.php b/vendor/league/html-to-markdown/src/Converter/DefaultConverter.php index 8de0af210..e71dd10cc 100644 --- a/vendor/league/html-to-markdown/src/Converter/DefaultConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/DefaultConverter.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\Configuration; @@ -8,27 +10,17 @@ use League\HTMLToMarkdown\ElementInterface; class DefaultConverter implements ConverterInterface, ConfigurationAwareInterface { - const DEFAULT_CONVERTER = '_default'; + public const DEFAULT_CONVERTER = '_default'; - /** - * @var Configuration - */ + /** @var Configuration */ protected $config; - /** - * @param Configuration $config - */ - public function setConfig(Configuration $config) + 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 { // If strip_tags is false (the default), preserve tags that don't have Markdown equivalents, // such as <span> 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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\Configuration; @@ -8,38 +10,28 @@ use League\HTMLToMarkdown\ElementInterface; class DivConverter implements ConverterInterface, ConfigurationAwareInterface { - /** - * @var Configuration - */ + /** @var Configuration */ protected $config; - /** - * @param Configuration $config - */ - public function setConfig(Configuration $config) + 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 { 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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\Configuration; @@ -8,50 +10,63 @@ use League\HTMLToMarkdown\ElementInterface; class EmphasisConverter implements ConverterInterface, ConfigurationAwareInterface { - /** - * @var Configuration - */ + /** @var Configuration */ protected $config; - /** - * @param Configuration $config - */ - public function setConfig(Configuration $config) + protected function getNormTag(?ElementInterface $element): string + { + if ($element !== null && ! $element->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 <em>foo</em><em>bar</em> 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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\Configuration; @@ -8,35 +10,25 @@ use League\HTMLToMarkdown\ElementInterface; class HardBreakConverter implements ConverterInterface, ConfigurationAwareInterface { - /** - * @var Configuration - */ + /** @var Configuration */ protected $config; - /** - * @param Configuration $config - */ - public function setConfig(Configuration $config) + 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 { $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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\Configuration; @@ -8,37 +10,27 @@ use League\HTMLToMarkdown\ElementInterface; class HeaderConverter implements ConverterInterface, ConfigurationAwareInterface { - const STYLE_ATX = 'atx'; - const STYLE_SETEXT = 'setext'; + public const STYLE_ATX = 'atx'; + public const STYLE_SETEXT = 'setext'; - /** - * @var Configuration - */ + /** @var Configuration */ protected $config; - /** - * @param Configuration $config - */ - public function setConfig(Configuration $config) + 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 { - $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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; class HorizontalRuleConverter implements ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { return "- - - - - -\n\n"; } @@ -19,8 +16,8 @@ class HorizontalRuleConverter implements ConverterInterface /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('hr'); + return ['hr']; } } diff --git a/vendor/league/html-to-markdown/src/Converter/ImageConverter.php b/vendor/league/html-to-markdown/src/Converter/ImageConverter.php index 657c769c2..5cd8aec67 100644 --- a/vendor/league/html-to-markdown/src/Converter/ImageConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/ImageConverter.php @@ -1,20 +1,17 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; class ImageConverter implements ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { - $src = $element->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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\Configuration; @@ -8,28 +10,19 @@ use League\HTMLToMarkdown\ElementInterface; class LinkConverter implements ConverterInterface, ConfigurationAwareInterface { - /** - * @var Configuration - */ + /** @var Configuration */ protected $config; - /** - * @param Configuration $config - */ - public function setConfig(Configuration $config) { + 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 { - $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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; class ListBlockConverter implements ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { return $element->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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\Configuration; @@ -8,39 +10,26 @@ use League\HTMLToMarkdown\ElementInterface; class ListItemConverter implements ConverterInterface, ConfigurationAwareInterface { - /** - * @var Configuration - */ + /** @var Configuration */ protected $config; - /** - * @var string - */ + /** @var string|null */ protected $listItemStyle; - /** - * @param Configuration $config - */ - public function setConfig(Configuration $config) + 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 { // 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 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; class ParagraphConverter implements ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { $value = $element->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( - '<!--' - ); + $escapable = [ + '<!--', + ]; foreach ($escapable as $i) { - if (strpos($line, $i) !== false) { - // Found an escapable character, escaping it - $line = substr_replace($line, '\\', strpos($line, $i), 0); + if (($pos = \strpos($line, $i)) === false) { + continue; } + + // Found an escapable character, escaping it + $line = \substr_replace($line, '\\', $pos, 0); } return $line; } - /** - * @param string $line - * - * @return string - */ - private function escapeOtherCharactersRegex($line) + private function escapeOtherCharactersRegex(string $line): string { - $regExs = array( + $regExs = [ // Match numbers ending on ')' or '.' that are at the beginning of the line. // They will be escaped if immediately followed by a space or newline. - '/^[0-9]+(?=(\)|\.)( |$))/' - ); + '/^[0-9]+(?=(\)|\.)( |$))/', + ]; foreach ($regExs as $i) { - if (preg_match($i, $line, $match)) { - // Matched an escapable character, adding a backslash on the string before the offending character - $line = substr_replace($line, '\\', strlen($match[0]), 0); + if (! \preg_match($i, $line, $match)) { + continue; } + + // Matched an escapable character, adding a backslash on the string before the offending character + $line = \substr_replace($line, '\\', \strlen($match[0]), 0); } return $line; diff --git a/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php b/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php index 321c898b1..7d8ccc132 100644 --- a/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/PreformattedConverter.php @@ -1,20 +1,17 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; class PreformattedConverter implements ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { - $pre_content = html_entity_decode($element->getChildrenAsString()); - $pre_content = str_replace(array('<pre>', '</pre>'), '', $pre_content); + $preContent = \html_entity_decode($element->getChildrenAsString()); + $preContent = \str_replace(['<pre>', '</pre>'], '', $preContent); /* * Checking for the code tag. @@ -23,36 +20,37 @@ class PreformattedConverter implements ConverterInterface * 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 . "\n\n"; + $firstBacktick = \strpos(\trim($preContent), '`'); + $lastBacktick = \strrpos(\trim($preContent), '`'); + if ($firstBacktick === 0 && $lastBacktick === \strlen(\trim($preContent)) - 1) { + return $preContent . "\n\n"; } // If the execution reaches this point it means it's just a pre tag, with no code tag nested // Empty lines are a special case - if ($pre_content === '') { + if ($preContent === '') { return "```\n```\n\n"; } // Normalizing new lines - $pre_content = preg_replace('/\r\n|\r|\n/', "\n", $pre_content); + $preContent = \preg_replace('/\r\n|\r|\n/', "\n", $preContent); + \assert(\is_string($preContent)); // Ensure there's a newline at the end - if (strrpos($pre_content, "\n") !== strlen($pre_content) - strlen("\n")) { - $pre_content .= "\n"; + if (\strrpos($preContent, "\n") !== \strlen($preContent) - \strlen("\n")) { + $preContent .= "\n"; } // Use three backticks - return "```\n" . $pre_content . "```\n\n"; + return "```\n" . $preContent . "```\n\n"; } /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('pre'); + return ['pre']; } } diff --git a/vendor/league/html-to-markdown/src/Converter/TableConverter.php b/vendor/league/html-to-markdown/src/Converter/TableConverter.php new file mode 100644 index 000000000..2e63e9a5b --- /dev/null +++ b/vendor/league/html-to-markdown/src/Converter/TableConverter.php @@ -0,0 +1,113 @@ +<?php + +declare(strict_types=1); + +namespace League\HTMLToMarkdown\Converter; + +use League\HTMLToMarkdown\Configuration; +use League\HTMLToMarkdown\ConfigurationAwareInterface; +use League\HTMLToMarkdown\ElementInterface; +use League\HTMLToMarkdown\PreConverterInterface; + +class TableConverter implements ConverterInterface, PreConverterInterface, ConfigurationAwareInterface +{ + /** @var Configuration */ + protected $config; + + public function setConfig(Configuration $config): void + { + $this->config = $config; + } + + /** @var array<string, string> */ + private static $alignments = [ + 'left' => ':--', + 'right' => '--:', + 'center' => ':-:', + ]; + + /** @var array<int, string>|null */ + private $columnAlignments = []; + + /** @var string|null */ + private $caption = null; + + public function preConvert(ElementInterface $element): void + { + $tag = $element->getTagName(); + // Only table cells and caption are allowed to contain content. + // Remove all text between other table elements. + if ($tag === 'th' || $tag === 'td' || $tag === 'caption') { + return; + } + + foreach ($element->getChildren() as $child) { + if ($child->isText()) { + $child->setFinalMarkdown(''); + } + } + } + + public function convert(ElementInterface $element): string + { + $value = $element->getValue(); + + switch ($element->getTagName()) { + case 'table': + $this->columnAlignments = []; + if ($this->caption) { + $side = $this->config->getOption('table_caption_side'); + if ($side === 'top') { + $value = $this->caption . "\n" . $value; + } elseif ($side === 'bottom') { + $value .= $this->caption; + } + + $this->caption = null; + } + + return $value . "\n"; + case 'caption': + $this->caption = \trim($value); + + return ''; + case 'tr': + $value .= "|\n"; + if ($this->columnAlignments !== null) { + $value .= '|' . \implode('|', $this->columnAlignments) . "|\n"; + + $this->columnAlignments = null; + } + + return $value; + case 'th': + case 'td': + if ($this->columnAlignments !== null) { + $align = $element->getAttribute('align'); + + $this->columnAlignments[] = self::$alignments[$align] ?? '---'; + } + + $value = \str_replace("\n", ' ', $value); + $value = \str_replace('|', $this->config->getOption('table_pipe_escape') ?? '\|', $value); + + return '| ' . \trim($value) . ' '; + case 'thead': + case 'tbody': + case 'tfoot': + case 'colgroup': + case 'col': + return $value; + default: + return ''; + } + } + + /** + * @return string[] + */ + public function getSupportedTags(): array + { + return ['table', 'tr', 'th', 'td', 'thead', 'tbody', 'tfoot', 'colgroup', 'col', 'caption']; + } +} diff --git a/vendor/league/html-to-markdown/src/Converter/TextConverter.php b/vendor/league/html-to-markdown/src/Converter/TextConverter.php index 6236a1e9d..465dd4883 100644 --- a/vendor/league/html-to-markdown/src/Converter/TextConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/TextConverter.php @@ -1,48 +1,48 @@ <?php +declare(strict_types=1); + namespace League\HTMLToMarkdown\Converter; use League\HTMLToMarkdown\ElementInterface; class TextConverter implements ConverterInterface { - /** - * @param ElementInterface $element - * - * @return string - */ - public function convert(ElementInterface $element) + public function convert(ElementInterface $element): string { $markdown = $element->getValue(); // Remove leftover \n at the beginning of the line - $markdown = ltrim($markdown, "\n"); + $markdown = \ltrim($markdown, "\n"); // Replace sequences of invisible characters with spaces - $markdown = preg_replace('~\s+~u', ' ', $markdown); + $markdown = \preg_replace('~\s+~u', ' ', $markdown); + \assert(\is_string($markdown)); // Escape the following characters: '*', '_', '[', ']' and '\' - if ($element->getParent() && $element->getParent()->getTagName() !== 'div') { - $markdown = preg_replace('~([*_\\[\\]\\\\])~u', '\\\\$1', $markdown); + if (($parent = $element->getParent()) && $parent->getTagName() !== 'div') { + $markdown = \preg_replace('~([*_\\[\\]\\\\])~u', '\\\\$1', $markdown); + \assert(\is_string($markdown)); } - $markdown = preg_replace('~^#~u', '\\\\#', $markdown); + $markdown = \preg_replace('~^#~u', '\\\\#', $markdown); + \assert(\is_string($markdown)); if ($markdown === ' ') { $next = $element->getNext(); - if (!$next || $next->isBlock()) { + if (! $next || $next->isBlock()) { $markdown = ''; } } - return htmlspecialchars($markdown, ENT_NOQUOTES, 'UTF-8'); + return \htmlspecialchars($markdown, ENT_NOQUOTES, 'UTF-8'); } /** * @return string[] */ - public function getSupportedTags() + public function getSupportedTags(): array { - return array('#text'); + return ['#text']; } } |