Skip to content

Commit

Permalink
Make Html2Text functions static
Browse files Browse the repository at this point in the history
  • Loading branch information
soundasleep committed Dec 8, 2014
1 parent 96561e0 commit 5d2e98a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
6 changes: 2 additions & 4 deletions html2text.php
Expand Up @@ -24,11 +24,9 @@
require_once(__DIR__ . "/src/Html2TextException.php");

function convert_html_to_text($html) {
$obj = new Html2Text\Html2Text();
return $obj->convert($html);
return Html2Text\Html2Text::convert($html);
}

function fix_newlines($text) {
$obj = new Html2Text\Html2Text();
return $obj->fixNewlines($text);
return Html2Text\Html2Text::fixNewlines($text);
}
20 changes: 10 additions & 10 deletions src/Html2Text.php
Expand Up @@ -33,15 +33,15 @@ class Html2Text {
* @return string the HTML converted, as best as possible, to text
* @throws Html2TextException if the HTML could not be loaded as a {@link DOMDocument}
*/
function convert($html) {
$html = $this->fixNewlines($html);
static function convert($html) {
$html = static::fixNewlines($html);

$doc = new \DOMDocument();
if (!$doc->loadHTML($html)) {
throw new Html2TextException("Could not load HTML - badly formed?", $html);
}

$output = $this->iterateOverNode($doc);
$output = static::iterateOverNode($doc);

// remove leading and trailing spaces on each line
$output = preg_replace("/[ \t]*\n[ \t]*/im", "\n", $output);
Expand All @@ -60,7 +60,7 @@ function convert($html) {
* @param string text text with any number of \r, \r\n and \n combinations
* @return string the fixed text
*/
function fixNewlines($text) {
static function fixNewlines($text) {
// replace \r\n to \n
$text = str_replace("\r\n", "\n", $text);
// remove \rs
Expand All @@ -69,7 +69,7 @@ function fixNewlines($text) {
return $text;
}

function nextChildName($node) {
static function nextChildName($node) {
// get the next child
$nextNode = $node->nextSibling;
while ($nextNode != null) {
Expand All @@ -86,7 +86,7 @@ function nextChildName($node) {
return $nextName;
}

function prevChildName($node) {
static function prevChildName($node) {
// get the previous child
$nextNode = $node->previousSibling;
while ($nextNode != null) {
Expand All @@ -103,7 +103,7 @@ function prevChildName($node) {
return $nextName;
}

function iterateOverNode($node) {
static function iterateOverNode($node) {
if ($node instanceof \DOMText) {
// Replace whitespace characters with a space (equivilant to \s)
return preg_replace("/[\\t\\n\\f\\r ]+/im", " ", $node->wholeText);
Expand All @@ -113,8 +113,8 @@ function iterateOverNode($node) {
return "";
}

$nextName = $this->nextChildName($node);
$prevName = $this->prevChildName($node);
$nextName = static::nextChildName($node);
$prevName = static::prevChildName($node);

$name = strtolower($node->nodeName);

Expand Down Expand Up @@ -160,7 +160,7 @@ function iterateOverNode($node) {
for ($i = 0; $i < $node->childNodes->length; $i++) {
$n = $node->childNodes->item($i);

$text = $this->iterateOverNode($n);
$text = static::iterateOverNode($n);

$output .= $text;
}
Expand Down

0 comments on commit 5d2e98a

Please sign in to comment.