| 1 | package XML::Grammar::Fortune::ToText; | ||||||||||||||||||||||||||||||||
| 2 | 2 | $XML::Grammar::Fortune::ToText::VERSION = '0.1001'; | |||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use XML::LibXML qw/ XML_CDATA_SECTION_NODE XML_ELEMENT_NODE XML_TEXT_NODE /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Text::Format (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use List::Util (qw(max)); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use MooX qw/ late /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | has '_formatter' => ( | ||||||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 16 | 38 | ▶ | default => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 38 | return Text::Format->new( | |||||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||||||
| 19 | columns => 78, | ||||||||||||||||||||||||||||||||
| 20 | firstIndent => 0, | ||||||||||||||||||||||||||||||||
| 21 | leftMargin => 0, | ||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||
| 24 | }, | ||||||||||||||||||||||||||||||||
| 25 | 2 | ); | |||||||||||||||||||||||||||||||
| 26 | 2 | has '_is_first_line' => ( isa => "Bool", is => 'rw' ); | |||||||||||||||||||||||||||||||
| 27 | 2 | has '_input' => ( is => 'rw', init_arg => 'input', required => 1, ); | |||||||||||||||||||||||||||||||
| 28 | 2 | has '_output' => ( is => 'rw', init_arg => 'output', required => 1, ); | |||||||||||||||||||||||||||||||
| 29 | 2 | has '_this_line' => ( isa => 'Str', is => 'rw', default => '', ); | |||||||||||||||||||||||||||||||
| 30 | has '_buf' => ( | ||||||||||||||||||||||||||||||||
| 31 | isa => 'ScalarRef[Str]', | ||||||||||||||||||||||||||||||||
| 32 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 33 | 38 | ▶ | default => sub { my $s = ''; return \$s; } | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | 2 | ); | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | sub _out | ||||||||||||||||||||||||||||||||
| 38 | 668 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_out: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 39 | 668 | my $self = shift; | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 668 | ${ $self->_buf() } .= join( '', @_ ); | |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 668 | return; | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | sub _start_new_line | ||||||||||||||||||||||||||||||||
| 47 | 254 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_start_new_line: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 48 | 254 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 254 | return $self->_out("\n"); | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | sub _render_single_fortune_cookie | ||||||||||||||||||||||||||||||||
| 54 | 42 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_single_fortune_cookie: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 55 | 42 | my ( $self, $fortune_node ) = @_; | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 42 | my ($node) = $fortune_node->findnodes("raw|irc|screenplay|quote"); | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 42 | my $method = sprintf( "_process_%s_node", $node->localname() ); | |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 42 | $self->$method($node); | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 42 | $self->_render_info_if_exists($fortune_node); | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | $self->_iterate_on_child_elems( | ||||||||||||||||||||||||||||||||
| 66 | $fortune_node, | ||||||||||||||||||||||||||||||||
| 67 | "seealso", | ||||||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | process => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 70 | 2 | my $seealso = shift; | |||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 2 | my $s = "See Also:"; | |||||||||||||||||||||||||||||||
| 73 | 2 | $self->_out( "\n${s}\n" . ( "=" x ( length($s) ) . "\n" ) ); | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | $self->_iterate_on_child_elems( | ||||||||||||||||||||||||||||||||
| 76 | $seealso, "ol|ul", | ||||||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | process => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 79 | 2 | my $para = shift; | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 2 | return $self->_render_generalized_para($para); | |||||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||||||
| 84 | 2 | ); | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 2 | $self->_out("\n"); | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 2 | return; | |||||||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | 42 | ); | |||||||||||||||||||||||||||||||
| 92 | 42 | return; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | sub _output_next_fortune_delim | ||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_output_next_fortune_delim: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 97 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 4 | return $self->_out("%\n"); | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 168 | ▶ | sub _do_nothing { } | ||||||||||||||||||||||||||||||
|
Subroutine
_do_nothing: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | sub _iterate_on_child_elems | ||||||||||||||||||||||||||||||||
| 105 | 172 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_iterate_on_child_elems: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 106 | 172 | my ( $self, $top_elem, $xpath, $args ) = @_; | |||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 172 | my $process = $args->{'process'}; | |||||||||||||||||||||||||||||||
| 109 | 172 | my $if_remainaing_meth = $args->{'if_more'}; | |||||||||||||||||||||||||||||||
| 110 | 172 | ▶ | my $continue_cb = ( $args->{'cont'} || \&_do_nothing ); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'cont'} || \&XML::Grammar::Fortune::ToText::_do_nothing
MC/DC100%1/1
$args->{'cont'} || \&XML::Grammar::Fortune::ToText::_do_nothing
$args->{'cont'}
Truth table100%2/2
A
A= $args->{'cont'}
| |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | my $process_cb = ( | ||||||||||||||||||||||||||||||||
| 113 | ( ref($process) eq "CODE" ) | ||||||||||||||||||||||||||||||||
| 114 | ? $process | ||||||||||||||||||||||||||||||||
| 115 | 164 | ▶ | : sub { return $self->$process(shift); } | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 116 | 172 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 172 | my $list = $top_elem->findnodes($xpath); | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 172 | ▶ | while ( my $elem = $list->shift() ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||||||
| 122 | 252 | $process_cb->($elem); | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | continue | ||||||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||||||
| 126 | 252 | $continue_cb->(); | |||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 252 | ▶ | if ( $list->size() ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||||||
| 130 | 120 | $self->$if_remainaing_meth(); | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 172 | return; | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | sub run | ||||||||||||||||||||||||||||||||
| 139 | 38 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 140 | 38 | my $self = shift; | |||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 38 | my $xml = XML::LibXML->new->parse_file( $self->_input() ); | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 38 | $self->_iterate_on_child_elems( | |||||||||||||||||||||||||||||||
| 145 | $xml, | ||||||||||||||||||||||||||||||||
| 146 | "//fortune", | ||||||||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||||||
| 148 | process => '_render_single_fortune_cookie', | ||||||||||||||||||||||||||||||||
| 149 | if_more => '_output_next_fortune_delim', | ||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 38 | my $buf = ${ $self->_buf() }; | |||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 38 | $buf =~ s/[ \t]+$//gms; | |||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 38 | print { $self->_output() } $buf; | |||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 38 | return; | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | sub _render_info_if_exists | ||||||||||||||||||||||||||||||||
| 163 | 42 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_info_if_exists: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 164 | 42 | my ( $self, $fortune_node ) = @_; | |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 42 | ▶ | if ( my ($info_node) = $fortune_node->findnodes("descendant::info") ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||||||
| 168 | 42 | $self->_render_info_node($info_node); | |||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 42 | return; | |||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | sub _process_raw_node | ||||||||||||||||||||||||||||||||
| 175 | 4 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_process_raw_node: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||
| 176 | 4 | my ( $self, $raw_node ) = @_; | |||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 4 | my ($text_node) = $raw_node->findnodes("body/text"); | |||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 4 | my @text_childs = $text_node->childNodes(); | |||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | 4 | ▶ | if ( @text_childs != 1 ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | { | ||||||||||||||||||||||||||||||||
| 184 | 0 | Carp::confess('@cdata is not 1'); | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 4 | my $cdata = $text_childs[0]; | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 4 | ▶ | if ( $cdata->nodeType() != XML_CDATA_SECTION_NODE() ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 190 | { | ||||||||||||||||||||||||||||||||
| 191 | 0 | Carp::confess("Not a cdata"); | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | 4 | my $value = $cdata->nodeValue(); | |||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | 4 | $value =~ s{\n+\z}{}g; | |||||||||||||||||||||||||||||||
| 197 | 4 | $self->_out("$value\n"); | |||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | 4 | return; | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | sub _process_irc_node | ||||||||||||||||||||||||||||||||
| 203 | 10 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_process_irc_node: called CC=11 SCAR=24.3
| |||||||||||||||||||||||||||||||||
| 204 | 10 | my ( $self, $irc_node ) = @_; | |||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | 10 | my ($body_node) = $irc_node->findnodes("body"); | |||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 10 | my @lines_list = $body_node->findnodes("saying|me_is|joins|leaves"); | |||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | use List::Util qw(max); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 10 | my $longest_nick_len = 0; | |||||||||||||||||||||||||||||||
| 213 | 10 | my @messages; | |||||||||||||||||||||||||||||||
| 214 | 10 | foreach my $line (@lines_list) | |||||||||||||||||||||||||||||||
| 215 | { | ||||||||||||||||||||||||||||||||
| 216 | 46 | ▶ | if ( $line->localname() eq "saying" ) | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 217 | { | ||||||||||||||||||||||||||||||||
| 218 | 36 | my $nick = $line->getAttribute("who"); | |||||||||||||||||||||||||||||||
| 219 | 36 | push @messages, | |||||||||||||||||||||||||||||||
| 220 | { | ||||||||||||||||||||||||||||||||
| 221 | type => "say", | ||||||||||||||||||||||||||||||||
| 222 | nick => $nick, | ||||||||||||||||||||||||||||||||
| 223 | msg => $line->textContent(), | ||||||||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | 36 | $longest_nick_len = max( $longest_nick_len, length($nick) ); | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | elsif ( $line->localname() eq "me_is" ) | ||||||||||||||||||||||||||||||||
| 229 | { | ||||||||||||||||||||||||||||||||
| 230 | 2 | my $nick = $line->getAttribute("who"); | |||||||||||||||||||||||||||||||
| 231 | 2 | push @messages, | |||||||||||||||||||||||||||||||
| 232 | { | ||||||||||||||||||||||||||||||||
| 233 | type => "me_is", | ||||||||||||||||||||||||||||||||
| 234 | nick => $nick, | ||||||||||||||||||||||||||||||||
| 235 | msg => $line->textContent(), | ||||||||||||||||||||||||||||||||
| 236 | }; | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 2 | $longest_nick_len = max( $longest_nick_len, length("*") ); | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | elsif ( $line->localname() eq "joins" ) | ||||||||||||||||||||||||||||||||
| 241 | { | ||||||||||||||||||||||||||||||||
| 242 | 4 | my $nick = $line->getAttribute("who"); | |||||||||||||||||||||||||||||||
| 243 | 4 | push @messages, | |||||||||||||||||||||||||||||||
| 244 | { | ||||||||||||||||||||||||||||||||
| 245 | type => "joins", | ||||||||||||||||||||||||||||||||
| 246 | nick => $nick, | ||||||||||||||||||||||||||||||||
| 247 | msg => $line->textContent(), | ||||||||||||||||||||||||||||||||
| 248 | }; | ||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 4 | $longest_nick_len = max( $longest_nick_len, length("<--") ); | |||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | elsif ( $line->localname() eq "leaves" ) | ||||||||||||||||||||||||||||||||
| 253 | { | ||||||||||||||||||||||||||||||||
| 254 | 4 | my $nick = $line->getAttribute("who"); | |||||||||||||||||||||||||||||||
| 255 | 4 | push @messages, | |||||||||||||||||||||||||||||||
| 256 | { | ||||||||||||||||||||||||||||||||
| 257 | type => "leaves", | ||||||||||||||||||||||||||||||||
| 258 | nick => $nick, | ||||||||||||||||||||||||||||||||
| 259 | msg => $line->textContent(), | ||||||||||||||||||||||||||||||||
| 260 | }; | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | 4 | $longest_nick_len = max( $longest_nick_len, length("-->") ); | |||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||
| 264 | else | ||||||||||||||||||||||||||||||||
| 265 | { | ||||||||||||||||||||||||||||||||
| 266 | 0 | Carp::confess( | |||||||||||||||||||||||||||||||
| 267 | 'Unimplemented localname "' . $line->localname() . '"' ); | ||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 10 | my $formatter = Text::Format->new( | |||||||||||||||||||||||||||||||
| 273 | { | ||||||||||||||||||||||||||||||||
| 274 | columns => 72 - 1 - 2 - $longest_nick_len, | ||||||||||||||||||||||||||||||||
| 275 | firstIndent => 0, | ||||||||||||||||||||||||||||||||
| 276 | leftMargin => 0, | ||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||
| 278 | ); | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | 10 | my $line_starts_at = $longest_nick_len + 1 # For the < | |||||||||||||||||||||||||||||||
| 281 | + 1 # For the > | ||||||||||||||||||||||||||||||||
| 282 | + 1 # For the space at the beginning | ||||||||||||||||||||||||||||||||
| 283 | + 2 # For the space after the nick. | ||||||||||||||||||||||||||||||||
| 284 | ; | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 10 | my $nick_len_with_delim = $longest_nick_len + 2; | |||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | 10 | foreach my $m (@messages) | |||||||||||||||||||||||||||||||
| 289 | { | ||||||||||||||||||||||||||||||||
| 290 | 46 | my %cmds = ( "me_is" => "*", "joins" => "-->", "leaves" => "<--", ); | |||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | 46 | ▶ | if ( $m->{'type'} eq "say" ) | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 293 | { | ||||||||||||||||||||||||||||||||
| 294 | 36 | my @lines = ( $formatter->format( [ $m->{'msg'} ] ) ); | |||||||||||||||||||||||||||||||
| 295 | $self->_out( | ||||||||||||||||||||||||||||||||
| 296 | " " | ||||||||||||||||||||||||||||||||
| 297 | . sprintf( "%${nick_len_with_delim}s", | ||||||||||||||||||||||||||||||||
| 298 | 36 | "<" . $m->{'nick'} . ">" ) | |||||||||||||||||||||||||||||||
| 299 | . " " | ||||||||||||||||||||||||||||||||
| 300 | . $lines[0] | ||||||||||||||||||||||||||||||||
| 301 | ); | ||||||||||||||||||||||||||||||||
| 302 | $self->_out( | ||||||||||||||||||||||||||||||||
| 303 | join( "", | ||||||||||||||||||||||||||||||||
| 304 | 36 | map { ( " " x $line_starts_at ) . $_ } | |||||||||||||||||||||||||||||||
| 305 | @lines[ 1 .. $#lines ] ) | ||||||||||||||||||||||||||||||||
| 306 | ); | ||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||
| 308 | elsif ( $m->{'type'} eq "raw" ) | ||||||||||||||||||||||||||||||||
| 309 | { | ||||||||||||||||||||||||||||||||
| 310 | 0 | $self->_out( $m->{'msg'} . "\n" ); | |||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | elsif ( $m->{'type'} eq "change_nick" ) | ||||||||||||||||||||||||||||||||
| 313 | { | ||||||||||||||||||||||||||||||||
| 314 | $self->_out( ( " " x ($line_starts_at) ) | ||||||||||||||||||||||||||||||||
| 315 | . $m->{'old'} | ||||||||||||||||||||||||||||||||
| 316 | . " is now known as " | ||||||||||||||||||||||||||||||||
| 317 | 0 | . $m->{'new'} | |||||||||||||||||||||||||||||||
| 318 | . "\n" ); | ||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||
| 320 | elsif ( exists( $cmds{ $m->{'type'} } ) ) | ||||||||||||||||||||||||||||||||
| 321 | { | ||||||||||||||||||||||||||||||||
| 322 | my @lines = | ||||||||||||||||||||||||||||||||
| 323 | 10 | $formatter->format( [ $m->{'nick'} . " " . $m->{'msg'} ] ); | |||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | $self->_out( | ||||||||||||||||||||||||||||||||
| 326 | " " | ||||||||||||||||||||||||||||||||
| 327 | . sprintf( | ||||||||||||||||||||||||||||||||
| 328 | 10 | "%${nick_len_with_delim}s", $cmds{ $m->{'type'} } | |||||||||||||||||||||||||||||||
| 329 | ) | ||||||||||||||||||||||||||||||||
| 330 | . " " | ||||||||||||||||||||||||||||||||
| 331 | . $lines[0] | ||||||||||||||||||||||||||||||||
| 332 | ); | ||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | $self->_out( | ||||||||||||||||||||||||||||||||
| 335 | join( "", | ||||||||||||||||||||||||||||||||
| 336 | 0 | map { ( " " x $line_starts_at ) . $_ } | |||||||||||||||||||||||||||||||
| 337 | @lines[ 1 .. $#lines ] ) | ||||||||||||||||||||||||||||||||
| 338 | ); | ||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | 10 | return; | |||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | sub _render_screenplay_paras | ||||||||||||||||||||||||||||||||
| 346 | 30 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_screenplay_paras: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 347 | 30 | my ( $self, $portion ) = @_; | |||||||||||||||||||||||||||||||
| 348 | 30 | return $self->_render_portion_paras( $portion, { para_is => "para" } ); | |||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | sub _is_portion_desc | ||||||||||||||||||||||||||||||||
| 352 | 60 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_is_portion_desc: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 353 | 60 | my ( $self, $portion ) = @_; | |||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | 60 | return ( $portion->localname() eq "description" ); | |||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | sub _get_screenplay_portion_opening | ||||||||||||||||||||||||||||||||
| 359 | 30 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_screenplay_portion_opening: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 360 | 30 | my ( $self, $portion ) = @_; | |||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | 30 | ▶ | return $self->_is_portion_desc($portion) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 363 | ? "[" | ||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | # A saying. | ||||||||||||||||||||||||||||||||
| 366 | : ( $portion->getAttribute("character") . ": " ); | ||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | sub _handle_screenplay_portion | ||||||||||||||||||||||||||||||||
| 370 | 30 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_handle_screenplay_portion: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 371 | 30 | my ( $self, $portion ) = @_; | |||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||
| 373 | 30 | $self->_this_line( $self->_get_screenplay_portion_opening($portion) ); | |||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||
| 375 | 30 | $self->_render_screenplay_paras($portion); | |||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||
| 377 | 30 | ▶ | if ( $self->_is_portion_desc($portion) ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 378 | { | ||||||||||||||||||||||||||||||||
| 379 | 8 | $self->_out("]"); | |||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | 30 | $self->_start_new_line; | |||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | 30 | return; | |||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | sub _process_screenplay_node | ||||||||||||||||||||||||||||||||
| 388 | 8 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_process_screenplay_node: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 389 | 8 | my ( $self, $play_node ) = @_; | |||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | 8 | my ($body_node) = $play_node->findnodes("body"); | |||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | 8 | $self->_iterate_on_child_elems( | |||||||||||||||||||||||||||||||
| 394 | $body_node, | ||||||||||||||||||||||||||||||||
| 395 | "description|saying", | ||||||||||||||||||||||||||||||||
| 396 | { | ||||||||||||||||||||||||||||||||
| 397 | process => '_handle_screenplay_portion', | ||||||||||||||||||||||||||||||||
| 398 | if_more => '_start_new_line', | ||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||
| 400 | ); | ||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||
| 402 | 8 | return; | |||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | sub _out_formatted_line | ||||||||||||||||||||||||||||||||
| 406 | 178 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_out_formatted_line: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 407 | 178 | my $self = shift; | |||||||||||||||||||||||||||||||
| 408 | 178 | my $text = $self->_this_line(); | |||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | 178 | $text =~ s{\A\n+}{}ms; | |||||||||||||||||||||||||||||||
| 411 | 178 | $text =~ s{\n+\z}{}ms; | |||||||||||||||||||||||||||||||
| 412 | 178 | $text =~ s{\s+}{ }gms; | |||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||
| 414 | 178 | ▶ | if ( $self->_is_first_line() ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 415 | { | ||||||||||||||||||||||||||||||||
| 416 | 90 | $self->_is_first_line(0); | |||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||
| 418 | else | ||||||||||||||||||||||||||||||||
| 419 | { | ||||||||||||||||||||||||||||||||
| 420 | 88 | $self->_start_new_line; | |||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | 178 | my $output_string = $self->_formatter->format($text); | |||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | # Text::Format inserts a new line - remove it. | ||||||||||||||||||||||||||||||||
| 426 | 178 | chomp($output_string); | |||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||
| 428 | 178 | $self->_out($output_string); | |||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||
| 430 | 178 | $self->_this_line(""); | |||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||
| 432 | 178 | return; | |||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | sub _append_to_this_line | ||||||||||||||||||||||||||||||||
| 436 | 380 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_append_to_this_line: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 437 | 380 | my ( $self, $more_text ) = @_; | |||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | 380 | $self->_this_line( $self->_this_line() . $more_text ); | |||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||
| 442 | sub _append_different_formatting_node | ||||||||||||||||||||||||||||||||
| 443 | 68 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_append_different_formatting_node: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 444 | 68 | my ( $self, $prefix, $suffix, $node ) = @_; | |||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | 68 | return $self->_append_to_this_line( | |||||||||||||||||||||||||||||||
| 447 | $prefix . $node->textContent() . $suffix ); | ||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||
| 450 | { | ||||||||||||||||||||||||||||||||
| 451 | 2 | my @_highlights = | |||||||||||||||||||||||||||||||
| 452 | ( [ '/', [qw(em i italics)] ], [ '*', [qw(b bold strong)] ] ); | ||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||
| 454 | my %_formats_map = ( | ||||||||||||||||||||||||||||||||
| 455 | ( | ||||||||||||||||||||||||||||||||
| 456 | map { | ||||||||||||||||||||||||||||||||
| 457 | 2 | my ( $f, $tags ) = @$_; | |||||||||||||||||||||||||||||||
| 458 | 4 | map { $_ => [ ($f) x 2 ] } @$tags | |||||||||||||||||||||||||||||||
| 459 | } @_highlights | ||||||||||||||||||||||||||||||||
| 460 | ), | ||||||||||||||||||||||||||||||||
| 461 | 'inlinedesc' => [ '[', ']' ], | ||||||||||||||||||||||||||||||||
| 462 | ); | ||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||
| 464 | sub _get_node_formatting_delims | ||||||||||||||||||||||||||||||||
| 465 | 60 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_node_formatting_delims: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 466 | 60 | my ( $self, $node ) = @_; | |||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||
| 468 | 60 | my $name = $node->localname(); | |||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||
| 470 | return exists( $_formats_map{$name} ) | ||||||||||||||||||||||||||||||||
| 471 | 60 | ▶ | ? $_formats_map{$name} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 472 | : [ (q{}) x 2 ]; | ||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||
| 476 | sub _handle_format_node | ||||||||||||||||||||||||||||||||
| 477 | 2 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_handle_format_node: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 478 | 60 | my ( $self, $node ) = @_; | |||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||
| 480 | $self->_append_different_formatting_node( | ||||||||||||||||||||||||||||||||
| 481 | 60 | @{ $self->_get_node_formatting_delims($node) }, $node, ); | |||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||
| 483 | 60 | return; | |||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||
| 486 | sub _get_formatted_node_text | ||||||||||||||||||||||||||||||||
| 487 | 228 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_formatted_node_text: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 488 | 228 | my $self = shift; | |||||||||||||||||||||||||||||||
| 489 | 228 | my $node = shift; | |||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||
| 491 | 228 | my $text = $node->textContent(); | |||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||
| 493 | # Intent: format the text. | ||||||||||||||||||||||||||||||||
| 494 | # Trim leading and trailing nelines. | ||||||||||||||||||||||||||||||||
| 495 | # $text =~ s{\A\n+}{}ms; | ||||||||||||||||||||||||||||||||
| 496 | # $text =~ s{\n+\z}{}ms; | ||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||
| 498 | # Convert a sequence of spaces to a single space. | ||||||||||||||||||||||||||||||||
| 499 | 228 | $text =~ s{\s+}{ }gms; | |||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||
| 501 | 228 | return $text; | |||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||
| 504 | sub _render_para | ||||||||||||||||||||||||||||||||
| 505 | 146 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_para: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 506 | 146 | my ( $self, $para ) = @_; | |||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||
| 508 | 146 | my $first_text = 1; | |||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||
| 510 | 146 | foreach my $node ( $para->childNodes() ) | |||||||||||||||||||||||||||||||
| 511 | { | ||||||||||||||||||||||||||||||||
| 512 | 328 | ▶ | if ( $node->nodeType() == XML_ELEMENT_NODE() ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 513 | { | ||||||||||||||||||||||||||||||||
| 514 | 100 | my $name = $node->localname(); | |||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||
| 516 | 100 | ▶ | if ( $name eq "br" ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 517 | { | ||||||||||||||||||||||||||||||||
| 518 | 32 | $self->_out_formatted_line(); | |||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||
| 520 | elsif ( $name eq "a" ) | ||||||||||||||||||||||||||||||||
| 521 | { | ||||||||||||||||||||||||||||||||
| 522 | 8 | $self->_append_different_formatting_node( "[", | |||||||||||||||||||||||||||||||
| 523 | ( "](" . $node->getAttribute("href") . ")" ), $node ); | ||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||
| 525 | else | ||||||||||||||||||||||||||||||||
| 526 | { | ||||||||||||||||||||||||||||||||
| 527 | 60 | $self->_handle_format_node($node); | |||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||||||
| 530 | elsif ( $node->nodeType() == XML_TEXT_NODE() ) | ||||||||||||||||||||||||||||||||
| 531 | { | ||||||||||||||||||||||||||||||||
| 532 | 228 | my $text = $self->_get_formatted_node_text($node); | |||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||
| 534 | 228 | ▶ | if ($first_text) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 535 | { | ||||||||||||||||||||||||||||||||
| 536 | 140 | $text =~ s/\A\s+//; | |||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||
| 539 | 228 | $self->_append_to_this_line($text); | |||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||
| 542 | continue | ||||||||||||||||||||||||||||||||
| 543 | { | ||||||||||||||||||||||||||||||||
| 544 | 328 | $first_text = 0; | |||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||
| 548 | sub _render_quote_list | ||||||||||||||||||||||||||||||||
| 549 | 30 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_quote_list: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 550 | 30 | my ( $self, $ul ) = @_; | |||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||
| 552 | 30 | my $is_bullets = ( $ul->localname() eq "ul" ); | |||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||
| 554 | 30 | my $idx = 1; | |||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||
| 556 | $self->_iterate_on_child_elems( | ||||||||||||||||||||||||||||||||
| 557 | $ul, "li", | ||||||||||||||||||||||||||||||||
| 558 | { | ||||||||||||||||||||||||||||||||
| 559 | 84 | ▶ | process => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 560 | 84 | my $li = shift; | |||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||
| 562 | 84 | ▶ | $self->_append_to_this_line( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 563 | ( $is_bullets ? "*" : "$idx." ) . " " ); | ||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | 84 | $self->_render_para($li); | |||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||
| 567 | 84 | return; | |||||||||||||||||||||||||||||||
| 568 | }, | ||||||||||||||||||||||||||||||||
| 569 | 84 | ▶ | cont => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 570 | 84 | $idx++; | |||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||
| 572 | 84 | $self->_out_formatted_line(); | |||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||
| 574 | 84 | return; | |||||||||||||||||||||||||||||||
| 575 | }, | ||||||||||||||||||||||||||||||||
| 576 | 30 | if_more => '_start_new_line', | |||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||
| 578 | ); | ||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||
| 580 | 30 | return; | |||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||
| 583 | sub _render_quote_portion_paras | ||||||||||||||||||||||||||||||||
| 584 | 22 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_quote_portion_paras: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 585 | 22 | my ( $self, $node ) = @_; | |||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||
| 587 | 22 | $self->_render_portion_paras( $node, { para_is => "blockquote|p|ol|ul" } ); | |||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||
| 589 | 22 | return; | |||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||
| 592 | sub _render_quote_blockquote | ||||||||||||||||||||||||||||||||
| 593 | 2 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_quote_blockquote: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 594 | 2 | my ( $self, $node ) = @_; | |||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||
| 596 | 2 | $self->_out("<<<\n\n"); | |||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||
| 598 | 2 | $self->_render_quote_portion_paras($node); | |||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||
| 600 | 2 | $self->_out("\n\n>>>"); | |||||||||||||||||||||||||||||||
| 601 | } | ||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||
| 603 | sub _start_new_para | ||||||||||||||||||||||||||||||||
| 604 | 40 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_start_new_para: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 605 | 40 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||
| 607 | 40 | return $self->_out("\n\n"); | |||||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||
| 610 | sub _render_generalized_para | ||||||||||||||||||||||||||||||||
| 611 | 94 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_generalized_para: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 612 | 94 | my ( $self, $para ) = @_; | |||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||
| 614 | return ( | ||||||||||||||||||||||||||||||||
| 615 | 94 | ▶ | ( ( $para->localname() eq "ul" ) || ( $para->localname() eq "ol" ) ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$para->localname eq "ul" or $para->localname eq "ol"
MC/DC100%2/2
$para->localname eq "ul" or $para->localname eq "ol"
$para->localname eq "ul"
$para->localname eq "ol"
Truth table100%3/3
A or B
A= $para->localname eq "ul"B= $para->localname eq "ol"
| |||||||||||||||||||||||||||||||||
| 616 | ? $self->_render_quote_list($para) | ||||||||||||||||||||||||||||||||
| 617 | : ( $para->localname() eq "blockquote" ) | ||||||||||||||||||||||||||||||||
| 618 | ? $self->_render_quote_blockquote($para) | ||||||||||||||||||||||||||||||||
| 619 | : $self->_render_para($para) | ||||||||||||||||||||||||||||||||
| 620 | ); | ||||||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||
| 623 | sub _move_to_next_line | ||||||||||||||||||||||||||||||||
| 624 | 92 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_move_to_next_line: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 625 | 92 | my $self = shift; | |||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||
| 627 | 92 | ▶ | if ( $self->_this_line() =~ m{\S} ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 628 | { | ||||||||||||||||||||||||||||||||
| 629 | 62 | $self->_out_formatted_line(); | |||||||||||||||||||||||||||||||
| 630 | 62 | $self->_this_line(""); | |||||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||
| 633 | 92 | return; | |||||||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||
| 636 | sub _handle_portion_paragraph | ||||||||||||||||||||||||||||||||
| 637 | 92 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_handle_portion_paragraph: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 638 | 92 | my ( $self, $para ) = @_; | |||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||
| 640 | 92 | $self->_is_first_line(1); | |||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||
| 642 | 92 | $self->_render_generalized_para($para); | |||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||
| 644 | 92 | $self->_move_to_next_line; | |||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||
| 646 | 92 | return; | |||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||
| 649 | sub _render_portion_paras | ||||||||||||||||||||||||||||||||
| 650 | 52 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_portion_paras: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 651 | 52 | my ( $self, $portion, $args ) = @_; | |||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||
| 653 | 52 | my $para_name = $args->{para_is}; | |||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||
| 655 | 52 | $self->_iterate_on_child_elems( | |||||||||||||||||||||||||||||||
| 656 | $portion, | ||||||||||||||||||||||||||||||||
| 657 | $para_name, | ||||||||||||||||||||||||||||||||
| 658 | { | ||||||||||||||||||||||||||||||||
| 659 | process => '_handle_portion_paragraph', | ||||||||||||||||||||||||||||||||
| 660 | if_more => '_start_new_para', | ||||||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||||||
| 662 | ); | ||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||
| 664 | 52 | return; | |||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||
| 667 | sub _process_quote_node | ||||||||||||||||||||||||||||||||
| 668 | 20 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_process_quote_node: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 669 | 20 | my ( $self, $quote_node ) = @_; | |||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||
| 671 | 20 | my ($body_node) = $quote_node->findnodes("body"); | |||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||
| 673 | 20 | $self->_render_quote_portion_paras($body_node); | |||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||
| 675 | 20 | $self->_start_new_line; | |||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||
| 677 | 20 | return; | |||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||
| 680 | 2 | my @info_fields_order = (qw(work author channel tagline)); | |||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||
| 682 | my %info_fields_order_map = | ||||||||||||||||||||||||||||||||
| 683 | 2 | ( map { $info_fields_order[$_] => $_ + 1 } ( 0 .. $#info_fields_order ) ); | |||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||
| 685 | sub _info_field_value | ||||||||||||||||||||||||||||||||
| 686 | 120 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_info_field_value: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 687 | 120 | my $self = shift; | |||||||||||||||||||||||||||||||
| 688 | 120 | my $field = shift; | |||||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||||||
| 690 | 120 | ▶ | return $info_fields_order_map{ $field->localname() } || (-1); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$info_fields_order_map{$field->localname} || -1
MC/DC100%1/1
$info_fields_order_map{$field->localname} || -1
$info_fields_order_map{$field->localname}
Truth table100%2/2
A
A= $info_fields_order_map{$field->localname}
| |||||||||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||
| 693 | sub _calc_info_field_processed_content | ||||||||||||||||||||||||||||||||
| 694 | 90 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_calc_info_field_processed_content: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 695 | 90 | my $self = shift; | |||||||||||||||||||||||||||||||
| 696 | 90 | my $field_node = shift; | |||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||
| 698 | 90 | my $content = $field_node->textContent(); | |||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||
| 700 | # Squash whitespace including newlines into a single space. | ||||||||||||||||||||||||||||||||
| 701 | 90 | $content =~ s{\s+}{ }g; | |||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||
| 703 | # Remove leading and trailing space - it is not desirable here | ||||||||||||||||||||||||||||||||
| 704 | # because we want it formatted consistently. | ||||||||||||||||||||||||||||||||
| 705 | 90 | $content =~ s{\A\s+}{}; | |||||||||||||||||||||||||||||||
| 706 | 90 | $content =~ s{\s+\z}{}; | |||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||
| 708 | 90 | return $content; | |||||||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||
| 711 | sub _output_info_value | ||||||||||||||||||||||||||||||||
| 712 | 80 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_output_info_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 713 | 80 | my ( $self, $string ) = @_; | |||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||
| 715 | 80 | return $self->_out( ( ' ' x 4 ) . '-- ' . $string . "\n" ); | |||||||||||||||||||||||||||||||
| 716 | } | ||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||
| 718 | sub _out_info_field_node | ||||||||||||||||||||||||||||||||
| 719 | 90 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_out_info_field_node: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 720 | 90 | my ( $self, $info_node, $field_node ) = @_; | |||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||
| 722 | 90 | my $name = $field_node->localname(); | |||||||||||||||||||||||||||||||
| 723 | 90 | my $value = $self->_calc_info_field_processed_content($field_node); | |||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||
| 725 | 90 | ▶ | if ( $name eq "author" ) | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 726 | { | ||||||||||||||||||||||||||||||||
| 727 | 30 | $self->_output_info_value($value); | |||||||||||||||||||||||||||||||
| 728 | } | ||||||||||||||||||||||||||||||||
| 729 | ▶ | elsif ( ( $name eq "work" ) || ( $name eq "tagline" ) ) | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$name eq "work" or $name eq "tagline"
MC/DC100%2/2
$name eq "work" or $name eq "tagline"
$name eq "work"
$name eq "tagline"
Truth table100%3/3
A or B
A= $name eq "work"B= $name eq "tagline"
| |||||||||||||||||||||||||||||||||
| 730 | { | ||||||||||||||||||||||||||||||||
| 731 | 40 | my $url = ""; | |||||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||||||
| 733 | 40 | ▶ | if ( $field_node->hasAttribute("href") ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 734 | { | ||||||||||||||||||||||||||||||||
| 735 | 14 | $url = " ( " . $field_node->getAttribute("href") . " )"; | |||||||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||
| 738 | 40 | $self->_output_info_value( $value . $url ); | |||||||||||||||||||||||||||||||
| 739 | } | ||||||||||||||||||||||||||||||||
| 740 | elsif ( $name eq "channel" ) | ||||||||||||||||||||||||||||||||
| 741 | { | ||||||||||||||||||||||||||||||||
| 742 | 10 | my $channel = $field_node->textContent(); | |||||||||||||||||||||||||||||||
| 743 | 10 | my $network = $info_node->findnodes("network")->shift()->textContent(); | |||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||
| 745 | 10 | $self->_output_info_value("$channel, $network"); | |||||||||||||||||||||||||||||||
| 746 | } | ||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||
| 748 | 90 | return; | |||||||||||||||||||||||||||||||
| 749 | } | ||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||
| 751 | sub _get_info_node_fields | ||||||||||||||||||||||||||||||||
| 752 | 42 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_info_node_fields: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 753 | 42 | my ( $self, $info_node ) = @_; | |||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||
| 755 | return reverse | ||||||||||||||||||||||||||||||||
| 756 | 42 | sort { $self->_info_field_value($a) <=> $self->_info_field_value($b) } | |||||||||||||||||||||||||||||||
| 757 | $info_node->findnodes("*"); | ||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||
| 761 | sub _render_info_node | ||||||||||||||||||||||||||||||||
| 762 | 42 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_render_info_node: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 763 | 42 | my ( $self, $info_node ) = @_; | |||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||
| 765 | 42 | ▶ | if ( my @f = $self->_get_info_node_fields($info_node) ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 766 | { | ||||||||||||||||||||||||||||||||
| 767 | 40 | $self->_start_new_line; | |||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||
| 769 | 40 | foreach my $field_node (@f) | |||||||||||||||||||||||||||||||
| 770 | { | ||||||||||||||||||||||||||||||||
| 771 | 90 | $self->_out_info_field_node( $info_node, $field_node ); | |||||||||||||||||||||||||||||||
| 772 | } | ||||||||||||||||||||||||||||||||
| 773 | } | ||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||
| 775 | 42 | return; | |||||||||||||||||||||||||||||||
| 776 | } | ||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||
| 778 | 2 | 1; | |||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||
| 780 | __END__ | ||||||||||||||||||||||||||||||||