| 1 | package MojoMojo::Formatter::GoogleSearch; | ||||||||||||||||||||
| 2 | 27 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 27 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 27 | ▶ | use parent qw/MojoMojo::Formatter/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | MojoMojo::Formatter::GoogleSearch - Linked Google Search Engine by writing {{google:<search kind> <keyword>}} | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =cut | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 27 | my $CONF = { | |||||||||||||||||||
| 13 | web => { | ||||||||||||||||||||
| 14 | base => 'http://www.google.com/search', | ||||||||||||||||||||
| 15 | }, | ||||||||||||||||||||
| 16 | image => { | ||||||||||||||||||||
| 17 | base => 'http://www.google.com/images', | ||||||||||||||||||||
| 18 | }, | ||||||||||||||||||||
| 19 | movie => { | ||||||||||||||||||||
| 20 | base => 'http://www.google.com/search', | ||||||||||||||||||||
| 21 | param => { | ||||||||||||||||||||
| 22 | tbs => 'vid:1', | ||||||||||||||||||||
| 23 | }, | ||||||||||||||||||||
| 24 | }, | ||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | Normally, to hyperlink to a Search Engine, you'd write: | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | [google SearchWord](http://www.google.com/search?q=SearchWord) | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | This plugin lets you write just | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | {{google SearchWord}} | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | not just Search Web, you can search images and movies | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | {{google:image SearchWord}} | ||||||||||||||||||||
| 40 | {{google:movie SearchWord}} | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head1 METHODS | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head2 format_content_order | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | The SearchEngine formatter has no special requirements | ||||||||||||||||||||
| 47 | in terms of the order it gets run in, so it has a priority of 16. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 620 | ▶ | sub format_content_order { 16 } | ||||||||||||||||||
|
Subroutine
format_content_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head2 format_content | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | Calls the formatter. Takes a ref to the content as well as the context object. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 131 | ▶ | sub format_content { | ||||||||||||||||||
|
Subroutine
format_content: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 60 | 131 | my ( $class, $content, $c ) = @_; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 131 | ▶ | return unless $class->module_loaded; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 131 | my @lines = split /\n/, $$content; | |||||||||||||||||||
| 65 | 131 | $$content = ''; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 131 | my $re = $class->gen_re( qr/google:?([^\s]+)?\s+(.+)/ ); | |||||||||||||||||||
| 68 | my $lang = $c->sessionid | ||||||||||||||||||||
| 69 | 131 | ▶ | ? $c->session->{lang} : $c->pref('default_lang') || 'en'; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$c->pref("default_lang") or "en"
MC/DC100%1/1
$c->pref("default_lang") or "en"
$c->pref("default_lang")
Truth table100%2/2
A
A= $c->pref("default_lang")
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 131 | for my $line (@lines) { | |||||||||||||||||||
| 72 | 737 | ▶ | if ( $line =~ m/$re/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 7 | $line = $class->process($c, $line, $re, $lang); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 737 | $$content .= $line . "\n"; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =head2 process | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Here the actual formatting is done. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||
| 85 | 7 | ▶ | sub process { | ||||||||||||||||||
|
Subroutine
process: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 86 | 7 | my $class = shift; | |||||||||||||||||||
| 87 | 7 | my ($c, $line, $re, $lang) = @_; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 7 | my $google_search = $c->loc('Google Search Link'); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 7 | $line =~ m/$re/; | |||||||||||||||||||
| 92 | 7 | ▶ | my $kind = $1 || 'web'; | ||||||||||||||||||
|
Condition100%2/2
$1 || 'web'
MC/DC100%1/1
$1 || 'web'
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||
| 93 | 7 | my $keyword = $2; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 7 | ▶ | unless ($CONF->{$kind}->{base}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | $line =~ s/$re/"$google_search: ". $c->loc('invalid Kind of Search')/e; | |||||||||||||||||||
| 97 | 0 | return $line; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 7 | my %param; | |||||||||||||||||||
| 101 | 7 | $param{q} = $keyword; | |||||||||||||||||||
| 102 | 7 | ▶ | if ($CONF->{$kind}->{param}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 2 | for my $key (keys %{ $CONF->{$kind}->{param} }) { | |||||||||||||||||||
| 104 | 2 | $param{$key} = $CONF->{$kind}->{param}->{$key}; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 7 | my $uri = URI->new($CONF->{$kind}->{base}); | |||||||||||||||||||
| 109 | 7 | $uri->query_form( map {$_ => $param{$_} } sort keys %param ); | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 7 | $line =~ s!$re!<a href="$uri">$keyword</a>!; | |||||||||||||||||||
| 112 | 7 | $c->stash->{precompile_off} = 1; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 7 | return $line; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head1 SEE ALSO | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | L<MojoMojo> and L<Module::Pluggable::Ordered>. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head1 AUTHORS | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Dai Okabayashi, L<bayashi at cpan . org> | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head1 LICENSE | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | This library is free software. You can redistribute it and/or modify | ||||||||||||||||||||
| 129 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 27 | 1; | |||||||||||||||||||