doc/SectionRenderer: Support images

refs #4820
This commit is contained in:
Eric Lippmann 2014-07-29 11:10:06 +02:00
parent a0867ce33a
commit 55d3818ebb
1 changed files with 27 additions and 1 deletions

View File

@ -6,10 +6,13 @@ namespace Icinga\Module\Doc;
require_once 'IcingaVendor/Parsedown/Parsedown.php';
use Icinga\Module\Doc\Exception\ChapterNotFoundException;
use DOMDocument;
use DOMXPath;
use RecursiveIteratorIterator;
use Parsedown;
use Zend_View_Helper_Url;
use Icinga\Module\Doc\Exception\ChapterNotFoundException;
use Icinga\Web\Url;
use Icinga\Web\View;
/**
@ -156,6 +159,24 @@ class SectionRenderer extends Renderer
return '<pre>' . highlight_string(htmlspecialchars_decode($match[1]), true) . '</pre>';
}
/**
* 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);
/* @var $img \DOMElement */
$img->setAttribute('src', Url::fromPath($img->getAttribute('src'))->getAbsoluteUrl());
return substr_replace($doc->saveXML($img), '', -2, 1); // Replace '/>' with '>'
}
/**
* Render the section
*
@ -181,6 +202,11 @@ class SectionRenderer extends Renderer
array($this, 'highlightPhp'),
$this->parsedown->text(implode('', $section->getContent()))
);
$html = preg_replace_callback(
'/<img[^>]+>/',
array($this, 'replaceImg'),
$html
);
$content[] = preg_replace_callback(
'/<a\s+(?P<attribs>[^>]*?\s+)?href="#(?P<fragment>[^"]+)"/',
array($callback, 'render'),