doc: Don't fail on duplicate IDs

This commit is contained in:
Eric Lippmann 2016-03-30 15:29:50 +02:00
parent a5119a7a2e
commit 2667457eec
1 changed files with 33 additions and 9 deletions

View File

@ -4,6 +4,7 @@
namespace Icinga\Module\Doc; namespace Icinga\Module\Doc;
use CachingIterator; use CachingIterator;
use RecursiveIteratorIterator;
use SplFileObject; use SplFileObject;
use SplStack; use SplStack;
use Icinga\Data\Tree\SimpleTree; use Icinga\Data\Tree\SimpleTree;
@ -61,7 +62,7 @@ class DocParser
); );
} }
$this->path = $path; $this->path = $path;
$this->docIterator = new DirectoryIterator($path, 'md'); $this->docIterator = new DirectoryIterator($path, 'md', DirectoryIterator::FILES_FIRST);
} }
/** /**
@ -117,9 +118,35 @@ class DocParser
} else { } else {
$id = null; $id = null;
} }
/** @noinspection PhpUndefinedVariableInspection */
return array($header, $id, $level, $headerStyle); return array($header, $id, $level, $headerStyle);
} }
/**
* Generate unique section ID
*
* @param string $id
* @param string $filename
* @param SimpleTree $tree
*
* @return string
*/
protected function uuid($id, $filename, SimpleTree $tree)
{
if ($tree->getNode($id) !== null) {
$id = $id . '-' . md5($filename);
}
$offset = 0;
while ($tree->getNode($id)) {
if ($offset++ === 0) {
$id .= '-' . $offset;
} else {
$id = substr($id, 0, -1) . $offset;
}
}
return $id;
}
/** /**
* Get the documentation tree * Get the documentation tree
* *
@ -128,7 +155,7 @@ class DocParser
public function getDocTree() public function getDocTree()
{ {
$tree = new SimpleTree(); $tree = new SimpleTree();
foreach ($this->docIterator as $filename) { foreach (new RecursiveIteratorIterator($this->docIterator) as $filename) {
$file = new SplFileObject($filename); $file = new SplFileObject($filename);
$lastLine = null; $lastLine = null;
$stack = new SplStack(); $stack = new SplStack();
@ -154,9 +181,9 @@ class DocParser
} else { } else {
$noFollow = false; $noFollow = false;
} }
if ($tree->getNode($id) !== null) {
$id = uniqid($id); $id = $this->uuid($id, $filename, $tree);
}
$section = new DocSection(); $section = new DocSection();
$section $section
->setId($id) ->setId($id)
@ -178,10 +205,7 @@ class DocParser
} else { } else {
if ($stack->isEmpty()) { if ($stack->isEmpty()) {
$title = ucfirst($file->getBasename('.' . pathinfo($file->getFilename(), PATHINFO_EXTENSION))); $title = ucfirst($file->getBasename('.' . pathinfo($file->getFilename(), PATHINFO_EXTENSION)));
$id = $title; $id = $this->uuid($title, $filename, $tree);
if ($tree->getNode($id) !== null) {
$id = uniqid($id);
}
$section = new DocSection(); $section = new DocSection();
$section $section
->setId($id) ->setId($id)