Upstream thephpleague/html-to-markdown state of the touched files before aa29149d30
This commit is contained in:
1 parent
94062cea69
commit
87f03af20c
3 files changed
+121
-2
No files matched your search
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\HTMLToMarkdown\Converter;
|
||||
|
||||
use League\HTMLToMarkdown\Coerce;
|
||||
use League\HTMLToMarkdown\Configuration;
|
||||
use League\HTMLToMarkdown\ConfigurationAwareInterface;
|
||||
use League\HTMLToMarkdown\ElementInterface;
|
||||
|
||||
class EmphasisConverter implements ConverterInterface, ConfigurationAwareInterface
|
||||
{
|
||||
/** @var Configuration */
|
||||
protected $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;
|
||||
}
|
||||
|
||||
public function convert(ElementInterface $element): string
|
||||
{
|
||||
$tag = $this->getNormTag($element);
|
||||
$value = $element->getValue();
|
||||
|
||||
if (! \trim($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ($tag === 'em') {
|
||||
$style = Coerce::toString($this->config->getOption('italic_style'));
|
||||
} else {
|
||||
$style = Coerce::toString($this->config->getOption('bold_style'));
|
||||
}
|
||||
|
||||
$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 . $preStyle . \trim($value) . $postStyle . $suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSupportedTags(): array
|
||||
{
|
||||
return ['em', 'i', 'strong', 'b'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\HTMLToMarkdown\Converter;
|
||||
|
||||
use League\HTMLToMarkdown\ElementInterface;
|
||||
|
||||
class TextConverter implements ConverterInterface
|
||||
{
|
||||
public function convert(ElementInterface $element): string
|
||||
{
|
||||
$markdown = $element->getValue();
|
||||
|
||||
// Remove leftover \n at the beginning of the line
|
||||
$markdown = \ltrim($markdown, "\n");
|
||||
|
||||
// Replace sequences of invisible characters with spaces
|
||||
$markdown = \preg_replace('~\s+~u', ' ', $markdown);
|
||||
\assert(\is_string($markdown));
|
||||
|
||||
// Escape the following characters: '*', '_', '[', ']' and '\'
|
||||
if (($parent = $element->getParent()) && $parent->getTagName() !== 'div') {
|
||||
$markdown = \preg_replace('~([*_\\[\\]\\\\])~u', '\\\\$1', $markdown);
|
||||
\assert(\is_string($markdown));
|
||||
}
|
||||
|
||||
$markdown = \preg_replace('~^#~u', '\\\\#', $markdown);
|
||||
\assert(\is_string($markdown));
|
||||
|
||||
if ($markdown === ' ') {
|
||||
$next = $element->getNext();
|
||||
if (! $next || $next->isBlock()) {
|
||||
$markdown = '';
|
||||
}
|
||||
}
|
||||
|
||||
return \htmlspecialchars($markdown, ENT_NOQUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSupportedTags(): array
|
||||
{
|
||||
return ['#text'];
|
||||
}
|
||||
}
|
||||
@@ -163,8 +163,6 @@ class HtmlConverterTest extends TestCase
|
||||
$this->assertHtmlGivesMarkdown('<a href="google.com">google.com</a>', '[google.com](google.com)');
|
||||
$this->assertHtmlGivesMarkdown('<a href="https://www.google.com">https://www.google.com</a>', '<https://www.google.com>');
|
||||
$this->assertHtmlGivesMarkdown('<a href="ftp://files.example.com">ftp://files.example.com</a>', '<ftp://files.example.com>');
|
||||
$this->assertHtmlGivesMarkdown('<a href="https://example.com/?a=1&b=2">https://example.com/?a=1&b=2</a>', '<https://example.com/?a=1&b=2>');
|
||||
$this->assertHtmlGivesMarkdown('<a href="https://example.com/<b>">https://example.com/<b></a>', '[https://example.com/<b>](https://example.com/<b>)');
|
||||
$this->assertHtmlGivesMarkdown('<a href="mailto:test@example.com">test@example.com</a>', '<test@example.com>');
|
||||
$this->assertHtmlGivesMarkdown('<a href="mailto:test+foo@example.bar-baz.com">test+foo@example.bar-baz.com</a>', '<test+foo@example.bar-baz.com>');
|
||||
|
||||
|
||||
Reference in new issue
Block a user