diff options
author | Andres Rey <[email protected]> | 2016-11-23 12:17:22 +0000 |
---|---|---|
committer | Andres Rey <[email protected]> | 2016-11-23 12:17:22 +0000 |
commit | 8a24f09d3da047c3b489e7789db7da217ec398fa (patch) | |
tree | 974215305fd03c3c3c16fcd5afd609f1152f6cd4 | |
parent | a1b4d982e6e4799d0575b083a333f59d089876a7 (diff) |
Added horrible way to restore score. Should be gone in next versions (or whe I learn to code properly)
-rw-r--r-- | src/HTMLParser.php | 12 | ||||
-rw-r--r-- | src/Readability.php | 20 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/HTMLParser.php b/src/HTMLParser.php index bd7774b..49c220d 100644 --- a/src/HTMLParser.php +++ b/src/HTMLParser.php @@ -406,6 +406,18 @@ class HTMLParser } /* + * TODO This is an horrible hack because I don't know how to properly pass by reference. + * When candidates are added to the $candidates array, they lose the reference to the original object + * and on each loop, the object inside $candidates doesn't get updated. This function restores the score + * by getting it of the data-readability tag. This should be fixed using proper references and good coding + * practices (which I lack) + */ + + foreach ($candidates as $candidate) { + $candidate->reloadScore(); + } + + /* * After we've calculated scores, loop through all of the possible * candidate nodes we found and find the one with the highest score. */ diff --git a/src/Readability.php b/src/Readability.php index 6bc9603..a6390c9 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -433,4 +433,24 @@ class Readability extends Element implements ReadabilityInterface { return $this->initialized; } + + /** + * Reloads the score stores in the data-readability tag. + * + * @return int|bool + */ + public function reloadScore() + { + if (method_exists($this->node, 'getAttribute')) { + if ($this->node->hasAttribute('data-readability')) { + $this->initialized = true; + $score = $this->node->getAttribute('data-readability'); + $this->setContentScore($score); + + return $score; + } + } + + return false; + } } |