File Coverage

blib/lib/MojoMojo/Formatter/CPANHyperlink.pm
Criterion Covered Total %
statement 14 14 100.0
branch 2 2 100.0
condition n/a
subroutine 3 3 100.0
pod 2 2 100.0
total 21 21 100.0


line stmt bran cond sub pod time code
1             package MojoMojo::Formatter::CPANHyperlink;
2              
3 27     27   19805 use parent qw/MojoMojo::Formatter/;
  27         450  
  27         160  
4              
5             =head1 NAME
6              
7             MojoMojo::Formatter::CPANHyperlink - automatically hyperlink CPAN modules when
8             using the syntax {{cpan Some::Module}}
9              
10             =head1 DESCRIPTION
11              
12             Normally, to hyperlink to a CPAN module, you'd write:
13              
14             [MojoMojo::Formatter::CPANHyperlink](http://search.cpan.org/perldoc?MojoMojo::Formatter::CPANHyperlink)
15              
16             This plugin lets you write just
17              
18             {{cpan MojoMojo::Formatter::CPANHyperlink}}
19              
20             Just like POD, it supports adding a section after the module name:
21              
22             {{cpan Catalyst::Manual::Cookbook/Deployment}}
23              
24             will create a link to
25              
26             http://search.cpan.org/perldoc?Catalyst::Manual::Cookbook#Deployment
27              
28             Sections can contain any characters, except two consecutive closed braces:
29              
30             {{cpan Catalyst::Test/($res, $c) = ctx request( ... ); }}
31              
32             will link to
33              
34             http://search.cpan.org/perldoc?Catalyst::Test#($res,_$c)_=_ctx_request(_..._);
35              
36             In anticipation of future plugin syntax, you can optionally add a trailing slash
37              
38             {{cpan Some::Module/Section /}}
39              
40             =head1 METHODS
41              
42             =head2 format_content_order
43              
44             The CPANHyperlink formatter has no special requirements in terms of the order
45             it gets run in, so it has a priority of 10.
46              
47             =cut
48              
49 868     868 1 2671 sub format_content_order { 10 }
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             sub format_content {
58 132     132 1 5469 my ( $class, $content ) = @_;
59              
60 132         573 my $component = qr/(?:[_[:alpha:]]\w*)/;
61 132         2022 my $cpan_module = qr[ $component (?: ::$component )* ]x;
62 132         515 my $section = qr[ .*? (?= \s* /? }} ) ]x;
63              
64 132         2223 $$content =~ s[
65             \{\{cpan \s+ ($cpan_module) (?: / ($section))? \s* \/? }}
66             ] [
67 14         68 my ($module, $section) = ($1, $2);
68 14 100       54 if (defined $section) {
69 2         8 (my $anchor=$section) =~ s/\s/_/g;
70 2         13 qq(<a href="http://search.cpan.org/perldoc?$module#$anchor" class="external">$section in $module</a>)
71             } else {
72 12         91 qq(<a href="http://search.cpan.org/perldoc?$module" class="external">$module</a>)
73             }
74             ]eixg;
75             }
76              
77              
78             =head1 SEE ALSO
79              
80             L<MojoMojo> and L<Module::Pluggable::Ordered>.
81              
82             =head1 AUTHORS
83              
84             Dan Dascalescu, L<http://dandascalescu.com>
85              
86             =head1 LICENSE
87              
88             This library is free software. You can redistribute it and/or modify
89             it under the same terms as Perl itself.
90              
91             =cut
92              
93             1;