mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 14:36:23 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
41d7440c6e | ||
|
|
252bb4ef42 | ||
|
|
7c30d76b6e | ||
|
|
111cb08034 | ||
|
|
74fbf6f009 | ||
|
|
1565a6819a |
@@ -0,0 +1,8 @@
|
|||||||
|
/.editorconfig export-ignore
|
||||||
|
/.gitattributes export-ignore
|
||||||
|
/.gitignore export-ignore
|
||||||
|
/.scrutinizer.yml export-ignore
|
||||||
|
/.travis.yml export-ignore
|
||||||
|
/README.md export-ignore
|
||||||
|
/phpunit.xml.dist export-ignore
|
||||||
|
/tests export-ignore
|
||||||
@@ -3,11 +3,11 @@
|
|||||||
[](https://travis-ci.org/j0k3r/php-readability)
|
[](https://travis-ci.org/j0k3r/php-readability)
|
||||||
[](https://scrutinizer-ci.com/g/j0k3r/php-readability/?branch=master)
|
[](https://scrutinizer-ci.com/g/j0k3r/php-readability/?branch=master)
|
||||||
|
|
||||||
This is an extract of the Readability class from the [full-text-rss](https://github.com/Dither/full-text-rss) fork. It kind be defined as a better version of the original [php-readability](https://bitbucket.org/fivefilters/php-readability/overview).
|
This is an extract of the Readability class from this [full-text-rss](https://github.com/Dither/full-text-rss) fork. It can be defined as a better version of the original [php-readability](https://bitbucket.org/fivefilters/php-readability/overview).
|
||||||
|
|
||||||
## Differences
|
## Differences
|
||||||
|
|
||||||
The default php-readability lib is really old and needs to be improved. I found a great fork of [full-text-rss](http://fivefilters.org/content-only/) from @Dither which improve the Readability class.
|
The default php-readability lib is really old and needs to be improved. I found a great fork of full-text-rss from [@Dither](https://github.com/Dither/full-text-rss) which improve the Readability class.
|
||||||
|
|
||||||
- I've extracted the class from its fork to be able to use it out of the box
|
- I've extracted the class from its fork to be able to use it out of the box
|
||||||
- I've added some simple tests
|
- I've added some simple tests
|
||||||
@@ -15,6 +15,12 @@ The default php-readability lib is really old and needs to be improved. I found
|
|||||||
|
|
||||||
**But** the code is still really hard to understand / read ...
|
**But** the code is still really hard to understand / read ...
|
||||||
|
|
||||||
|
## 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 ..
|
||||||
|
|
||||||
|
Since Composer doesn't support suggestion on PHP extension, I write this suggestion here.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```php
|
```php
|
||||||
@@ -26,6 +32,8 @@ $url = 'http://www.medialens.org/index.php/alerts/alert-archive/alerts-2013/729-
|
|||||||
$html = file_get_contents($url);
|
$html = file_get_contents($url);
|
||||||
|
|
||||||
$readability = new Readability($html, $url);
|
$readability = new Readability($html, $url);
|
||||||
|
// or without Tidy
|
||||||
|
// $readability = new Readability($html, $url, 'libxml', false);
|
||||||
$result = $readability->init();
|
$result = $readability->init();
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
|
|||||||
+1
-2
@@ -24,8 +24,7 @@
|
|||||||
"role": "Developer (original JS version)"
|
"role": "Developer (original JS version)"
|
||||||
}],
|
}],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.3",
|
"php": ">=5.3.3"
|
||||||
"ext-tidy": ">=1.2"
|
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": { "Readability\\": "src/" }
|
"psr-4": { "Readability\\": "src/" }
|
||||||
|
|||||||
+2
-2
@@ -69,10 +69,10 @@ class Readability
|
|||||||
* 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|\binfo|annoy|clock|date|time|author|intro|links|hidd?e|about|archive|\bprint|bookmark|tags|share|search|social|robot|published|combx|comment|mast(?:head)|subscri|community|category|disqus|extra|head(?:er|note)|floor|foot(?:er|note)|menu|tool|function|nav|remark|rss|shoutbox|tool|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|\binfo|annoy|clock|date|time|author|intro|links|hidd?e|about|archive|\bprint|bookmark|tags|share|search|social|robot|published|combx|comment|mast(?:head)|subscri|community|category|disqus|extra|head|head(?:er|note)|floor|foot(?:er|note)|menu|tool|function|nav|remark|rss|shoutbox|tool|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/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',
|
||||||
'divToPElements' => '/<(?:blockquote|code|div|article|footer|aside|img|p|pre|dl|ol|ul)/mi',
|
'divToPElements' => '/<(?:blockquote|code|div|article|footer|aside|img|p|pre|dl|ol|ul)/mi',
|
||||||
'killBreaks' => '/(<br\s*\/?>([ \r\n\s]| ?)*)+/',
|
'killBreaks' => '/(<br\s*\/?>([ \r\n\s]| ?)*)+/',
|
||||||
'media' => '!//(?:[^\.\?/]+\.)?(?:youtu(?:be)?|soundcloud|dailymotion|vimeo|pornhub|xvideos|twitvid|rutube|viddler)\.(?:com|be|org|net)/!i',
|
'media' => '!//(?:[^\.\?/]+\.)?(?:youtu(?:be)?|soundcloud|dailymotion|vimeo|pornhub|xvideos|twitvid|rutube|viddler)\.(?:com|be|org|net)/!i',
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ class ReadabilityTested extends Readability
|
|||||||
|
|
||||||
class ReadabilityTest extends \PHPUnit_Framework_TestCase
|
class ReadabilityTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @requires extension tidy
|
||||||
|
*/
|
||||||
public function testConstructDefault()
|
public function testConstructDefault()
|
||||||
{
|
{
|
||||||
$readability = new ReadabilityTested('');
|
$readability = new ReadabilityTested('');
|
||||||
@@ -30,6 +33,9 @@ class ReadabilityTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires extension tidy
|
||||||
|
*/
|
||||||
public function testConstructSimple()
|
public function testConstructSimple()
|
||||||
{
|
{
|
||||||
$readability = new ReadabilityTested('<html/>', 'http://0.0.0.0');
|
$readability = new ReadabilityTested('<html/>', 'http://0.0.0.0');
|
||||||
@@ -41,6 +47,28 @@ class ReadabilityTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertInstanceOf('DomDocument', $readability->dom);
|
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testConstructDefaultWithoutTidy()
|
||||||
|
{
|
||||||
|
$readability = new ReadabilityTested('', null, 'libxml', false);
|
||||||
|
|
||||||
|
$this->assertNull($readability->url);
|
||||||
|
$this->assertContains('Parsing URL', $readability->getDebugText());
|
||||||
|
$this->assertNotContains('Tidying document', $readability->getDebugText());
|
||||||
|
$this->assertNull($readability->getDomainRegexp());
|
||||||
|
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConstructSimpleWithoutTidy()
|
||||||
|
{
|
||||||
|
$readability = new ReadabilityTested('<html/>', 'http://0.0.0.0', 'libxml', false);
|
||||||
|
|
||||||
|
$this->assertEquals('http://0.0.0.0', $readability->url);
|
||||||
|
$this->assertContains('Parsing URL', $readability->getDebugText());
|
||||||
|
$this->assertNotContains('Tidying document', $readability->getDebugText());
|
||||||
|
$this->assertEquals('/0\.0\.0\.0/', $readability->getDomainRegexp());
|
||||||
|
$this->assertInstanceOf('DomDocument', $readability->dom);
|
||||||
|
}
|
||||||
|
|
||||||
public function testInitNoContent()
|
public function testInitNoContent()
|
||||||
{
|
{
|
||||||
$readability = new ReadabilityTested('<html/>', 'http://0.0.0.0');
|
$readability = new ReadabilityTested('<html/>', 'http://0.0.0.0');
|
||||||
|
|||||||
Reference in New Issue
Block a user