Socialtext/Parser.pm

filter: Statementstmt 100% 69 / 69 Branchbran 50.0% 2 / 4 Conditioncond 57.1% 8 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 3 totaltotal 83.3% 100 / 120 CC 8 SCAR 4.6
CC
8
Cov
91%
__ANON__
3.0
re_huggy
2.1
__ANON__
2.0
CRAP 1.6
↑ Summary Socialtext.pm »
1 ##
2 # name: WikiText::Socialtext::Parser
3 # abstract: Socialtext WikiText Parser Module
4 # author: Ingy döt Net <ingy@cpan.org>
5 # license: perl
6 # copyright: 2008, 2010, 2011
7  
8 package WikiText::Socialtext::Parser;
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use base 'WikiText::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Reusable regexp generators used by the grammar
14 4 my $ALPHANUM = '\p{Letter}\p{Number}\pM';
15  
16 # These are all stolen from URI.pm
17 4 my $reserved = q{;/?:@&=+$,[]#};
18 4 my $mark = q{-_.!~*'()};
19 4 my $unreserved = "A-Za-z0-9\Q$mark\E";
20 4 my $uric = quotemeta($reserved) . $unreserved . "%";
21 4 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 4 my $im_re = join '|', keys %im_types;
32  
33 26 ▶sub create_grammar {
Subroutine create_grammar: called CC=1 SCAR=0.0
Pod undocumented
34 26 my $all_phrases = [
35 qw(waflphrase asis wikilink a im mail tt b i del)
36 ];
37 26 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 2 ▶ filter => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 2 my $node = shift;
61 2 $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 27 ▶ filter => sub { chomp },
Subroutine __ANON__: called CC=1 SCAR=0.0
87 },
88  
89 else => {
90 match => qr/^(.*)\n/,
91 phrases => [],
92 2 ▶ filter => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 2 my $node = shift;
94 2 $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 4 ▶ filter => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
106 4 s/^>\ ?//gm;
107 },
108 },
109  
110 waflparagraph => {
111 match => qr/^\{(.*)\}[\ \t]*\n(?:\s*\n)?/,
112 1 ▶ filter => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
113 1 my $node = shift;
114 1 my ($function, $options) = split /[: ]/, $node->{text}, 2;
115 1 ▶ my $replacement = defined $1 ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 1 1
116 1 ▶ $options = '' unless defined $options; # protect against an undefined here
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 0 1 1
117 1 $options =~ s/\s*(.*?)\s*/$replacement/;
118 1 $node->{attributes}{function} = $function;
119 1 $node->{attributes}{options} = $options;
120 1 undef $_;
121 },
122 },
123  
124 hx => {
125 match => qr/^(\^+) *(.*?)(\s+=+)?\s*?\n+/,
126 phrases => $all_phrases,
127 5 ▶ filter => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
128 5 my $node = shift;
129 5 $node->{type} = 'h' . length($node->{1});
130 5 $_ = $node->{text} = $node->{2};
131 },
132 },
133  
134 ul => {
135 match => re_list('[\*\-\+]'),
136 blocks => [qw(ul ol subl li)],
137 4 ▶ filter => sub { s/^[\*\-\+\#] *//mg },
Subroutine __ANON__: called CC=1 SCAR=0.0
138 },
139  
140 ol => {
141 match => re_list('\#'),
142 blocks => [qw(ul ol subl li)],
143 1 ▶ filter => sub { s/^[\*\#] *//mg },
Subroutine __ANON__: called CC=1 SCAR=0.0
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 6 ▶ filter => sub { s/\s+\z// },
Subroutine __ANON__: called CC=1 SCAR=0.0
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 2 ▶ filter => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
205 2 my $node = shift;
206 2 $node->{attributes}{target} = $node->{2};
207 2 ▶ $_ = $node->{1} || $node->{2};
Condition66%2/3
$node->{'1'} || $node->{'2'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$node->{'1'} || $node->{'2'}
$node->{'1'} $node->{'2'}
Truth table66%2/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 => WikiText::Socialtext::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 1 ▶ filter => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
233 1 my $node = shift;
234 1 my ($type, $id) = split /:/, $node->{text}, 2;
235 1 $node->{attributes}{type} = $type;
236 1 $node->{attributes}{id} = $id;
237 1 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 1 ▶ filter => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
254 1 my $node = shift;
255 1 my ($label, $function, $options) = @{$node}{qw(1 2 3)};
256 1 ▶ $label ||= '';
Condition50%1/2
$label ||= ""
l!l
10
MC/DC0%0/1
$label ||= ""
$label
Truth table50%1/2
A
A= $label
Aresult
11
00
257 1 $node->{attributes}{function} = $function;
258 1 $node->{attributes}{options} = $options;
259 1 $_ = $label;
260 },
261 },
262  
263 mail => {
264 match => qr/
265 (?:"([^"]*)"\s*)?
266 <?
267 (?:mailto:)?
268 ([\w+%\-\.]+@(?:[\w\-]+\.)+[\w\-]+)
269 >?
270 /x,
271 3 ▶ filter => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
272 3 my $node = shift;
273 3 ▶ $_ = $node->{1} || $node->{2};
Condition66%2/3
$node->{'1'} || $node->{'2'}
l!l&&r!l&&!r
120
MC/DC0%0/2
$node->{'1'} || $node->{'2'}
$node->{'1'} $node->{'2'}
Truth table66%2/3
A || B
A= $node->{'1'}B= $node->{'2'}
ABresult
1X1
011
000
274 3 $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 5 ▶ filter => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
292 5 my $node = shift;
293 5 ▶ $_ = $node->{1} || $node->{2};
Condition66%2/3
$node->{'1'} || $node->{'2'}
l!l&&r!l&&!r
230
MC/DC0%0/2
$node->{'1'} || $node->{'2'}
$node->{'1'} $node->{'2'}
Truth table66%2/3
A || B
A= $node->{'1'}B= $node->{'2'}
ABresult
1X1
011
000
294 5 $node->{attributes}{target} = $node->{2};
295 },
296 },
297  
298 asis => {
299 match => qr/
300 \{\{
301 (.*?)
302 \}\}(\}*)
303 /xs,
304 3 ▶ filter => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
305 3 my $node = shift;
306 3 $node->{type} = '';
307 3 $_ = $node->{1} . $node->{2};
308 },
309 },
310  
311 26 };
312 }
313  
314 104 ▶sub re_huggy {
Subroutine re_huggy: called CC=2 SCAR=7.6
Pod undocumented
315 104 my $brace1 = shift;
316 104 ▶ my $brace2 = shift || $brace1;
Condition33%1/3
shift() || $brace1
l!l&&r!l&&!r
01040
MC/DC0%0/2
shift() || $brace1
shift() $brace1
Truth table33%1/3
A || B
A= shift()B= $brace1
ABresult
1X1
011
000
317  
318 104 qr/
319 (?:^|(?<=[^{$ALPHANUM}$brace1]))$brace1(?=\S)(?!$brace2)
320 (.*?)
321 $brace2(?=[^{$ALPHANUM}$brace2]|\z)
322 /x;
323 }
324  
325 52 ▶sub re_list {
Subroutine re_list: called CC=1 SCAR=0.0
Pod undocumented
326 52 my $bullet = shift;
327 52 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 4 1;
↑ Summary Socialtext.pm »