doc module: Let `DocParser' throw `DocEmptyException' and `ChapterNotFound' exception

`DocEmptyException' is thrown during construction when a documentation directory is empty.
`ChapterNotFound' is thrown when a chapter was not found when calling `getChapter()'.

refs #4820
This commit is contained in:
Eric Lippmann 2014-06-30 15:24:40 +02:00
parent c48f7f9fba
commit 1bbfa9b9ca
1 changed files with 28 additions and 17 deletions

View File

@ -9,6 +9,9 @@ require_once 'IcingaVendor/Parsedown/Parsedown.php';
use Parsedown; use Parsedown;
use Icinga\Data\Tree\Node; use Icinga\Data\Tree\Node;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
use Icinga\Module\Doc\Exception\ChapterNotFoundException;
use Icinga\Module\Doc\Exception\DocEmptyException;
use Icinga\Module\Doc\Exception\DocException;
/** /**
* Parser for documentation written in Markdown * Parser for documentation written in Markdown
@ -22,13 +25,21 @@ class DocParser
*/ */
protected $path; protected $path;
/**
* Iterator over documentation files
*
* @var DocIterator
*/
protected $docIterator;
/** /**
* Create a new documentation parser for the given path * Create a new documentation parser for the given path
* *
* @param string $path Path to the documentation * @param string $path Path to the documentation
* *
* @throws DocException * @throws DocException If the documentation directory does not exist
* @throws NotReadableError * @throws NotReadableError If the documentation directory is not readable
* @throws DocEmptyException If the documentation directory is empty
*/ */
public function __construct($path) public function __construct($path)
{ {
@ -38,13 +49,18 @@ class DocParser
if (! is_readable($path)) { if (! is_readable($path)) {
throw new NotReadableError('Doc directory `' . $path . '\' is not readable'); throw new NotReadableError('Doc directory `' . $path . '\' is not readable');
} }
$docIterator = new DocIterator($path);
if ($docIterator->count() === 0) {
throw new DocEmptyException('Doc directory `' . $path . '\' is empty');
}
$this->path = $path; $this->path = $path;
$this->docIterator = $docIterator;
} }
/** /**
* Retrieve the table of contents * Retrieve the table of contents
* *
* @return Node * @return Node
*/ */
public function getToc() public function getToc()
{ {
@ -52,7 +68,7 @@ class DocParser
'level' => 0, 'level' => 0,
'node' => new Node() 'node' => new Node()
)); ));
foreach (new DocIterator($this->path) as $fileObject) { foreach ($this->docIterator as $fileObject) {
$line = null; $line = null;
$currentChapterName = null; $currentChapterName = null;
while (! $fileObject->eof()) { while (! $fileObject->eof()) {
@ -104,16 +120,15 @@ class DocParser
* @param string $chapterName * @param string $chapterName
* *
* @return string * @return string
* @throws ChapterNotFoundException If the chapter was not found
*/ */
public function getChapter($chapterName) public function getChapter($chapterName)
{ {
$cat = array();
$tocStack = array((object) array( $tocStack = array((object) array(
'level' => 0, 'level' => 0,
'node' => new Node() 'node' => new Node()
)); ));
$chapterFound = false; foreach ($this->docIterator as $fileObject) {
foreach (new DocIterator($this->path) as $fileObject) {
$line = null; $line = null;
$currentChapterName = null; $currentChapterName = null;
$chapter = array(); $chapter = array();
@ -153,18 +168,14 @@ class DocParser
$chapter[] = $line; $chapter[] = $line;
} }
if ($currentChapterName === $chapterName) { if ($currentChapterName === $chapterName) {
$chapterFound = true; return preg_replace_callback(
$cat = $chapter; '#<pre><code class="language-php">(.*?)\</code></pre>#s',
} array($this, 'highlight'),
if (! $chapterFound) { Parsedown::instance()->text(implode('', $chapter))
$cat = array_merge($cat, $chapter); );
} }
} }
return preg_replace_callback( throw new ChapterNotFoundException('Chapter \'' . $chapterName . '\' not found');
'#<pre><code class="language-php">(.*?)\</code></pre>#s',
array($this, 'highlight'),
Parsedown::instance()->text(implode('', $cat))
);
} }
/** /**