| 1 | package Search::Sitemap::URL; | ||
| 2 | $Search::Sitemap::URL::VERSION = '2.13_01'; | ||
| 3 | 3 | ▶ | use 5.008003; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | our $AUTHORITY = 'cpan:JASONK'; | ||
| 7 | 3 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use MooseX::ClassAttribute; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use MooseX::Types::Moose qw( Bool ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 3 | ▶ | use MooseX::Types::URI qw( Uri ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 3 | ▶ | use Search::Sitemap::Types qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | SitemapChangeFreq SitemapLastMod SitemapPriority | ||
| 13 | 3 | ); | |
| 14 | 0 | ▶ | use XML::Twig qw(); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use POSIX qw( strftime ); | ||
| 16 | use HTML::Entities qw( encode_entities ); | ||
| 17 | use Scalar::Util qw( blessed ); | ||
| 18 | use namespace::clean -except => 'meta'; | ||
| 19 | |||
| 20 | class_has 'encode_entities' => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => Bool, | ||
| 23 | ); | ||
| 24 | |||
| 25 | class_has 'lenient' => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => Bool, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has 'loc' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => Uri, | ||
| 33 | coerce => 1, | ||
| 34 | predicate => 'has_loc', | ||
| 35 | clearer => 'clear_loc', | ||
| 36 | ); | ||
| 37 | has 'changefreq' => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => SitemapChangeFreq, | ||
| 40 | coerce => 1, | ||
| 41 | predicate => 'has_changefreq', | ||
| 42 | clearer => 'clear_changefreq', | ||
| 43 | ); | ||
| 44 | has 'lastmod' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => SitemapLastMod, | ||
| 47 | coerce => 1, | ||
| 48 | predicate => 'has_lastmod', | ||
| 49 | clearer => 'clear_lastmod', | ||
| 50 | ); | ||
| 51 | has 'priority' => ( | ||
| 52 | is => 'rw', | ||
| 53 | isa => SitemapPriority, | ||
| 54 | coerce => 1, | ||
| 55 | predicate => 'has_priority', | ||
| 56 | clearer => 'clear_priority', | ||
| 57 | ); | ||
| 58 | has 'mobile' => ( | ||
| 59 | is => 'rw', | ||
| 60 | isa => Bool, | ||
| 61 | predicate => 'has_mobile', | ||
| 62 | clearer => 'clear_mobile', | ||
| 63 | ); | ||
| 64 | |||
| 65 | sub _loc_as_elt { | ||
| 66 | my $self = shift; | ||
| 67 | return unless $self->has_loc; | ||
| 68 | my $loc = XML::Twig::Elt->new( | ||
| 69 | '#PCDATA' => encode_entities( $self->loc->as_string ) | ||
| 70 | ); | ||
| 71 | $loc->set_asis( 1 ); | ||
| 72 | return $loc; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _mobile_as_elt { | ||
| 76 | my $self = shift; | ||
| 77 | return unless $self->mobile; | ||
| 78 | return XML::Twig::Elt->new( 'mobile', namespace => 'mobile' ); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub as_elt { | ||
| 82 | my ( $self, $type, @fields ) = @_; | ||
| 83 | |||
| 84 | $type ||= 'url'; | ||
| 85 | unless ( @fields ) { @fields = qw( loc changefreq lastmod priority ) } | ||
| 86 | |||
| 87 | my @elements = (); | ||
| 88 | for my $f ( @fields ) { | ||
| 89 | my $exists = $self->can( "has_$f" ); | ||
| 90 | next if $exists and not $self->$exists; | ||
| 91 | |||
| 92 | my $method = '_'.$f.'_as_elt'; | ||
| 93 | my $val; | ||
| 94 | if ( $self->can( $method ) ) { | ||
| 95 | $val = $self->$method(); | ||
| 96 | } else { | ||
| 97 | $val = XML::Twig::Elt->new( '#PCDATA' => $self->$f() ); | ||
| 98 | } | ||
| 99 | next unless $val; | ||
| 100 | next unless blessed $val; | ||
| 101 | next unless $val->isa( 'XML::Twig::Elt' ); | ||
| 102 | push( @elements, $val->wrap_in( $f ) ); | ||
| 103 | } | ||
| 104 | return XML::Twig::Elt->new( $type, {}, @elements ); | ||
| 105 | } | ||
| 106 | |||
| 107 | __PACKAGE__->meta->make_immutable; | ||
| 108 | 1; | ||
| 109 | __END__ | ||