Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

[ NEW PLUGIN ] Keyword_Highlighter v1

Quote Reply
[ NEW PLUGIN ] Keyword_Highlighter v1
Hi guys. Got a new plugin for ya'll Cool

This one is pretty simple ... but gives a nice effect on your site.

In short, it will put a background highlight over search words. I would post a demo here.. but I don't think the forum supports <span></span> codes :(

You can download the plugin here (attached).

...or, click here: http://www.ultranerds.com/...bin/details/122.html

If you have any questions, please don't hesitate to ask.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] [ NEW PLUGIN ] Keyword_Highlighter v1 In reply to
Here's a cool mod =)

In Highlight_Keywords.pm, and replace sub Highlight_String, with the following code;

Code:
# This is where we pass it in to be highlighted...
# <%Plugins::Keyword_Highlighter::Highlight_String($FieldName,$query)%>
sub Highlight_String {

# grab our options...
my $value = $_[0];
my $query = $_[1];
my @words = split / /, $query;

my @colors = qw(FFFF99 FFCC99 FFCCCC CC99FF CCFF99 CC9900 CC6699 CCFFCC 999966 CC6699);

my $count = 0;
foreach (@words) {

if (lc($_) eq 'and') { next; }

my $formatted_query_ucf = ucfirst(lc($_));
my $formatted_query_uc = uc(lc($_));
my $formatted_query_lc = lc($_);

# now lets make the pretty colours!
my $highlight_string = qq|style="background-color: #| . $colors[$count] . q|"|;
my $replace = qq|<span $highlight_string>$_</span>|;
my $replace2 = qq|<span $highlight_string>$formatted_query_ucf</span>|;
my $replace3 = qq|<span $highlight_string>$formatted_query_lc</span>|;
my $replace4 = qq|<span $highlight_string>$formatted_query_uc</span>|;
$value =~ s|\Q$_|$replace|g;
$value =~ s|\Q$formatted_query_ucf|$replace2|g;
$value =~ s|\Q$formatted_query_lc|$replace3|g;
$value =~ s|\Q$formatted_query_uc|$replace4|g;

$count++;
}

# send it back...
return $value;
}

This will intelligently use the same color for each keyword (depending on where its located in the query).

Please note, I did try using str4ings like "red", "cyan", etc ... but it caused a LOT of problems in the results ... so don't try it Tongue

Enjoy!

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] [ NEW PLUGIN ] Keyword_Highlighter v1 In reply to
How do I get the the highlighter to highlight with one color per keyword, because I m getting so many colors for the same keyword and that makes it untidy
Quote Reply
Re: [Abusaki] [ NEW PLUGIN ] Keyword_Highlighter v1 In reply to
Not very easily I'm afriad :( I spent quite a while trying to do this. Basically, you could try editing the .pm file, so that Highlight_String() looks like;

Code:
# This is where we pass it in to be highlighted...
# <%Plugins::Keyword_Highlighter::Highlight_String($FieldName,$query)%>
sub Highlight_String {

# grab our options...
my $value = $_[0];
my $query = $_[1];
my @words = split / /, $query;

# define our random color.
my $max = $#words;
my @colors = map {
join "", map { sprintf "%02x", rand(255) } (0..2)
} (0..$max);

foreach (@words) {

my $color;
if (/^[abcd]/i) {
$color = $colors[0];
} elsif (/^[efgh]/i) {
$color = $colors[1];
} elsif (/^\d/i) {
$color = $colors[2];
} elsif (/^[ijklmn]/i) {
$color = $colors[3];
} elsif (/^[opqrstu]/i) {
$color = $colors[3];
} elsif (/^[vwxyz]/i) {
$color = $colors[3];
} elsif (/^\W/i) {
$color = $colors[3];
}

# now lets make the pretty colours!
my $highlight_string = qq|style="background-color: #$color"|;
my $replace = qq|<span $highlight_string>$_</span>|;
$value =~ s|\Q$_|$replace|ig;

}

# send it back...
return $value;

}

Please note, this is UNTESTED (and its early morning here =)), so please be aware it may not work "as-is".

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Dec 24, 2004, 12:38 AM
Quote Reply
Re: [Andy] [ NEW PLUGIN ] Keyword_Highlighter v1 In reply to
Hi Andy,
this is what I got

Error: Unable to load module: Plugins::Keyword_Highlighter. Reason:
Error: No subroutine 'Plugins::Keyword_Highlighter::Highlight_String' in 'Plugins/Keyword_Highlighter.pm',
Can't locate Plugins.pm in @INC (@INC contains: /home/httpd/vhosts/xxxxxxxxxxxxxxx/cgi-bin/admin /usr/lib/perl5/5.8.1/i386-linux-thread-multi /usr/lib/perl5/5.8.1 /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at GT::Template::_call_func line 783.
Quote Reply
Re: [Abusaki] [ NEW PLUGIN ] Keyword_Highlighter v1 In reply to
Sorry, there was one line missing;

my $color;

I've put it in the above post for you (in red).

Hopefully that should stop it giving any errors Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!