doc/parser: Return the DocTocHtmlRenderer instead of an array

refs #4820
This commit is contained in:
Eric Lippmann 2014-05-28 17:18:07 +02:00
parent 2f1303a13b
commit d3a9f17fc2

View File

@ -10,8 +10,6 @@ use RecursiveIteratorIterator;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use Parsedown; use Parsedown;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
use Icinga\Web\Menu;
use Icinga\Web\Url;
/** /**
* Parser for documentation written in Markdown * Parser for documentation written in Markdown
@ -59,10 +57,10 @@ class DocParser
); );
$fileInfos = iterator_to_array($iter); $fileInfos = iterator_to_array($iter);
natcasesort($fileInfos); natcasesort($fileInfos);
$cat = array(); $cat = array();
$toc = array((object) array( $tocStack = array((object) array(
'level' => 0, 'level' => 0,
'item' => new Menu('doc') 'node' => new DocToc()
)); ));
$itemPriority = 1; $itemPriority = 1;
foreach ($fileInfos as $fileInfo) { foreach ($fileInfos as $fileInfo) {
@ -84,30 +82,29 @@ class DocParser
list($header, $level) = $header; list($header, $level) = $header;
$id = $this->extractHeaderId($header); $id = $this->extractHeaderId($header);
$nofollow = false; $nofollow = false;
$this->reduceToc($toc, $level); $this->reduceToc($tocStack, $level);
if ($id === null) { if ($id === null) {
$path = array(); $path = array();
foreach (array_slice($toc, 1) as $entry) { foreach (array_slice($tocStack, 1) as $entity) {
$path[] = $entry->item->getTitle(); $path[] = $entity->node->getValue()->title;
} }
$path[] = $header; $path[] = $header;
$id = implode('-', $path); $id = implode('-', $path);
$nofollow = true; $nofollow = true;
} }
$id = urlencode(str_replace('.', '.', strip_tags($id))); $id = urlencode(str_replace('.', '.', strip_tags($id)));
$item = end($toc)->item->addChild( $node = end($tocStack)->node->appendChild(
$id, (object) array(
array(
'id' => $id, 'id' => $id,
'title' => $header, 'title' => $header,
'priority' => $itemPriority++, // Post-increment is on purpose 'priority' => $itemPriority++, // Post-increment is on purpose
'nofollow' => $nofollow 'nofollow' => $nofollow
) )
); );
$toc[] = ((object) array( $tocStack[] = (object) array(
'level' => $level, 'level' => $level,
'item' => $item 'node' => $node
)); );
$line = '<a name="' . $id . '"></a>' . PHP_EOL . $line; $line = '<a name="' . $id . '"></a>' . PHP_EOL . $line;
} }
$cat[] = $line; $cat[] = $line;
@ -120,7 +117,7 @@ class DocParser
array($this, 'highlight'), array($this, 'highlight'),
$html $html
); );
return array($html, $toc[0]->item); return array($html, new DocTocHtmlRenderer($tocStack[0]->node));
} }
/** /**
@ -200,14 +197,14 @@ class DocParser
} }
/** /**
* Reduce the toc to the given level * Reduce the toc stack to the given level
* *
* @param array &$toc * @param array &$tocStack
* @param int $level * @param int $level
*/ */
protected function reduceToc(array &$toc, $level) { protected function reduceToc(array &$tocStack, $level) {
while (end($toc)->level >= $level) { while (end($tocStack)->level >= $level) {
array_pop($toc); array_pop($tocStack);
} }
} }
} }