| 1 | package MojoMojo::Formatter::IDLink; | ||||||||||||||||||||
| 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::IDLink - Linked {{id:<service name> <word>}} | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =cut | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 27 | my $CONF = { | |||||||||||||||||||
| 13 | tw => 'http://twitter.com/%s', | ||||||||||||||||||||
| 14 | htb => 'http://b.hatena.ne.jp/%s', | ||||||||||||||||||||
| 15 | htd => 'http://d.hatena.ne.jp/%s', | ||||||||||||||||||||
| 16 | cpan => 'http://search.cpan.org/~%s/', | ||||||||||||||||||||
| 17 | fb => 'http://facebook.com/%s', | ||||||||||||||||||||
| 18 | }; | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 27 | my $DEFAULT = 'tw'; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | if you write: | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | {{id bayashi}} | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | it will format like this | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | <a href="http://twitter.com/bayashi">bayashi</a> | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | you can write: | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | {{id:cpan bayashi}} | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | it will format like this | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | <a href="http://search.cpan.org/~bayashi/">bayashi</a> | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =head1 METHODS | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head2 format_content_order | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | The IDLink formatter has no special requirements | ||||||||||||||||||||
| 45 | in terms of the order it gets run in, so it has a priority of 10. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 868 | ▶ | sub format_content_order { 10 } | ||||||||||||||||||
|
Subroutine
format_content_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head2 format_content | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | Calls the formatter. Takes a ref to the content as well as the context object. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 129 | ▶ | sub format_content { | ||||||||||||||||||
|
Subroutine
format_content: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 58 | 129 | my ( $class, $content, $c ) = @_; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 129 | ▶ | return unless $$content; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 129 | my @lines = split /\n/, $$content; | |||||||||||||||||||
| 63 | 129 | $$content = ''; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 129 | my $re = $class->gen_re( qr/id(?::([^\s]+))?\s+(.+)/ ); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 129 | for my $line (@lines) { | |||||||||||||||||||
| 68 | 651 | ▶ | if ( $line =~ m/$re/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 5 | $line = $class->process($c, $line, $re, $1, $2); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | 651 | $$content .= $line . "\n"; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =head2 process | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Here the actual formatting is done. | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||
| 81 | 5 | ▶ | sub process { | ||||||||||||||||||
|
Subroutine
process: called CC=3 SCAR=12.9
Pod
documented
| |||||||||||||||||||||
| 82 | 5 | my $class = shift; | |||||||||||||||||||
| 83 | 5 | my ($c, $line, $re, $site, $id) = @_; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 5 | ▶ | $site ||= $DEFAULT; | ||||||||||||||||||
|
Condition66%2/3
$site ||= $DEFAULT
MC/DC0%0/2
$site ||= $DEFAULT
$site
$DEFAULT
Truth table66%2/3
A ||= B
A= $siteB= $DEFAULT
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 5 | ▶ | unless ($CONF->{$site}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | my $sites = join ',', keys %{$CONF}; | |||||||||||||||||||
| 89 | 0 | $line =~ s/$re/"IDLink: ". $c->loc('identifier is wrong.'). " use [$sites]"/e; | |||||||||||||||||||
| 90 | 0 | return $line; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 5 | my $url = sprintf($CONF->{$site}, $id); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 5 | $line =~ s!$re!<a href="$url">$id</a>!; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 5 | return $line; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =head1 SEE ALSO | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | L<MojoMojo> and L<Module::Pluggable::Ordered>. | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head1 AUTHORS | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | Dai Okabayashi, L<bayashi at cpan . org> | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =head1 LICENSE | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | This library is free software. You can redistribute it and/or modify | ||||||||||||||||||||
| 112 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 27 | 1; | |||||||||||||||||||