Compiler/Target/HTML.pm

filter: Statementstmt 92.5% 75 / 81 Branchbran 83.3% 15 / 18 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 90.0% 27 / 30 Podpod 0.0% 0 / 23 totaltotal 75.6% 121 / 160 CC 12 SCAR 8.0
CC
12
Cov
89%
_compile
6.2
paragraph_link
3.0
paragraph_image
3.0
CRAP 2.2
« Compiler/Test.pm ↑ Summary Compiler.pm »
1 package Markdown::Compiler::Target::HTML;
2 18 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 18 has tree => (
6 is => 'ro',
7 );
8  
9 has result => (
10 is => 'ro',
11 lazy => 1,
12 57 ▶ builder => sub { shift->html },
Subroutine _build_result: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
13 18 );
14  
15 18 has html => (
16 is => 'ro',
17 lazy => 1,
18 builder => '_build_html',
19 );
20  
21 has functions => (
22 is => 'ro',
23 lazy => 1,
24 64 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 return +{
26 64 'Markdown::Compiler::Parser::Node::Metadata' => 'noop',
27  
28 'Markdown::Compiler::Parser::Node::Header' => 'header',
29  
30 'Markdown::Compiler::Parser::Node::HR' => 'hr',
31  
32 'Markdown::Compiler::Parser::Node::Paragraph' => 'paragraph',
33 'Markdown::Compiler::Parser::Node::Paragraph::InlineCode' => 'paragraph_inlinecode',
34 'Markdown::Compiler::Parser::Node::Paragraph::BoldItalic' => 'paragraph_bolditalic',
35 'Markdown::Compiler::Parser::Node::Paragraph::Bold' => 'paragraph_bold',
36 'Markdown::Compiler::Parser::Node::Paragraph::Italic' => 'paragraph_italic',
37 'Markdown::Compiler::Parser::Node::Paragraph::String' => 'paragraph_string',
38 'Markdown::Compiler::Parser::Node::Paragraph::Link' => 'paragraph_link',
39 'Markdown::Compiler::Parser::Node::Paragraph::Image' => 'paragraph_image',
40  
41 'Markdown::Compiler::Parser::Node::Table' => 'table',
42 'Markdown::Compiler::Parser::Node::Table::Row' => 'table_row',
43 'Markdown::Compiler::Parser::Node::Table::Cell' => 'table_cell',
44 'Markdown::Compiler::Parser::Node::Table::HeaderCell' => 'table_header_cell',
45  
46 'Markdown::Compiler::Parser::Node::BlockQuote' => 'blockquote',
47 'Markdown::Compiler::Parser::Node::BlockQuote::String' => 'blockquote_string',
48  
49 'Markdown::Compiler::Parser::Node::CodeBlock' => 'codeblock',
50 'Markdown::Compiler::Parser::Node::CodeBlock::String' => 'codeblock_string',
51  
52 'Markdown::Compiler::Parser::Node::List::Ordered' => 'ordered_list',
53 'Markdown::Compiler::Parser::Node::List::Unordered' => 'unordered_list',
54 'Markdown::Compiler::Parser::Node::List::Unordered::Item' => 'list_item',
55 'Markdown::Compiler::Parser::Node::List::Ordered::Item' => 'list_item',
56 'Markdown::Compiler::Parser::Node::List::Item::String' => 'list_item_string',
57 }
58 }
59 18 );
60  
61 64 ▶sub _build_html {
Subroutine _build_html: called CC=1 SCAR=0.0
62 64 my ( $self ) = @_;
63  
64 64 return $self->_compile( dclone $self->tree );
65 }
66  
67 173 ▶sub _compile {
Subroutine _compile: called CC=6 SCAR=18.3
68 173 my ( $self, $tree ) = @_;
69  
70 173 my $str;
71  
72 173 ▶ while ( defined ( my $node = shift @{ $tree } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $node = shift @{$tree;})) 421 173 594
73 # Children should be compiled first.
74 421 ▶ if ( $node->{children} and @{$node->{children}} >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{'children'} and @{$node->{'children'};} >= 1) { } 109 312 421
Condition66%2/3
$node->{"children"} and @{$node->{"children"};} >= 1
!ll&&!rl&&r
3120109
MC/DC50%1/2
$node->{"children"} and @{$node->{"children"};} >= 1
$node->{"children"} @{$node->{"children"};} >= 1
Truth table66%2/3
A and B
A= $node->{"children"}B= @{$node->{"children"};} >= 1
ABresult
0X0
100
111
75  
76 # If this node can be compiled, then we will compile it, giving it the content
77 109 ▶ if ( my $code = $self->can($self->functions->{$node->{class}}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->can($self->functions->{$node->{"class"}})) 109 0 109
78 109 $str .= $code->($self, $node, $self->_compile(@{$node->{children}}));
79 109 next;
80 }
81 0 warn "This is an odd place to be.... children but the parent can't be compiled?";
82 }
83  
84 # This node has no children to compile.
85 else {
86 312 ▶ if ( my $code = $self->can($self->functions->{$node->{class}}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->can($self->functions->{$node->{'class'}})) { } 312 0 312
87 312 $str .= $code->($self, $node);
88 312 next;
89 } else {
90 0 die "Error no handler found for token type " . ref($node) . "\n";
91 }
92  
93 }
94 }
95 173 return $str;
96 }
97  
98 0 ▶sub noop { "" }
Subroutine noop: not called CC=1 SCAR=6.9
Pod undocumented
99  
100 1 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod undocumented
101 1 my ( $self, $node, $content ) = @_;
102  
103 1 my $header = "h" . $node->{size};
104  
105 1 return "<$header>$content</$header>\n\n";
106  
107 }
108  
109 0 ▶sub hr {
Subroutine hr: not called CC=1 SCAR=6.9
Pod undocumented
110 0 return "<hr />";
111 }
112  
113 57 ▶sub paragraph {
Subroutine paragraph: called CC=1 SCAR=0.0
Pod undocumented
114 57 my ( $self, $node, $content ) = @_;
115  
116 57 return "<p>$content</p>\n\n";
117  
118 }
119  
120 2 ▶sub paragraph_inlinecode {
Subroutine paragraph_inlinecode: called CC=1 SCAR=0.0
Pod undocumented
121 2 my ( $self, $node, $content ) = @_;
122  
123 2 return '<span class="inline-code">' . $content . '</span>';
124  
125 }
126  
127 0 ▶sub paragraph_bolditalic {
Subroutine paragraph_bolditalic: not called CC=1 SCAR=6.9
Pod undocumented
128 0 my ( $self, $node, $content ) = @_;
129  
130 0 return "<strong><em>$content</em></strong>";
131  
132 }
133  
134 6 ▶sub paragraph_bold {
Subroutine paragraph_bold: called CC=1 SCAR=0.0
Pod undocumented
135 6 my ( $self, $node, $content ) = @_;
136  
137 6 return "<strong>$content</strong>";
138  
139 }
140  
141 7 ▶sub paragraph_italic {
Subroutine paragraph_italic: called CC=1 SCAR=0.0
Pod undocumented
142 7 my ( $self, $node, $content ) = @_;
143  
144 7 return "<em>$content</em>";
145 }
146  
147 275 ▶sub paragraph_string {
Subroutine paragraph_string: called CC=1 SCAR=0.0
Pod undocumented
148 275 my ( $self, $node ) = @_;
149  
150 275 return $node->{content};
151  
152 }
153  
154 10 ▶sub paragraph_link {
Subroutine paragraph_link: called CC=3 SCAR=11.0
Pod undocumented
155 10 my ( $self, $node ) = @_;
156  
157 10 ▶ if ( $node->{title} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{'title'}) { } 1 9 10
158 return sprintf( '<a href="%s" title="%s">%s</a>',
159 $node->{href},
160 $node->{title},
161 $node->{text},
162 1 );
163 } else {
164 return sprintf( '<a href="%s">%s</a>',
165 $node->{href},
166 $node->{text} ? $node->{text} : $node->{href},
167 9 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$node->{'text'} ? : 7 2 9
168 }
169  
170 }
171  
172 4 ▶sub paragraph_image {
Subroutine paragraph_image: called CC=3 SCAR=11.0
Pod undocumented
173 4 my ( $self, $node ) = @_;
174  
175 4 ▶ if ( $node->{title} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{'title'}) { } 1 3 4
176 return sprintf( '<img src="%s" title="%s" alt="%s">',
177 $node->{href},
178 $node->{title},
179 $node->{text}
180 1 );
181 } else {
182 return sprintf( '<img src="%s" alt="%s">',
183 $node->{href},
184 $node->{text} ? $node->{text} : $node->{href},
185 3 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$node->{'text'} ? : 1 2 3
186 }
187 }
188  
189 3 ▶sub table_header_cell {
Subroutine table_header_cell: called CC=1 SCAR=0.0
Pod undocumented
190 3 my ( $self, $node, $content ) = @_;
191
192 3 return sprintf( "<th>%s%s</th>\n", $node->{content}, $content );
193  
194 }
195  
196 3 ▶sub table_cell {
Subroutine table_cell: called CC=1 SCAR=0.0
Pod undocumented
197 3 my ( $self, $node, $content ) = @_;
198
199 3 return sprintf( "<td>%s%s</td>\n", $node->{content}, $content );
200  
201 }
202  
203 6 ▶sub table_row {
Subroutine table_row: called CC=1 SCAR=0.0
Pod undocumented
204 6 my ( $self, $node, $content ) = @_;
205
206 6 return "<tr>\n$content</tr>\n";
207 }
208  
209 3 ▶sub table {
Subroutine table: called CC=1 SCAR=0.0
Pod undocumented
210 3 my ( $self, $node, $content ) = @_;
211  
212 3 return "<table>\n$content</table>\n\n";
213 }
214  
215 1 ▶sub blockquote {
Subroutine blockquote: called CC=1 SCAR=0.0
Pod undocumented
216 1 my ( $self, $node, $content ) = @_;
217  
218 1 return "<blockquote>$content</blockquote>\n\n";
219 }
220  
221 5 ▶sub blockquote_string {
Subroutine blockquote_string: called CC=2 SCAR=7.2
Pod undocumented
222 5 my ( $self, $node ) = @_;
223  
224 5 ▶ return $node->{content} || "";
Condition50%1/2
$node->{'content'} || ''
l!l
50
MC/DC0%0/1
$node->{'content'} || ''
$node->{'content'}
Truth table50%1/2
A
A= $node->{'content'}
Aresult
11
00
225 }
226  
227 2 ▶sub codeblock {
Subroutine codeblock: called CC=2 SCAR=7.2
Pod undocumented
228 2 my ( $self, $node, $content ) = @_;
229  
230 return $node->{language}
231 2 ▶ ? "<pre><code class=\"" . $node->{language} . "\">\n$content\n</code></pre>\n\n"
Branch: 1 branch
BranchTrueFalseTotal
$node->{'language'} ? : 0 2 2
232 : "<pre><code class=\"plaintext\">\n$content\n</code></pre>\n\n"
233 }
234  
235 6 ▶sub codeblock_string {
Subroutine codeblock_string: called CC=1 SCAR=0.0
Pod undocumented
236 6 my ( $self, $node ) = @_;
237  
238 6 return $node->{content};
239  
240 }
241  
242 1 ▶sub ordered_list {
Subroutine ordered_list: called CC=1 SCAR=0.0
Pod undocumented
243 1 my ( $self, $node, $content ) = @_;
244  
245 1 return "<ol>\n$content\n</ol>\n";
246  
247 }
248  
249 4 ▶sub unordered_list {
Subroutine unordered_list: called CC=1 SCAR=0.0
Pod undocumented
250 4 my ( $self, $node, $content ) = @_;
251  
252 4 return "<ul>\n$content\n</ul>\n";
253  
254 }
255  
256 13 ▶sub list_item {
Subroutine list_item: called CC=1 SCAR=0.0
Pod undocumented
257 13 my ( $self, $node, $content ) = @_;
258  
259 13 return "<li>$content</li>\n";
260  
261 }
262  
263 12 ▶sub list_item_string {
Subroutine list_item_string: called CC=1 SCAR=0.0
Pod undocumented
264 12 my ( $self, $node ) = @_;
265  
266 12 return $node->{content};
267 }
268  
269  
270  
271  
272  
273  
274  
275 # package Markdown::Compiler::Parser::Node;
276 # package Markdown::Compiler::Parser::Node::Header;
277 # package Markdown::Compiler::Parser::Node::HR;
278 # package Markdown::Compiler::Parser::Node::Paragraph;
279 # package Markdown::Compiler::Parser::Node::Paragraph::BoldItalic;
280 # package Markdown::Compiler::Parser::Node::Paragraph::Bold;
281 # package Markdown::Compiler::Parser::Node::Paragraph::Italic;
282 # package Markdown::Compiler::Parser::Node::Paragraph::String;
283 # package Markdown::Compiler::Parser::Node::Paragraph::Link;
284 # package Markdown::Compiler::Parser::Node::Paragraph::Image;
285 # package Markdown::Compiler::Parser::Node::Table;
286 # package Markdown::Compiler::Parser::Node::Blockquote;
287 # package Markdown::Compiler::Parser::Node::Codeblock;
288 # package Markdown::Compiler::Parser::Node::List;
289  
290  
291  
292 18 1;
« Compiler/Test.pm ↑ Summary Compiler.pm »