A module to highlight words or patterns in \s-1html\s0 documents
use HTML::Highlight; # create the highlighter object my $hl = new HTML::Highlight ( words => [ 'word', 'any', 'car', 'some phrase' ], wildcards => [ undef, '%', '*', undef ], colors => [ '#FF0000', 'red', 'green', 'rgb(255, 0, 0)' ], czech_language => 0, debug => 0 ); # Remember that you don't need to specify your own colors. # The default colors should be optimal. # Now you can use the object to highlight patterns in a document # by passing content of the document to its highlight() method. # The highlighter object "remembers" its configuration. my $highlighted_document = $hl->highlight($document);
This module was originaly created to work together with fulltext indexing module DBIx::TextIndex to highlight search results.
A need for a highlighter that takes wildcard matches and \s-1HTML\s0 tags into account and supports czech language (or other Slavic languages) was the motivation to create this module.
This module provides Google-like highlighting of words or patterns in \s-1HTML\s0 documents. This feature is typically used to highlight search results.
my $hl = new HTML::Highlight ( words => [], wildcards => [], colors => [], czech_language => 0, debug => 0 ); This is a constructor of the highlighter object. It takes an array of even number of parameters. The words parameter is a reference to an array of words to highlight. The wildcards parameter is a reference to an array of wildcards, that are applied to corresponding words in the words array. A wildcard can be either undef or one of '%' or '*'. The \*(L"%\*(R" character means \*(L"match any characters\*(R": "%" applied to 'car' ==> matches "car", "cars", "careful", ... The \*(L"*\*(R" character means \*(L"match also plural form of the word\*(R": "*" applied to 'car' ==> matches only "car" or "cars" An undefined wildcard means \*(L"match exactly the corresponding word\*(R": undefined wildcard applied to 'car' ==> matches only "car" The colors parameter is a reference to an array of \s-1CSS\s0 color identificators, that are used to highlight the corresponding words in the words array. Default Google-like colors are used if you don't specify your own colors. Number of colors can be lower than number of words - in this case the colors are rotated and some of the words are therefore highlighted using the same color. The highlighter takes \s-1HTML\s0 tags into account and therefore does not \*(L"highlight\*(R" a word or a pattern inside a tag. A support for diacritics insenstive matching for \s-1ISO-8859-2\s0 languages (for for example the czech language) can be activated using the czech_language option. This feature requires a module CzFast that is available on \s-1CPAN\s0 in a directory of author \s-1TRIPIE\s0 or at http://geocities.com/tripiecz/. Your system's locales must be set correctly to use the czech_language feature.
my $hl_document = $hl->highlight($document); The only parameter is a document in that you want to highlight the words that were passed to the constructor of the highlighter object. The method returns a version of the document in which the words are highlighted.
my $sections = $hl->preview_context($document, $num); This method takes two parameters. The first one is the document you want to scan for the words that were passed to the constructor of the highlighter object. The second parameter is an optional integer that specifies maximum number of characters in each of the context sections (see below). This parameter defaults to 80 characters if it's not specified. Minimum allowed value of this parameter is 60. The method returns a reference to an array of sections of the document in which the words that were passed to the constructor appear. \s-1HTML\s0 tags are removed before the document is proccessed and are not present in the ouput. This feature is typically used in search engines to preview a context in which words from a search query appear in the resulting documents. The words are always in the middle of each of the sections. The number of sections this method returns is equal to the number of words passed to the constructor of the highlighter object. That means only the first occurence of each of the words is taken into account.
No official support is provided, but I welcome any comments, patches and suggestions on my email.
I am aware of no bugs.
http://geocities.com/tripiecz/
Tomas Styblo, [email protected], CPAN-ID \s-1TRIPIE\s0
Prague, the Czech republic
HTML::Highlight - A module to highlight words or patterns in \s-1HTML\s0 documents
Copyright (C) 2000 Tomas Styblo ([email protected])
This module is free software; you can redistribute it and/or modify it under the terms of either:
a) the \s-1GNU\s0 General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or
b) the \*(L"Artistic License\*(R" which comes with this module.
This program is distributed in the hope that it will be useful, but \s-1WITHOUT\s0 \s-1ANY\s0 \s-1WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. See either the \s-1GNU\s0 General Public License or the Artistic License for more details.
You should have received a copy of the Artistic License with this module, in the file Artistic. If not, I'll be glad to provide one.
You should have received a copy of the \s-1GNU\s0 General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, \s-1MA\s0 02111-1307 \s-1USA\s0
perl\|(1).