| 1 | package MojoMojo::Formatter::Include; | ||||||||||
| 2 | |||||||||||
| 3 | 27 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 27 | ▶ | use parent 'MojoMojo::Formatter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 27 | eval { | |||||||||
| 7 | 27 | require URI::Fetch; | |||||||||
| 8 | 27 | require LWP::Simple; # LWP::Simple is indeed required, and URI::Fetch doesn't depend on it | |||||||||
| 9 | }; | ||||||||||
| 10 | 27 | my $dependencies_installed = !$@; | |||||||||
| 11 | |||||||||||
| 12 | =head2 module_loaded | ||||||||||
| 13 | |||||||||||
| 14 | Return true if the module is loaded. | ||||||||||
| 15 | |||||||||||
| 16 | =cut | ||||||||||
| 17 | |||||||||||
| 18 | 125 | ▶ | sub module_loaded { $dependencies_installed } | ||||||||
|
Subroutine
module_loaded: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 19 | |||||||||||
| 20 | 27 | 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 | ▶ | sub format_content_order { 5 } | ||||||||
|
Subroutine
format_content_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 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 | 124 | ▶ | sub format_content { | ||||||||
|
Subroutine
format_content: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||
| 56 | 124 | my ( $class, $content, $c ) = @_; | |||||||||
| 57 | 124 | ▶ | return unless $class->module_loaded; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | # Regexp::Common::URI is overkill | ||||||||||
| 59 | 124 | my $re = $class->gen_re(qr( | |||||||||
| 60 | include \s+ (\S+) | ||||||||||
| 61 | )x); | ||||||||||
| 62 | 0 | ▶ | if ( $$content =~ s/$re/$class->include( $c, $1 )/eg ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub include { | ||||||||
|
Subroutine
include: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 76 | 0 | my ( $class, $c, $url ) = @_; | |||||||||
| 77 | 0 | $url = URI->new($url); | |||||||||
| 78 | 0 | ▶ | return $c->loc('x is not a valid URL', $url) unless $url; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | # check if we're including a page from the same wiki | ||||||||||
| 80 | 0 | my $rel = $url->rel( $c->req->base ); | |||||||||
| 81 | 0 | ▶ | if (not $rel->scheme) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | # if so, then return the inline version of the page is requests | ||||||||||
| 83 | 0 | ▶ | return $c->subreq( ($rel.'' eq './' ? '/' : '/'.$rel).'.inline' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | } | ||||||||||
| 85 | 0 | my $res = URI::Fetch->fetch( $url, Cache => $c->cache ); | |||||||||
| 86 | 0 | ▶ | return $res->content if defined $res; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 27 | 1; | |||||||||