mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 14:36:23 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ac82f3c87 | ||
|
|
85fb92a042 | ||
|
|
f2a43b476c | ||
|
|
6def743902 | ||
|
|
8a44926392 | ||
|
|
8c7740f073 | ||
|
|
6a4720c951 | ||
|
|
7a9a82b543 | ||
|
|
8a91d36294 | ||
|
|
669adfb20f | ||
|
|
445b889efb | ||
|
|
8b1c3f147d | ||
|
|
a505a13d0c | ||
|
|
9fe0d07805 |
+1
-1
@@ -46,7 +46,7 @@ before_script:
|
|||||||
|
|
||||||
script:
|
script:
|
||||||
- mkdir -p build/logs
|
- 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;
|
- if [ "$CS_FIXER" = "run" ]; then php vendor/bin/php-cs-fixer fix --verbose --dry-run ; fi;
|
||||||
|
|
||||||
after_script:
|
after_script:
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ The default php-readability lib is really old and needs to be improved. I found
|
|||||||
|
|
||||||
## Requirements
|
## 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
|
## Usage
|
||||||
|
|
||||||
|
|||||||
+8
-2
@@ -25,11 +25,17 @@
|
|||||||
}],
|
}],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.3",
|
"php": ">=5.3.3",
|
||||||
"monolog/monolog": "^1.13"
|
"ext-mbstring": "*",
|
||||||
|
"psr/log": "^1.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"satooshi/php-coveralls": "~0.6",
|
"satooshi/php-coveralls": "~0.6",
|
||||||
"friendsofphp/php-cs-fixer": "<2"
|
"friendsofphp/php-cs-fixer": "<2",
|
||||||
|
"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": {
|
"autoload": {
|
||||||
"psr-4": { "Readability\\": "src/" }
|
"psr-4": { "Readability\\": "src/" }
|
||||||
|
|||||||
+20
-13
@@ -45,19 +45,31 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
*/
|
*/
|
||||||
public function __set($name, $value)
|
public function __set($name, $value)
|
||||||
{
|
{
|
||||||
if ($name === 'innerHTML') {
|
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
|
// first, empty the element
|
||||||
for ($x = $this->childNodes->length - 1; $x >= 0; --$x) {
|
for ($x = $this->childNodes->length - 1; $x >= 0; --$x) {
|
||||||
$this->removeChild($this->childNodes->item($x));
|
$this->removeChild($this->childNodes->item($x));
|
||||||
}
|
}
|
||||||
|
|
||||||
// $value holds our new inner HTML
|
// $value holds our new inner HTML
|
||||||
if ($value !== '') {
|
$value = trim($value);
|
||||||
|
if (empty($value)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure bad entity won't generate warning
|
||||||
|
$previousError = libxml_use_internal_errors(true);
|
||||||
|
|
||||||
$f = $this->ownerDocument->createDocumentFragment();
|
$f = $this->ownerDocument->createDocumentFragment();
|
||||||
|
|
||||||
// appendXML() expects well-formed markup (XHTML)
|
// appendXML() expects well-formed markup (XHTML)
|
||||||
// @ to suppress PHP warnings
|
$result = $f->appendXML($value);
|
||||||
$result = @$f->appendXML($value);
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
if ($f->hasChildNodes()) {
|
if ($f->hasChildNodes()) {
|
||||||
$this->appendChild($f);
|
$this->appendChild($f);
|
||||||
@@ -72,7 +84,7 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
// We use it (and suppress the warning) because an HTML fragment will
|
// 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.
|
// 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.
|
// Note: despite the warning, if loadHTML succeeds it will return true.
|
||||||
$result = @$f->loadHTML('<htmlfragment>' . $value . '</htmlfragment>');
|
$result = $f->loadHTML('<htmlfragment>' . $value . '</htmlfragment>');
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
$import = $f->getElementsByTagName('htmlfragment')->item(0);
|
||||||
@@ -81,16 +93,11 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
$importedNode = $this->ownerDocument->importNode($child, true);
|
$importedNode = $this->ownerDocument->importNode($child, true);
|
||||||
$this->appendChild($importedNode);
|
$this->appendChild($importedNode);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// oh well, we tried, we really did. :(
|
|
||||||
// this element is now empty
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
libxml_clear_errors();
|
||||||
$trace = debug_backtrace();
|
libxml_use_internal_errors($previousError);
|
||||||
trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+1
-1
@@ -83,7 +83,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
* Defined up here so we don't instantiate them repeatedly in loops.
|
* Defined up here so we don't instantiate them repeatedly in loops.
|
||||||
*/
|
*/
|
||||||
public $regexps = array(
|
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',
|
'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',
|
'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',
|
'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()
|
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', '');
|
$readability->addPostFilter('!<strong[^>]*>(.*?)</strong>!is', '');
|
||||||
|
|
||||||
$res = $readability->init();
|
$res = $readability->init();
|
||||||
|
|||||||
Reference in New Issue
Block a user