BBCode/HTML.pm

filter: Statementstmt 96.2% 77 / 80 Branchbran 83.3% 25 / 30 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 94.7% 18 / 19 Podpod 100% 3 / 3 totaltotal 93.4% 128 / 137 CC 15 SCAR 7.5
CC
15
Cov
93%
__ANON__
4.0
__ANON__
3.0
__ANON__
2.1
CRAP 2.1
« BBCode/Markdown.pm ↑ Summary
1 package Parse::BBCode::HTML;
2 15 $Parse::BBCode::HTML::VERSION = '0.15';
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 our @EXPORT_OK = qw/ &defaults &default_escapes &optional /;
9  
10 15 my $email_valid = 0;
11 15 eval {
12 require
13 15 Email::Valid;
14 };
15 15 ▶$email_valid = 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 15 0 15
16  
17 15 my %colors = (
18 aqua => 1,
19 black => 1,
20 blue => 1,
21 fuchsia => 1,
22 gray => 1,
23 grey => 1,
24 green => 1,
25 lime => 1,
26 maroon => 1,
27 navy => 1,
28 olive => 1,
29 purple => 1,
30 red => 1,
31 silver => 1,
32 teal => 1,
33 white => 1,
34 yellow => 1,
35 );
36  
37 my %default_tags = (
38 'b' => '<b>%s</b>',
39 'i' => '<i>%s</i>',
40 'u' => '<u>%s</u>',
41 'img' => '<img src="%{link}A" alt="[%{html}s]" title="%{html}s">',
42 'url' => 'url:<a href="%{link}A" rel="nofollow">%s</a>',
43 'email' => 'url:<a href="mailto:%{email}A">%s</a>',
44 'size' => '<span style="font-size: %{num}a">%s</span>',
45 'color' => '<span style="color: %{htmlcolor}a">%s</span>',
46 'list' => {
47 parse => 1,
48 class => 'block',
49 13 ▶ code => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
50 13 my ($parser, $attr, $content, $attribute_fallback, $tag) = @_;
51 13 $$content =~ s/^\n+//;
52 13 $$content =~ s/\n+\z//;
53 13 my $type = "ul";
54 13 my $style = '';
55 13 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 3 10 13
56 3 ▶ if ($attr eq '1') {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr eq '1') { } 2 1 3
elsif ($attr eq 'a') { } 1 0 1
57 2 $type = "ol";
58 }
59 elsif ($attr eq 'a') {
60 1 $type = "ol";
61 1 $style = ' style="list-style-type: lower-alpha"';
62 }
63 }
64 13 return "<$type$style>$$content</$type>";
65 },
66 },
67 '*' => {
68 parse => 1,
69 30 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
70 30 my ($parser, $attr, $content, $attribute_fallback, $tag, $info) = @_;
71 30 $$content =~ s/\n+\z//;
72 30 ▶ if ($info->{stack}->[-2] eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"stack"}[-2] eq "list") 27 3 30
73 27 return "<li>$$content</li>",
74 }
75 3 return Parse::BBCode::escape_html($tag->raw_text);
76 },
77 close => 0,
78 class => 'block',
79 },
80 'quote' => {
81 2 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
82 2 my ($parser, $attr, $content) = @_;
83 2 my $title = 'Quote';
84 2 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 2 0 2
85 2 $title = Parse::BBCode::escape_html($attr);
86 }
87 2 return <<"EOM";
88 <div class="bbcode_quote_header">$title:
89 <div class="bbcode_quote_body">$$content</div></div>
90 EOM
91 },
92 parse => 1,
93 class => 'block',
94 },
95 'code' => {
96 4 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
97 4 my ($parser, $attr, $content) = @_;
98 4 my $title = 'Code';
99 4 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 0 4 4
100 0 $title = Parse::BBCode::escape_html($attr);
101 }
102 4 $content = Parse::BBCode::escape_html($$content);
103 4 return <<"EOM";
104 <div class="bbcode_code_header">$title:
105 <div class="bbcode_code_body">$content</div></div>
106 EOM
107 },
108 15 parse => 0,
109 class => 'block',
110 },
111 'noparse' => '%{html}s',
112 );
113 15 my %optional_tags = (
114 'html' => '%{noescape}s',
115 );
116  
117 my %default_escapes = (
118 37 ▶ html => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 37 Parse::BBCode::escape_html($_[2]),
120 },
121 7 ▶ uri => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 7 uri_escape($_[2]),
123 },
124 30 ▶ link => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
125 30 my ($p, $tag, $var) = @_;
126 30 ▶ if ($var =~ m{^ (?: [a-z]+:// | / ) \S+ \z}ix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ m[^ (?: [a-z]+:// | / ) \S+ \z]xi) { } 25 5 30
127 # allow proto:// and absolute links /
128 }
129 else {
130 # invalid
131 5 return;
132 }
133 25 $var = Parse::BBCode::escape_html($var);
134 25 return $var;
135 },
136 6 ▶ email => $email_valid ? sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
137 6 my ($p, $tag, $var) = @_;
138 # extracts the address part of the email or undef
139 6 my $valid = Email::Valid->address($var);
140 6 ▶ return $valid ? Parse::BBCode::escape_html($valid) : '';
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 4 2 6
141 0 ▶ } : sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 my ($p, $tag, $var) = @_;
143 0 $var = Parse::BBCode::escape_html($var);
144 },
145 6 ▶ htmlcolor => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
146 6 my $color = $_[2];
147 6 ▶ ($color =~ m/^(?:#[0-9a-fA-F]{6})\z/ || exists $colors{lc $color})
Branch: 1 branch
BranchTrueFalseTotal
$color =~ /^(?:#[0-9a-fA-F]{6})\z/ || exists $colors{lc $color} ? : 4 2 6
Condition100%3/3
$color =~ /^(?:#[0-9a-fA-F]{6})\z/ or exists $colors{lc $color}
l!l&&r!l&&!r
222
MC/DC100%2/2
$color =~ /^(?:#[0-9a-fA-F]{6})\z/ or exists $colors{lc $color}
$color =~ /^(?:#[0-9a-fA-F]{6})\z/ exists $colors{lc $color}
Truth table100%3/3
A or B
A= $color =~ /^(?:#[0-9a-fA-F]{6})\z/B= exists $colors{lc $color}
ABresult
1X1
011
000
148 ? $color : 'inherit'
149 },
150 4 ▶ num => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
151 4 ▶ $_[2] =~ m/^[0-9]+\z/ ? $_[2] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[2] =~ /^[0-9]+\z/ ? : 2 2 4
152 },
153 15 ▶);
Branch: 1 branch
BranchTrueFalseTotal
$email_valid ? : 15 0 15
154  
155  
156 13 ▶sub defaults {
Subroutine defaults: called CC=2 SCAR=6.9
Pod documented
157 13 my ($class, @keys) = @_;
158 return @keys
159 13 ▶ ? (map { $_ => $default_tags{$_} } grep { defined $default_tags{$_} } @keys)
Branch: 1 branch
BranchTrueFalseTotal
@keys ? : 1 12 13
160 : %default_tags;
161 }
162  
163 36 ▶sub default_escapes {
Subroutine default_escapes: called CC=2 SCAR=6.9
Pod documented
164 36 my ($class, @keys) = @_;
165 return @keys
166 36 ▶ ? (map { $_ => $default_escapes{$_} } grep { defined $default_escapes{$_} } @keys)
Branch: 1 branch
BranchTrueFalseTotal
@keys ? : 1 35 36
167 : %default_escapes;
168 }
169  
170 2 ▶sub optional {
Subroutine optional: called CC=2 SCAR=6.9
Pod documented
171 2 my ($class, @keys) = @_;
172 return @keys
173 2 ▶ ? (map { $_ => $optional_tags{$_} } grep { defined $optional_tags{$_} } @keys)
Branch: 1 branch
BranchTrueFalseTotal
@keys ? : 1 1 2
174 : %optional_tags;
175 }
176  
177  
178  
179 15 1;
180  
181 __END__
« BBCode/Markdown.pm ↑ Summary