| 1 | package MojoMojo::Formatter::WikipediaLink; | ||||||||||||||||||||||||
| 2 | 26 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 3 | 26 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 26 | ▶ | use parent qw/MojoMojo::Formatter/; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 26 | ▶ | use utf8; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||
| 7 | =encoding utf8 | ||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||
| 11 | MojoMojo::Formatter::WikipediaLink - Linked Wikipedia by writing {{wikipedia:<lang> <word>}} | ||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||
| 15 | Normally, to hyperlink to the Wikipedia, you'd write: | ||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||
| 17 | [wikipedia Hello](http://en.wikipedia.org/wiki/Hello) | ||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||
| 19 | This plugin lets you write just | ||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||
| 21 | {{wikipedia Hello}} | ||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||
| 23 | not just Link to Wikipedia in English page, you can use many languages | ||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||
| 25 | {{wikipedia:ja こんにちは}} | ||||||||||||||||||||||||
| 26 | {{wikipedia:fr Salut}} | ||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||
| 28 | Actually, if you wrote this without a language ex.{{wikipedia Foo}}, | ||||||||||||||||||||||||
| 29 | select location of Wikipedia Link | ||||||||||||||||||||||||
| 30 | is getting default-language setting of MojoMojo. | ||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||
| 32 | =head1 METHODS | ||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||
| 34 | =head2 format_content_order | ||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||
| 36 | The WikipediaLink formatter has no special requirements | ||||||||||||||||||||||||
| 37 | in terms of the order it gets run in, so it has a priority of 17. | ||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||
| 41 | 992 | ▶ | sub format_content_order { 17 } | ||||||||||||||||||||||
|
Subroutine
format_content_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||
| 43 | =head2 format_content | ||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||
| 45 | Calls the formatter. Takes a ref to the content as well as the context object. | ||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||
| 49 | 128 | ▶ | sub format_content { | ||||||||||||||||||||||
|
Subroutine
format_content: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||
| 50 | 128 | my ( $class, $content, $c ) = @_; | |||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | 128 | my @lines = split /\n/, $$content; | |||||||||||||||||||||||
| 53 | 128 | $$content = ''; | |||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||
| 55 | 128 | my $re = $class->gen_re( qr/[wW]ikipedia(?::([^\s]+))?\s+(.+)/ ); | |||||||||||||||||||||||
| 56 | my $lang = $c->sessionid | ||||||||||||||||||||||||
| 57 | 128 | ▶ | ? $c->session->{lang} : $c->pref('default_lang') || 'en'; | ||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$c->pref("default_lang") or "en"
MC/DC0%0/1
$c->pref("default_lang") or "en"
$c->pref("default_lang")
Truth table50%1/2
A
A= $c->pref("default_lang")
| |||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||
| 59 | 128 | for my $line (@lines) { | |||||||||||||||||||||||
| 60 | 734 | ▶ | if ( $line =~ m/$re/ ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 61 | 4 | $line = $class->process($c, $line, $re, $lang); | |||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||
| 63 | 734 | $$content .= $line . "\n"; | |||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||
| 68 | =head2 process | ||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | Here the actual formatting is done. | ||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||
| 73 | 4 | ▶ | sub process { | ||||||||||||||||||||||
|
Subroutine
process: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||
| 74 | 4 | my $class = shift; | |||||||||||||||||||||||
| 75 | 4 | my ($c, $line, $re, $lang) = @_; | |||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||
| 77 | 4 | $line =~ m/$re/; | |||||||||||||||||||||||
| 78 | 4 | ▶ | my $wikipedia_lang = $1 || $c->pref('default_lang') || 'en'; | ||||||||||||||||||||||
|
Condition60%3/5
$1 || $c->pref('default_lang') || 'en'
$1 or $c->pref("default_lang")
MC/DC0%0/2
$1 || $c->pref('default_lang') || 'en'
$1
$c->pref("default_lang")
Truth table66%2/3
A or B
A= $1B= $c->pref("default_lang")
| |||||||||||||||||||||||||
| 79 | 4 | my $keyword = $2; | |||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | 4 | my $uri = URI->new("http://$wikipedia_lang.wikipedia.org/"); | |||||||||||||||||||||||
| 82 | 4 | $uri->path("/wiki/$keyword"); | |||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||
| 84 | 4 | $line =~ s!$re!<a href="$uri">$keyword</a>!; | |||||||||||||||||||||||
| 85 | 4 | $c->stash->{precompile_off} = 1; | |||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||
| 87 | 4 | return $line; | |||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||
| 91 | =head1 SEE ALSO | ||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||
| 93 | L<MojoMojo> and L<Module::Pluggable::Ordered>. | ||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||
| 95 | =head1 AUTHORS | ||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||
| 97 | Dai Okabayashi, L<bayashi at cpan . org> | ||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||
| 99 | =head1 LICENSE | ||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||
| 101 | This library is free software. You can redistribute it and/or modify | ||||||||||||||||||||||||
| 102 | it under the same terms as Perl itself. | ||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||
| 106 | 26 | 1; | |||||||||||||||||||||||