Knit/PodParser.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 100% 20 / 20 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 95.4% 21 / 22 Podpod 0.0% 0 / 4 totaltotal 95.3% 123 / 129 CC 14 SCAR 12.2
CC
14
Cov
99%
parse_pod
9.0
__ANON__
2.0
podname
2.0
CRAP 3.4
« Knit/Plugin/HeadsToSections.pm ↑ Summary Knit/Document/Mojo.pm »
1 package Pod::Knit::PodParser;
2 14 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: parse POD into an XML documen
4 14 $Pod::Knit::PodParser::VERSION = '0.0.1';
5  
6 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 14 ▶use Pod::Simple::PullParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use XML::Writer::Simpler;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use List::AllUtils qw/ first_index /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 14 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 ▶use experimental 'smartmatch', 'signatures', 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 14 extends 'Pod::Simple::PullParser';
18  
19 has levels => (
20 traits => [ 'Array' ],
21 handles => { all_levels => 'elements' },
22 is => 'ro',
23 lazy => 1,
24 5 ▶ default => sub { [
Subroutine __ANON__: called CC=1 SCAR=0.0
25 [],
26 [ qw/ B L / ],
27 [qw/ Para Verbatim /],
28 [qw/ item-text /],
29 [qw/ over-text /],
30 5 ( map { ["head$_"] } reverse 1..4),
31 [ 'Document' ],
32 ]
33 },
34 14 );
35  
36 has commands => (
37 is => 'ro',
38 lazy => 1,
39 13 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 +{
41 'Document' => { },
42 'item-text' => { section => 1, },
43 'verbatim' => { section => 0, },
44 13 map {( "head$_" => { section => 1, } )} 1..4,
45 }
46 },
47 14 );
48  
49 has xml => (
50 is => 'ro',
51 lazy => 1,
52 13 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 13 XML::Writer::Simpler->new( OUTPUT => 'self' );
54 },
55 14 clearer => 'clear_xml',
56 );
57  
58 has to_xml => (
59 is => 'ro',
60 lazy => 1,
61 0 ▶ default => sub($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 $self->run;
63 },
64 14 );
65  
66 13 ▶sub parse($self,$source) {
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
67 13 $self->set_source( \$source );
68 3 ▶ $self->strip_verbatim_indent(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 3 my $lines = shift;
70 3 (my $indent = $lines->[0]) =~ s/\S.*//;
71 3 return $indent;
72 13 });
73  
74 13 $self->parse_pod;
75 13 my $xml = $self->xml->to_string;
76 13 $self->clear_xml;
77 13 return $xml;
78 }
79  
80 36 ▶sub podname ( $self, $token ) {
Subroutine podname: called CC=2 SCAR=6.9
Pod undocumented
81 36 ▶ $self->commands->{$token}{alias} || $token;
Branch: 1 branch
BranchTrueFalseTotal
$self->commands->{$token}{"alias"} or $token 1 35 36
82 }
83  
84 36 ▶sub node_level($self,$token) {
Subroutine node_level: called CC=1 SCAR=0.0
Pod undocumented
85 36 $token = $self->podname($token);
86 36 ▶ first_index { $token ~~ @$_ } $self->all_levels;
Subroutine __ANON__: called CC=1 SCAR=0.0
87 }
88  
89 77 ▶sub parse_pod($self, $end_cond = undef ) {
Subroutine parse_pod: called CC=9 SCAR=22.0
Pod undocumented
90 77 ▶ while( my $token = $self->get_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->get_token) 193 25 218
91 193 ▶ if( $end_cond and $end_cond->($token) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_cond and &$end_cond($token)) 52 141 193
Condition100%3/3
$end_cond and &$end_cond($token)
!ll&&!rl&&r
2112052
MC/DC100%2/2
$end_cond and &$end_cond($token)
$end_cond &$end_cond($token)
Truth table100%3/3
A and B
A= $end_condB= &$end_cond($token)
ABresult
0X0
100
111
92 52 $self->unget_token($token);
93 52 return;
94 }
95  
96 141 ▶ if( $token->is_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is_text) 41 100 141
97 41 $self->xml->characters( $token->text );
98 41 next;
99 }
100  
101 100 my $tag = $token->tagname;
102
103 100 ▶ next if $token->is_end;
Branch: 1 branch
BranchTrueFalseTotal
if $token->is_end 50 50 100
104  
105 50 my $normalized = $tag;
106 50 ▶ if( my $alias = $self->commands->{$tag}{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alias = $self->commands->{$tag}{"alias"}) 3 47 50
107 3 $normalized = $alias;
108 3 $token->attr( class => $tag );
109 }
110  
111 $self->xml->tag( $self->commands->{$normalized}{section} ? 'section' : $normalized, [
112 140 map { s/~//gr } $token->attr_hash->%*
113 50 ▶ ], sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
114 50 ▶ if( $self->commands->{$normalized}{section} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->commands->{$normalized}{'section'}) { } 14 36 50
115  
116 14 ▶ $self->xml->tag( $normalized, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 28 ▶ $self->parse_pod( sub($tag) {
Subroutine __ANON__: called CC=2 SCAR=6.9
118 28 ▶ $tag->is_end and $tag->is_tag( $token->tagname)
Branch: 1 branch
BranchTrueFalseTotal
$tag->is_end and $tag->is_tag($token->tagname) 14 14 28
119 14 });
120 14 });
121  
122 14 my $level = $self->node_level($normalized);
123  
124 55 ▶ $self->parse_pod( sub($tag) {
Subroutine __ANON__: called CC=2 SCAR=6.9
125 55 ▶ return $tag->is_start && $level <= $self->node_level( $tag->tagname )
Condition100%3/3
$tag->is_start && $level <= $self->node_level($tag->tagname)
!ll&&!rl&&r
33148
MC/DC100%2/2
$tag->is_start && $level <= $self->node_level($tag->tagname)
$tag->is_start $level <= $self->node_level($tag->tagname)
Truth table100%3/3
A && B
A= $tag->is_startB= $level <= $self->node_level($tag->tagname)
ABresult
0X0
100
111
126 14 });
127 }
128 else {
129 89 ▶ $self->parse_pod( sub($tag) { $tag->is_end and $tag->is_tag(
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$tag->is_end and $tag->is_tag($token->tagname) 39 50 89
130 $token->tagname
131 36 ) } );
132 }
133 50 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$self->commands->{$normalized}{'section'} ? : 14 36 50
134 }
135 }
136  
137 14 1;
138  
139 __END__
« Knit/Plugin/HeadsToSections.pm ↑ Summary Knit/Document/Mojo.pm »