From 16d5d65a05591e18ba721f67abe0a039bf0d7fcb Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Fri, 6 Jun 2014 14:10:35 +0200 Subject: [PATCH] doc: Fix actions and view scripts DocParser usage refs #4820 --- .../controllers/IcingawebController.php | 2 +- .../controllers/IndexController.php | 1 - .../controllers/ModuleController.php | 3 +- .../views/scripts/icingaweb/chapter.phtml | 38 +++++++++++++++++++ .../views/scripts/icingaweb/toc.phtml | 26 ++++++++----- .../views/scripts/index/index.phtml | 4 +- .../views/scripts/module/chapter.phtml | 38 +++++++++++++++++++ .../views/scripts/module/index.phtml | 14 +++---- .../views/scripts/module/toc.phtml | 30 +++++++++------ .../views/scripts/partials/chapter.phtml | 1 - modules/doc/library/Doc/DocController.php | 14 +++---- 11 files changed, 131 insertions(+), 40 deletions(-) create mode 100644 modules/doc/application/views/scripts/icingaweb/chapter.phtml create mode 100644 modules/doc/application/views/scripts/module/chapter.phtml delete mode 100644 modules/doc/application/views/scripts/partials/chapter.phtml diff --git a/modules/doc/application/controllers/IcingawebController.php b/modules/doc/application/controllers/IcingawebController.php index 0820a82ca..092de4025 100644 --- a/modules/doc/application/controllers/IcingawebController.php +++ b/modules/doc/application/controllers/IcingawebController.php @@ -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')); } } diff --git a/modules/doc/application/controllers/IndexController.php b/modules/doc/application/controllers/IndexController.php index aa44c9c5d..63b5e8cdf 100644 --- a/modules/doc/application/controllers/IndexController.php +++ b/modules/doc/application/controllers/IndexController.php @@ -8,6 +8,5 @@ class Doc_IndexController extends DocController { public function indexAction() { - } } diff --git a/modules/doc/application/controllers/ModuleController.php b/modules/doc/application/controllers/ModuleController.php index b864ff324..07092c4ed 100644 --- a/modules/doc/application/controllers/ModuleController.php +++ b/modules/doc/application/controllers/ModuleController.php @@ -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; } } diff --git a/modules/doc/application/views/scripts/icingaweb/chapter.phtml b/modules/doc/application/views/scripts/icingaweb/chapter.phtml new file mode 100644 index 000000000..328d0bebe --- /dev/null +++ b/modules/doc/application/views/scripts/icingaweb/chapter.phtml @@ -0,0 +1,38 @@ +getHelper('Url'); +$view = $this; +?> +[^>]*?\s+)?href="#(?P[^"]+)"/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( + 'nofollow ? 'rel="nofollow" ' : '', + $url->getAbsoluteUrl() + ); + } + return $match[0]; + }, + $chapterHtml +); ?> diff --git a/modules/doc/application/views/scripts/icingaweb/toc.phtml b/modules/doc/application/views/scripts/icingaweb/toc.phtml index 065071f01..bf91ffc77 100644 --- a/modules/doc/application/views/scripts/icingaweb/toc.phtml +++ b/modules/doc/application/views/scripts/icingaweb/toc.phtml @@ -1,15 +1,23 @@
-

documentation

+

documentation

- getHelper('Url'); $view = $this; - ?> - 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('
  • %s
  • ', $url->getAbsoluteUrl(), $section->title); - }); ?> + ?> + 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( + '
  • %s
  • ', + $section->nofollow ? 'rel="nofollow" ' : '', + $url->getAbsoluteUrl(), + $section->title + ); + }); ?>
    diff --git a/modules/doc/application/views/scripts/index/index.phtml b/modules/doc/application/views/scripts/index/index.phtml index 4381bc2a0..3cebdca69 100644 --- a/modules/doc/application/views/scripts/index/index.phtml +++ b/modules/doc/application/views/scripts/index/index.phtml @@ -1,5 +1,5 @@

    Available documentations

    diff --git a/modules/doc/application/views/scripts/module/chapter.phtml b/modules/doc/application/views/scripts/module/chapter.phtml new file mode 100644 index 000000000..59a6ecb33 --- /dev/null +++ b/modules/doc/application/views/scripts/module/chapter.phtml @@ -0,0 +1,38 @@ +getHelper('Url'); +$view = $this; +?> +[^>]*?\s+)?href="#(?P[^"]+)"/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( + 'nofollow ? 'rel="nofollow" ' : '', + $url->getAbsoluteUrl() + ); + } + return $match[0]; + }, + $chapterHtml +); ?> diff --git a/modules/doc/application/views/scripts/module/index.phtml b/modules/doc/application/views/scripts/module/index.phtml index bbd74345a..c3522c4c2 100644 --- a/modules/doc/application/views/scripts/module/index.phtml +++ b/modules/doc/application/views/scripts/module/index.phtml @@ -1,10 +1,10 @@

    Module documentations

    diff --git a/modules/doc/application/views/scripts/module/toc.phtml b/modules/doc/application/views/scripts/module/toc.phtml index b3be77d79..ec33c507c 100644 --- a/modules/doc/application/views/scripts/module/toc.phtml +++ b/modules/doc/application/views/scripts/module/toc.phtml @@ -1,17 +1,25 @@
    -

    documentation

    +

    documentation

    - getHelper('Url'); $view = $this; - ?> - 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('
  • %s
  • ', $url->getAbsoluteUrl(), $section->title); - }); ?> + ?> + 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( + '
  • %s
  • ', + $section->nofollow ? 'rel="nofollow" ' : '', + $url->getAbsoluteUrl(), + $section->title + ); + }); ?>
    diff --git a/modules/doc/application/views/scripts/partials/chapter.phtml b/modules/doc/application/views/scripts/partials/chapter.phtml deleted file mode 100644 index b0e1fe0cb..000000000 --- a/modules/doc/application/views/scripts/partials/chapter.phtml +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/doc/library/Doc/DocController.php b/modules/doc/library/Doc/DocController.php index ef44de478..bc335f381 100644 --- a/modules/doc/library/Doc/DocController.php +++ b/modules/doc/library/Doc/DocController.php @@ -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; } }