Skip to content

Commit

Permalink
Tested toText() support
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed May 9, 2010
1 parent 2050537 commit 0938d7f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -23,7 +23,7 @@ the jQuery API.

PHPricot is motivated by Rubys HPricot but only a distantly related port.

## Installation
## Installation of Required Libraries

This currently a linux only installation guide (please contribute Windows or MacOS if
you find the time).
Expand Down
3 changes: 2 additions & 1 deletion lib/PHPricot/Document.php
Expand Up @@ -35,6 +35,7 @@ public function toText()
foreach ($this->childNodes AS $node) {
$txt .= $node->toText();
}
return $txt;
$txt = preg_replace('(([\s]{3,}))', ' ', $txt);
return rtrim($txt);
}
}
23 changes: 21 additions & 2 deletions lib/PHPricot/Nodes/Element.php
Expand Up @@ -142,6 +142,15 @@ public function val()
public function toText()
{
$out = '';

if ($this->name == 'h1') {
$out .= "\n" . str_repeat('*', 65) . "\n";
} else if ($this->name == 'h2') {
$out .= "\n" . str_repeat('-', 65) . "\n";
} else if ($this->name == 'li') {
$out .= " * ";
}

foreach ($this->childNodes AS $child) {
$out .= $child->toText();
}
Expand All @@ -150,10 +159,20 @@ public function toText()
$out .= "\n";
} else if ($this->name == 'p') {
$out .= "\n\n";
} else if ($this->name == 'a' && isset($this->attributes['href'])) {
$out .= "[#" . $this->attributes['href'] . "]";
} else if ($this->name == 'a' && isset($this->attributes['href']) && strpos($this->attributes['href'], "#") !== 0) {
$out .= "(" . $this->attributes['href'] . ")";
} else if ($this->name == 'img' && isset($this->attributes['src'])) {
$out .= "[image:" . $this->attributes['src'] . "]";
} else if ($this->name == 'h1') {
$out .= "\n" . str_repeat('*', 65) . "\n\n";
} else if (in_array($this->name, array('h2', 'h3', 'h4', 'h5', 'h6'))) {
$out .= "\n" . str_repeat('-', 65) . "\n\n";
} else if ($this->name == 'td') {
$out .= "\t";
} else if ($this->name == 'li') {
$out .= "\n";
} else if ($this->name == 'ul') {
$out .= "\n";
}

return $out;
Expand Down
2 changes: 1 addition & 1 deletion lib/PHPricot/Nodes/Text.php
Expand Up @@ -28,6 +28,6 @@ public function toHtml()

public function toText()
{
return $this->text;
return str_replace(" ", " ", $this->text);
}
}
55 changes: 55 additions & 0 deletions tests/PHPricot/ToTextTest.php
@@ -0,0 +1,55 @@
<?php

class PHPricot_ToTextTest extends PHPUnit_Framework_TestCase
{
public function testHeader1()
{
$query = new PHPricot_Query('<h1>Foo</h1><p>bar <a href="#foo">baz</a></p>');

$expected = <<<ETT
*****************************************************************
Foo
*****************************************************************
bar baz
ETT;
$this->assertEquals($expected, $query->getDocument()->toText());
}

public function testHeader2()
{
$query = new PHPricot_Query('<h2>Foo</h2>');

$expected = <<<ETT
-----------------------------------------------------------------
Foo
-----------------------------------------------------------------
ETT;
$this->assertEquals($expected, $query->getDocument()->toText());
}


public function testHeader3()
{
$query = new PHPricot_Query('<h3>Foo</h3>');

$expected = <<<ETT
Foo
-----------------------------------------------------------------
ETT;
$this->assertEquals($expected, $query->getDocument()->toText());
}

public function testList()
{
$query = new PHPricot_Query('<ul><li>Foo</li><li>Bar</li></ul>');

$expected = <<<ETT
* Foo
* Bar
ETT;
$this->assertEquals($expected, $query->getDocument()->toText());
}
}

0 comments on commit 0938d7f

Please sign in to comment.