doc/lib: Add DocSearchMatch::highlight()

refs #6630
This commit is contained in:
Eric Lippmann 2015-02-11 13:15:48 +01:00
parent 1053e62e20
commit ab78757483
1 changed files with 64 additions and 0 deletions

View File

@ -4,12 +4,21 @@
namespace Icinga\Module\Doc\Search;
use UnexpectedValueException;
use Icinga\Application\Icinga;
use Icinga\Web\View;
/**
* A doc search match
*/
class DocSearchMatch
{
/**
* CSS class for highlighting matches
*
* @type string
*/
const HIGHLIGHT_CSS_CLASS = 'search-highlight';
/**
* Header match
*
@ -52,6 +61,13 @@ class DocSearchMatch
*/
protected $matches = array();
/**
* View
*
* @type View|null
*/
protected $view;
/**
* Set the line
*
@ -139,6 +155,54 @@ class DocSearchMatch
return $this->matches;
}
/**
* Set the view
*
* @param View $view
*
* @return $this
*/
public function setView(View $view)
{
$this->view = $view;
return $this;
}
/**
* Get the view
*
* @return View
*/
public function getView()
{
if ($this->view === null) {
$this->view = Icinga::app()->getViewRenderer()->view;
}
return $this->view;
}
/**
* Get the line having matches highlighted
*
* @return string
*/
public function highlight()
{
$highlighted = '';
$offset = 0;
$matches = $this->getMatches();
ksort($matches);
foreach ($matches as $position => $match) {
$highlighted .= $this->getView()->escape(substr($this->line, $offset, $position - $offset))
. '<span class="' . static::HIGHLIGHT_CSS_CLASS .'">'
. $this->getView()->escape($match)
. '</span>';
$offset = $position + strlen($match);
}
$highlighted .= $this->getView()->escape(substr($this->line, $offset));
return $highlighted;
}
/**
* Whether the match is empty
*