Kwim/Parser.pm

filter: Statementstmt 24.6% 17 / 69 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 14.2% 3 / 21 Podpod 0.0% 0 / 3 totaltotal 16.6% 20 / 120 CC 8 SCAR 14.8
CC
8
Cov
20%
__ANON__
12.0
__ANON__
6.0
__ANON__
6.0
CRAP 4.4
↑ Summary Kwim.pm »
1 ##
2 # name: Stardoc::Kwim::Parser
3 # abstract: Stardoc Kwim Parser Module
4 # author: Ingy döt Net <ingy@cpan.org>
5 # license: perl
6 # copyright: 2008, 2010, 2011
7  
8 package Stardoc::Kwim::Parser;
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use base 'WikiText::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Reusable regexp generators used by the grammar
14 1 my $ALPHANUM = '\p{Letter}\p{Number}\pM';
15  
16 # These are all stolen from URI.pm
17 1 my $reserved = q{;/?:@&=+$,[]#};
18 1 my $mark = q{-_.!~*'()};
19 1 my $unreserved = "A-Za-z0-9\Q$mark\E";
20 1 my $uric = quotemeta($reserved) . $unreserved . "%";
21 1 my %im_types = (
22 yahoo => 'yahoo',
23 ymsgr => 'yahoo',
24 callto => 'callto',
25 skype => 'callto',
26 callme => 'callto',
27 aim => 'aim',
28 msn => 'msn',
29 asap => 'asap',
30 );
31 1 my $im_re = join '|', keys %im_types;
32  
33 0 ▶sub create_grammar {
Subroutine create_grammar: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my $all_phrases = [
35 qw(waflphrase asis wikilink a im mail tt b i del)
36 ];
37 0 my $all_blocks = [
38 qw(
39 pre wafl_block
40 hr hx
41 waflparagraph
42 ul ol
43 blockquote table
44 p empty
45 else
46 )
47 ];
48  
49 return {
50 _all_blocks => $all_blocks,
51 _all_phrases => $all_phrases,
52  
53 top => {
54 blocks => $all_blocks,
55 },
56  
57 empty => {
58 match => qr/^\s*\n/,
59 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 my $node = shift;
61 0 $node->{type} = '';
62 },
63 },
64  
65 wafl_block => {
66 match => qr/(?:^\.([\w\-]+)\ *\n)((?:.*\n)*?)(?:\.\1\ *\n|\z)/,
67 },
68  
69 p => {
70 match => qr/^( # Capture whole thing
71 (?m:
72 ^(?! # All consecutive lines *not* starting with
73 (?:
74 [\#\-\*]+[\ ] |
75 [\^\|\>] |
76 \.\w+\s*\n |
77 \{[^\}]+\}\s*\n
78 )
79 )
80 .*\S.*\n
81 )+
82 )
83 (\s*\n)* # and all blank lines after
84 /x,
85 phrases => $all_phrases,
86 0 ▶ filter => sub { chomp },
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 },
88  
89 else => {
90 match => qr/^(.*)\n/,
91 phrases => [],
92 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 my $node = shift;
94 0 $node->{type} = 'p';
95 },
96 },
97  
98 pre => {
99 match => qr/^(?m:^\.pre\ *\n)((?:.*\n)*?)(?m:^\.pre\ *\n)(?:\s*\n)?/,
100 },
101  
102 blockquote => {
103 match => qr/^((?m:^>.*\n)+)(\s*\n)?/,
104 blocks => $all_blocks,
105 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 s/^>\ ?//gm;
107 },
108 },
109  
110 waflparagraph => {
111 match => qr/^\{(.*)\}[\ \t]*\n(?:\s*\n)?/,
112 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
113 0 my $node = shift;
114 0 my ($function, $options) = split /[: ]/, $node->{text}, 2;
115 0 ▶ my $replacement = defined $1 ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 0 0
116 0 ▶ $options = '' unless defined $options; # protect against an undefined here
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 0 0 0
117 0 $options =~ s/\s*(.*?)\s*/$replacement/;
118 0 $node->{attributes}{function} = $function;
119 0 $node->{attributes}{options} = $options;
120 0 undef $_;
121 },
122 },
123  
124 hx => {
125 match => qr/^(\=+) *(.*?)(\s+=+)?\s*?\n+/,
126 phrases => $all_phrases,
127 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 my $node = shift;
129 0 $node->{type} = 'h' . length($node->{1});
130 0 $_ = $node->{text} = $node->{2};
131 },
132 },
133  
134 ul => {
135 match => re_list('[\*\-\+]'),
136 blocks => [qw(ul ol subl li)],
137 0 ▶ filter => sub { s/^[\*\-\+\#] *//mg },
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 },
139  
140 ol => {
141 match => re_list('\#'),
142 blocks => [qw(ul ol subl li)],
143 0 ▶ filter => sub { s/^[\*\#] *//mg },
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 },
145  
146 subl => {
147 type => 'li',
148  
149 match => qr/^( # Block must start at beginning
150 # Capture everything in $1
151 (.*)\n # Capture the whole first line
152 [\*\#]+\ .*\n # Line starting with one or more bullet
153 (?:[\*\#]+\ .*\n)* # Lines starting with '*' or '#'
154 )(?:\s*\n)?/x, # Eat trailing lines
155 blocks => [qw(ul ol li2)],
156 },
157  
158 li => {
159 match => qr/(.*)\n/, # Capture the whole line
160 phrases => $all_phrases,
161 },
162  
163 li2 => {
164 type => '',
165 match => qr/(.*)\n/, # Capture the whole line
166 phrases => $all_phrases,
167 },
168  
169 hr => {
170 match => qr/^--+(?:\s*\n)?/,
171 },
172  
173 table => {
174 match => qr/^(
175 (
176 (?m:^\|.*\|\ \n(?=\|))
177 |
178 (?m:^\|.*\|\ \ +\n)
179 |
180 (?ms:^\|.*?\|\n)
181 )+
182 )(?:\s*\n)?/x,
183 blocks => ['tr'],
184 },
185  
186 tr => {
187 match => qr/^((?m:^\|.*?\|(?:\n| \n(?=\|)| +\n)))/s,
188 blocks => ['td'],
189 0 ▶ filter => sub { s/\s+\z// },
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 },
191  
192 # XXX Need to support blocks in TD
193 td => {
194 match => qr/\|?\s*(.*?)\s*\|\n?/s,
195 phrases => $all_phrases,
196 },
197  
198 wikilink => {
199 match => qr/
200 (?:"([^"]*)"\s*)?(?:^|(?<=[^$ALPHANUM]))\[(?=[^\s\[\]])
201 (.*?)
202 \](?=[^$ALPHANUM]|\z)
203 /x,
204 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
205 0 my $node = shift;
206 0 $node->{attributes}{target} = $node->{2};
207 0 ▶ $_ = $node->{1} || $node->{2};
Condition0%0/3
$node->{'1'} || $node->{'2'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$node->{'1'} || $node->{'2'}
$node->{'1'} $node->{'2'}
Truth table0%0/3
A || B
A= $node->{'1'}B= $node->{'2'}
ABresult
1X1
011
000
208 },
209 },
210  
211 b => {
212 match => re_huggy(q{\*}),
213 phrases => $all_phrases,
214 },
215  
216 tt => {
217 match => re_huggy(q{\`}),
218 },
219  
220 i => {
221 match => Stardoc::Kwim::Parser::re_huggy(q{\_}),
222 phrases => $all_phrases,
223 },
224  
225 del => {
226 match => re_huggy(q{\-}),
227 phrases => $all_phrases,
228 },
229  
230 im => {
231 match => qr/(\b(?:$im_re)\:[^\s\>\)]+)/,
232 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 0 my $node = shift;
234 0 my ($type, $id) = split /:/, $node->{text}, 2;
235 0 $node->{attributes}{type} = $type;
236 0 $node->{attributes}{id} = $id;
237 0 undef $_;
238 },
239 },
240  
241 waflphrase => {
242 match => qr/
243 (?:^|(?<=[\s\-]))
244 (?:"(.+?)")?
245 \{
246 ([\w-]+)
247 (?=[\:\ \}])
248 (?:\s*:)?
249 \s*(.*?)\s*
250 \}
251 (?=[^A-Za-z0-9]|\z)
252 /x,
253 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
254 0 my $node = shift;
255 0 my ($label, $function, $options) = @{$node}{qw(1 2 3)};
256 0 ▶ $label ||= '';
Condition0%0/2
$label ||= ""
l!l
00
MC/DC0%0/1
$label ||= ""
$label
Truth table0%0/2
A
A= $label
Aresult
11
00
257 0 $node->{attributes}{function} = $function;
258 0 $node->{attributes}{options} = $options;
259 0 $_ = $label;
260 },
261 },
262  
263 mail => {
264 match => qr/
265 (?:"([^"]*)"\s*)?
266 <?
267 (?:mailto:)?
268 ([\w+%\-\.]+@(?:[\w\-]+\.)+[\w\-]+)
269 >?
270 /x,
271 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
272 0 my $node = shift;
273 0 ▶ $_ = $node->{1} || $node->{2};
Condition0%0/3
$node->{'1'} || $node->{'2'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$node->{'1'} || $node->{'2'}
$node->{'1'} $node->{'2'}
Truth table0%0/3
A || B
A= $node->{'1'}B= $node->{'2'}
ABresult
1X1
011
000
274 0 $node->{attributes}{address} = $node->{2};
275 },
276 },
277  
278 a => {
279 type => 'hyperlink',
280 match => qr{
281 (?:"([^"]*)"\s*)?
282 <?
283 (
284 (?:http|https|ftp|irc|file):
285 (?://)?
286 [$uric]+
287 [A-Za-z0-9/#]
288 )
289 >?
290 }x,
291 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
292 0 my $node = shift;
293 0 ▶ $_ = $node->{1} || $node->{2};
Condition0%0/3
$node->{'1'} || $node->{'2'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$node->{'1'} || $node->{'2'}
$node->{'1'} $node->{'2'}
Truth table0%0/3
A || B
A= $node->{'1'}B= $node->{'2'}
ABresult
1X1
011
000
294 0 $node->{attributes}{target} = $node->{2};
295 },
296 },
297  
298 asis => {
299 match => qr/
300 \{\{
301 (.*?)
302 \}\}(\}*)
303 /xs,
304 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 0 my $node = shift;
306 0 $node->{type} = '';
307 0 $_ = $node->{1} . $node->{2};
308 },
309 },
310  
311 0 };
312 }
313  
314 0 ▶sub re_huggy {
Subroutine re_huggy: not called CC=2 SCAR=17.9
Pod undocumented
315 0 my $brace1 = shift;
316 0 ▶ my $brace2 = shift || $brace1;
Condition0%0/3
shift() || $brace1
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $brace1
shift() $brace1
Truth table0%0/3
A || B
A= shift()B= $brace1
ABresult
1X1
011
000
317  
318 0 qr/
319 (?:^|(?<=[^{$ALPHANUM}$brace1]))$brace1(?=\S)(?!$brace2)
320 (.*?)
321 $brace2(?=[^{$ALPHANUM}$brace2]|\z)
322 /x;
323 }
324  
325 0 ▶sub re_list {
Subroutine re_list: not called CC=1 SCAR=6.9
Pod undocumented
326 0 my $bullet = shift;
327 0 return qr/^( # Block must start at beginning
328 # Capture everything in $1
329 ^$bullet+\ .*\n # Line starting with one or more bullet
330 (?:[\*\-\+\#]+\ .*\n)* # Lines starting with '*' or '#'
331 )(?:\s*\n)?/x, # Eat trailing lines
332 }
333  
334 1 1;
↑ Summary Kwim.pm »