function highlight_keywords($text, $keywords, $sprint = '<strong class="highlight">%s</strong>') { $highlighter = new sfLuceneHighlighterXHTMLPart($text); $highlighter->setMasterDtd(sfConfig::get('app_lucene_xhtml_dtd', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')); $marker = new sfLuceneHighlighterMarkerSprint($sprint); $harness = new sfLuceneHighlighterMarkerHarness(array($marker)); $keywords = sfLuceneHighlighterKeywordNamedInsensitive::explode($harness, $keywords); $highlighter->addKeywords($keywords); return $highlighter->highlight()->export(); }
$expected = '<p>This is part of a document, dedicated to <h>foobar</h>.</p><p>Look, a <h>foobar</h></p>'; $keyword = new sfLuceneHighlighterKeywordNamed(new sfLuceneHighlighterMarkerSprint('<h>%s</h>'), 'foobar'); $highlighter = new sfLuceneHighlighterXHTMLPart($given); $highlighter->setMasterDtd($dtd); $highlighter->addKeywords(array($keyword)); $highlighter->highlight(); $t->is($highlighter->export(), $expected, '->highlight() highlights a part of the document and returns just that part'); $given = '<html><body><p>This is part of a document, dedicated to foobar.</p></body></html>'; $expected = '<html xmlns="http://www.w3.org/1999/xhtml"><body><p>This is part of a document, dedicated to <h>foobar</h>.</p></body></html>'; $keyword = new sfLuceneHighlighterKeywordNamed(new sfLuceneHighlighterMarkerSprint('<h>%s</h>'), 'foobar'); $highlighter = new sfLuceneHighlighterXHTMLPart($given); $highlighter->setMasterDtd($dtd); $highlighter->addKeywords(array($keyword)); $highlighter->highlight(); $t->is($highlighter->export(), $expected, '->highlight() does not fail if it is really a full document'); $given = '<p>This is pàrt of a document, dedicated to foobar.</p>'; $expected = '<p>This is pàrt of a document, dedicated to <h>foobar</h>.</p>'; $keyword = new sfLuceneHighlighterKeywordNamed(new sfLuceneHighlighterMarkerSprint('<h>%s</h>'), 'foobar'); $highlighter = new sfLuceneHighlighterXHTMLPart($given); $highlighter->setMasterDtd($dtd); $highlighter->addKeywords(array($keyword)); $highlighter->highlight(); $t->is($highlighter->export(), $expected, '->highlight() handles entities correctly'); $given = '<p>Présentation du document, dédié au foobar.</p>'; $expected = '<p>Présentation du document, dédié au <h>foobar</h>.</p>'; $keyword = new sfLuceneHighlighterKeywordNamed(new sfLuceneHighlighterMarkerSprint('<h>%s</h>'), 'foobar'); $highlighter = new sfLuceneHighlighterXHTMLPart($given); $highlighter->setMasterDtd($dtd); $highlighter->addKeywords(array($keyword)); $highlighter->highlight(); $t->is($highlighter->export(), $expected, '->highlight() utf-8 characters');