File Coverage

blib/lib/Swim/Tree.pm
Criterion Covered Total %
statement 20 132 15.1
branch 2 22 9.0
condition 2 8 25.0
subroutine 5 31 16.1
pod 0 30 0.0
total 29 223 13.0


line stmt bran cond sub pod time code
1             package Swim::Tree;
2 2     2   750 use Pegex::Base;
  2         3  
  2         8  
3             extends 'Pegex::Tree';
4              
5             has meta => {};
6              
7             sub got_block_func {
8 0     0 0 0 my ($self, $content) = @_;
9 0         0 return {bfunc => $content};
10             }
11              
12             sub got_block_blank {
13 0     0 0 0 my ($self, $text) = @_;
14 0         0 $self->add('blank');
15             }
16              
17             sub got_block_comment {
18 0     0 0 0 my ($self, $text) = @_;
19 0         0 $self->add(comment => $text);
20             }
21              
22             sub got_line_comment {
23 0     0 0 0 my ($self, $text) = @_;
24 0         0 $self->add(comment => $text);
25             }
26              
27             sub got_block_rule {
28 0     0 0 0 my ($self, $text) = @_;
29 0         0 $self->add(rule => '');
30             }
31              
32             sub got_block_meta {
33 0     0 0 0 my ($self, $text) = @_;
34 0         0 require Swim::Util;
35 0         0 $self->{meta} = Swim::Util->merge_meta($self->meta, $text);
36 0         0 return;
37             }
38              
39             sub got_block_head {
40 0     0 0 0 my ($self, $got) = @_;
41 0         0 my $marker = shift @$got;
42 0         0 my ($text) = grep defined, @$got;
43 0         0 chomp $text;
44 0         0 my $level = length $marker;
45 0         0 $self->add_parse("head$level" => $text);
46             }
47              
48             sub got_block_pref {
49 0     0 0 0 my ($self, $text) = @_;
50 0         0 chomp $text;
51 0         0 $text =~ s/^ //gm;
52 0         0 $self->add("pref" => $text);
53             }
54              
55             sub got_block_list_bullet {
56 0     0 0 0 my ($self, $text) = @_;
57 0         0 my @items = map {s/^ //gm; $_} split /^\*\ /m, $text;
  0         0  
  0         0  
58 0         0 shift @items;
59             my $items = [
60             map {
61 0         0 my $item = $self->add_parse(item => $_, 'block-list-item');
  0         0  
62 0 0       0 if ($item->{item}[0]{para}) {
63 0         0 $item->{item}[0] = $item->{item}[0]{para};
64             }
65 0         0 $item;
66             } @items
67             ];
68 0         0 return { list => $items };
69             }
70              
71             sub got_block_list_number {
72 0     0 0 0 my ($self, $text) = @_;
73 0         0 my @items = map {s/^ //gm; $_} split /^\+\ /m, $text;
  0         0  
  0         0  
74 0         0 shift @items;
75             my $items = [
76             map {
77 0         0 my $item = $self->add_parse(oitem => $_, 'block-list-item');
  0         0  
78 0 0       0 if ($item->{oitem}[0]{para}) {
79 0         0 $item->{oitem}[0] = $item->{oitem}[0]{para};
80             }
81 0         0 $item;
82             } @items
83             ];
84 0         0 return { olist => $items };
85             }
86              
87             sub got_block_list_data {
88 0     0 0 0 my ($self, $text) = @_;
89 0         0 my @items = map {s/^ //gm; $_} split /^\-\ /m, $text;
  0         0  
  0         0  
90 0         0 shift @items;
91             my $items = [
92             map {
93 0         0 my ($term, $def, $rest);
  0         0  
94 0 0       0 if (s/(.*?) :: +(\S.*)\n//) {
95 0         0 ($term, $def, $rest) = ($1, $2, $_);
96 0         0 $def = $self->collapse($self->parse($def));
97             }
98             else {
99 0         0 s/(.*)\n//;
100 0         0 ($term, $def, $rest) = ($1, '', $_);
101             }
102 0         0 $term = $self->collapse($self->parse($term));
103 0         0 my $result = [$term, $def];
104 0 0       0 if (length $rest) {
105 0         0 push @$result, $self->parse($rest, 'block-list-item');
106             }
107 0         0 $result;
108             } @items
109             ];
110 0         0 return { data => $items };
111             }
112              
113             sub got_block_title {
114 0     0 0 0 my ($self, $pair) = @_;
115 0         0 my ($name, $abstract) = @$pair;
116 0 0       0 if (defined $abstract) {
117 0         0 $name = $self->collapse($self->parse($name));
118 0         0 $abstract = $self->collapse($self->parse($abstract));
119 0         0 return {title => [ $name, $abstract ]};
120             }
121             else {
122 0         0 $self->add_parse(title => $name);
123             }
124             }
125              
126             sub got_block_verse {
127 0     0 0 0 my ($self, $text) = @_;
128 0         0 $self->add_parse(verse => $text);
129             }
130              
131             sub got_block_para {
132 1     1 0 4637 my ($self, $text) = @_;
133 1         5 $self->add_parse(para => $text);
134             }
135              
136             sub got_phrase_meta {
137 0     0 0 0 my ($self, $content) = @_;
138 0         0 my $text;
139 0 0 0     0 if ($content =~ /^(\w+)$/ and defined(my $value = $self->meta->{$1})) {
140 0         0 $text = $value;
141             }
142             else {
143 0         0 $text = "<\$$content>";
144             }
145 0         0 return $text;
146 0         0 $self->add(text => $text);
147             }
148              
149             sub got_phrase_func {
150 0     0 0 0 my ($self, $content) = @_;
151 0         0 return {pfunc => $content};
152             }
153              
154             sub got_phrase_code {
155 0     0 0 0 my ($self, $content) = @_;
156 0         0 $self->add(code => $content);
157             }
158              
159             sub got_phrase_bold {
160 0     0 0 0 my ($self, $content) = @_;
161 0         0 $self->add(bold => $content);
162             }
163              
164             sub got_phrase_emph {
165 0     0 0 0 my ($self, $content) = @_;
166 0         0 $self->add(emph => $content);
167             }
168              
169             sub got_phrase_del {
170 0     0 0 0 my ($self, $content) = @_;
171 0         0 $self->add(del => $content);
172             }
173              
174             sub got_phrase_under {
175 0     0 0 0 my ($self, $content) = @_;
176 0         0 $self->add(under => $content);
177             }
178              
179             sub got_phrase_hyper_named {
180 0     0 0 0 my ($self, $content) = @_;
181 0         0 my ($text, $link) = @$content;
182 0         0 { hyper => { link => $link, text => $text } };
183             }
184              
185             sub got_phrase_hyper_explicit {
186 0     0 0 0 my ($self, $content) = @_;
187 0         0 { hyper => { link => $content, text => '' } };
188             }
189              
190             sub got_phrase_hyper_implicit {
191 0     0 0 0 my ($self, $content) = @_;
192 0         0 { hyper => { link => $content, text => '' } };
193             }
194              
195             sub got_phrase_link_named {
196 0     0 0 0 my ($self, $content) = @_;
197 0         0 my ($text, $link) = @$content;
198 0         0 { link => { link => $link, text => $text } };
199             }
200              
201             sub got_phrase_link_plain {
202 0     0 0 0 my ($self, $content) = @_;
203 0         0 { link => { link => $content, text => '' } };
204             }
205              
206             #------------------------------------------------------------------------------
207             sub add {
208 0     0 0 0 my ($self, $tag, $content) = @_;
209 0 0       0 if (ref $content) {
210 0         0 $content = $content->[0];
211 0 0       0 if (@$content == 1) {
    0          
212 0         0 $content = $content->[0]
213             }
214             elsif (@$content > 1) {
215 0         0 $content = $self->collapse($content);
216             }
217             }
218 0         0 return { $tag => $content }
219             }
220              
221             sub add_parse {
222 1     1 0 3 my ($self, $tag, $text, $start) = @_;
223 1         3 return { $tag => $self->collapse($self->parse($text, $start)) };
224             }
225              
226             sub parse {
227 1     1 0 2 my ($self, $text, $start) = @_;
228 1 50       10 if (not $start) {
229 1         4 $start = 'text-markup';
230 1         3 chomp $text;
231             }
232 1   50     5 my $debug = $self->{parser}{debug} || undef;
233 1         6 my $receiver = 'Swim::Tree'->new(meta => $self->meta);
234 1         70 my $parser = Pegex::Parser->new(
235             grammar => 'Swim::Grammar'->new(start => $start),
236             receiver => $receiver,
237             debug => $debug,
238             );
239 1         88 $parser->parse($text, $start);
240             }
241              
242             sub collapse {
243 1     1 0 4711 my ($self, $content) = @_;
244 1         5 for (my $i = 0; $i < @$content; $i++) {
245 1 50       4 next if ref $content->[$i];
246 1   33     8 while ($i + 1 < @$content and not ref $content->[$i + 1]) {
247 0         0 $content->[$i] .= splice(@$content, $i + 1, 1);
248             }
249             }
250 1         5 $content;
251             }
252              
253             1;