aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/league/html-to-markdown/src/Converter/CodeConverter.php
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2021-06-05 08:47:24 +0000
committerMario <mario@mariovavti.com>2021-06-05 08:47:24 +0000
commitfd8a5ff4c49fc1361f9928ea4d33e6d24d43a3a5 (patch)
tree7560147f31a273fb8f0e05476f99f1deea0e12de /vendor/league/html-to-markdown/src/Converter/CodeConverter.php
parent4db384da34595adef68be6226e8b331b4d7b7f31 (diff)
downloadvolse-hubzilla-fd8a5ff4c49fc1361f9928ea4d33e6d24d43a3a5.tar.gz
volse-hubzilla-fd8a5ff4c49fc1361f9928ea4d33e6d24d43a3a5.tar.bz2
volse-hubzilla-fd8a5ff4c49fc1361f9928ea4d33e6d24d43a3a5.zip
composer update league/html-to-markdown
Diffstat (limited to 'vendor/league/html-to-markdown/src/Converter/CodeConverter.php')
-rw-r--r--vendor/league/html-to-markdown/src/Converter/CodeConverter.php45
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;
}
}