File: Include.pm

package info (click to toggle)
libmojomojo-perl 1.11%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,496 kB
  • ctags: 927
  • sloc: perl: 14,671; sh: 148; xml: 120; makefile: 8; ruby: 6
file content (105 lines) | stat: -rw-r--r-- 2,622 bytes parent folder | download | duplicates (3)
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
94
95
96
97
98
99
100
101
102
103
104
105
package MojoMojo::Formatter::Include;

use strict;
use parent 'MojoMojo::Formatter';

eval {
    require URI::Fetch;
    require LWP::Simple;  # LWP::Simple is indeed required, and URI::Fetch doesn't depend on it
};
my $dependencies_installed = !$@;

=head2 module_loaded

Return true if the module is loaded.

=cut

sub module_loaded { $dependencies_installed }

our $VERSION = '0.01';

=head1 NAME

MojoMojo::Formatter::Include - Include files in your content.

=head1 DESCRIPTION

Include files verbatim in your content, by writing {{include <url>}}. Can
be used for transclusion from the same wiki, in which case the
L<inline|MojoMojo::Controller::Page/inline> version of the page is pulled.

=head1 METHODS

=head2 format_content_order

Format order can be 1-99. The Include formatter runs on 5, before all
formatters (except L<Redirect|MojoMojo::Formatter::Redirect>), so that
included content (most often from the same wiki) can be parsed for markup.
To avoid markup interpretation, surround the {{include <url>}} with a
C<< <div> >>:

    <div>Some uninterpreted Markdown: {{include http://mysite.com/rawmarkdown.txt}}</div>

=cut

sub format_content_order { 5 }

=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, $c ) = @_;
    return unless $class->module_loaded;
    # Regexp::Common::URI is overkill
    my $re = $class->gen_re(qr(
        include \s+ (\S+)
    )x);
    if ( $$content =~ s/$re/$class->include( $c, $1 )/eg ) {
        # we don't want to precompile a page with comments so turn it off
        $c->stash->{precompile_off} = 1;
    }
}

=head2 include <c> <url>

Returns the content at the URL. Will store a cached version in
C<< $c->cache >>.

=cut

sub include {
    my ( $class, $c, $url ) = @_;
    $url = URI->new($url);
    return $c->loc('x is not a valid URL', $url) unless $url;
    # check if we're including a page from the same wiki
    my $rel = $url->rel( $c->req->base );
    if (not $rel->scheme) {
        # if so, then return the inline version of the page is requests
        return $c->subreq( ($rel.'' eq './' ? '/' : '/'.$rel).'.inline'  );
    }
    my $res = URI::Fetch->fetch( $url, Cache => $c->cache );
    return $res->content if defined $res;
    return $c->loc('Could not retrieve x', $url);
}

=head1 SEE ALSO

L<MojoMojo>, L<Module::Pluggable::Ordered>, L<URI::Fetch>

=head1 AUTHORS

Marcus Ramberg <mramberg@cpan.org>

=head1 LICENSE

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

=cut

1;