Skip to content

Commit 3058b65

Browse files
committed
[Mime] Add a way to control the HTML to text conversion
1 parent e6d5c73 commit 3058b65

6 files changed

+132
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\HtmlToTextConverter;
13+
14+
/**
15+
* @author Fabien Potencier <[email protected]>
16+
*/
17+
class DefaultHtmlToTextConverter implements HtmlToTextConverterInterface
18+
{
19+
public function convert(string $html, string $charset): string
20+
{
21+
return strip_tags(preg_replace('{<(head|style)\b.*?</\1>}is', '', $html));
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\HtmlToTextConverter;
13+
14+
/**
15+
* @author Fabien Potencier <[email protected]>
16+
*/
17+
interface HtmlToTextConverterInterface
18+
{
19+
/**
20+
* Converts and HTML representation of a Message to a text representation.
21+
*
22+
* The output must use the same charset as the HTML one.
23+
*/
24+
public function convert(string $html, string $charset): string;
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\HtmlToTextConverter;
13+
14+
use League\HTMLToMarkdown\HtmlConverter;
15+
use League\HTMLToMarkdown\HtmlConverterInterface;
16+
17+
/**
18+
* @author Fabien Potencier <[email protected]>
19+
*/
20+
class LeagueHtmlToMarkdownConverter implements HtmlToTextConverterInterface
21+
{
22+
public function __construct(
23+
private HtmlConverterInterface $converter = new HtmlConverter([
24+
'hard_break' => true,
25+
'strip_tags' => true,
26+
'remove_nodes' => 'head style',
27+
]),
28+
) {
29+
}
30+
31+
public function convert(string $html, string $charset): string
32+
{
33+
return $this->converter->convert($html);
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests\HtmlToTextConverter;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mime\HtmlToTextConverter\DefaultHtmlToTextConverter;
16+
17+
class DefaultHtmlToTextConverterTest extends TestCase
18+
{
19+
public function testConvert()
20+
{
21+
$converter = new DefaultHtmlToTextConverter();
22+
$this->assertSame('HTML', $converter->convert('<head><meta charset="utf-8"></head><b>HTML</b><style>css</style>', 'UTF-8'));
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests\HtmlToTextConverter;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mime\HtmlToTextConverter\LeagueHtmlToMarkdownConverter;
16+
17+
class LeagueHtmlToMarkdownConverterTest extends TestCase
18+
{
19+
public function testConvert()
20+
{
21+
$converter = new LeagueHtmlToMarkdownConverter();
22+
$this->assertSame('**HTML**', $converter->convert('<head><meta charset="utf-8"></head><b>HTML</b><style>css</style>', 'UTF-8'));
23+
}
24+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"require-dev": {
2424
"egulias/email-validator": "^2.1.10|^3.1",
25+
"league/html-to-markdown": "^5.0",
2526
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
2627
"symfony/dependency-injection": "^5.4|^6.0",
2728
"symfony/property-access": "^5.4|^6.0",

0 commit comments

Comments
 (0)