File Coverage

blib/lib/WikiText/Sample/Parser.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition 1 3 33.3
subroutine 6 6 100.0
pod 0 3 0.0
total 21 26 80.7


line stmt bran cond sub pod time code
1             package WikiText::Sample::Parser;
2 5     5   4119 use base 'WikiText::Parser';
  5         10  
  5         1090  
3              
4             sub create_grammar {
5 18     18 0 65 my $all_blocks = [ 'h1', 'h2', 'h3', 'hr', 'p', 'pre' ];
6              
7 18         43 my $all_phrases = [ 'b', 'i' ];
8              
9             return {
10             # Parsing starts at the "top" level document
11             top => {
12             blocks => $all_blocks, # A document consists of top level blocks
13             },
14             p => {
15             match => qr/^ # Blocks must start at beginning
16             ( # Capture paragraph in $1
17             ((?!(?: # Stop at certain blocks
18             [\=] | # Headings
19             \s+\S
20             ))
21             .*\S.*\n)+ # Otherwise, collect non-empty lines
22             )
23             (?:\s*\n)? # Eat trailing newlines
24             /x,
25             phrases => $all_phrases,
26 18     18   54 filter => sub { chomp },
27             },
28             pre => {
29             match => qr/^
30             (
31             ((?!(?: # Stop at certain blocks
32             \S # Anything starting with nonspace
33             ))
34             (?m: ^\ +.*\S.*\n))+ # otherwise grab lines starting with space
35             )
36             (\s*\n)* # and all blank lines after
37             /x,
38 3     3   19 filter => sub { s/^\s*//mg; s/\s*$//mg; },
  3         26  
39             },
40 18         478 h1 => {
41             match => re_header(1),
42             },
43             h2 => {
44             match => re_header(2),
45             },
46             h3 => {
47             match => re_header(3),
48             },
49             hr => {
50             match => qr/^----\n(?:\s*\n)?/,
51             },
52             b => {
53             phrases => $all_phrases,
54             match => phrase("'''"),
55             },
56             i => {
57             phrases => $all_phrases,
58             match => phrase("''"),
59             },
60             };
61             }
62              
63             # Reusable regexp generators used by the grammar
64             sub phrase {
65 36     36 0 59 my $brace1 = shift;
66 36   33     143 my $brace2 = shift || $brace1;
67              
68 36         920 return qr/
69             ${brace1} # Opening phrase markup
70             (.*?'*) # Capture content in $1
71             ${brace2} # Closing phrase markup
72             /x;
73             }
74              
75             sub re_header {
76 54     54 0 82 my $level = shift;
77 54         1446 return qr/^ # Block must begin at position 0
78             \={$level} # Proper number of '=' chars
79             \ + # 1 or more spaces
80             (.*?) # Capture header content in $1
81             \s*\n # Eat trailing whitespace and newlines
82             /x;
83             }
84              
85             1;