mirror of
https://github.com/j0k3r/php-readability.git
synced 2026-09-27 06:26:17 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a490fac07 | ||
|
|
6f6b1f9e2b | ||
|
|
ea1368fac0 | ||
|
|
be81eb2a4f | ||
|
|
ff78c63e6d |
@@ -0,0 +1 @@
|
|||||||
|
github: j0k3r
|
||||||
@@ -47,15 +47,17 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
{
|
{
|
||||||
if ('innerHTML' !== $name) {
|
if ('innerHTML' !== $name) {
|
||||||
$trace = debug_backtrace();
|
$trace = debug_backtrace();
|
||||||
trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
|
trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], \E_USER_NOTICE);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first, empty the element
|
// first, empty the element
|
||||||
|
if (isset($this->childNodes)) {
|
||||||
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
|
||||||
$value = trim($value);
|
$value = trim($value);
|
||||||
@@ -112,15 +114,17 @@ class JSLikeHTMLElement extends \DOMElement
|
|||||||
if ('innerHTML' === $name) {
|
if ('innerHTML' === $name) {
|
||||||
$inner = '';
|
$inner = '';
|
||||||
|
|
||||||
|
if (isset($this->childNodes)) {
|
||||||
foreach ($this->childNodes as $child) {
|
foreach ($this->childNodes as $child) {
|
||||||
$inner .= $this->ownerDocument->saveXML($child);
|
$inner .= $this->ownerDocument->saveXML($child);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $inner;
|
return $inner;
|
||||||
}
|
}
|
||||||
|
|
||||||
$trace = debug_backtrace();
|
$trace = debug_backtrace();
|
||||||
trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
|
trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], \E_USER_NOTICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
|
|||||||
+23
-12
@@ -291,6 +291,11 @@ class Readability implements LoggerAwareInterface
|
|||||||
$innerDiv->appendChild($articleContent);
|
$innerDiv->appendChild($articleContent);
|
||||||
$overlay->appendChild($innerDiv);
|
$overlay->appendChild($innerDiv);
|
||||||
|
|
||||||
|
// without tidy the body can (sometimes) be wiped, so re-create it
|
||||||
|
if (false === isset($this->body->childNodes)) {
|
||||||
|
$this->body = $this->dom->createElement('body');
|
||||||
|
}
|
||||||
|
|
||||||
// Clear the old HTML, insert the new content.
|
// Clear the old HTML, insert the new content.
|
||||||
$this->body->setInnerHtml('');
|
$this->body->setInnerHtml('');
|
||||||
$this->body->appendChild($overlay);
|
$this->body->appendChild($overlay);
|
||||||
@@ -335,9 +340,9 @@ class Readability implements LoggerAwareInterface
|
|||||||
$footnoteLink = $articleLink->cloneNode(true);
|
$footnoteLink = $articleLink->cloneNode(true);
|
||||||
$refLink = $this->dom->createElement('a');
|
$refLink = $this->dom->createElement('a');
|
||||||
$footnote = $this->dom->createElement('li');
|
$footnote = $this->dom->createElement('li');
|
||||||
$linkDomain = @parse_url($footnoteLink->getAttribute('href'), PHP_URL_HOST);
|
$linkDomain = @parse_url($footnoteLink->getAttribute('href'), \PHP_URL_HOST);
|
||||||
if (!$linkDomain && isset($this->url)) {
|
if (!$linkDomain && isset($this->url)) {
|
||||||
$linkDomain = @parse_url($this->url, PHP_URL_HOST);
|
$linkDomain = @parse_url($this->url, \PHP_URL_HOST);
|
||||||
}
|
}
|
||||||
|
|
||||||
$linkText = $this->getInnerText($articleLink);
|
$linkText = $this->getInnerText($articleLink);
|
||||||
@@ -934,7 +939,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
case 'DD':
|
case 'DD':
|
||||||
case 'DT':
|
case 'DT':
|
||||||
case 'LI':
|
case 'LI':
|
||||||
$readability->value -= 2 * round($this->getLinkDensity($node), 0, PHP_ROUND_HALF_UP);
|
$readability->value -= 2 * round($this->getLinkDensity($node), 0, \PHP_ROUND_HALF_UP);
|
||||||
break;
|
break;
|
||||||
case 'ASIDE':
|
case 'ASIDE':
|
||||||
case 'FOOTER':
|
case 'FOOTER':
|
||||||
@@ -988,6 +993,12 @@ class Readability implements LoggerAwareInterface
|
|||||||
for ($nodeIndex = 0; ($node = $allElements->item($nodeIndex)); ++$nodeIndex) {
|
for ($nodeIndex = 0; ($node = $allElements->item($nodeIndex)); ++$nodeIndex) {
|
||||||
$tagName = $node->tagName;
|
$tagName = $node->tagName;
|
||||||
|
|
||||||
|
$nodeContent = $node->getInnerHTML();
|
||||||
|
if (empty($nodeContent)) {
|
||||||
|
$this->logger->debug('Skipping empty node');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Some well known site uses sections as paragraphs.
|
// Some well known site uses sections as paragraphs.
|
||||||
if (0 === strcasecmp($tagName, 'p') || 0 === strcasecmp($tagName, 'td') || 0 === strcasecmp($tagName, 'pre') || 0 === strcasecmp($tagName, 'section')) {
|
if (0 === strcasecmp($tagName, 'p') || 0 === strcasecmp($tagName, 'td') || 0 === strcasecmp($tagName, 'pre') || 0 === strcasecmp($tagName, 'section')) {
|
||||||
$nodesToScore[] = $node;
|
$nodesToScore[] = $node;
|
||||||
@@ -996,11 +1007,11 @@ class Readability implements LoggerAwareInterface
|
|||||||
// Turn divs into P tags where they have been used inappropriately
|
// Turn divs into P tags where they have been used inappropriately
|
||||||
// (as in, where they contain no other block level elements).
|
// (as in, where they contain no other block level elements).
|
||||||
if (0 === strcasecmp($tagName, 'div') || 0 === strcasecmp($tagName, 'article') || 0 === strcasecmp($tagName, 'section')) {
|
if (0 === strcasecmp($tagName, 'div') || 0 === strcasecmp($tagName, 'article') || 0 === strcasecmp($tagName, 'section')) {
|
||||||
if (!preg_match($this->regexps['divToPElements'], $node->getInnerHTML())) {
|
if (!preg_match($this->regexps['divToPElements'], $nodeContent)) {
|
||||||
$newNode = $this->dom->createElement('p');
|
$newNode = $this->dom->createElement('p');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$newNode->setInnerHtml($node->getInnerHTML());
|
$newNode->setInnerHtml($nodeContent);
|
||||||
|
|
||||||
$node->parentNode->replaceChild($newNode, $node);
|
$node->parentNode->replaceChild($newNode, $node);
|
||||||
--$nodeIndex;
|
--$nodeIndex;
|
||||||
@@ -1025,7 +1036,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (XML_TEXT_NODE === $childNode->nodeType) {
|
if (\XML_TEXT_NODE === $childNode->nodeType) {
|
||||||
$p = $this->dom->createElement('p');
|
$p = $this->dom->createElement('p');
|
||||||
$p->setInnerHtml($childNode->nodeValue);
|
$p->setInnerHtml($childNode->nodeValue);
|
||||||
$p->setAttribute('data-readability-styled', 'true');
|
$p->setAttribute('data-readability-styled', 'true');
|
||||||
@@ -1151,7 +1162,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
// relatively small link density (5% or less) and be mostly unaffected by this operation.
|
// relatively small link density (5% or less) and be mostly unaffected by this operation.
|
||||||
// If not for this we would have used XPath to find maximum @readability.
|
// If not for this we would have used XPath to find maximum @readability.
|
||||||
$readability = $item->getAttributeNode('readability');
|
$readability = $item->getAttributeNode('readability');
|
||||||
$readability->value = round($readability->value * (1 - $this->getLinkDensity($item)), 0, PHP_ROUND_HALF_UP);
|
$readability->value = round($readability->value * (1 - $this->getLinkDensity($item)), 0, \PHP_ROUND_HALF_UP);
|
||||||
|
|
||||||
if (!$topCandidate || $readability->value > (int) $topCandidate->getAttribute('readability')) {
|
if (!$topCandidate || $readability->value > (int) $topCandidate->getAttribute('readability')) {
|
||||||
$this->logger->debug('Candidate: ' . $item->getNodePath() . ' (' . $item->getAttribute('class') . ':' . $item->getAttribute('id') . ') with score ' . $readability->value);
|
$this->logger->debug('Candidate: ' . $item->getNodePath() . ' (' . $item->getAttribute('class') . ':' . $item->getAttribute('id') . ') with score ' . $readability->value);
|
||||||
@@ -1223,7 +1234,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
$siblingNode = $siblingNodes->item($s);
|
$siblingNode = $siblingNodes->item($s);
|
||||||
$siblingNodeName = $siblingNode->nodeName;
|
$siblingNodeName = $siblingNode->nodeName;
|
||||||
$append = false;
|
$append = false;
|
||||||
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . ((XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
|
$this->logger->debug('Looking at sibling node: ' . $siblingNode->getNodePath() . ((\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
|
||||||
|
|
||||||
if ($siblingNode->isSameNode($topCandidate)) {
|
if ($siblingNode->isSameNode($topCandidate)) {
|
||||||
$append = true;
|
$append = true;
|
||||||
@@ -1232,11 +1243,11 @@ class Readability implements LoggerAwareInterface
|
|||||||
$contentBonus = 0;
|
$contentBonus = 0;
|
||||||
|
|
||||||
// Give a bonus if sibling nodes and top candidates have the same classname.
|
// Give a bonus if sibling nodes and top candidates have the same classname.
|
||||||
if (XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->getAttribute('class') === $topCandidate->getAttribute('class') && '' !== $topCandidate->getAttribute('class')) {
|
if (\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->getAttribute('class') === $topCandidate->getAttribute('class') && '' !== $topCandidate->getAttribute('class')) {
|
||||||
$contentBonus += ((int) $topCandidate->getAttribute('readability')) * 0.2;
|
$contentBonus += ((int) $topCandidate->getAttribute('readability')) * 0.2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
|
if (\XML_ELEMENT_NODE === $siblingNode->nodeType && $siblingNode->hasAttribute('readability') && (((int) $siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold) {
|
||||||
$append = true;
|
$append = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1381,7 +1392,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
$this->logger->debug('Parsing URL: ' . $this->url);
|
$this->logger->debug('Parsing URL: ' . $this->url);
|
||||||
|
|
||||||
if ($this->url) {
|
if ($this->url) {
|
||||||
$this->domainRegExp = '/' . strtr(preg_replace('/www\d*\./', '', parse_url($this->url, PHP_URL_HOST)), ['.' => '\.']) . '/';
|
$this->domainRegExp = '/' . strtr(preg_replace('/www\d*\./', '', parse_url($this->url, \PHP_URL_HOST)), ['.' => '\.']) . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
mb_internal_encoding('UTF-8');
|
mb_internal_encoding('UTF-8');
|
||||||
@@ -1428,7 +1439,7 @@ class Readability implements LoggerAwareInterface
|
|||||||
|
|
||||||
$this->dom = new \DOMDocument();
|
$this->dom = new \DOMDocument();
|
||||||
$this->dom->preserveWhiteSpace = false;
|
$this->dom->preserveWhiteSpace = false;
|
||||||
$this->dom->loadHTML($this->html, LIBXML_NOBLANKS | LIBXML_COMPACT | LIBXML_NOERROR);
|
$this->dom->loadHTML($this->html, \LIBXML_NOBLANKS | \LIBXML_COMPACT | \LIBXML_NOERROR);
|
||||||
|
|
||||||
libxml_use_internal_errors(false);
|
libxml_use_internal_errors(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -332,9 +332,9 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
public function testAutoClosingIframeNotThrowingException()
|
public function testAutoClosingIframeNotThrowingException()
|
||||||
{
|
{
|
||||||
error_reporting(E_ALL | E_STRICT);
|
error_reporting(\E_ALL | \E_STRICT);
|
||||||
ini_set('display_errors', true);
|
ini_set('display_errors', true);
|
||||||
set_error_handler([$this, 'error2Exception'], E_ALL | E_STRICT);
|
set_error_handler([$this, 'error2Exception'], \E_ALL | \E_STRICT);
|
||||||
|
|
||||||
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru-RU" prefix="og: http://ogp.me/ns#">
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru-RU" prefix="og: http://ogp.me/ns#">
|
||||||
@@ -483,6 +483,19 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertContains('<a href="#fnref1:fnfeed_2" rev="footnote"', $readability->getContent()->getInnerHtml());
|
$this->assertContains('<a href="#fnref1:fnfeed_2" rev="footnote"', $readability->getContent()->getInnerHtml());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testWithWipedBody()
|
||||||
|
{
|
||||||
|
// from https://www.cs.cmu.edu/~rgs/alice-table.html
|
||||||
|
$html = file_get_contents('tests/fixtures/wipedBody.html');
|
||||||
|
|
||||||
|
$readability = $this->getReadability($html, 'http://0.0.0.0', 'libxml', false);
|
||||||
|
$readability->debug = true;
|
||||||
|
$res = $readability->init();
|
||||||
|
|
||||||
|
$this->assertTrue($res);
|
||||||
|
$this->assertContains('<a href="alice-I.html">Down the Rabbit-Hole</a>', $readability->getContent()->getInnerHtml());
|
||||||
|
}
|
||||||
|
|
||||||
private function getReadability($html, $url = null, $parser = 'libxml', $useTidy = true)
|
private function getReadability($html, $url = null, $parser = 'libxml', $useTidy = true)
|
||||||
{
|
{
|
||||||
$readability = new Readability($html, $url, $parser, $useTidy);
|
$readability = new Readability($html, $url, $parser, $useTidy);
|
||||||
|
|||||||
Vendored
+67
@@ -0,0 +1,67 @@
|
|||||||
|
|
||||||
|
<HTML>
|
||||||
|
<HEAD>
|
||||||
|
<TITLE>Alice's Adventures in Wonderland (Project Gutenberg)</TITLE>
|
||||||
|
</HEAD>
|
||||||
|
|
||||||
|
<frameset Rows="50, *">
|
||||||
|
<frame src="alice-finfo.html">
|
||||||
|
<frame src="alice-ftitle.html" name="alice-main">
|
||||||
|
</frameset>
|
||||||
|
|
||||||
|
<noframes>
|
||||||
|
<BODY>
|
||||||
|
<H1>Alice's Adventures in Wonderland</H1>
|
||||||
|
<H1>Lewis Carroll</H1>
|
||||||
|
<H1>The Millennium Fulcrum Edition 3.0</H1>
|
||||||
|
|
||||||
|
NOTE: This is a hypertext formatted version of the Project Gutenberg edition.
|
||||||
|
For more information, check the
|
||||||
|
<A HREF="alice-small.txt">small print</A>
|
||||||
|
or check out the
|
||||||
|
<A HREF="ftp://uiarchive.cso.uiuc.edu/pub/etext/gutenberg/etext91/alice30.txt">
|
||||||
|
full ascii text</A>. The original Tenniel illustrations are also available
|
||||||
|
due to the efforts of Project Gutenberg. You can if you like, grab them as a
|
||||||
|
<A HREF="ftp://uiarchive.cso.uiuc.edu/pub/etext/gutenberg/etext94/algif10.zip">
|
||||||
|
"zip file"</A> or read the <A HREF="algif-small.txt">small print</A>
|
||||||
|
that comes with them.
|
||||||
|
This document is part of a small, but growing collection of html formatted
|
||||||
|
etexts. (Others may be found in either my <A
|
||||||
|
HREF="http://www.cs.cmu.edu/Web/People/rgs/rgs-home.html">home page</A> or
|
||||||
|
John Ockerbloom's indexes by <A
|
||||||
|
HREF="http://www.cs.cmu.edu/Web/bookauthors.html">author</A> and <A
|
||||||
|
HREF="http://www.cs.cmu.edu/Web/booktitles.html">title</A>.)
|
||||||
|
I am still trying to figure out whether anyone else is interested in these
|
||||||
|
on-line readable documents. If you appreciate this document or would like to
|
||||||
|
see more such, send me mail at "rgs@cs.cmu.edu".
|
||||||
|
<P>
|
||||||
|
<A HREF="alice01a.gif"><IMG SRC="alice01th.gif"></A>
|
||||||
|
<P>
|
||||||
|
<H2>CONTENTS</H2>
|
||||||
|
<PRE>
|
||||||
|
CHAPTER I: <A HREF="alice-I.html">Down the Rabbit-Hole</A>
|
||||||
|
CHAPTER II: <A HREF="alice-II.html">The Pool of Tears</A>
|
||||||
|
CHAPTER III: <A HREF="alice-III.html">A Caucus-Race and a Long Tale</A>
|
||||||
|
CHAPTER IV: <A HREF="alice-IV.html">The Rabbit Sends in a Little Bill</A>
|
||||||
|
CHAPTER V: <A HREF="alice-V.html">Advice from a Caterpillar</A>
|
||||||
|
CHAPTER VI: <A HREF="alice-VI.html">Pig and Pepper</A>
|
||||||
|
CHAPTER VII: <A HREF="alice-VII.html">A Mad Tea-Party</A>
|
||||||
|
CHAPTER VIII: <A HREF="alice-VIII.html">The Queen's Croquet-Ground</A>
|
||||||
|
CHAPTER IX: <A HREF="alice-IX.html">The Mock Turtle's Story</A>
|
||||||
|
CHAPTER X: <A HREF="alice-X.html">The Lobster Quadrille</A>
|
||||||
|
CHAPTER XI: <A HREF="alice-XI.html">Who Stole the Tarts?</A>
|
||||||
|
CHAPTER XII: <A HREF="alice-XII.html">Alice's Evidence</A>
|
||||||
|
</PRE>
|
||||||
|
|
||||||
|
<ADDRESS><A HREF="mailto:rgs@cs.cmu.edu">Robert Stockton</A></ADDRESS>
|
||||||
|
<P>
|
||||||
|
<!- Access counter added 5/25 1:49am ->
|
||||||
|
<A href="http://www.dbasics.com/cgi-bin/pages.cgi?143205747"><IMG SRC="http://www.dbasics.com/cgi-bin/counter.cgi?143205747.2&(none)"></A> Access statistics from htmlZine
|
||||||
|
<!- This page has been visited
|
||||||
|
A HREF="http://counter.digits.com/wc?--info=yes&--name=rgsalice"
|
||||||
|
IMG SRC="http://counter.digits.com/wc/-d/4/-r/-z/rgsalice"
|
||||||
|
ALIGN=absmiddle WIDTH=60 HEIGHT=20 BORDER=0 HSPACE=4 ALT="????"/A
|
||||||
|
times since March 2, 1996. ->
|
||||||
|
</BODY>
|
||||||
|
</noframes>
|
||||||
|
</HTML>
|
||||||
Reference in New Issue
Block a user