HTMLFormatter.pm

filter: Statementstmt 99.3% 154 / 155 Branchbran 88.5% 62 / 70 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 95.2% 239 / 251 CC 41 SCAR 31.4
CC
41
Cov
96%
format
35.1
_match_tag
4.0
_close_paragraph
3.1
CRAP 23.0
« PullParser.pm ↑ Summary
1 package Text::CaffeinatedMarkup::HTMLFormatter;
2  
3 3 ▶use v5.10;
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  
7 3 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 Log::Log4perl->easy_init($OFF);
9  
10 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Text::CaffeinatedMarkup::PullParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use HTML::Escape qw/escape_html/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 3 my %tags = (
16 STRONG_OPEN => '<strong>',
17 STRONG_CLOSE => '</strong>',
18 EMPHASIS_OPEN => '<em>',
19 EMPHASIS_CLOSE => '</em>',
20 UNDERLINE_OPEN => '<u>',
21 UNDERLINE_CLOSE => '</u>',
22 DEL_OPEN => '<del>',
23 DEL_CLOSE => '</del>',
24  
25 PARAGRAPH_OPEN => '<p>',
26 PARAGRAPH_CLOSE => '</p>',
27  
28 BLOCKQUOTE_OPEN => '<blockquote>',
29 BLOCKQUOTE_CLOSE=> '</blockquote>',
30  
31 BLOCKQUOTE_CITE_OPEN => '<cite>',
32 BLOCKQUOTE_CITE_CLOSE => '</cite>',
33 );
34  
35  
36 14 ▶has 'tag_stack' => (is=>'rw',default=>sub{[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
37 3 has 'is_paragraph_open' => (is=>'rw');
38 3 has 'num_breaks' => (is=>'rw');
39  
40 3 has 'is_in_row' => (is=>'rw');
41 3 has 'is_in_column' => (is=>'rw');
42 3 has 'row_has_num_columns' => (is=>'rw');
43 3 has 'row_columns' => (is=>'rw');
44  
45 40 ▶sub format {
Subroutine format: called CC=35 SCAR=35.6
Pod documented
46 40 my ($self, $pml) = @_;
47  
48 40 my $parser = Text::CaffeinatedMarkup::PullParser->new(pml => $pml);
49  
50 40 my @tokens = $parser->get_all_tokens;
51
52 36 my $output_html = '';
53 36 my $cur_column_html = '';
54 36 my $html = \$output_html;
55  
56 36 $self->num_breaks(0);
57  
58 36 $self->is_paragraph_open(0);
59 36 $self->is_in_row(0);
60 36 $self->row_has_num_columns(-1);
61 36 $self->row_columns([]);
62
63 36 foreach my $token (@tokens) {
64  
65 171 my $type = $token->{type};
66  
67 171 ▶ if ($type eq 'NEWLINE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'NEWLINE') { } 37 134 171
68 # Start storing breaks. We output as soon as we get something different
69 # (see the else). If there's only one then you get a BR, otherwise you
70 # get a paragraph.
71 37 $self->num_breaks( $self->num_breaks+1 );
72 37 next;
73 }
74 else {
75 134 ▶ unless ($type eq 'HEADER') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type eq "HEADER") 127 7 134
76  
77 127 ▶ if ($self->num_breaks == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->num_breaks == 1) { } 2 125 127
elsif ($self->num_breaks > 1) { } 13 112 125
78 2 $$html .= '<br>';
79 }
80 elsif ($self->num_breaks > 1) {
81 13 ▶ $$html .= $self->_close_paragraph if $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_paragraph_open 13 0 13
82 13 $$html .= $self->_open_paragraph;
83 }
84 }
85 134 $self->num_breaks(0);
86 }
87  
88 134 ▶ if ($type eq 'QUOTE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "QUOTE") 2 132 134
89  
90 2 ▶ $self->_close_paragraph if $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_paragraph_open 0 2 2
91  
92 2 $$html .= $tags{BLOCKQUOTE_OPEN};
93 2 $$html .= $token->{body};
94  
95 2 ▶ if ($token->{cite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"cite"}) 1 1 2
96 1 $$html .= $tags{BLOCKQUOTE_CITE_OPEN}.$token->{cite}.$tags{BLOCKQUOTE_CITE_CLOSE};
97 }
98  
99 2 $$html .= $tags{BLOCKQUOTE_CLOSE};
100 }
101  
102 134 ▶ if ($type eq 'ROW') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ROW") 14 120 134
103 14 ▶ if ($self->is_in_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_in_row) { } 7 7 14
104 # Finalise row
105  
106 7 ▶ if ($self->is_in_column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_in_column) 7 0 7
107 7 ▶ $cur_column_html .= $self->_close_paragraph if $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_paragraph_open 7 0 7
108 # Already in a column, so output it to the column store
109 7 push @{$self->row_columns}, $cur_column_html;
110 7 $cur_column_html = '';
111 }
112  
113 7 $html = \$output_html;
114  
115 7 my $num_columns = $self->_num_columns_in_cur_row;
116  
117  
118  
119 7 $$html .= '<div class="clearfix col-'.$num_columns.'">'."\n";
120  
121 7 foreach my $column (@{$self->row_columns}) {
122 12 $$html .= '<div class="column">' . "\n$column" . "</div>\n";
123 }
124  
125 7 $$html .= "</div>\n"; # End of row
126  
127 # Reset the columns when we close out the row rather than
128 # when starting so that you can always query "num columns"
129 # and it will be right in context for wherever the parsing is.
130 7 $self->row_columns([]);
131 7 $self->is_in_column(0);
132 7 $self->is_in_row(0);
133
134 }
135 else {
136 7 ▶ $$html .= $self->_close_paragraph if $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_paragraph_open 2 5 7
137 7 $self->is_in_row(1);
138 }
139 14 next;
140 }
141  
142 120 ▶ if ($type eq 'COLUMN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "COLUMN") 12 108 120
143 # TODO error if not in row!
144 12 $html = \$cur_column_html;
145  
146 12 ▶ if ($self->is_in_column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_in_column) 5 7 12
147 5 ▶ $cur_column_html .= $self->_close_paragraph if $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_paragraph_open 5 0 5
148 # Already in a column, so output it to the column store
149 5 push @{$self->row_columns}, $cur_column_html;
150 5 $cur_column_html = '';
151 }
152
153 12 $self->is_in_column(1);
154 12 $self->row_has_num_columns( $self->row_has_num_columns+1 );
155 }
156  
157 120 ▶ if ($type =~ /^(STRONG|EMPHASIS|UNDERLINE|DEL)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^(STRONG|EMPHASIS|UNDERLINE|DEL)$/o) 12 108 120
158 12 TRACE "Type [$1]";
159 12 $$html .= $self->_match_tag($1);
160 12 next;
161 }
162  
163 108 ▶ if ($type eq 'LINK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "LINK") 10 98 108
164 # TODO - target
165 10 my $href = $token->{href};
166 10 ▶ my $text = $token->{text} || $token->{href};
Condition66%2/3
$token->{'text'} || $token->{'href'}
l!l&&r!l&&!r
910
MC/DC0%0/2
$token->{'text'} || $token->{'href'}
$token->{'text'} $token->{'href'}
Truth table66%2/3
A || B
A= $token->{'text'}B= $token->{'href'}
ABresult
1X1
011
000
167 10 $$html .= qq|<a href="$href" target="_new">$text</a>|;
168 10 next;
169 }
170  
171 98 ▶ if ($type eq 'IMAGE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "IMAGE") 19 79 98
172 19 my @options;
173 19 ▶ if ($token->{options}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"options"}) 17 2 19
174 17 @options = split /,/,$token->{options};
175 }
176  
177 19 my $align = '';
178 19 my $height = '';
179 19 my $width = '';
180  
181 19 foreach my $option (@options) {
182 35 ▶ $align = ' class="pulled-left"' if $option eq '<<';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "<<" 10 25 35
183 35 ▶ $align = ' class="pulled-right"' if $option eq '>>';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq ">>" 1 34 35
184 35 ▶ $align = ' class="stretched"' if $option eq '<>';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "<>" 1 34 35
185 35 ▶ $align = ' class="centered"' if $option eq '><';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "><" 1 34 35
186  
187 35 ▶ if ($option =~ /^H(.+)$/) { $height = qq| height="$1px"| }
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^H(.+)$/) 11 24 35
188 35 ▶ if ($option =~ /^W(.+)$/) { $width = qq| width="$1px"| }
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^W(.+)$/) 11 24 35
189 }
190
191 19 $$html .= '<img src="'.$token->{src}.'"'.$align.$width.$height.'>';
192 19 next;
193 }
194  
195 79 ▶ if ($type eq 'HEADER') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HEADER") 7 72 79
196 7 $$html .= "\n<h".$token->{level}.'>'.$token->{text}.'</h'.$token->{level}.">\n";
197 7 next;
198 }
199  
200 72 ▶ if ($type eq 'STRING') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "STRING") 58 14 72
201 58 ▶ $$html .= $self->_open_paragraph unless $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_paragraph_open 24 34 58
202 58 $$html .= escape_html($token->{content});
203 58 next;
204 }
205  
206  
207  
208 # Shouldn't get here!
209 # TODO error
210  
211 }
212  
213 # If there's a paragraph open, close it!
214 36 ▶ $output_html .= $tags{PARAGRAPH_CLOSE} if $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_paragraph_open 14 22 36
215  
216 36 return $output_html;
217 }
218  
219 # ------------------------------------------------------------------------------
220  
221 7 ▶sub _num_columns_in_cur_row {
Subroutine _num_columns_in_cur_row: called CC=1 SCAR=0.0
222 7 my ($self) = @_;
223 7 return scalar @{$self->row_columns};
224 }
225  
226 # ------------------------------------------------------------------------------
227  
228 12 ▶sub _match_tag {
Subroutine _match_tag: called CC=4 SCAR=13.9
229 12 my ($self, $type) = @_;
230  
231 12 ▶ if (@{$self->tag_stack} && $self->tag_stack->[0] eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->tag_stack;} and $self->tag_stack->[0] eq $type) { } 6 6 12
Condition100%3/3
@{$self->tag_stack;} and $self->tag_stack->[0] eq $type
!ll&&!rl&&r
156
MC/DC100%2/2
@{$self->tag_stack;} and $self->tag_stack->[0] eq $type
@{$self->tag_stack;} $self->tag_stack->[0] eq $type
Truth table100%3/3
A and B
A= @{$self->tag_stack;}B= $self->tag_stack->[0] eq $type
ABresult
0X0
100
111
232 # Close tag
233 6 $self->_pop_stack;
234 6 return $tags{$type."_CLOSE"};
235 }
236 else {
237 # Open tag
238 6 my $html = '';
239 # If a paragraph isn't open then we need to open one!
240 6 ▶ $html = $self->_open_paragraph unless $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_paragraph_open 4 2 6
241 6 $self->_push_stack($type);
242 6 return $html . $tags{$type."_OPEN"};
243 }
244 0 return;
245 }
246  
247 # ------------------------------------------------------------------------------
248  
249 47 ▶sub _push_stack {
Subroutine _push_stack: called CC=1 SCAR=0.0
250 47 my ($self, $type) = @_;
251 47 unshift @{$self->tag_stack}, $type;
252 }
253  
254 # ------------------------------------------------------------------------------
255  
256 33 ▶sub _pop_stack {
Subroutine _pop_stack: called CC=1 SCAR=0.0
257 33 my ($self) = @_;
258 33 return shift @{$self->tag_stack};
259 }
260  
261 # ------------------------------------------------------------------------------
262  
263 41 ▶sub _open_paragraph {
Subroutine _open_paragraph: called CC=2 SCAR=7.0
264 41 my ($self) = @_;
265 41 ▶ die "Can't open paragraph - already open!" if $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_paragraph_open 0 41 41
266 41 $self->_push_stack('PARAGRAPH');
267 41 $self->is_paragraph_open(1);
268 41 return $tags{PARAGRAPH_OPEN};
269 }
270  
271 # ------------------------------------------------------------------------------
272  
273 27 ▶sub _close_paragraph {
Subroutine _close_paragraph: called CC=3 SCAR=11.2
274 27 my ($self) = @_;
275 27 ▶ die "Can't close paragraph - already closed!" unless $self->is_paragraph_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_paragraph_open 0 27 27
276 27 ▶ die "Can't close paragraph - bad stack match" unless $self->tag_stack->[0] eq 'PARAGRAPH';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->tag_stack->[0] eq "PARAGRAPH" 0 27 27
277 27 $self->_pop_stack;
278 27 $self->is_paragraph_open(0);
279 27 return $tags{PARAGRAPH_CLOSE}."\n";
280 }
281  
282 3 1;
283  
284 __END__
« PullParser.pm ↑ Summary