From 3b20e499404d6b451a385522e91341b652197a21 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 10 Feb 2015 17:13:17 +0100 Subject: [PATCH] doc/lib: Add renderer for doc searches refs #6630 --- .../library/Doc/Search/DocSearchRenderer.php | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 modules/doc/library/Doc/Search/DocSearchRenderer.php diff --git a/modules/doc/library/Doc/Search/DocSearchRenderer.php b/modules/doc/library/Doc/Search/DocSearchRenderer.php new file mode 100644 index 000000000..97f830a93 --- /dev/null +++ b/modules/doc/library/Doc/Search/DocSearchRenderer.php @@ -0,0 +1,147 @@ +content[] = ''; + } + + /** + * {@inheritdoc} + */ + public function beginChildren() + { + if ($this->getInnerIterator()->getMatches()) { + $this->content[] = ''; + } + } + + public function highlight($line, array $matches) + { + $highlighted = ''; + $offset = 0; + ksort($matches); + foreach ($matches as $position => $match) { + $highlighted .= $this->getView()->escape(substr($line, $offset, $position - $offset)) + . '' + . $this->getView()->escape($match) + . ''; + $offset = $position + strlen($match); + } + $highlighted .= $this->getView()->escape(substr($line, $offset)); + return $highlighted; + } + + /** + * {@inheritdoc} + */ + public function render() + { + foreach ($this as $section) { + if (($matches = $this->getInnerIterator()->getMatches()) === null) { + continue; + } + $title = $this->getView()->escape($section->getTitle()); + $contentMatches = array(); + foreach ($matches as $match) { + if ($match->getMatchType() === DocSearchMatch::MATCH_HEADER) { + $title = $this->highlight($match->getLine(), $match->getMatches()); + } else { + $contentMatches[] = sprintf( + '

%s

', + $this->highlight($match->getLine(), $match->getMatches()) + ); + } + } + $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, + array('highlight' => $this->getInnerIterator()->getSearch()->getInput()) + ); + /** @type \Icinga\Web\Url $url */ + $url->setAnchor($this->encodeAnchor($section->getId())); + $this->content[] = sprintf( + '
  • %s', + $section->getNoFollow() ? 'rel="nofollow" ' : '', + $url->getAbsoluteUrl(), + $title + ); + if (! empty($contentMatches)) { + $this->content = array_merge($this->content, $contentMatches); + } + if (! $section->hasChildren()) { + $this->content[] = '
  • '; + } + } + return implode("\n", $this->content); + } +}