Qgoda/HTMLFilter/TOC.pm

filter: Statementstmt 27.4% 37 / 135 Branchbran 14.0% 9 / 64 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 5 totaltotal 24.8% 56 / 225 CC 38 SCAR 43.5
CC
38
Cov
23%
end
196.3
__deepen
56.0
start
40.8
CRAP 77.7
« Qgoda/PluginUtils.pm ↑ Summary Template/Plugin/Qgoda.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::HTMLFilter::TOC;
20 2 $Qgoda::HTMLFilter::TOC::VERSION = 'v0.9.2';
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Qgoda;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Qgoda::Util qw(html_escape slugify empty write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Locale::TextDomain qw(qgoda);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use base qw(Qgoda::Processor);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub new {
30 2 ▶ my ($class, %args) = @_;
Subroutine new: called CC=5 SCAR=17.0
Pod undocumented
31  
32 my $content_tag = exists $args{content_tag}
33 2 ▶ ? $args{content_tag} : 'qgoda-content';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'content_tag'} ? : 2 0 2
34 my $toc_tag = exists $args{toc_tag}
35 2 ▶ ? $args{toc_tag} : 'qgoda-toc';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'toc_tag'} ? : 2 0 2
36 2 ▶ my $start = exists $args{start} ? $args{start} : 2;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'start'} ? : 2 0 2
37 2 ▶ my $end = exists $args{end} ? $args{end} : 6; my $self = {
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'end'} ? : 2 0 2
38 __content_tag => $content_tag,
39 __toc_tag => $toc_tag,
40 __start => $start,
41 __end => $end,
42 __template => $args{template},
43 2 };
44  
45 2 bless $self, $class;
46 }
47  
48 sub start_document {
49 3 ▶ my ($self, $chunk, %args) = @_;
Subroutine start_document: called CC=1 SCAR=0.0
Pod undocumented
50  
51 3 delete $self->{__active};
52 3 delete $self->{__slugs};
53  
54 3 return $chunk;
55 }
56  
57 sub end_document {
58 3 ▶ my ($self, $chunk, %args) = @_;
Subroutine end_document: called CC=1 SCAR=0.0
Pod undocumented
59  
60 3 delete $self->{__active};
61  
62 3 return $chunk;
63 }
64  
65 sub start {
66 30 ▶ my ($self, $chunk, %args) = @_;
Subroutine start: called CC=8 SCAR=37.1
Pod undocumented
67  
68 30 ▶ if ($args{tagname} eq $self->{__content_tag}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'tagname'} eq $self->{'__content_tag'}) { } 0 30 30
elsif ($args{'tagname'} eq $self->{'__toc_tag'}) { } 0 30 30
69 0 $self->{__active} = 1;
70 0 $self->{__headlines} = [];
71 0 ▶ $self->{__slugs} ||= {};
Condition0%0/2
$self->{"__slugs"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"__slugs"} ||= {}
$self->{"__slugs"}
Truth table0%0/2
A
A= $self->{"__slugs"}
Aresult
11
00
72 0 $self->{__items} = [];
73 0 $self->{__path} = [0];
74  
75 0 return $chunk;
76 } elsif ($args{tagname} eq $self->{__toc_tag}) {
77 # Normalize.
78 0 return "<$self->{__toc_tag} />";
79 }
80  
81 30 ▶ return $chunk if !$self->{__active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__active"} 30 0 30
82  
83 0 ▶ return $chunk if $args{tagname} !~ /^h([[1-9][0-9]*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"tagname"} =~ /^h([[1-9][0-9]*)$/ 0 0 0
84 0 my $level = $1;
85 0 ▶ return $chunk if $level < $self->{__start};
Branch: 1 branch
BranchTrueFalseTotal
if $level < $self->{"__start"} 0 0 0
86 0 ▶ return $chunk if $level > $self->{__end};
Branch: 1 branch
BranchTrueFalseTotal
if $level > $self->{"__end"} 0 0 0
87  
88 0 $chunk .= '<qgoda-toc-marker />';
89  
90 0 return $chunk;
91 }
92  
93 sub end {
94 27 ▶ my ($self, $chunk, %args) = @_;
Subroutine end: called CC=15 SCAR=52.8
Pod undocumented
95  
96 27 ▶ if ($args{tagname} eq $self->{__content_tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"tagname"} eq $self->{"__content_tag"}) 0 27 27
97 0 ▶ return '' if !$self->{__active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__active"} 0 0 0
98 0 $self->{__active} = 0;
99  
100 0 ▶ return '' if ${$args{output}} !~ m{<$self->{__toc_tag} />};
Branch: 1 branch
BranchTrueFalseTotal
unless ${$args{"output"};} =~ m[<$self->{"__toc_tag"} />] 0 0 0
101  
102 0 my $toc = $self->__generateTOC($args{asset});
103 0 ${$args{output}} =~ s{<$self->{__toc_tag} />}{$toc}g;
104  
105 0 return '';
106 }
107  
108 27 ▶ return $chunk if !$self->{__active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__active"} 27 0 27
109  
110 0 ▶ return $chunk if $args{tagname} !~ /^h([[1-9][0-9]*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"tagname"} =~ /^h([[1-9][0-9]*)$/ 0 0 0
111 0 my $hlevel = $1;
112 0 ▶ return $chunk if $hlevel < $self->{__start};
Branch: 1 branch
BranchTrueFalseTotal
if $hlevel < $self->{"__start"} 0 0 0
113 0 ▶ return $chunk if $hlevel > $self->{__end};
Branch: 1 branch
BranchTrueFalseTotal
if $hlevel > $self->{"__end"} 0 0 0
114  
115 0 ▶ return $chunk if ${$args{output}} !~ s{<qgoda-toc-marker />(.*)}{}s;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$args{"output"};} =~ s[<qgoda-toc-marker />(.*)][]s 0 0 0
116  
117 0 my $text = $1;
118 0 my $level = $hlevel - $self->{__start} + 1;
119 0 my $depth = @{$self->{__path}};
120  
121 0 my $valid = 1;
122 0 ▶ if ($depth > $level) {
Branch: 3 branches
BranchTrueFalseTotal
if ($depth > $level) { } 0 0 0
elsif ($depth + 1 == $level) { } 0 0 0
elsif ($depth == $level) { } 0 0 0
123 0 foreach ($level .. $depth - 1) {
124 0 pop @{$self->{__path}};
125 }
126 0 ++$self->{__path}->[-1];
127 } elsif ($depth + 1 == $level) {
128 0 push @{$self->{__path}}, 1;
129 } elsif ($depth == $level) {
130 0 ++$self->{__path}->[-1];
131 } else {
132 0 undef $valid;
133 }
134  
135 0 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) 0 0 0
136 0 my $slug = $text;
137 0 $slug =~ s{<.*?>}{}s;
138 0 $slug = html_escape slugify $slug;
139 0 ▶ while ($self->{__slugs}->{$slug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__slugs"}{$slug}) 0 0 0
140 0 $slug .= '-';
141 }
142 0 $self->{__slugs}->{$slug} = 1;
143
144 0 ${$args{output}} .= qq{<a href="#" name="$slug" id="$slug"></a>};
145 0 push @{$self->{__items}}, {
146 slug => $slug,
147 0 path => [@{$self->{__path}}],
148 text => $text
149 };
150 }
151  
152 0 ${$args{output}} .= $text;
153  
154 0 return $chunk;
155 }
156  
157 sub __deepen {
158 0 ▶ my ($self, $items) = @_;
Subroutine __deepen: not called CC=7 SCAR=40.3
159  
160 0 ▶ return [] unless $items && @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless $items and @$items 0 0 0
Condition0%0/3
$items and @$items
!ll&&!rl&&r
000
MC/DC0%0/2
$items and @$items
$items @$items
Truth table0%0/3
A and B
A= $itemsB= @$items
ABresult
0X0
100
111
161  
162 0 my $root = {
163 children => [],
164 };
165  
166 0 foreach my $item (@$items) {
167 0 my @path = @{$item->{path}};
168 0 $item->{children} = [];
169 0 my $cursor = $root->{children};
170 0 ▶ for (my $i = 0; $i < $#path; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#path) 0 0 0
171 0 $cursor = $cursor->[$path[$i] - 1]->{children};
172 }
173 0 $cursor->[$path[-1] - 1] = $item;
174 }
175  
176 0 foreach my $item (@$items) {
177 0 ▶ delete $item->{children} if !@{$item->{children}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$item->{"children"};} 0 0 0
178 }
179  
180 0 return $root->{children};
181 }
182  
183 sub __generateTOC {
184 0 ▶ my ($self, $asset) = @_;
Subroutine __generateTOC: not called CC=3 SCAR=24.8
185  
186 0 my $root = $self->__deepen($self->{__items});
187  
188 0 ▶ return '' if !@$root;
Branch: 1 branch
BranchTrueFalseTotal
unless @$root 0 0 0
189  
190 0 my $template = $self->{__template};
191 0 ▶ if (empty $template) {
Branch: 1 branch
BranchTrueFalseTotal
if (&empty($template)) 0 0 0
192 0 $template = $self->__generateDefaultTemplate;
193 }
194  
195 0 my $qgoda = Qgoda->new;
196 0 my $processor = $qgoda->getProcessor('Qgoda::Processor::TT2');
197 0 my $content = qq{[% INCLUDE $template %]};
198 0 $asset->{toc} = $root;
199  
200 0 return $processor->process($content, $asset);
201 }
202  
203 sub __generateDefaultTemplate {
204 0 ▶ my ($self) = @_;
Subroutine __generateDefaultTemplate: not called CC=5 SCAR=34.0
205  
206 0 my $config = Qgoda->new->config;
207  
208 0 my $template = 'components/toc.html';
209 my $template_file = File::Spec->catfile($config->{paths}->{views},
210 0 $template);
211 0 ▶ return $template if -e $template_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $template_file 0 0 0
212 my $level_template_file = File::Spec->catfile($config->{paths}->{views},
213 0 'components/toc/level.html');
214
215 0 my $logger = Qgoda->new->logger;
216 0 $logger->warning(__x("writing default template '{template}'",
217 template => $level_template_file));
218 0 my ($mlcode, $code, $level_code) = split "===\n", join '', <DATA>;
219 0 ▶ $code = $mlcode if $config->{po}->{textdomain};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"po"}{"textdomain"} 0 0 0
220 0 ▶ if (!write_file $level_template_file, $level_code) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&write_file($level_template_file, $level_code)) 0 0 0
221 0 die __x("error writing template file '{template}': {error}\n",
222 template => $level_template_file, error => $!);
223 }
224 0 $logger->warning(__x("writing default template '{template}'",
225 template => $template_file));
226 0 ▶ if (!write_file $template_file, $code) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&write_file($template_file, $code)) 0 0 0
227 0 die __x("error writing template file '{template}': {error}\n",
228 template => $template_file, error => $!);
229 }
230  
231 0 return $template;
232 }
233  
234 2 1;
235  
236 __DATA__
237 [% USE gtx = Gettext(config.po.textdomain, asset.lingua) %]
238 [% IF asset.toc.size %]
239 <div class="toc">
240 <div class="toc-title">[% gtx.gettext('Table Of Contents') %]</div>
241 [% INCLUDE components/toc/level.html
242 items = asset.toc
243 depth = 1 %]
244 </div>
245 [% END %]
246 ===
247 [% IF asset.toc.size %]
248 <div class="toc">
249 <div class="toc-title">Table Of Contents</div>
250 [% INCLUDE components/toc/level.html
251 items = asset.toc
252 depth = 1 %]
253 </div>
254 [% END %]
255 ===
256 <ul class="toclevel-[% depth %]">
257 [% FOREACH item IN items %]
258 <li class="toclevel-[% depth %]">
259 [% item.path.join('.') %]
260 <a href="#[% item.slug %]">[% item.text %]</a>
261 [% IF item.children %]
262 [% INCLUDE components/toc/level.html
263 items = item.children
264 depth = depth + 1 %]
265 [% END %]
266 </li>
267 [% END %]
268 </ul>
« Qgoda/PluginUtils.pm ↑ Summary Template/Plugin/Qgoda.pm »