mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-29 16:54:04 +02:00
Doc: Construct parser with the path to the documentation
Before, the parser decided which path to used based on a given module name. Now, the parser requires the path to the documentation. Further the toc items no longer include a URL. The must URL must be generated from a render function or view script. refs #4820
This commit is contained in:
parent
b58ec5f445
commit
6ce739e23d
@ -9,7 +9,7 @@ require_once 'vendor/Parsedown/Parsedown.php';
|
|||||||
use RecursiveIteratorIterator;
|
use RecursiveIteratorIterator;
|
||||||
use RecursiveDirectoryIterator;
|
use RecursiveDirectoryIterator;
|
||||||
use Parsedown;
|
use Parsedown;
|
||||||
use Icinga\Application\Icinga;
|
use Icinga\Exception\NotReadableError;
|
||||||
use Icinga\Web\Menu;
|
use Icinga\Web\Menu;
|
||||||
use Icinga\Web\Url;
|
use Icinga\Web\Url;
|
||||||
|
|
||||||
@ -18,36 +18,30 @@ use Icinga\Web\Url;
|
|||||||
*/
|
*/
|
||||||
class DocParser
|
class DocParser
|
||||||
{
|
{
|
||||||
protected $dir;
|
/**
|
||||||
|
* Path to the documentation
|
||||||
protected $module;
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new documentation parser for the given module or the application
|
* Create a new documentation parser for the given path
|
||||||
*
|
*
|
||||||
* @param string $module
|
* @param string $path Path to the documentation
|
||||||
*
|
*
|
||||||
* @throws DocException
|
* @throws DocException
|
||||||
|
* @throws NotReadableError
|
||||||
*/
|
*/
|
||||||
public function __construct($module = null)
|
public function __construct($path)
|
||||||
{
|
{
|
||||||
if ($module === null) {
|
if (! is_dir($path)) {
|
||||||
$dir = Icinga::app()->getApplicationDir('/../doc');
|
throw new DocException('Doc directory `' . $path .'\' does not exist');
|
||||||
} else {
|
|
||||||
$mm = Icinga::app()->getModuleManager();
|
|
||||||
if (! $mm->hasInstalled($module)) {
|
|
||||||
throw new DocException('Module is not installed');
|
|
||||||
}
|
|
||||||
if (! $mm->hasEnabled($module)) {
|
|
||||||
throw new DocException('Module is not enabled');
|
|
||||||
}
|
|
||||||
$dir = $mm->getModuleDir($module, '/doc');
|
|
||||||
}
|
}
|
||||||
if (! is_dir($dir)) {
|
if (! is_readable($path)) {
|
||||||
throw new DocException('Doc directory `' . $dir .'\' does not exist');
|
throw new NotReadableError('Doc directory `' . $path .'\' is not readable');
|
||||||
}
|
}
|
||||||
$this->dir = $dir;
|
$this->path = $path;
|
||||||
$this->module = $module;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,7 +54,7 @@ class DocParser
|
|||||||
{
|
{
|
||||||
$iter = new RecursiveIteratorIterator(
|
$iter = new RecursiveIteratorIterator(
|
||||||
new MarkdownFileIterator(
|
new MarkdownFileIterator(
|
||||||
new RecursiveDirectoryIterator($this->dir)
|
new RecursiveDirectoryIterator($this->path)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$fileInfos = iterator_to_array($iter);
|
$fileInfos = iterator_to_array($iter);
|
||||||
@ -89,7 +83,7 @@ class DocParser
|
|||||||
if ($header !== null) {
|
if ($header !== null) {
|
||||||
list($header, $level) = $header;
|
list($header, $level) = $header;
|
||||||
$id = $this->extractHeaderId($header);
|
$id = $this->extractHeaderId($header);
|
||||||
$attribs = array();
|
$nofollow = false;
|
||||||
$this->reduceToc($toc, $level);
|
$this->reduceToc($toc, $level);
|
||||||
if ($id === null) {
|
if ($id === null) {
|
||||||
$path = array();
|
$path = array();
|
||||||
@ -98,20 +92,16 @@ class DocParser
|
|||||||
}
|
}
|
||||||
$path[] = $header;
|
$path[] = $header;
|
||||||
$id = implode('-', $path);
|
$id = implode('-', $path);
|
||||||
$attribs['rel'] = 'nofollow';
|
$nofollow = true;
|
||||||
}
|
}
|
||||||
$id = urlencode(str_replace('.', '.', strip_tags($id)));
|
$id = urlencode(str_replace('.', '.', strip_tags($id)));
|
||||||
$item = end($toc)->item->addChild(
|
$item = end($toc)->item->addChild(
|
||||||
$id,
|
$id,
|
||||||
array(
|
array(
|
||||||
// TODO(el): URL should be generated from a route else we always have to adapt the
|
'id' => $id,
|
||||||
// URL here when we change URLs
|
'title' => $header,
|
||||||
'url' => Url::fromPath(
|
|
||||||
$this->module === null ? 'doc/icingaweb' : 'doc/module/' . $this->module
|
|
||||||
)->setAnchor($id)->getRelativeUrl(),
|
|
||||||
'title' => htmlspecialchars($header),
|
|
||||||
'priority' => $itemPriority++, // Post-increment is on purpose
|
'priority' => $itemPriority++, // Post-increment is on purpose
|
||||||
'attribs' => $attribs
|
'nofollow' => $nofollow
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$toc[] = ((object) array(
|
$toc[] = ((object) array(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user