# google -- Turn a string of keywords in a blog entry into a link
#           to search at Google
#
# Author: Joey Gibson <joey@joeygibson.com>
# Version: $Id: google,v 1.3 2003/11/26 05:46:05 jgibson Exp $
#
# History:
# $Log: google,v $
# Revision 1.3  2003/11/26 05:46:05  jgibson
# Now supports the use of meta-google_keywords: instead of the old KW: line.
# It will work with both ways of doing it, but the meta version is now preferred.
#
# Revision 1.2  2003/03/02 04:36:45  jgibson
# Added licenses to google and uainclude.
#
# Revision 1.1.1.1  2003/03/02 04:25:57  jgibson
#
#

package google;

# -- Configurable Variables --
# What string should the keywords line start with?
$keyword_string = "KW:";

# What should the HTML for the beginning of the link
#  look like?
$link_start = qq{ | <a href="http://www.google.com/search?q=};

# What should the link title be?
$link_title = qq{Google};

# What should go after we close the anchor?
$link_end = qq{};

# Should the link open a new window or not?
#  0 = no, 1 = yes
$open_new_window = 1;
# ----------------------------

# -- Don't mess with the stuff below this line

$google = '';

sub start
{
	1;
}

sub story
{
	my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
	$google = '';

	if ($meta::google_keywords)
	{
		$google = $link_start;
		$google .= join "+", (split /\s+/, $meta::google_keywords);
		chomp $google;
	} else
	{
		my $first_line = (split /\n/, $$body_ref)[0];

		return 1 unless $first_line =~ /^(?:<!--)?\s*$keyword_string/;
	
		$$body_ref = substr($$body_ref, length($first_line));

		@chunks = split /\s+/, $first_line;
		$google = $link_start;

		shift @chunks;

		foreach (@chunks)
		{
			next if /$keyword_string/;
			next if /^.{1}$/;
			next if /\<?--\>?/;
			$google .= $_ . "+";
		}
		
		chop $google;
	}

	$google .= "\"";

	if ($open_new_window)
	{
		$google .= " target=\"googleWindow\"";
	}
	
	$google .= ">" . $link_title . "</a>";
	$google .= $link_end if $link_end !~ /^$/;
	
	1;
}

sub end
{
	1;
}

1;
