MojoMojo::Formatter::GoogleSearch - Linked Google Search Engine by writing {{google:<search kind> <keyword>}}


MojoMojo documentation Contained in the MojoMojo distribution.

Index


Code Index:

NAME

Top

MojoMojo::Formatter::GoogleSearch - Linked Google Search Engine by writing {{google:<search kind> <keyword>}}

DESCRIPTION

Top

Normally, to hyperlink to a Search Engine, you'd write:

    [google SearchWord](http://www.google.com/search?q=SearchWord)

This plugin lets you write just

    {{google SearchWord}}

not just Search Web, you can search images and movies

    {{google:image SearchWord}}
    {{google:movie SearchWord}}

METHODS

Top

format_content_order

The SearchEngine formatter has no special requirements in terms of the order it gets run in, so it has a priority of 16.

format_content

Calls the formatter. Takes a ref to the content as well as the context object.

process

Here the actual formatting is done.

SEE ALSO

Top

MojoMojo and Module::Pluggable::Ordered.

AUTHORS

Top

Dai Okabayashi, bayashi at cpan . org

LICENSE

Top

This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.


MojoMojo documentation Contained in the MojoMojo distribution.
package MojoMojo::Formatter::GoogleSearch;
use strict;
use warnings;
use parent qw/MojoMojo::Formatter/;

my $CONF = {
    web => {
        base => 'http://www.google.com/search',
    },
    image  => {
        base => 'http://www.google.com/images',
    },
    movie  => {
        base => 'http://www.google.com/search',
        param => {
            tbs => 'vid:1',
        },
    },
};

sub format_content_order { 16 }

sub format_content {
    my ( $class, $content, $c ) = @_;

    return unless $class->module_loaded;

    my @lines = split /\n/, $$content;
    $$content = '';

    my $re   = $class->gen_re( qr/google:?([^\s]+)?\s+(.+)/ );
    my $lang = $c->sessionid
                ? $c->session->{lang} : $c->pref('default_lang') || 'en';

    for my $line (@lines) {
        if ( $line =~ m/$re/ ) {
            $line = $class->process($c, $line, $re, $lang);
        }
        $$content .= $line . "\n";
    }

}

sub process {
    my $class = shift;
    my ($c, $line, $re, $lang) = @_;

    my $google_search = $c->loc('Google Search Link');

    $line =~ m/$re/;
    my $kind    = $1 || 'web';
    my $keyword = $2;

    unless ($CONF->{$kind}->{base}) {
        $line =~ s/$re/"$google_search: ". $c->loc('invalid Kind of Search')/e;
        return $line;
    }

    my %param;
    $param{q} = $keyword;
    if ($CONF->{$kind}->{param}) {
        for my $key (keys %{ $CONF->{$kind}->{param} }) {
            $param{$key} = $CONF->{$kind}->{param}->{$key};
        }
    }

    my $uri = URI->new($CONF->{$kind}->{base});
    $uri->query_form( %param );

    $line =~ s!$re!<a href="$uri">$keyword</a>!;
    $c->stash->{precompile_off} = 1;

    return $line;
}


1;