2013-10-16 14:45:23 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}
|
|
|
|
|
|
|
|
namespace Icinga\Module\Doc;
|
|
|
|
|
2014-01-24 16:41:37 +01:00
|
|
|
require_once 'vendor/Parsedown/Parsedown.php';
|
2013-10-16 14:45:23 +02:00
|
|
|
|
|
|
|
use \RecursiveIteratorIterator;
|
|
|
|
use \RecursiveDirectoryIterator;
|
2014-01-24 16:41:37 +01:00
|
|
|
use \Parsedown;
|
2014-02-11 15:27:42 +01:00
|
|
|
use Icinga\Application\Icinga;
|
2014-02-05 12:35:44 +01:00
|
|
|
use Icinga\Web\Menu;
|
2014-02-11 15:27:42 +01:00
|
|
|
use Icinga\Web\Url;
|
2013-10-16 14:45:23 +02:00
|
|
|
|
|
|
|
/**
|
2014-01-24 16:41:37 +01:00
|
|
|
* Parser for documentation written in Markdown
|
2013-10-16 14:45:23 +02:00
|
|
|
*/
|
2014-02-03 15:39:53 +01:00
|
|
|
class DocParser
|
2013-10-16 14:45:23 +02:00
|
|
|
{
|
2014-02-11 15:27:42 +01:00
|
|
|
protected $dir;
|
|
|
|
|
|
|
|
protected $module;
|
|
|
|
|
2013-10-16 14:45:23 +02:00
|
|
|
/**
|
2014-02-11 15:27:42 +01:00
|
|
|
* Create a new documentation parser for the given module or the application
|
|
|
|
*
|
|
|
|
* @param string $module
|
2013-10-16 14:45:23 +02:00
|
|
|
*
|
2014-02-11 15:27:42 +01:00
|
|
|
* @throws DocException
|
|
|
|
*/
|
|
|
|
public function __construct($module = null)
|
|
|
|
{
|
|
|
|
if ($module === null) {
|
|
|
|
$dir = Icinga::app()->getApplicationDir('/../doc');
|
|
|
|
} 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)) {
|
|
|
|
throw new DocException('Doc directory does not exist');
|
|
|
|
}
|
|
|
|
$this->dir = $dir;
|
|
|
|
$this->module = $module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve table of contents and HTML converted from markdown files sorted by filename
|
2013-10-16 14:45:23 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2014-02-11 15:27:42 +01:00
|
|
|
* @throws DocException
|
2013-10-16 14:45:23 +02:00
|
|
|
*/
|
2014-02-11 15:27:42 +01:00
|
|
|
public function getDocumentation()
|
2013-10-16 14:45:23 +02:00
|
|
|
{
|
|
|
|
$iter = new RecursiveIteratorIterator(
|
|
|
|
new MarkdownFileIterator(
|
2014-02-11 15:27:42 +01:00
|
|
|
new RecursiveDirectoryIterator($this->dir)
|
2013-10-16 14:45:23 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$fileInfos = iterator_to_array($iter);
|
|
|
|
natcasesort($fileInfos);
|
2014-02-05 12:35:44 +01:00
|
|
|
$cat = array();
|
2014-02-11 15:27:42 +01:00
|
|
|
$toc = array((object) array(
|
|
|
|
'level' => 0,
|
|
|
|
'item' => new Menu('doc')
|
|
|
|
));
|
2014-02-11 16:35:36 +01:00
|
|
|
$itemPriority = 1;
|
2013-10-16 14:45:23 +02:00
|
|
|
foreach ($fileInfos as $fileInfo) {
|
|
|
|
try {
|
|
|
|
$fileObject = $fileInfo->openFile();
|
|
|
|
} catch (RuntimeException $e) {
|
2014-02-11 15:27:42 +01:00
|
|
|
throw new DocException($e->getMessage());
|
2013-10-16 14:45:23 +02:00
|
|
|
}
|
|
|
|
if ($fileObject->flock(LOCK_SH) === false) {
|
2014-02-11 15:27:42 +01:00
|
|
|
throw new DocException('Couldn\'t get the lock');
|
2013-10-16 14:45:23 +02:00
|
|
|
}
|
2014-02-11 16:35:36 +01:00
|
|
|
$line = null;
|
2013-10-16 14:45:23 +02:00
|
|
|
while (!$fileObject->eof()) {
|
2014-02-11 15:27:42 +01:00
|
|
|
// Save last line for setext-style headers
|
|
|
|
$lastLine = $line;
|
|
|
|
$line = $fileObject->fgets();
|
|
|
|
$header = $this->extractHeader($line, $lastLine);
|
|
|
|
if ($header !== null) {
|
|
|
|
list($header, $level) = $header;
|
|
|
|
$id = $this->extractHeaderId($header);
|
|
|
|
$attribs = array();
|
|
|
|
$this->reduceToc($toc, $level);
|
|
|
|
if ($id === null) {
|
|
|
|
$path = array();
|
|
|
|
foreach (array_slice($toc, 1) as $entry) {
|
|
|
|
$path[] = $entry->item->getTitle();
|
|
|
|
}
|
|
|
|
$path[] = $header;
|
|
|
|
$id = implode('-', $path);
|
|
|
|
$attribs['rel'] = 'nofollow';
|
2014-02-05 12:35:44 +01:00
|
|
|
}
|
2014-02-11 16:35:36 +01:00
|
|
|
$id = urlencode(str_replace('.', '.', strip_tags($id)));
|
2014-02-11 15:27:42 +01:00
|
|
|
$item = end($toc)->item->addChild(
|
|
|
|
$id,
|
|
|
|
array(
|
|
|
|
'url' => Url::fromPath(
|
|
|
|
'doc/module/view',
|
|
|
|
array(
|
|
|
|
'name' => $this->module
|
|
|
|
)
|
2014-02-11 16:35:36 +01:00
|
|
|
)->setAnchor($id)->getRelativeUrl(),
|
2014-02-11 15:27:42 +01:00
|
|
|
'title' => htmlspecialchars($header),
|
|
|
|
'priority' => $itemPriority++,
|
|
|
|
'attribs' => $attribs
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$toc[] = ((object) array(
|
2014-02-05 12:35:44 +01:00
|
|
|
'level' => $level,
|
|
|
|
'item' => $item
|
|
|
|
));
|
2014-02-11 15:27:42 +01:00
|
|
|
$line = '<span id="' . $id . '"></span>' . PHP_EOL . $line;
|
2013-10-16 14:45:23 +02:00
|
|
|
}
|
|
|
|
$cat[] = $line;
|
|
|
|
}
|
|
|
|
$fileObject->flock(LOCK_UN);
|
|
|
|
}
|
2014-01-24 16:41:37 +01:00
|
|
|
$html = Parsedown::instance()->parse(implode('', $cat));
|
2014-02-11 15:27:42 +01:00
|
|
|
return array($html, $toc[0]->item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract atx- or setext-style headers from the given lines
|
|
|
|
*
|
|
|
|
* @param string $line
|
|
|
|
* @param string $lastLine
|
|
|
|
*
|
|
|
|
* @return array|null An array containing the header and the header level or null if there's nothing to extract
|
|
|
|
*/
|
|
|
|
protected function extractHeader($line, $lastLine)
|
|
|
|
{
|
|
|
|
if (!$line) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$header = null;
|
|
|
|
if ($line &&
|
|
|
|
$line[0] === '#' &&
|
|
|
|
preg_match('/^#+/', $line, $match) === 1
|
|
|
|
) {
|
|
|
|
// Atx-style
|
|
|
|
$level = strlen($match[0]);
|
|
|
|
$header = trim(substr($line, $level));
|
|
|
|
if (!$header) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} elseif (
|
|
|
|
$line &&
|
|
|
|
($line[0] === '=' || $line[0] === '-') &&
|
|
|
|
preg_match('/^[=-]+\s*$/', $line, $match) === 1
|
|
|
|
) {
|
|
|
|
// Setext
|
|
|
|
$header = trim($lastLine);
|
|
|
|
if (!$header) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($match[0][0] === '=') {
|
|
|
|
$level = 1;
|
|
|
|
} else {
|
|
|
|
$level = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($header === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return array($header, $level);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract header id in an a or a span tag
|
|
|
|
*
|
|
|
|
* @param string &$header
|
|
|
|
*
|
|
|
|
* @return id|null
|
|
|
|
*/
|
|
|
|
protected function extractHeaderId(&$header)
|
|
|
|
{
|
|
|
|
if ($header[0] === '<' &&
|
|
|
|
preg_match('#(?:<(?P<tag>a|span) id="(?P<id>.+)"></(?P=tag)>)#u', $header, $match)
|
|
|
|
) {
|
|
|
|
$header = str_replace($match[0], '', $header);
|
|
|
|
return $match['id'];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduce the toc to the given level
|
|
|
|
*
|
|
|
|
* @param array &$toc
|
|
|
|
* @param int $level
|
|
|
|
*/
|
|
|
|
protected function reduceToc(array &$toc, $level) {
|
|
|
|
while (end($toc)->level >= $level) {
|
|
|
|
array_pop($toc);
|
|
|
|
}
|
2013-10-16 14:45:23 +02:00
|
|
|
}
|
|
|
|
}
|