/** * Replace link * * @param array $match * * @return string */ protected function replaceLink($match) { if (($section = $this->tree->getNode($this->decodeAnchor($match['fragment']))) === null) { return $match[0]; } /** @var \Icinga\Module\Doc\DocSection $section */ $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); /** @var \Icinga\Web\Url $url */ $url->setAnchor($this->encodeAnchor($section->getId())); return sprintf('<a %s%shref="%s"', strlen($match['attribs']) ? trim($match['attribs']) . ' ' : '', $section->getNoFollow() ? 'rel="nofollow" ' : '', $url->getAbsoluteUrl()); }
/** * Get the documentation tree * * @return SimpleTree */ public function getDocTree() { $tree = new SimpleTree(); foreach ($this->docIterator as $fileInfo) { /** @var $fileInfo \SplFileInfo */ $file = $fileInfo->openFile(); $lastLine = null; $stack = new SplStack(); $cachingIterator = new CachingIterator($file, CachingIterator::TOSTRING_USE_CURRENT); for ($cachingIterator->rewind(); $line = $cachingIterator->valid(); $cachingIterator->next()) { $fileIterator = $cachingIterator->getInnerIterator(); $line = $cachingIterator->current(); $header = $this->extractHeader($line, $fileIterator->valid() ? $fileIterator->current() : null); if ($header !== null) { list($title, $id, $level, $headerStyle) = $header; while (!$stack->isEmpty() && $stack->top()->getLevel() >= $level) { $stack->pop(); } if ($id === null) { $path = array(); foreach ($stack as $section) { /** @var $section DocSection */ $path[] = $section->getTitle(); } $path[] = $title; $id = implode('-', $path); $noFollow = true; } else { $noFollow = false; } if ($tree->getNode($id) !== null) { $id = uniqid($id); } $section = new DocSection(); $section->setId($id)->setTitle($title)->setLevel($level)->setNoFollow($noFollow); if ($stack->isEmpty()) { $section->setChapter($section); $tree->addChild($section); } else { $section->setChapter($stack->bottom()); $tree->addChild($section, $stack->top()); } $stack->push($section); if ($headerStyle === static::HEADER_SETEXT) { $cachingIterator->next(); continue; } } else { if ($stack->isEmpty()) { $title = ucfirst($file->getBasename('.' . pathinfo($file->getFilename(), PATHINFO_EXTENSION))); $id = $title; if ($tree->getNode($id) !== null) { $id = uniqid($id); } $section = new DocSection(); $section->setId($id)->setTitle($title)->setLevel(1)->setNoFollow(true); $section->setChapter($section); $tree->addChild($section); $stack->push($section); } $stack->top()->appendContent($line); } } } return $tree; }
/** * Generate unique section ID * * @param string $id * @param string $filename * @param SimpleTree $tree * * @return string */ protected function uuid($id, $filename, SimpleTree $tree) { $id = str_replace(' ', '-', $id); if ($tree->getNode($id) === null) { return $id; } $id = $id . '-' . md5($filename); $offset = 0; while ($tree->getNode($id)) { if ($offset++ === 0) { $id .= '-' . $offset; } else { $id = substr($id, 0, -1) . $offset; } } return $id; }
/** * Get the documentation tree * * @return SimpleTree */ public function getDocTree() { $tree = new SimpleTree(); $stack = new SplStack(); foreach ($this->docIterator as $fileInfo) { /** @var $fileInfo \SplFileInfo */ $file = $fileInfo->openFile(); $lastLine = null; foreach ($file as $line) { $header = $this->extractHeader($line, $lastLine); if ($header !== null) { list($title, $id, $level) = $header; while (!$stack->isEmpty() && $stack->top()->getLevel() >= $level) { $stack->pop(); } if ($id === null) { $path = array(); foreach ($stack as $section) { /** @var $section DocSection */ $path[] = $section->getTitle(); } $path[] = $title; $id = implode('-', $path); $noFollow = true; } else { $noFollow = false; } if ($tree->getNode($id) !== null) { $id = uniqid($id); } $section = new DocSection(); $section->setId($id)->setTitle($title)->setLevel($level)->setNoFollow($noFollow); if ($stack->isEmpty()) { $section->setChapter($section); $tree->addChild($section); } else { $section->setChapter($stack->bottom()); $tree->addChild($section, $stack->top()); } $stack->push($section); } else { if ($stack->isEmpty()) { throw new LogicException('Heading required'); } $stack->top()->appendContent($line); } // Save last line for setext-style headers $lastLine = $line; } } return $tree; }