Mojito/Filter/MojoMojo/Converter.pm

filter: Statementstmt 43.1% 19 / 44 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 5 / 10 Podpod 100% 4 / 4 totaltotal 36.3% 28 / 77 CC 9 SCAR 31.9
CC
9
Cov
32%
toc
56.0
convert_content
12.0
_build_html_toc
2.0
CRAP 24.4
« Mojito/Collection/CRUD/Elasticsearch.pm ↑ Summary Mojito/Page/Git.pm »
1 3 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mojito::Filter::MojoMojo::Converter;
3 3 $Mojito::Filter::MojoMojo::Converter::VERSION = '0.25';
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use HTML::Toc;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use HTML::TocInsertor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 has content => (
10 is => 'rw',
11 required => 1,
12 );
13 3 has original_content => ( is => 'rw', );
14 3 has html_toc => (
15 is => 'ro',
16 lazy => 1,
17 builder => '_build_html_toc',
18 );
19 0 ▶sub _build_html_toc {
Subroutine _build_html_toc: not called CC=1 SCAR=6.9
20 0 HTML::Toc->new();
21 }
22  
23 =head1 Methods
24  
25 =head2 convert_content
26  
27 Run through the list of MojoMojo converters.
28 Currently it's just the <pre lang="$lang"> to <pre class="prettyprint">
29  
30 =cut
31  
32 0 ▶sub convert_content {
Subroutine convert_content: not called CC=3 SCAR=24.8
Pod documented
33 0 my ($self) = (shift);
34  
35 0 ▶ return if !$self->content;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->content 0 0 0
36 0 $self->$_ for qw/ pre_lang toc /;
37 0 return $self->content;
38 }
39  
40 =head2 pre_lang
41  
42 Turn MojoMojo <pre lang="$foo">bar</pre> into:
43 <pre class="prettyprint">bar</pre>
44  
45 =cut
46  
47 0 ▶sub pre_lang {
Subroutine pre_lang: not called CC=1 SCAR=6.9
Pod documented
48 0 my ($self) = @_;
49  
50 0 my $content = $self->content;
51 0 $content =~
52 s/<pre\s+lang=[^>]*>(.*?)<\/pre>/<pre class="prettyprint">$1<\/pre>/sig;
53 0 $self->content($content);
54 }
55  
56 =head2 toc
57  
58 Graciously pilfered from dandv's hard work on MojoMojo.
59  
60 Calls the formatter. Takes a ref to the content as well as the context object.
61 The syntax for the TOC plugin invocation is:
62  
63 {{toc M- }} # start from Header level M
64 {{toc -N }} # stop at Header level N
65 {{toc M-N }} # process only header levels M..N
66  
67 where M is the minimum heading level to include in the TOC, and N is the
68 maximum level (depth). For example, suppose you only have one H1 on the page
69 so it doesn't make sense to add it to the TOC; also, assume you and don't want
70 to include any headers smaller than H3. The {{toc}} markup to achieve that would be:
71  
72 {{toc 2-3}}
73  
74 Defaults to 1-6.
75  
76 =cut
77  
78 0 ▶sub toc {
Subroutine toc: not called CC=7 SCAR=40.3
Pod documented
79 0 my ($self) = @_;
80  
81 0 my $content = $self->content;
82 0 my $toc_params_RE = qr/\s+ (\d+)? \s* - \s* (\d+)?/x;
83 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s[ \{\{ toc (?:$toc_params_RE)? \s* \/? }} ][<div class="toc">\n<!--mojomojoTOCwillgohere-->\n</div>]xi) 0 0 0
84 # replace the {{toc ..}} markup tag and parse potential parameters
85 $content =~ s[
86 \{\{ toc (?:$toc_params_RE)? \s* \/? }}
87 ][<div class="toc">\n<!--mojomojoTOCwillgohere-->\n</div>]ix) {
88 0 my ($toc_h_min, $toc_h_max);
89 0 ▶ $toc_h_min = $1 || 1;
Condition0%0/2
$1 || 1
l!l
00
MC/DC0%0/1
$1 || 1
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
90 0 ▶ $toc_h_max = $2 || 9; # in practice, there are no more than 6 heading levels
Condition0%0/2
$2 || 9
l!l
00
MC/DC0%0/1
$2 || 9
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
91 0 ▶ $toc_h_min = 9 if $toc_h_min > 9; # prevent TocGenerator error for headings >= 10
Branch: 1 branch
BranchTrueFalseTotal
if $toc_h_min > 9 0 0 0
92 0 ▶ $toc_h_max = 9 if $toc_h_max > 9 or $toc_h_max < $toc_h_min; # {{toc 3-1}} is wrong; make it {{toc 3-9}} instead
Branch: 1 branch
BranchTrueFalseTotal
if $toc_h_max > 9 or $toc_h_max < $toc_h_min 0 0 0
Condition0%0/3
$toc_h_max > 9 or $toc_h_max < $toc_h_min
l!l&&r!l&&!r
000
MC/DC0%0/2
$toc_h_max > 9 or $toc_h_max < $toc_h_min
$toc_h_max > 9 $toc_h_max < $toc_h_min
Truth table0%0/3
A or B
A= $toc_h_max > 9B= $toc_h_max < $toc_h_min
ABresult
1X1
011
000
93  
94 0 my $toc = $self->html_toc;
95 0 my $tocInsertor = HTML::TocInsertor->new();
96  
97 0 $toc->setOptions({
98 header => '', # by default, \n<!-- Table of Contents generated by Perl - HTML::Toc -->\n
99 footer => '',
100 insertionPoint => 'replace <!--mojomojoTOCwillgohere-->',
101 doLinkToId => 0,
102 levelToToc => "[$toc_h_min-$toc_h_max]",
103 # templateAnchorName => \&assembleAnchorName,
104 });
105  
106 # http://search.cpan.org/dist/HTML-Toc/Toc.pod#HTML::TocInsertor::insert()
107 0 $tocInsertor->insert($toc, $content, {output => \$content});
108 0 $self->content($content);
109 }
110 }
111  
112 =head2 BUILD
113  
114 Store original content
115  
116 =cut
117  
118 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod documented
119 0 my ($self) = (shift);
120 0 $self->original_content( $self->content );
121 }
122  
123 3 1
« Mojito/Collection/CRUD/Elasticsearch.pm ↑ Summary Mojito/Page/Git.pm »