2014-07-28 19:11:15 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-07-28 19:11:15 +02:00
|
|
|
|
2015-02-11 14:10:12 +01:00
|
|
|
namespace Icinga\Module\Doc\Renderer;
|
2014-07-28 19:11:15 +02:00
|
|
|
|
2014-11-14 11:50:56 +01:00
|
|
|
require_once 'Parsedown/Parsedown.php';
|
2014-07-28 19:11:15 +02:00
|
|
|
|
2014-07-29 11:10:06 +02:00
|
|
|
use DOMDocument;
|
|
|
|
use DOMXPath;
|
2014-07-28 19:11:15 +02:00
|
|
|
use Parsedown;
|
2015-02-10 17:08:48 +01:00
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
use Icinga\Data\Tree\SimpleTree;
|
2014-07-29 11:10:06 +02:00
|
|
|
use Icinga\Module\Doc\Exception\ChapterNotFoundException;
|
2015-02-11 14:10:12 +01:00
|
|
|
use Icinga\Module\Doc\DocSectionFilterIterator;
|
2015-02-11 13:20:30 +01:00
|
|
|
use Icinga\Module\Doc\Search\DocSearch;
|
|
|
|
use Icinga\Module\Doc\Search\DocSearchMatch;
|
|
|
|
use Icinga\Web\Dom\DomNodeIterator;
|
2014-07-29 11:10:06 +02:00
|
|
|
use Icinga\Web\Url;
|
2014-07-28 19:11:15 +02:00
|
|
|
use Icinga\Web\View;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Section renderer
|
|
|
|
*/
|
2015-02-11 14:05:18 +01:00
|
|
|
class DocSectionRenderer extends DocRenderer
|
2014-07-28 19:11:15 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-02-10 17:08:48 +01:00
|
|
|
* Content to render
|
2014-07-28 19:11:15 +02:00
|
|
|
*
|
2015-03-12 13:39:17 +01:00
|
|
|
* @var array
|
2014-07-28 19:11:15 +02:00
|
|
|
*/
|
2015-02-10 17:08:48 +01:00
|
|
|
protected $content = array();
|
2014-07-28 19:11:15 +02:00
|
|
|
|
2015-02-11 13:20:30 +01:00
|
|
|
/**
|
|
|
|
* Search criteria to highlight
|
|
|
|
*
|
2015-03-12 13:39:17 +01:00
|
|
|
* @var string
|
2015-02-11 13:20:30 +01:00
|
|
|
*/
|
|
|
|
protected $highlightSearch;
|
|
|
|
|
2014-07-28 19:11:15 +02:00
|
|
|
/**
|
|
|
|
* Parsedown instance
|
|
|
|
*
|
2015-03-12 13:39:17 +01:00
|
|
|
* @var Parsedown
|
2014-07-28 19:11:15 +02:00
|
|
|
*/
|
|
|
|
protected $parsedown;
|
|
|
|
|
|
|
|
/**
|
2015-02-10 17:08:48 +01:00
|
|
|
* Documentation tree
|
2014-07-28 19:11:15 +02:00
|
|
|
*
|
2015-03-12 13:39:17 +01:00
|
|
|
* @var SimpleTree
|
2014-07-28 19:11:15 +02:00
|
|
|
*/
|
2015-02-10 17:08:48 +01:00
|
|
|
protected $tree;
|
2014-07-28 19:11:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new section renderer
|
|
|
|
*
|
2015-02-10 17:08:48 +01:00
|
|
|
* @param SimpleTree $tree The documentation tree
|
|
|
|
* @param string|null $chapter If not null, the chapter to filter for
|
2014-07-28 19:11:15 +02:00
|
|
|
*
|
|
|
|
* @throws ChapterNotFoundException If the chapter to filter for was not found
|
|
|
|
*/
|
2015-02-10 17:08:48 +01:00
|
|
|
public function __construct(SimpleTree $tree, $chapter = null)
|
2014-07-28 19:11:15 +02:00
|
|
|
{
|
2015-02-10 17:08:48 +01:00
|
|
|
if ($chapter !== null) {
|
2015-02-11 14:06:41 +01:00
|
|
|
$filter = new DocSectionFilterIterator($tree->getIterator(), $chapter);
|
2015-02-10 17:08:48 +01:00
|
|
|
if ($filter->isEmpty()) {
|
2014-07-28 19:11:15 +02:00
|
|
|
throw new ChapterNotFoundException(
|
2015-02-10 17:08:48 +01:00
|
|
|
mt('doc', 'Chapter %s not found'), $chapter
|
2014-07-28 19:11:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
parent::__construct(
|
|
|
|
$filter,
|
|
|
|
RecursiveIteratorIterator::SELF_FIRST
|
|
|
|
);
|
|
|
|
} else {
|
2015-02-10 17:08:48 +01:00
|
|
|
parent::__construct($tree->getIterator(), RecursiveIteratorIterator::SELF_FIRST);
|
2014-07-28 19:11:15 +02:00
|
|
|
}
|
2015-02-10 17:08:48 +01:00
|
|
|
$this->tree = $tree;
|
2014-07-28 19:11:15 +02:00
|
|
|
$this->parsedown = Parsedown::instance();
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:20:30 +01:00
|
|
|
/**
|
|
|
|
* Set the search criteria to highlight
|
|
|
|
*
|
|
|
|
* @param string $highlightSearch
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setHighlightSearch($highlightSearch)
|
|
|
|
{
|
|
|
|
$this->highlightSearch = $highlightSearch;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the search criteria to highlight
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getHighlightSearch()
|
|
|
|
{
|
|
|
|
return $this->highlightSearch;
|
|
|
|
}
|
|
|
|
|
2014-07-28 19:11:15 +02:00
|
|
|
/**
|
|
|
|
* Syntax highlighting for PHP code
|
|
|
|
*
|
2015-02-10 17:08:48 +01:00
|
|
|
* @param array $match
|
2014-07-28 19:11:15 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function highlightPhp($match)
|
|
|
|
{
|
|
|
|
return '<pre>' . highlight_string(htmlspecialchars_decode($match[1]), true) . '</pre>';
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:20:30 +01:00
|
|
|
/**
|
|
|
|
* Highlight search criteria
|
|
|
|
*
|
|
|
|
* @param string $html
|
|
|
|
* @param DocSearch $search Search criteria
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function highlightSearch($html, DocSearch $search)
|
|
|
|
{
|
|
|
|
$doc = new DOMDocument();
|
2015-02-12 10:24:55 +01:00
|
|
|
@$doc->loadHTML($html);
|
2015-02-11 13:20:30 +01:00
|
|
|
$iter = new RecursiveIteratorIterator(new DomNodeIterator($doc), RecursiveIteratorIterator::SELF_FIRST);
|
|
|
|
foreach ($iter as $node) {
|
|
|
|
if ($node->nodeType !== XML_TEXT_NODE
|
|
|
|
|| ($node->parentNode->nodeType === XML_ELEMENT_NODE && $node->parentNode->tagName === 'code')
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$text = $node->nodeValue;
|
|
|
|
if (($match = $search->search($text)) === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$matches = $match->getMatches();
|
|
|
|
ksort($matches);
|
|
|
|
$offset = 0;
|
2015-02-11 13:48:23 +01:00
|
|
|
$fragment = $doc->createDocumentFragment();
|
2015-02-11 13:20:30 +01:00
|
|
|
foreach ($matches as $position => $match) {
|
2015-02-11 13:48:23 +01:00
|
|
|
$fragment->appendChild($doc->createTextNode(substr($text, $offset, $position - $offset)));
|
|
|
|
$fragment->appendChild($doc->createElement('span', $match))
|
|
|
|
->setAttribute('class', DocSearchMatch::HIGHLIGHT_CSS_CLASS);
|
2015-02-11 13:20:30 +01:00
|
|
|
$offset = $position + strlen($match);
|
|
|
|
}
|
2015-02-11 13:48:23 +01:00
|
|
|
$fragment->appendChild($doc->createTextNode(substr($text, $offset)));
|
|
|
|
$node->parentNode->replaceChild($fragment, $node);
|
2015-02-11 13:20:30 +01:00
|
|
|
}
|
2015-02-12 10:24:55 +01:00
|
|
|
// Remove <!DOCTYPE
|
|
|
|
$doc->removeChild($doc->doctype);
|
|
|
|
// Remove <html><body> and </body></html>
|
|
|
|
return substr($doc->saveHTML(), 12, -15);
|
2015-02-11 13:20:30 +01:00
|
|
|
}
|
|
|
|
|
2015-02-10 17:08:48 +01:00
|
|
|
/**
|
|
|
|
* Markup notes
|
|
|
|
*
|
|
|
|
* @param array $match
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function markupNotes($match)
|
|
|
|
{
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
$doc->loadHTML($match[0]);
|
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
$blockquote = $xpath->query('//blockquote[1]')->item(0);
|
2015-03-12 13:39:17 +01:00
|
|
|
/** @var \DOMElement $blockquote */
|
2015-02-10 17:08:48 +01:00
|
|
|
if (strtolower(substr(trim($blockquote->nodeValue), 0, 5)) === 'note:') {
|
|
|
|
$blockquote->setAttribute('class', 'note');
|
|
|
|
}
|
|
|
|
return $doc->saveXML($blockquote);
|
|
|
|
}
|
|
|
|
|
2014-07-29 11:10:06 +02:00
|
|
|
/**
|
|
|
|
* Replace img src tags
|
|
|
|
*
|
|
|
|
* @param $match
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function replaceImg($match)
|
|
|
|
{
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
$doc->loadHTML($match[0]);
|
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
$img = $xpath->query('//img[1]')->item(0);
|
2015-03-12 13:39:17 +01:00
|
|
|
/** @var \DOMElement $img */
|
2014-07-29 11:10:06 +02:00
|
|
|
$img->setAttribute('src', Url::fromPath($img->getAttribute('src'))->getAbsoluteUrl());
|
|
|
|
return substr_replace($doc->saveXML($img), '', -2, 1); // Replace '/>' with '>'
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:08:48 +01:00
|
|
|
/**
|
|
|
|
* Replace link
|
|
|
|
*
|
|
|
|
* @param array $match
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function replaceLink($match)
|
2014-11-20 15:29:46 +01:00
|
|
|
{
|
2015-02-10 17:08:48 +01:00
|
|
|
if (($section = $this->tree->getNode($this->decodeAnchor($match['fragment']))) === null) {
|
|
|
|
return $match[0];
|
2014-11-20 15:29:46 +01:00
|
|
|
}
|
2015-03-12 13:39:17 +01:00
|
|
|
/** @var \Icinga\Module\Doc\DocSection $section */
|
2015-02-10 17:08:48 +01:00
|
|
|
$path = $this->getView()->getHelper('Url')->url(
|
|
|
|
array_merge(
|
|
|
|
$this->urlParams,
|
|
|
|
array(
|
|
|
|
'chapter' => $this->encodeUrlParam($section->getChapter()->getId())
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$this->url,
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
$url = $this->getView()->url($path);
|
2015-03-12 13:39:17 +01:00
|
|
|
/** @var \Icinga\Web\Url $url */
|
2015-02-10 17:08:48 +01:00
|
|
|
$url->setAnchor($this->encodeAnchor($section->getId()));
|
|
|
|
return sprintf(
|
|
|
|
'<a %s%shref="%s"',
|
|
|
|
strlen($match['attribs']) ? trim($match['attribs']) . ' ' : '',
|
|
|
|
$section->getNoFollow() ? 'rel="nofollow" ' : '',
|
|
|
|
$url->getAbsoluteUrl()
|
|
|
|
);
|
2014-11-20 15:29:46 +01:00
|
|
|
}
|
|
|
|
|
2014-07-28 19:11:15 +02:00
|
|
|
/**
|
2015-02-10 17:08:48 +01:00
|
|
|
* {@inheritdoc}
|
2014-07-28 19:11:15 +02:00
|
|
|
*/
|
2015-02-10 17:08:48 +01:00
|
|
|
public function render()
|
2014-07-28 19:11:15 +02:00
|
|
|
{
|
2015-02-11 13:20:30 +01:00
|
|
|
$search = null;
|
|
|
|
if (($highlightSearch = $this->getHighlightSearch()) !== null) {
|
|
|
|
$search = new DocSearch($highlightSearch);
|
|
|
|
}
|
2015-02-10 17:08:48 +01:00
|
|
|
foreach ($this as $section) {
|
2015-02-11 13:20:30 +01:00
|
|
|
$title = $section->getTitle();
|
|
|
|
if ($search !== null && ($match = $search->search($title)) !== null) {
|
|
|
|
$title = $match->highlight();
|
|
|
|
} else {
|
|
|
|
$title = $this->getView()->escape($title);
|
|
|
|
}
|
2015-02-10 17:08:48 +01:00
|
|
|
$this->content[] = sprintf(
|
2014-08-19 11:30:56 +02:00
|
|
|
'<a name="%1$s"></a><h%2$d>%3$s</h%2$d>',
|
2015-02-11 14:05:18 +01:00
|
|
|
static::encodeAnchor($section->getId()),
|
2014-07-28 19:11:15 +02:00
|
|
|
$section->getLevel(),
|
2015-02-11 13:20:30 +01:00
|
|
|
$title
|
2014-07-28 19:11:15 +02:00
|
|
|
);
|
2015-02-12 10:24:55 +01:00
|
|
|
$html = $this->parsedown->text(implode('', $section->getContent()));
|
|
|
|
if (empty($html)) {
|
2015-02-11 13:20:30 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-07-28 19:11:15 +02:00
|
|
|
$html = preg_replace_callback(
|
|
|
|
'#<pre><code class="language-php">(.*?)</code></pre>#s',
|
|
|
|
array($this, 'highlightPhp'),
|
2015-02-12 10:24:55 +01:00
|
|
|
$html
|
2014-07-28 19:11:15 +02:00
|
|
|
);
|
2014-07-29 11:10:06 +02:00
|
|
|
$html = preg_replace_callback(
|
|
|
|
'/<img[^>]+>/',
|
|
|
|
array($this, 'replaceImg'),
|
|
|
|
$html
|
|
|
|
);
|
2014-11-20 15:29:46 +01:00
|
|
|
$html = preg_replace_callback(
|
|
|
|
'#<blockquote>.+</blockquote>#ms',
|
2015-02-10 17:08:48 +01:00
|
|
|
array($this, 'markupNotes'),
|
2014-11-20 15:29:46 +01:00
|
|
|
$html
|
|
|
|
);
|
2015-02-11 13:20:30 +01:00
|
|
|
$html = preg_replace_callback(
|
2015-02-12 10:24:55 +01:00
|
|
|
'/<a\s+(?P<attribs>[^>]*?\s+)?href="(?:(?!http:\/\/)[^"#]*)#(?P<fragment>[^"]+)"/',
|
2015-02-10 17:08:48 +01:00
|
|
|
array($this, 'replaceLink'),
|
2014-07-28 19:11:15 +02:00
|
|
|
$html
|
|
|
|
);
|
2015-02-11 13:20:30 +01:00
|
|
|
if ($search !== null) {
|
|
|
|
$html = $this->highlightSearch($html, $search);
|
|
|
|
}
|
|
|
|
$this->content[] = $html;
|
2014-07-28 19:11:15 +02:00
|
|
|
}
|
2015-02-10 17:08:48 +01:00
|
|
|
return implode("\n", $this->content);
|
2014-07-28 19:11:15 +02:00
|
|
|
}
|
|
|
|
}
|