Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ protected function converter(&$text)
{
$this->convertBlockquotes($text);
$this->convertPre($text);
$this->convertOrderedLists($text);
$text = preg_replace($this->search, $this->replace, $text);
$text = preg_replace_callback($this->callbackSearch, array($this, 'pregCallback'), $text);
$text = strip_tags($text);
Expand All @@ -391,6 +392,9 @@ protected function converter(&$text)
// This properly handles situation of """ in input string
$text = str_replace('|+|amp|+|', '&', $text);

// Convert the "* |+|ol|+|N|+|" markers of ordered list items into "N. ", see convertOrderedLists()
$text = preg_replace('/\* \|\+\|ol\|\+\|(-?\d+)\|\+\|/', '\\1. ', $text);

// Normalise empty lines
$text = preg_replace("/\n\s+\n/", "\n\n", $text);
$text = preg_replace("/[\n]{3,}/", "\n\n", $text);
Expand Down Expand Up @@ -501,6 +505,48 @@ protected function convertPre(&$text)
}
}

/**
* Helper function for numbering the items of ordered lists.
*
* Adds a "|+|ol|+|N|+|" marker after every LI tag of an OL list, which
* converter() turns into "N. " instead of the "* " bullet. Nested lists
* and the start attribute of OL are taken into account.
*
* @param string &$text HTML content
*/
protected function convertOrderedLists(&$text)
{
if (stripos($text, '<ol') === false) {
return;
}

$parts = preg_split('/(<\/?(?:ol|ul)\b[^>]*>|<li\b[^>]*>)/i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
// next item number of every open list, null for unordered lists
$lists = array();
foreach ($parts as $i => $part) {
// even parts are the content between the tags
if ($i % 2 === 0) {
continue;
}

if (preg_match('/^<(\/?)(ol|ul)\b/i', $part, $matches)) {
if ($matches[1] === '/') {
array_pop($lists);
} elseif (strtolower($matches[2]) === 'ol') {
$lists[] = preg_match('/\sstart\s*=\s*["\']?(-?\d+)/i', $part, $start) ? (int) $start[1] : 1;
} else {
$lists[] = null;
}
} elseif ($lists && end($lists) !== null) {
$level = count($lists) - 1;
$parts[$i] = $part . '|+|ol|+|' . $lists[$level] . '|+|';
$lists[$level]++;
}
}

$text = implode('', $parts);
}

/**
* Helper function for BLOCKQUOTE body conversion.
*
Expand Down
24 changes: 21 additions & 3 deletions test/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,32 @@ public function testOrderedList()
EOT;

$expected =<<<'EOT'
* Item 1
* Item 2
* Item 3
1. Item 1
2. Item 2
3. Item 3


EOT;

$html2text = new Html2Text($html);
$this->assertEquals($expected, $html2text->getText());
}

public function testOrderedListWithStart()
{
$html = '<ol start="5"><li>Item 5</li><li>Item 6</li></ol>';
$expected = "\t5. Item 5\n\t6. Item 6";

$html2text = new Html2Text($html);
$this->assertEquals($expected, trim($html2text->getText(), "\n"));
}

public function testNestedOrderedAndUnorderedLists()
{
$html = '<ol><li>One<ul><li>Sub a</li><li>Sub b</li></ul></li><li>Two<ol><li>Sub 1</li></ol></li></ol>';
$expected = "\t1. One\n\n\t* Sub a\n\t* Sub b\n\n\t2. Two\n\n\t1. Sub 1";

$html2text = new Html2Text($html);
$this->assertEquals($expected, trim($html2text->getText(), "\n"));
}
}