File Coverage

blib/lib/MojoMojo/Formatter/Include.pm
Criterion Covered Total %
statement 12 23 52.1
branch 2 12 16.6
condition n/a
subroutine 5 6 83.3
pod 4 4 100.0
total 23 45 51.1


line stmt bran cond sub pod time code
1             package MojoMojo::Formatter::Include;
2              
3 27     27   67834 use strict;
  27         72  
  27         781  
4 27     27   483 use parent 'MojoMojo::Formatter';
  27         286  
  27         145  
5              
6             eval {
7             require URI::Fetch;
8             require LWP::Simple; # LWP::Simple is indeed required, and URI::Fetch doesn't depend on it
9             };
10             my $dependencies_installed = !$@;
11              
12             =head2 module_loaded
13              
14             Return true if the module is loaded.
15              
16             =cut
17              
18 125     125 1 2272 sub module_loaded { $dependencies_installed }
19              
20             our $VERSION = '0.01';
21              
22             =head1 NAME
23              
24             MojoMojo::Formatter::Include - Include files in your content.
25              
26             =head1 DESCRIPTION
27              
28             Include files verbatim in your content, by writing {{include <url>}}. Can
29             be used for transclusion from the same wiki, in which case the
30             L<inline|MojoMojo::Controller::Page/inline> version of the page is pulled.
31              
32             =head1 METHODS
33              
34             =head2 format_content_order
35              
36             Format order can be 1-99. The Include formatter runs on 5, before all
37             formatters (except L<Redirect|MojoMojo::Formatter::Redirect>), so that
38             included content (most often from the same wiki) can be parsed for markup.
39             To avoid markup interpretation, surround the {{include <url>}} with a
40             C<< <div> >>:
41              
42             <div>Some uninterpreted Markdown: {{include http://mysite.com/rawmarkdown.txt}}</div>
43              
44             =cut
45              
46 868     868 1 2493 sub format_content_order { 5 }
47              
48             =head2 format_content
49              
50             Calls the formatter. Takes a ref to the content as well as the
51             context object.
52              
53             =cut
54              
55             sub format_content {
56 124     124 1 878 my ( $class, $content, $c ) = @_;
57 124 50       481 return unless $class->module_loaded;
58             # Regexp::Common::URI is overkill
59 124         1260 my $re = $class->gen_re(qr(
60             include \s+ (\S+)
61             )x);
62 124 50       1371 if ( $$content =~ s/$re/$class->include( $c, $1 )/eg ) {
  0            
63             # we don't want to precompile a page with comments so turn it off
64 0           $c->stash->{precompile_off} = 1;
65             }
66             }
67              
68             =head2 include <c> <url>
69              
70             Returns the content at the URL. Will store a cached version in
71             C<< $c->cache >>.
72              
73             =cut
74              
75             sub include {
76 0     0 1   my ( $class, $c, $url ) = @_;
77 0           $url = URI->new($url);
78 0 0         return $c->loc('x is not a valid URL', $url) unless $url;
79             # check if we're including a page from the same wiki
80 0           my $rel = $url->rel( $c->req->base );
81 0 0         if (not $rel->scheme) {
82             # if so, then return the inline version of the page is requests
83 0 0         return $c->subreq( ($rel.'' eq './' ? '/' : '/'.$rel).'.inline' );
84             }
85 0           my $res = URI::Fetch->fetch( $url, Cache => $c->cache );
86 0 0         return $res->content if defined $res;
87 0           return $c->loc('Could not retrieve x', $url);
88             }
89              
90             =head1 SEE ALSO
91              
92             L<MojoMojo>, L<Module::Pluggable::Ordered>, L<URI::Fetch>
93              
94             =head1 AUTHORS
95              
96             Marcus Ramberg <mramberg@cpan.org>
97              
98             =head1 LICENSE
99              
100             This library is free software. You can redistribute it and/or modify
101             it under the same terms as Perl itself.
102              
103             =cut
104              
105             1;