| 1 | package Statocles::Page::ListItem; | ||||||||||
| 2 | 18 | our $VERSION = '0.099'; | |||||||||
| 3 | # ABSTRACT: An item in a List page | ||||||||||
| 4 | |||||||||||
| 5 | 18 | ▶ | use Statocles::Base 'Class'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 18 | ▶ | use Mojo::DOM; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | #pod =attr page | ||||||||||
| 9 | #pod | ||||||||||
| 10 | #pod The L<page object|Statocles::Page> for this item in the list. | ||||||||||
| 11 | #pod | ||||||||||
| 12 | #pod =cut | ||||||||||
| 13 | |||||||||||
| 14 | 18 | has page => ( | |||||||||
| 15 | is => 'ro', | ||||||||||
| 16 | isa => ConsumerOf[ 'Statocles::Page' ], | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | #pod =attr rewrite_mode | ||||||||||
| 20 | #pod | ||||||||||
| 21 | #pod One of "absolute" or "full". Defaults to "absolute". | ||||||||||
| 22 | #pod | ||||||||||
| 23 | #pod If "absolute", will rewrite the content using the absolute path of the page. | ||||||||||
| 24 | #pod | ||||||||||
| 25 | #pod If "full", will use the full URL (the site base_url and the page URL) when | ||||||||||
| 26 | #pod rewriting the content. | ||||||||||
| 27 | #pod | ||||||||||
| 28 | #pod =cut | ||||||||||
| 29 | |||||||||||
| 30 | 18 | has rewrite_mode => ( | |||||||||
| 31 | is => 'ro', | ||||||||||
| 32 | isa => Enum[qw( absolute full )], | ||||||||||
| 33 | default => 'absolute', | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | #pod =method DOES | ||||||||||
| 37 | #pod | ||||||||||
| 38 | #pod This page proxies everything necessary to be a page object, without consuming | ||||||||||
| 39 | #pod the L<page role|Statocles::Page>. | ||||||||||
| 40 | #pod | ||||||||||
| 41 | #pod =cut | ||||||||||
| 42 | |||||||||||
| 43 | 2 | ▶ | sub DOES { | ||||||||
|
Subroutine
DOES: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 44 | 2 | my ( $self, $class ) = @_; | |||||||||
| 45 | 2 | return $self->page->DOES( $class ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | #pod =method AUTOLOAD | ||||||||||
| 49 | #pod | ||||||||||
| 50 | #pod Methods are proxyed to the L<page object|/page> so that this object appears | ||||||||||
| 51 | #pod mostly as the page inside of it. | ||||||||||
| 52 | #pod | ||||||||||
| 53 | #pod =cut | ||||||||||
| 54 | |||||||||||
| 55 | 18 | our $AUTOLOAD; | |||||||||
| 56 | 39 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.0
| |||||||||||
| 57 | 39 | my ( $self, @args ) = @_; | |||||||||
| 58 | 39 | my ( $method_name ) = $AUTOLOAD =~ /::([^:]+)$/; | |||||||||
| 59 | |||||||||||
| 60 | # We must be able to destroy ourselves | ||||||||||
| 61 | # This issue is fixed in perl 5.18 | ||||||||||
| 62 | 39 | ▶ | return if $method_name eq 'DESTROY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 17 | my $method = $self->page->can( $method_name ); | |||||||||
| 65 | 17 | ▶ | if ( !$method ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 1 | die sprintf q{ListItem page (%s %s) has no method "%s"}, | |||||||||
| 67 | $self->page->path, | ||||||||||
| 68 | ref $self->page, | ||||||||||
| 69 | $method_name; | ||||||||||
| 70 | } | ||||||||||
| 71 | 16 | return $method->( $self->page, @args ); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | #pod =method content | ||||||||||
| 75 | #pod | ||||||||||
| 76 | #pod my $html = $page->content; | ||||||||||
| 77 | #pod | ||||||||||
| 78 | #pod Get the content for this page. Rewrite any links, images, or other according to the | ||||||||||
| 79 | #pod L<rewrite_mode attributes|/rewrite_mode>. | ||||||||||
| 80 | #pod | ||||||||||
| 81 | #pod =cut | ||||||||||
| 82 | |||||||||||
| 83 | 10 | ▶ | sub _rewrite_content { | ||||||||
|
Subroutine
_rewrite_content: called CC=6 SCAR=17.9
| |||||||||||
| 84 | 10 | my ( $self, $content ) = @_; | |||||||||
| 85 | |||||||||||
| 86 | 10 | my $dom = Mojo::DOM->new( $content ); | |||||||||
| 87 | 10 | for my $attr ( qw( src href ) ) { | |||||||||
| 88 | 20 | for my $el ( $dom->find( "[$attr]" )->each ) { | |||||||||
| 89 | 22 | my $url = $el->attr( $attr ); | |||||||||
| 90 | |||||||||||
| 91 | # relative URLs must be absolute | ||||||||||
| 92 | 22 | ▶ | if ( $url !~ m{^(?:(?:[a-zA-Z]+:)|//?)} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 10 | $url = $self->page->dirname . '/' . $url; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | # absolute URLs may be full | ||||||||||
| 97 | 22 | ▶ | if ( $self->rewrite_mode eq 'full' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 11 | ▶ | if ( $url !~ m{^(?:(?:[a-zA-Z]+:)|//)} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 7 | $url = $self->page->site->url( $url ); | |||||||||
| 100 | } | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 22 | $el->attr( $attr => $url ); | |||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 10 | return "$dom"; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 2 | ▶ | sub content { | ||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 111 | 2 | my ( $self, @args ) = @_; | |||||||||
| 112 | 2 | my $content = $self->page->content( @args ); | |||||||||
| 113 | 2 | return $self->_rewrite_content( $content ); | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | #pod =method sections | ||||||||||
| 117 | #pod | ||||||||||
| 118 | #pod my @sections = $page->sections; | ||||||||||
| 119 | #pod | ||||||||||
| 120 | #pod Get a list of content divided into sections. The Markdown "---" marker divides | ||||||||||
| 121 | #pod sections. Rewrite any links, images, or other according to the L<rewrite_mode | ||||||||||
| 122 | #pod attributes|/rewrite_mode>. | ||||||||||
| 123 | #pod | ||||||||||
| 124 | #pod =cut | ||||||||||
| 125 | |||||||||||
| 126 | 5 | ▶ | sub sections { | ||||||||
|
Subroutine
sections: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 127 | 5 | my ( $self, @args ) = @_; | |||||||||
| 128 | 5 | return map { $self->_rewrite_content( $_ ) } $self->page->sections( @args ); | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 18 | 1; | |||||||||
| 132 | |||||||||||
| 133 | __END__ | ||||||||||