mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ac82f3c87 | ||
|
|
85fb92a042 | ||
|
|
f2a43b476c | ||
|
|
6def743902 | ||
|
|
8a44926392 | ||
|
|
8c7740f073 | ||
|
|
6a4720c951 | ||
|
|
7a9a82b543 | ||
|
|
8a91d36294 | ||
|
|
669adfb20f | ||
|
|
445b889efb | ||
|
|
8b1c3f147d |
+1
-1
@@ -46,7 +46,7 @@ before_script:
|
||||
|
||||
script:
|
||||
- mkdir -p build/logs
|
||||
- phpunit -v --coverage-clover build/logs/clover.xml
|
||||
- php vendor/bin/simple-phpunit -v --coverage-clover build/logs/clover.xml
|
||||
- if [ "$CS_FIXER" = "run" ]; then php vendor/bin/php-cs-fixer fix --verbose --dry-run ; fi;
|
||||
|
||||
after_script:
|
||||
|
||||
@@ -17,9 +17,9 @@ The default php-readability lib is really old and needs to be improved. I found
|
||||
|
||||
## Requirements
|
||||
|
||||
By default, this lib will use the [Tidy extension](https://github.com/htacg/tidy-html5) if it's available. Tidy is only used to cleanup the given HTML and avoid problems with bad HTML structure, etc ..
|
||||
By default, this lib will use the [Tidy extension](https://github.com/htacg/tidy-html5) if it's available. Tidy is only used to cleanup the given HTML and avoid problems with bad HTML structure, etc .. It'll be suggested by Composer.
|
||||
|
||||
Since Composer doesn't support suggestion on PHP extension, I write this suggestion here.
|
||||
Also, if you got problem from parsing a content without Tidy installed, please install it and try again.
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
+6
-1
@@ -25,12 +25,17 @@
|
||||
}],
|
||||
"require": {
|
||||
"php": ">=5.3.3",
|
||||
"ext-mbstring": "*",
|
||||
"psr/log": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"satooshi/php-coveralls": "~0.6",
|
||||
"friendsofphp/php-cs-fixer": "<2",
|
||||
"monolog/monolog": "^1.13"
|
||||
"monolog/monolog": "^1.13",
|
||||
"symfony/phpunit-bridge": "^3.2"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-tidy": "Used to clean up given HTML and to avoid problems with bad HTML structure."
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "Readability\\": "src/" }
|
||||
|
||||
+50
-43
@@ -45,52 +45,59 @@ class JSLikeHTMLElement extends \DOMElement
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
if ($name === 'innerHTML') {
|
||||
// first, empty the element
|
||||
for ($x = $this->childNodes->length - 1; $x >= 0; --$x) {
|
||||
$this->removeChild($this->childNodes->item($x));
|
||||
}
|
||||
|
||||
// $value holds our new inner HTML
|
||||
if ($value !== '') {
|
||||
$f = $this->ownerDocument->createDocumentFragment();
|
||||
|
||||
// appendXML() expects well-formed markup (XHTML)
|
||||
// @ to suppress PHP warnings
|
||||
$result = @$f->appendXML($value);
|
||||
if ($result) {
|
||||
if ($f->hasChildNodes()) {
|
||||
$this->appendChild($f);
|
||||
}
|
||||
} else {
|
||||
// $value is probably ill-formed
|
||||
$f = new \DOMDocument();
|
||||
$value = mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8');
|
||||
|
||||
// Using <htmlfragment> will generate a warning, but so will bad HTML
|
||||
// (and by this point, bad HTML is what we've got).
|
||||
// We use it (and suppress the warning) because an HTML fragment will
|
||||
// be wrapped around <html><body> tags which we don't really want to keep.
|
||||
// Note: despite the warning, if loadHTML succeeds it will return true.
|
||||
$result = @$f->loadHTML('<htmlfragment>' . $value . '</htmlfragment>');
|
||||
|
||||
if ($result) {
|
||||
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
||||
|
||||
foreach ($import->childNodes as $child) {
|
||||
$importedNode = $this->ownerDocument->importNode($child, true);
|
||||
$this->appendChild($importedNode);
|
||||
}
|
||||
} else {
|
||||
// oh well, we tried, we really did. :(
|
||||
// this element is now empty
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($name !== 'innerHTML') {
|
||||
$trace = debug_backtrace();
|
||||
trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// first, empty the element
|
||||
for ($x = $this->childNodes->length - 1; $x >= 0; --$x) {
|
||||
$this->removeChild($this->childNodes->item($x));
|
||||
}
|
||||
|
||||
// $value holds our new inner HTML
|
||||
$value = trim($value);
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ensure bad entity won't generate warning
|
||||
$previousError = libxml_use_internal_errors(true);
|
||||
|
||||
$f = $this->ownerDocument->createDocumentFragment();
|
||||
|
||||
// appendXML() expects well-formed markup (XHTML)
|
||||
$result = $f->appendXML($value);
|
||||
if ($result) {
|
||||
if ($f->hasChildNodes()) {
|
||||
$this->appendChild($f);
|
||||
}
|
||||
} else {
|
||||
// $value is probably ill-formed
|
||||
$f = new \DOMDocument();
|
||||
$value = mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8');
|
||||
|
||||
// Using <htmlfragment> will generate a warning, but so will bad HTML
|
||||
// (and by this point, bad HTML is what we've got).
|
||||
// We use it (and suppress the warning) because an HTML fragment will
|
||||
// be wrapped around <html><body> tags which we don't really want to keep.
|
||||
// Note: despite the warning, if loadHTML succeeds it will return true.
|
||||
$result = $f->loadHTML('<htmlfragment>' . $value . '</htmlfragment>');
|
||||
|
||||
if ($result) {
|
||||
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
||||
|
||||
foreach ($import->childNodes as $child) {
|
||||
$importedNode = $this->ownerDocument->importNode($child, true);
|
||||
$this->appendChild($importedNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
libxml_clear_errors();
|
||||
libxml_use_internal_errors($previousError);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@ class Readability implements LoggerAwareInterface
|
||||
* Defined up here so we don't instantiate them repeatedly in loops.
|
||||
*/
|
||||
public $regexps = array(
|
||||
'unlikelyCandidates' => '/display\s*:\s*none|ignore|\binfos?\b|annoy|clock|date|time|author|intro|links|hidd?e|about|archive|\bprint|bookmark|tags|tag-list|share|search|social|robot|published|combx|comment|mast(?:head)|subscri|community|category|disqus|extra|head|head(?:er|note)|floor|foot(?:er|note)|menu|tool\b|function|nav|remark|rss|shoutbox|widget|meta|banner|sponsor|adsense|inner-?ad|ad-|sponsor|\badv\b|\bads\b|agr?egate?|pager|sidebar|popup|tweet|twitter/i',
|
||||
'unlikelyCandidates' => '/display\s*:\s*none|ignore|\binfos?\b|annoy|clock|date|time|author|intro|hidd?e|about|archive|\bprint|bookmark|tags|tag-list|share|search|social|robot|published|combx|comment|mast(?:head)|subscri|community|category|disqus|extra|head|head(?:er|note)|floor|foot(?:er|note)|menu|tool\b|function|nav|remark|rss|shoutbox|widget|meta|banner|sponsor|adsense|inner-?ad|ad-|sponsor|\badv\b|\bads\b|agr?egate?|pager|sidebar|popup|tweet|twitter/i',
|
||||
'okMaybeItsACandidate' => '/article\b|contain|\bcontent|column|general|detail|shadow|lightbox|blog|body|entry|main|page/i',
|
||||
'positive' => '/read|full|article|body|\bcontent|contain|entry|main|markdown|page|attach|pagination|post|text|blog|story/i',
|
||||
'negative' => '/bottom|stat|info|discuss|e[\-]?mail|comment|reply|log.{2}(n|ed)|sign|single|combx|com-|contact|_nav|link|media|\bout|promo|\bad-|related|scroll|shoutbox|sidebar|sponsor|shopping|teaser|recommend/i',
|
||||
|
||||
@@ -443,7 +443,7 @@ class ReadabilityTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testPostFilters()
|
||||
{
|
||||
$readability = $this->getReadability('<div>' . str_repeat('<p>This <b>is</b> the awesome content :)</p>', 7) . '</div>', 'http://0.0.0.0');
|
||||
$readability = $this->getReadability('<div>' . str_repeat('<p>This <b>is</b> the awesome content :)</p>', 10) . '</div>', 'http://0.0.0.0');
|
||||
$readability->addPostFilter('!<strong[^>]*>(.*?)</strong>!is', '');
|
||||
|
||||
$res = $readability->init();
|
||||
|
||||
Reference in New Issue
Block a user