doc: Fix actions and view scripts DocParser usage

refs #4820
This commit is contained in:
Eric Lippmann 2014-06-06 14:10:35 +02:00
parent d446e0db2e
commit 16d5d65a05
11 changed files with 131 additions and 40 deletions

View File

@ -27,6 +27,6 @@ class Doc_IcingawebController extends DocController
if ($chapterName === null) {
throw new Zend_Controller_Action_Exception('Missing parameter "chapterName"', 404);
}
$this->renderChapter($chapterName, Icinga::app()->getApplicationDir('/../doc'));
$this->populateChapter($chapterName, Icinga::app()->getApplicationDir('/../doc'));
}
}

View File

@ -8,6 +8,5 @@ class Doc_IndexController extends DocController
{
public function indexAction()
{
}
}

View File

@ -63,6 +63,7 @@ class Doc_ModuleController extends DocController
throw new Zend_Controller_Action_Exception('Missing parameter "chapterName"', 404);
}
$moduleManager = Icinga::app()->getModuleManager();
$this->renderChapter($chapterName, $moduleManager->getModuleDir($moduleName, '/doc'));
$this->populateChapter($chapterName, $moduleManager->getModuleDir($moduleName, '/doc'));
$this->view->moduleName = $moduleName;
}
}

View File

@ -0,0 +1,38 @@
<?php
$urlHelper = $this->getHelper('Url');
$view = $this;
?>
<?= preg_replace_callback(
'/<a\s+(?P<attribs>[^>]*?\s+)?href="#(?P<fragment>[^"]+)"/im',
function($match) use ($toc, $urlHelper, $view) {
if (($node = $toc->findNodeBy(function ($node) use ($match) {
$section = $node->getValue();
if (($section->id === null && $section->chapterName === $match['fragment'])
|| $section->id === $match['fragment']
) {
return true;
}
return false;
}))) {
$section = $node->getValue();
$path = $urlHelper->url(
array('chapterName' => $section->chapterName),
'doc/icingaweb/chapter',
false,
false
);
$url = $view->url($path);
if ($section->id) {
$url->setAnchor($section->id);
}
return sprintf(
'<a %s%shref="%s"',
strlen($match['attribs']) ? trim($match['attribs']) . ' ' : '',
$section->nofollow ? 'rel="nofollow" ' : '',
$url->getAbsoluteUrl()
);
}
return $match[0];
},
$chapterHtml
); ?>

View File

@ -1,15 +1,23 @@
<div class="controls">
<h1><?= $docName ?> documentation</h1>
<h1><?= $docName ?> documentation</h1>
</div>
<div class="content" data-base-target="_next">
<?php
<?php
$urlHelper = $this->getHelper('Url');
$view = $this;
?>
<?= $tocRenderer->render(function ($section) use ($urlHelper, $view) {
// Chapter name is not yet defined
$path = $urlHelper->url(array('chapterName' => 'tbd'), 'doc/icingaweb/chapter', false, false);
$url = $view->url($path)->setAnchor($section->id);
return sprintf('<li><a href="%s">%s</a></li>', $url->getAbsoluteUrl(), $section->title);
}); ?>
?>
<?= $tocRenderer->render(function ($node) use ($urlHelper, $view) {
$section = $node->getValue();
$path = $urlHelper->url(array('chapterName' => $section->chapterName), 'doc/icingaweb/chapter', false, false);
$url = $view->url($path);
if ($section->id) {
$url->setAnchor($section->id);
}
return sprintf(
'<li><a %shref="%s">%s</a></li>',
$section->nofollow ? 'rel="nofollow" ' : '',
$url->getAbsoluteUrl(),
$section->title
);
}); ?>
</div>

View File

@ -1,5 +1,5 @@
<h1>Available documentations</h1>
<ul>
<li><a href="<?= $this->href('doc/icingaweb/toc'); ?>">Icinga Web 2</a></li>
<li><a href="<?= $this->href('doc/module/'); ?>">Module documentations</a></li>
<li><a href="<?= $this->href('doc/icingaweb/toc'); ?>">Icinga Web 2</a></li>
<li><a href="<?= $this->href('doc/module/'); ?>">Module documentations</a></li>
</ul>

View File

@ -0,0 +1,38 @@
<?php
$urlHelper = $this->getHelper('Url');
$view = $this;
?>
<?= preg_replace_callback(
'/<a\s+(?P<attribs>[^>]*?\s+)?href="#(?P<fragment>[^"]+)"/im',
function($match) use ($toc, $moduleName, $urlHelper, $view) {
if (($node = $toc->findNodeBy(function ($node) use ($match) {
$section = $node->getValue();
if (($section->id === null && $section->chapterName === $match['fragment'])
|| $section->id === $match['fragment']
) {
return true;
}
return false;
}))) {
$section = $node->getValue();
$path = $urlHelper->url(
array('moduleName' => $moduleName, 'chapterName' => $section->chapterName),
'doc/module/chapter',
false,
false
);
$url = $view->url($path);
if ($section->id) {
$url->setAnchor($section->id);
}
return sprintf(
'<a %s%shref="%s"',
strlen($match['attribs']) ? trim($match['attribs']) . ' ' : '',
$section->nofollow ? 'rel="nofollow" ' : '',
$url->getAbsoluteUrl()
);
}
return $match[0];
},
$chapterHtml
); ?>

View File

@ -1,10 +1,10 @@
<h1>Module documentations</h1>
<ul>
<?php foreach ($enabledModules as $module): ?>
<li>
<a href="<?= $this->getHelper('Url')->url(array('moduleName' => $module), 'doc/module/toc', false, false); ?>">
<?= $module ?>
</a>
</li>
<?php endforeach ?>
<?php foreach ($enabledModules as $module): ?>
<li>
<a href="<?= $this->getHelper('Url')->url(array('moduleName' => $module), 'doc/module/toc', false, false); ?>">
<?= $module ?>
</a>
</li>
<?php endforeach ?>
</ul>

View File

@ -1,17 +1,25 @@
<div class="controls">
<h1><?= $docName ?> documentation</h1>
<h1><?= $docName ?> documentation</h1>
</div>
<div class="content" data-base-target="_next">
<?php
<?php
$urlHelper = $this->getHelper('Url');
$view = $this;
?>
<?= $tocRenderer->render(function ($section) use ($urlHelper, $view, $moduleName) {
// Chapter name is not yet defined
$path = $urlHelper->url(
array('moduleName' => $moduleName, 'chapterName' => 'tbd'), 'doc/module/chapter', false, false
);
$url = $view->url($path)->setAnchor($section->id);
return sprintf('<li><a href="%s">%s</a></li>', $url->getAbsoluteUrl(), $section->title);
}); ?>
?>
<?= $tocRenderer->render(function ($node) use ($urlHelper, $view, $moduleName) {
$section = $node->getValue();
$path = $urlHelper->url(
array('moduleName' => $moduleName, 'chapterName' => $section->chapterName), 'doc/module/chapter', false, false
);
$url = $view->url($path);
if ($section->id) {
$url->setAnchor($section->id);
}
return sprintf(
'<li><a %shref="%s">%s</a></li>',
$section->nofollow ? 'rel="nofollow" ' : '',
$url->getAbsoluteUrl(),
$section->title
);
}); ?>
</div>

View File

@ -1 +0,0 @@
<?= $chapterHtml ?>

View File

@ -4,22 +4,22 @@
namespace Icinga\Module\Doc;
use Icinga\Data\Tree\NodeRenderer;
use Icinga\Web\Controller\ActionController;
class DocController extends ActionController
{
/**
* Render a chapter
* Populate a chapter
*
* @param string $chapterName Name of the chapter
* @param string $path Path to the documentation
*/
protected function renderChapter($chapterName, $path)
protected function populateChapter($chapterName, $path)
{
$parser = new DocParser($path);
list($docHtml, $docToc) = $parser->getDocAndToc();
$this->view->chapterHtml = $docHtml;
$this->_helper->viewRenderer('partials/chapter', null, true);
$this->view->chapterHtml = $parser->getChapter($chapterName);
$this->view->toc = $parser->getToc();
}
/**
@ -31,8 +31,8 @@ class DocController extends ActionController
protected function populateToc($path, $name)
{
$parser = new DocParser($path);
list($docHtml, $tocRenderer) = $parser->getDocAndToc();
$this->view->tocRenderer = $tocRenderer;
$toc = $parser->getToc();
$this->view->tocRenderer = new NodeRenderer($toc);
$this->view->docName = $name;
}
}