File: CPANHyperlink.pm

package info (click to toggle)
libmojomojo-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,272 kB
  • ctags: 879
  • sloc: perl: 14,055; sh: 145; xml: 120; ruby: 6; makefile: 2
file content (93 lines) | stat: -rw-r--r-- 2,304 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package MojoMojo::Formatter::CPANHyperlink;

use parent qw/MojoMojo::Formatter/;

=head1 NAME

MojoMojo::Formatter::CPANHyperlink - automatically hyperlink CPAN modules when
using the syntax {{cpan Some::Module}}

=head1 DESCRIPTION

Normally, to hyperlink to a CPAN module, you'd write:

    [MojoMojo::Formatter::CPANHyperlink](http://search.cpan.org/perldoc?MojoMojo::Formatter::CPANHyperlink)

This plugin lets you write just

    {{cpan MojoMojo::Formatter::CPANHyperlink}}

Just like POD, it supports adding a section after the module name:

    {{cpan Catalyst::Manual::Cookbook/Deployment}}

will create a link to

    http://search.cpan.org/perldoc?Catalyst::Manual::Cookbook#Deployment

Sections can contain any characters, except two consecutive closed braces:

    {{cpan Catalyst::Test/($res, $c) = ctx request( ... ); }}

will link to

    http://search.cpan.org/perldoc?Catalyst::Test#($res,_$c)_=_ctx_request(_..._);

In anticipation of future plugin syntax, you can optionally add a trailing slash

    {{cpan Some::Module/Section /}}

=head1 METHODS

=head2 format_content_order

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

=cut

sub format_content_order { 10 }

=head2 format_content

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

=cut

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

    my $component = qr/(?:[_[:alpha:]]\w*)/;
    my $cpan_module = qr[ $component (?: ::$component )* ]x;
    my $section = qr[ .*? (?= \s* /? }} ) ]x;

    $$content =~ s[
        {{cpan \s+ ($cpan_module) (?: / ($section))? \s* \/? }}
    ]  [
        my ($module, $section) = ($1, $2);
        if (defined $section) {
            (my $anchor=$section) =~ s/\s/_/g;
            qq(<a href="http://search.cpan.org/perldoc?$module#$anchor" class="external">$section in $module</a>)
        } else {
            qq(<a href="http://search.cpan.org/perldoc?$module" class="external">$module</a>)
        }
    ]eixg;
}


=head1 SEE ALSO

L<MojoMojo> and L<Module::Pluggable::Ordered>.

=head1 AUTHORS

Dan Dascalescu, L<http://dandascalescu.com>

=head1 LICENSE

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

=cut

1;