blib/lib/Text/Trac/InlineNode.pm

filter: Statementstmt 99.3% 154 / 155 Branchbran 95.6% 44 / 46 Conditioncond 57.1% 8 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 28 / 28 Podpod 0.0% 0 / 3 totaltotal 92.9% 236 / 254 SCAR 35.6
CC
35
Cov
96%
_make_ext_link
10.4
_replace
7.0
_make_link
6.0
CRAP 35.1
↑ Summary blib/lib/Text/Trac/P.pm »
1 package Text::Trac::InlineNode;
2  
3 53 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 53 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 53 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 ▶use Text::Trac::Macro;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 53 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 53 ▶use Text::Trac::LinkResolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 53 ▶use HTML::Entities qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 53 our $VERSION = '0.23';
12  
13 53 tie my %token_table, 'Tie::IxHash';
14  
15 #my $handler = $token_table{'!?\\[\\d+\\]|(?:\\b|!)r\\d+\\b(?!:\\d)'};
16 #$handler->format_link('test');
17  
18 53 my $link_scheme = '[\w.+-]+';
19 53 my $quoted_string = q{'[^']+'|"[^"]+"};
20 53 my $shref_target_first = '[\w/?!#@]';
21 53 my $shref_target_middle = '(?:\|(?=[^|\s])|[^|<>\s])';
22 53 my $shref_target_last = '[a-zA-Z0-9/=]';
23 53 my $shref = "!?$link_scheme:
24 (?:
25 $quoted_string
26 |$shref_target_first(?:$shref_target_middle*$shref_target_last)?
27 )
28 ";
29  
30 53 my $macro = '\[\[[\w/+-]+(?:\(.*\))?\]\]';
31  
32 53 my $lhref_relative_target = '[/.][^\s[\]]*';
33 53 my $lhref = "!?\\[
34 (?:
35 $link_scheme:
36 (?:$quoted_string|[^\\[\\]\\s]*)
37 |(?:$lhref_relative_target|[^\\[\\]\\s])
38 )
39 (?:
40 \\s+
41 $quoted_string
42 |[^\\]]+
43 )?
44 \\]
45 ";
46  
47 53 my $rules = join '|', ( map {"($_)"} ( keys %token_table ) );
48 53 $rules = qr/$rules/x;
49  
50 53 s/^\!\?// for values %token_table;
51 53 s/^\\// for values %token_table;
52  
53 sub new {
54 5462 ▶ my ( $class, $c ) = @_;
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
55  
56 # external link resolvers
57 5462 my %external_handler;
58 5462 for (@Text::Trac::LinkResolver::handlers) {
59 49158 my $class = 'Text::Trac::LinkResolver::' . ucfirst($_);
60 49158 $class->require;
61 49158 my $handler = $class->new($c);
62 49158 ▶ $token_table{ $handler->{pattern} } = $handler if defined $handler->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
if defined $handler->{"pattern"} 32772 16386 49158
63 49158 $external_handler{$_} = $handler;
64 }
65  
66 %token_table = (
67 5462 q{'''''} => 'bolditalic',
68 q{'''} => 'bold',
69 q{''} => 'italic',
70 '!?__' => 'underline',
71 '!?~~' => 'strike',
72 '!?,,' => 'subscript',
73 '!?\^' => 'superscript',
74 '`|\{\{\{|\}\}\}' => 'inline',
75 $macro => 'macro',
76 %token_table,
77 $lhref => 'lhref',
78 $shref => 'shref',
79 );
80  
81 5462 my $rules = join '|', ( map {"($_)"} ( keys %token_table ) );
82 5462 $rules = qr/$rules/x;
83  
84 5462 s/^\!\?// for values %token_table;
85 5462 s/^\\// for values %token_table;
86  
87 5462 my $self = {
88 context => $c,
89 open_tags => [],
90 rules => $rules,
91 external_handler => \%external_handler,
92 };
93 5462 bless $self, $class;
94 5462 return $self;
95 }
96  
97 sub parse {
98 2894 ▶ my ( $self, $rest ) = @_;
Subroutine parse: called CC=2 SCAR=6.9
Pod undocumented
99 2894 my $html = '';
100 2894 ▶ while ( $rest =~ /$self->{rules}/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /$self->{"rules"}/msx) 1486 2894 4380
101 1486 $html .= $self->escape($`) . $self->_replace( $&, $`, $' );
102 1486 $rest = $';
103 }
104 2894 return $html . $self->escape($rest);
105 }
106  
107 sub escape {
108 4557 ▶ my ( $self, $s ) = @_;
Subroutine escape: called CC=1 SCAR=0.0
Pod undocumented
109 4557 return HTML::Entities::encode( $s, '<>&"' );
110 }
111  
112 sub _replace {
113 1486 ▶ my ( $self, $match, $pre_match, $post_match ) = @_;
Subroutine _replace: called CC=7 SCAR=19.5
114 1486 ▶ if ( $match =~ s/^!// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match =~ s/^!//) { } 22 1464 1486
115 22 return $match;
116 }
117 else {
118 TOKEN:
119 1464 for my $token ( keys %token_table ) {
120 17343 ▶ if ( $match =~ /$token/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match =~ /$token/x) 1482 15861 17343
121 1482 my $formatter = $token_table{$token};
122 1482 ▶ if ( ref $formatter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $formatter) { } 89 1393 1482
123 89 for (qw/ log source attachment http /) {
124 340 ▶ next TOKEN if $match =~ /^\[?$_/;
Branch: 1 branch
BranchTrueFalseTotal
if $match =~ /^\[?$_/ 18 322 340
125 }
126 71 return $formatter->format_link($match);
127 }
128 else {
129 1393 my $method = "_${formatter}_formatter";
130 1393 return $self->$method( $match, $pre_match, $post_match );
131 }
132 }
133 }
134 }
135 }
136  
137 sub _simple_tag_handler {
138 652 ▶ my ( $self, $open_tag, $close_tag ) = @_;
Subroutine _simple_tag_handler: called CC=2 SCAR=6.9
139  
140 652 ▶ if ( $self->_is_open($open_tag) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_open($open_tag)) { } 285 367 652
141 285 $self->_close_tag($open_tag);
142 285 return $close_tag;
143 }
144 else {
145 367 $self->_open_tag($open_tag);
146 367 return $open_tag;
147 }
148 }
149  
150 sub _is_open {
151 656 ▶ my ( $self, $tag ) = @_;
Subroutine _is_open: called CC=1 SCAR=0.0
152 656 return grep { $tag eq $_ } @{ $self->{open_tags} };
153 }
154  
155 sub _open_tag {
156 369 ▶ my ( $self, $tag ) = @_;
Subroutine _open_tag: called CC=1 SCAR=0.0
157 369 push @{ $self->{open_tags} }, $tag;
158 }
159  
160 sub _close_tag {
161 287 ▶ my ( $self, $tag ) = @_;
Subroutine _close_tag: called CC=3 SCAR=11.0
162  
163 287 my $index = 0;
164 287 for ( @{ $self->{open_tags} } ) {
165 293 ▶ last if $tag eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq $_ 287 6 293
166 6 $index++;
167 }
168 287 splice @{ $self->{open_tags} }, $index;
169 }
170  
171 sub _bolditalic_formatter {
172 4 ▶ my $self = shift;
Subroutine _bolditalic_formatter: called CC=3 SCAR=11.0
173  
174 4 my $is_open = $self->_is_open('<i>');
175  
176 4 my $tmp;
177 4 ▶ if ($is_open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_open) 2 2 4
178 2 $tmp .= '</i>';
179 2 $self->_close_tag('<i>');
180 }
181  
182 4 $tmp .= $self->_bold_formatter;
183  
184 4 ▶ unless ($is_open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_open) 2 2 4
185 2 $tmp .= '<i>';
186 2 $self->_open_tag('<i>');
187 }
188  
189 4 return $tmp;
190 }
191  
192 sub _bold_formatter {
193 76 ▶ my $self = shift;
Subroutine _bold_formatter: called CC=1 SCAR=0.0
194 76 return $self->_simple_tag_handler( '<strong>', '</strong>' );
195 }
196  
197 sub _italic_formatter {
198 5 ▶ my $self = shift;
Subroutine _italic_formatter: called CC=1 SCAR=0.0
199 5 return $self->_simple_tag_handler( '<i>', '</i>' );
200 }
201  
202 sub _underline_formatter {
203 12 ▶ my ( $self, $match, $pre_match, $post_match ) = @_;
Subroutine _underline_formatter: called CC=2 SCAR=6.9
204 12 ▶ my $class_underline = $self->{context}->{class} ? q{class="underline"} : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'context'}{'class'} ? : 6 6 12
205 12 return $self->_simple_tag_handler( qq{<span $class_underline>}, '</span>' );
206 }
207  
208 sub _strike_formatter {
209 4 ▶ my ( $self, $match, $pre_match, $post_match ) = @_;
Subroutine _strike_formatter: called CC=1 SCAR=0.0
210 4 return $self->_simple_tag_handler( '<del>', '</del>' );
211 }
212  
213 sub _superscript_formatter {
214 5 ▶ my ( $self, $match, $pre_match, $post_match ) = @_;
Subroutine _superscript_formatter: called CC=1 SCAR=0.0
215 5 return $self->_simple_tag_handler( '<sup>', '</sup>' );
216 }
217  
218 sub _subscript_formatter {
219 4 ▶ my ( $self, $match, $pre_match, $post_match ) = @_;
Subroutine _subscript_formatter: called CC=1 SCAR=0.0
220 4 return $self->_simple_tag_handler( '<sub>', '</sub>' );
221 }
222  
223 sub _inline_formatter {
224 546 ▶ my ( $self, $match, $pre_match, $post_match ) = @_;
Subroutine _inline_formatter: called CC=1 SCAR=0.0
225 546 return $self->_simple_tag_handler( '<tt>', '</tt>' );
226 }
227  
228 sub _shref_formatter {
229 134 ▶ my ( $self, $match ) = @_;
Subroutine _shref_formatter: called CC=1 SCAR=0.0
230  
231 134 my ( $ns, $target ) = (
232 $match =~ m/($link_scheme):
233 (
234 $quoted_string
235 |$shref_target_first
236 (?:
237 $shref_target_middle*
238 $shref_target_last
239 )?
240 )
241 /x
242 );
243 134 return $self->_make_link( $ns, $target, $match, $match );
244 }
245  
246 sub _lhref_formatter {
247 587 ▶ my ( $self, $match ) = @_;
Subroutine _lhref_formatter: called CC=4 SCAR=13.9
248  
249 587 my ( $ns, $target, $label ) = (
250 $match =~ m/\[
251 ($link_scheme):
252 (
253 (?:$quoted_string|[^\]\s]*)
254 |(?:$lhref_relative_target|[^\]\s])
255 )
256 (?:
257 \s+
258 ($quoted_string|[^\]]+)
259 )?
260 \]
261 /x
262 );
263 587 ▶ if ( !$label ) { # e.g. `[http://target]` or `[wiki:target]`
Branch: 1 branch
BranchTrueFalseTotal
unless ($label) 98 489 587
264 98 ▶ if ($target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target) { } 85 13 98
265 85 ▶ if ( $target =~ m!^//! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ m[^//]) { } 29 56 85
266 29 $label = $ns . ':' . $target;
267 }
268 else {
269 56 $label = $target;
270 }
271 }
272 else { # e.g. `[search:]`
273 13 $label = $ns;
274 }
275 }
276 587 return $self->_make_link( $ns, $target, $match, $label );
277 }
278  
279 sub _make_link {
280 721 ▶ my ( $self, $ns, $target, $match, $label ) = @_;
Subroutine _make_link: called CC=6 SCAR=17.9
281 721 ▶ if ( defined $target && ( $target =~ m!^//! or $target eq 'mailto' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target and $target =~ m[^//] || $target eq 'mailto') { } 244 477 721
Condition83%5/6
defined $target and $target =~ m[^//] || $target eq "mailto"
!ll&&!rl&&r
13464244
$target =~ m[^//] or $target eq "mailto"
l!l&&r!l&&!r
2440464
MC/DC66%2/3
defined $target and $target =~ m[^//] || $target eq "mailto"
defined $target $target =~ m[^//] $target eq "mailto"
Truth table75%3/4
A and B or C
A= defined $targetB= $target =~ m[^//]C= $target eq "mailto"
ABCresult
0XX0
1000
11X1
1011
282 244 return $self->_make_ext_link( $ns . ':' . $target, $label );
283 }
284 else {
285 477 my $handler;
286 477 ▶ if ( defined $ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns) 464 13 477
287 464 $handler = $self->{external_handler}->{$ns};
288 }
289 477 ▶ return $handler ? $handler->format_link( $match, $target, $label ) : $match;
Branch: 1 branch
BranchTrueFalseTotal
$handler ? : 445 32 477
290 }
291 }
292  
293 sub _make_ext_link {
294 244 ▶ my ( $self, $url, $text, $title ) = @_;
Subroutine _make_ext_link: called CC=9 SCAR=23.4
295  
296 244 ▶ my $title_attr = $title ? qq{title="$title"} : '';
Branch: 1 branch
BranchTrueFalseTotal
$title ? : 0 244 244
297  
298 244 ▶ $title ||= $text;
Condition33%1/3
$title ||= $text
l!l&&r!l&&!r
02440
MC/DC0%0/2
$title ||= $text
$title $text
Truth table33%1/3
A ||= B
A= $titleB= $text
ABresult
1X1
011
000
299  
300 244 ▶ my $local = $self->{context}->{local} || '';
Condition50%1/2
$self->{'context'}{'local'} || ''
l!l
0244
MC/DC0%0/1
$self->{'context'}{'local'} || ''
$self->{'context'}{'local'}
Truth table50%1/2
A
A= $self->{'context'}{'local'}
Aresult
11
00
301 244 ▶ my $class_link = $self->{context}->{class} ? q{class="ext-link"} : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'context'}{'class'} ? : 47 197 244
302 244 ▶ my $class_icon = $self->{context}->{class} ? q{class="icon"} : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'context'}{'class'} ? : 47 197 244
303 244 ▶ my $span = $self->{context}{span} ? qq{<span $class_icon></span>} : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'context'}{'span'} ? : 100 144 244
304 244 ▶ if ( $url !~ /^$local/ or !$local ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $url =~ /^$local/ && $local) 244 0 244
Condition33%1/3
$url =~ /^$local/ && $local
!ll&&!rl&&r
02440
MC/DC0%0/2
$url =~ /^$local/ && $local
$url =~ /^$local/ $local
Truth table33%1/3
A && B
A= $url =~ /^$local/B= $local
ABresult
0X0
100
111
305 244 return qq{<a $class_link href="$url"$title_attr>$span$text</a>};
306 }
307 }
308  
309 sub _macro_formatter {
310 20 ▶ my ( $self, $match ) = @_;
Subroutine _macro_formatter: called CC=2 SCAR=6.9
311  
312 20 my ( $name, $args ) = ( $match =~ m!\[\[ ([\w/+-]+) (?:\( (.*) \))? \]\]!x );
313  
314 20 ▶ if ( $name =~ /br/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /br/i) { } 1 19 20
315 1 return '<br />';
316 }
317 else {
318 19 return Text::Trac::Macro->new->parse( $name, $args, $match );
319 }
320 }
321  
322 package Text::Trac::InlineNode::Initializer;
323  
324 53 1;
↑ Summary blib/lib/Text/Trac/P.pm »