| 1 | package SWISH::Prog::Doc; | ||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 9 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 9 | ▶ | use Data::Dump qw( dump ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 9 | ▶ | use base qw( SWISH::Prog::Class ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 9 | ▶ | use overload( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | '""' => \&as_string, | ||||||||||||||||||||
| 9 | 10 | ▶ | 'bool' => sub {1}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 9 | fallback => 1, | |||||||||||||||||||
| 11 | 9 | ); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 9 | ▶ | use SWISH::Prog::Headers; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 9 | our $VERSION = '0.75'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 9 | __PACKAGE__->mk_accessors( | |||||||||||||||||||
| 18 | qw( url modtime type parser content action size charset data version )); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 9 | ▶ | my $default_version = $ENV{SWISH3} ? 3 : 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 9 | my ( $locale, $lang, $charset ); | |||||||||||||||||||
| 23 | { | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # inside a block to reduce impact on any regex | ||||||||||||||||||||
| 26 | 9 | ▶ | use POSIX qw(locale_h); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 9 | ▶ | use locale; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 9 | $locale = setlocale(LC_CTYPE); | |||||||||||||||||||
| 30 | 9 | ( $lang, $charset ) = split( m/\./, $locale ); | |||||||||||||||||||
| 31 | 9 | ▶ | $charset ||= 'iso-8859-1'; | ||||||||||||||||||
|
Condition50%1/2
$charset ||= "iso-8859-1"
MC/DC0%0/1
$charset ||= "iso-8859-1"
$charset
Truth table50%1/2
A
A= $charset
| |||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =pod | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 NAME | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | SWISH::Prog::Doc - Document object class for passing to SWISH::Prog::Indexer | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =head1 SYNOPSIS | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | # subclass SWISH::Prog::Doc | ||||||||||||||||||||
| 43 | # and override filter() method | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | package MyDoc; | ||||||||||||||||||||
| 46 | use base qw( SWISH::Prog::Doc ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub filter { | ||||||||||||||||||||
| 49 | my $doc = shift; | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # alter url | ||||||||||||||||||||
| 52 | my $url = $doc->url; | ||||||||||||||||||||
| 53 | $url =~ s/my.foo.com/my.bar.org/; | ||||||||||||||||||||
| 54 | $doc->url( $url ); | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # alter content | ||||||||||||||||||||
| 57 | my $buf = $doc->content; | ||||||||||||||||||||
| 58 | $buf =~ s/foo/bar/gi; | ||||||||||||||||||||
| 59 | $doc->content( $buf ); | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 1; | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =head1 DESCRIPTION | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | SWISH::Prog::Doc is the base class for Doc objects in the SWISH::Prog | ||||||||||||||||||||
| 67 | framework. Doc objects are created by SWISH::Prog::Aggregator classes | ||||||||||||||||||||
| 68 | and processed by SWISH::Prog::Indexer classes. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | You can subclass SWISH::Prog::Doc and add a filter() method to alter | ||||||||||||||||||||
| 71 | the values of the Doc object before it is indexed. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head1 METHODS | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | All of the following methods may be overridden when subclassing | ||||||||||||||||||||
| 76 | this module, but the recommendation is to override only filter(). | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head2 new | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Instantiate Doc object. | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | All of the following params are also available as accessors/mutators. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =over | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =item url | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =item type | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =item content | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =item parser | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =item modtime | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =item size | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =item action | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =item debug | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =item charset | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =item data | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =item version | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Swish-e 2.x or Swish3 style headers. Value should be C<2> or C<3>. | ||||||||||||||||||||
| 109 | Default is C<2>. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =back | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head2 init | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Calls filter() on object. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 5 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||
| 122 | 5 | my $self = shift; | |||||||||||||||||||
| 123 | 5 | $self->SUPER::init(@_); | |||||||||||||||||||
| 124 | 5 | ▶ | $self->{charset} ||= $charset; | ||||||||||||||||||
|
Condition33%1/3
$self->{"charset"} ||= $charset
MC/DC0%0/2
$self->{"charset"} ||= $charset
$self->{"charset"}
$charset
Truth table33%1/3
A ||= B
A= $self->{"charset"}B= $charset
| |||||||||||||||||||||
| 125 | 5 | ▶ | $self->{version} ||= $default_version; | ||||||||||||||||||
|
Condition33%1/3
$self->{"version"} ||= $default_version
MC/DC0%0/2
$self->{"version"} ||= $default_version
$self->{"version"}
$default_version
Truth table33%1/3
A ||= B
A= $self->{"version"}B= $default_version
| |||||||||||||||||||||
| 126 | 5 | $self->filter(); | |||||||||||||||||||
| 127 | 5 | return $self; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head2 filter | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | Override this method to alter the values in the object prior to it | ||||||||||||||||||||
| 133 | being process()ed by the Indexer. | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | The default is to do nothing. | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | This method can also be set using the filter() callback in SWISH::Prog->new(). | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | ▶ | sub filter { } | ||||||||||||||||||
|
Subroutine
filter: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =head2 as_string | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | Return the Doc object rendered as a scalar string, ready to be indexed. | ||||||||||||||||||||
| 146 | This will include the proper headers. See SWISH::Prog::Headers. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | B<NOTE:> as_string() is also used if you use a Doc object as a string. | ||||||||||||||||||||
| 149 | Example: | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | print $doc->as_string; # one way | ||||||||||||||||||||
| 152 | print $doc; # same thing | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | # TODO cache this higher up? how else to set debug?? | ||||||||||||||||||||
| 157 | 9 | my $headers = SWISH::Prog::Headers->new(); | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | sub as_string { | ||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 160 | 0 | my $self = shift; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | # we ignore size() and let Headers compute it based on actual content() | ||||||||||||||||||||
| 163 | 0 | return $headers->head( | |||||||||||||||||||
| 164 | $self->content, | ||||||||||||||||||||
| 165 | { url => $self->url, | ||||||||||||||||||||
| 166 | modtime => $self->modtime, | ||||||||||||||||||||
| 167 | type => $self->type, | ||||||||||||||||||||
| 168 | action => $self->action, | ||||||||||||||||||||
| 169 | parser => $self->parser, | ||||||||||||||||||||
| 170 | version => $self->version, | ||||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | ) . $self->content; | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 9 | 1; | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||