mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 14:36:23 +00:00
Compare commits
52
Commits
1312ffbced
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44c3e02f01 | ||
|
|
a57cc7b25a | ||
|
|
67b869b37f | ||
|
|
a76eaa555a | ||
|
|
7701717086 | ||
|
|
0dab8c72c4 | ||
|
|
4c0b341bff | ||
|
|
9736f08a12 | ||
|
|
3e159d4e24 | ||
|
|
3677c91b29 | ||
|
|
c88b196ee6 | ||
|
|
ad05b00797 | ||
|
|
cbad259ba2 | ||
|
|
a3cc317102 | ||
|
|
425a42f239 | ||
|
|
9a8d33c6ba | ||
|
|
ced50c9c10 | ||
|
|
f991d80b5a | ||
|
|
1697cdbeee | ||
|
|
eb9d8d067c | ||
|
|
9052618a31 | ||
|
|
3d332b4056 | ||
|
|
6abe0c4054 | ||
|
|
00a4d5f34d | ||
|
|
959c0680a8 | ||
|
|
74c7b80b32 | ||
|
|
33b69bdb6b | ||
|
|
f8aabce4e0 | ||
|
|
2e9d05c239 | ||
|
|
85fbb6cbf3 | ||
|
|
67a2b07505 | ||
|
|
b71d73f0e0 | ||
|
|
1dc5f6bc78 | ||
|
|
f19d7ba19e | ||
|
|
a93488d727 | ||
|
|
ac24ef54a4 | ||
|
|
76547fef78 | ||
|
|
228bc7ee1d | ||
|
|
35b87585e7 | ||
|
|
4230b2d7ca | ||
|
|
85be584f94 | ||
|
|
03a960daf0 | ||
|
|
6f62bcf662 | ||
|
|
3e3114a492 | ||
|
|
f98247ed14 | ||
|
|
5a6e525ff5 | ||
|
|
0de328760a | ||
|
|
5aa9da6843 | ||
|
|
1bb7eec83c | ||
|
|
049bd07074 | ||
|
|
ca1b105f40 | ||
|
|
5ad159ebb1 |
+4
-4
@@ -30,12 +30,12 @@
|
||||
"masterminds/html5": "^2.7"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "3.95.8",
|
||||
"friendsofphp/php-cs-fixer": "3.95.26",
|
||||
"monolog/monolog": "^1.24|^2.1",
|
||||
"symfony/phpunit-bridge": "^4.4|^5.3|^6.0|^7.0",
|
||||
"phpstan/phpstan": "2.2.2",
|
||||
"phpstan/phpstan-phpunit": "2.0.16",
|
||||
"rector/rector": "2.4.6"
|
||||
"phpstan/phpstan": "2.2.14",
|
||||
"phpstan/phpstan-phpunit": "2.0.18",
|
||||
"rector/rector": "2.6.7"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-tidy": "Used to clean up given HTML and to avoid problems with bad HTML structure."
|
||||
|
||||
+73
-71
@@ -213,7 +213,9 @@ class Readability implements LoggerAwareInterface
|
||||
*/
|
||||
public function init(): bool
|
||||
{
|
||||
$this->loadHtml();
|
||||
if (!isset($this->dom)) {
|
||||
$this->loadHtml();
|
||||
}
|
||||
|
||||
if (!isset($this->dom->documentElement)) {
|
||||
return false;
|
||||
@@ -751,6 +753,76 @@ class Readability implements LoggerAwareInterface
|
||||
$this->flags &= ~$flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load HTML in a DOMDocument.
|
||||
* Apply Pre filters
|
||||
* Cleanup HTML using Tidy (or not).
|
||||
*/
|
||||
public function loadHtml(): void
|
||||
{
|
||||
$this->original_html = $this->html;
|
||||
|
||||
$this->logger->debug('Parsing URL: ' . $this->url);
|
||||
|
||||
if ($this->url) {
|
||||
$host = parse_url($this->url, \PHP_URL_HOST);
|
||||
if (null !== $host) {
|
||||
$this->domainRegExp = '/' . strtr((string) preg_replace('/www\d*\./', '', $host), ['.' => '\.']) . '/';
|
||||
}
|
||||
}
|
||||
|
||||
mb_internal_encoding('UTF-8');
|
||||
mb_http_output('UTF-8');
|
||||
mb_regex_encoding('UTF-8');
|
||||
|
||||
// HACK: dirty cleanup to replace some stuff; shouldn't use regexps with HTML but well...
|
||||
if (!$this->flagIsActive(self::FLAG_DISABLE_PREFILTER)) {
|
||||
foreach ($this->pre_filters as $search => $replace) {
|
||||
$this->html = preg_replace($search, $replace, $this->html);
|
||||
}
|
||||
unset($search, $replace);
|
||||
}
|
||||
|
||||
if ('' === trim($this->html)) {
|
||||
$this->html = '<html></html>';
|
||||
}
|
||||
|
||||
/*
|
||||
* Use tidy (if it exists).
|
||||
* This fixes problems with some sites which would otherwise trouble DOMDocument's HTML parsing.
|
||||
* Although sometimes it makes matters worse, which is why there is an option to disable it.
|
||||
*/
|
||||
if ($this->useTidy) {
|
||||
$this->logger->debug('Tidying document');
|
||||
|
||||
$tidy = tidy_repair_string($this->html, $this->tidy_config, 'UTF8');
|
||||
if (false !== $tidy && $this->html !== $tidy) {
|
||||
$this->tidied = true;
|
||||
$this->html = $tidy;
|
||||
$this->html = preg_replace('/[\r\n]+/is', "\n", $this->html);
|
||||
}
|
||||
unset($tidy);
|
||||
}
|
||||
|
||||
$this->html = self::entitizeNonAscii((string) $this->html);
|
||||
|
||||
if ('html5lib' === $this->parser || 'html5' === $this->parser) {
|
||||
$this->dom = (new HTML5())->loadHTML($this->html);
|
||||
}
|
||||
|
||||
if ('libxml' === $this->parser) {
|
||||
libxml_use_internal_errors(true);
|
||||
|
||||
$this->dom = new \DOMDocument();
|
||||
$this->dom->preserveWhiteSpace = false;
|
||||
$this->dom->loadHTML($this->html, \LIBXML_NOBLANKS | \LIBXML_COMPACT | \LIBXML_NOERROR);
|
||||
|
||||
libxml_use_internal_errors(false);
|
||||
}
|
||||
|
||||
$this->dom->registerNodeClass(\DOMElement::class, JSLikeHTMLElement::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the article title as an H1.
|
||||
*
|
||||
@@ -1375,76 +1447,6 @@ class Readability implements LoggerAwareInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load HTML in a DOMDocument.
|
||||
* Apply Pre filters
|
||||
* Cleanup HTML using Tidy (or not).
|
||||
*/
|
||||
private function loadHtml(): void
|
||||
{
|
||||
$this->original_html = $this->html;
|
||||
|
||||
$this->logger->debug('Parsing URL: ' . $this->url);
|
||||
|
||||
if ($this->url) {
|
||||
$host = parse_url($this->url, \PHP_URL_HOST);
|
||||
if (null !== $host) {
|
||||
$this->domainRegExp = '/' . strtr((string) preg_replace('/www\d*\./', '', $host), ['.' => '\.']) . '/';
|
||||
}
|
||||
}
|
||||
|
||||
mb_internal_encoding('UTF-8');
|
||||
mb_http_output('UTF-8');
|
||||
mb_regex_encoding('UTF-8');
|
||||
|
||||
// HACK: dirty cleanup to replace some stuff; shouldn't use regexps with HTML but well...
|
||||
if (!$this->flagIsActive(self::FLAG_DISABLE_PREFILTER)) {
|
||||
foreach ($this->pre_filters as $search => $replace) {
|
||||
$this->html = preg_replace($search, $replace, $this->html);
|
||||
}
|
||||
unset($search, $replace);
|
||||
}
|
||||
|
||||
if ('' === trim($this->html)) {
|
||||
$this->html = '<html></html>';
|
||||
}
|
||||
|
||||
/*
|
||||
* Use tidy (if it exists).
|
||||
* This fixes problems with some sites which would otherwise trouble DOMDocument's HTML parsing.
|
||||
* Although sometimes it makes matters worse, which is why there is an option to disable it.
|
||||
*/
|
||||
if ($this->useTidy) {
|
||||
$this->logger->debug('Tidying document');
|
||||
|
||||
$tidy = tidy_repair_string($this->html, $this->tidy_config, 'UTF8');
|
||||
if (false !== $tidy && $this->html !== $tidy) {
|
||||
$this->tidied = true;
|
||||
$this->html = $tidy;
|
||||
$this->html = preg_replace('/[\r\n]+/is', "\n", $this->html);
|
||||
}
|
||||
unset($tidy);
|
||||
}
|
||||
|
||||
$this->html = self::entitizeNonAscii((string) $this->html);
|
||||
|
||||
if ('html5lib' === $this->parser || 'html5' === $this->parser) {
|
||||
$this->dom = (new HTML5())->loadHTML($this->html);
|
||||
}
|
||||
|
||||
if ('libxml' === $this->parser) {
|
||||
libxml_use_internal_errors(true);
|
||||
|
||||
$this->dom = new \DOMDocument();
|
||||
$this->dom->preserveWhiteSpace = false;
|
||||
$this->dom->loadHTML($this->html, \LIBXML_NOBLANKS | \LIBXML_COMPACT | \LIBXML_NOERROR);
|
||||
|
||||
libxml_use_internal_errors(false);
|
||||
}
|
||||
|
||||
$this->dom->registerNodeClass(\DOMElement::class, JSLikeHTMLElement::class);
|
||||
}
|
||||
|
||||
private function getAncestors(\DOMElement $node, int $maxDepth = 0): array
|
||||
{
|
||||
$ancestors = [];
|
||||
|
||||
Reference in New Issue
Block a user