blib/lib/Pod/ROBODoc.pm

filter: Statementstmt 99.0% 108 / 109 Branchbran 77.7% 28 / 36 Conditioncond 60.0% 9 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 88.1% 163 / 185 CC 28 SCAR 22.0
CC
28
Cov
91%
_parse_robodoc
16.0
filter
8.1
_write_pod
6.0
CRAP 9.0
↑ Summary
1 package Pod::ROBODoc;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = '0.3';
7  
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 my $EMPTY = q{};
14 8 my $RD_BEGIN_MARK = qr{
15 ^ [*]{4} # starts with 4 asterisks
16 i? # optional "internal" flag
17 ([a-z*]) # header type
18 [*] # ends with an asterisk
19 [ ]* # optional trailing whitespace
20 (.*) # optional item name
21 }ox;
22 8 my $RD_END_MARK = qr{ \A [*]{4} \s* \z }ox;
23 8 my %RD_HEADERS = (
24 c => 'Class',
25 d => 'Constant',
26 f => 'Function',
27 h => 'Module',
28 m => 'Method',
29 s => 'Structure',
30 t => 'Type',
31 u => 'Unit Test',
32 v => 'Variable',
33 q{*} => $EMPTY,
34 );
35  
36 8 my @RD_TAGS = (
37 'NAME', 'COPYRIGHT', 'SYNOPSIS', 'USAGE',
38 'FUNCTION', 'DESCRIPTION', 'PURPOSE', 'AUTHOR',
39 'CREATION DATE', 'MODIFICATION HISTORY', 'INPUTS',
40 'ARGUMENTS', 'OPTIONS', 'PARAMETERS', 'SWITCHES',
41 'OUTPUT', 'SIDE EFFECTS', 'RESULT', 'RETURN VALUE',
42 'EXAMPLE', 'NOTES', 'DIAGNOSTICS', 'WARNINGS',
43 'ERRORS', 'BUGS', 'TODO', 'IDEAS',
44 'PORTABILITY', 'SEE ALSO', 'METHODS', 'NEW METHODS',
45 'ATTRIBUTES', 'NEW ATTRIBUTES', 'TAGS', 'COMMANDS',
46 'DERIVED FROM', 'DERIVED BY', 'USES', 'CHILDREN',
47 'USED BY', 'PARENTS', 'SOURCE', 'LICENSE',
48 );
49 8 my $RD_TAG_STRING;
50 my $RD_TAG_REGEX;
51  
52 sub new
53 8 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
54 8 my $class = shift;
55 8 my %params = validate( @_, {
56 keepsource => { default => 0 },
57 skipblanks => { default => 0 },
58 customtags => { default => [], type => ARRAYREF },
59 });
60  
61 8 my $self = bless { %params }, $class;
62  
63 8 $self->_load_custom_tags();
64  
65 8 return $self;
66 }
67  
68 sub filter
69 7 ▶{
Subroutine filter: called CC=8 SCAR=20.9
Pod documented
70 7 my $self = shift;
71 7 my %params = validate( @_, {
72 input => { default => undef, type => UNDEF | SCALAR },
73 output => { default => undef, type => UNDEF | SCALAR },
74 });
75  
76 ## Setup input file handle
77 5 my $in_fh;
78  
79 5 ▶ if ( $params{input} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'input'}) { } 4 1 5
80 4 ▶ $in_fh = IO::File->new( $params{input}, '<' )
Branch: 1 branch
BranchTrueFalseTotal
$in_fh = "IO::File"->new($params{"input"}, "<") or croak(qq[Can't open input file '$params{"input"}': $!]) 3 1 4
81 or croak "Can't open input file '$params{input}': $!";
82 }
83 else {
84 1 $in_fh = \*STDIN;
85 }
86  
87 ## Setup output file handle
88 4 my $out_fh;
89  
90 4 ▶ if ( $params{output} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'output'}) { } 3 1 4
91 3 ▶ $out_fh = IO::File->new( $params{output}, '>' )
Branch: 1 branch
BranchTrueFalseTotal
$out_fh = "IO::File"->new($params{"output"}, ">") or croak(qq[Can't open output file '$params{"output"}': $!]) 2 1 3
92 or croak "Can't open output file '$params{output}': $!";
93 }
94 else {
95 1 $out_fh = \*STDOUT;
96 }
97  
98 3 $self->_parse_robodoc( $in_fh );
99 3 ▶ $self->_write_pod ( $out_fh ) if @{ $self->{_parsed} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_parsed"};} 3 0 3
100  
101 3 ▶ $in_fh ->close or carp "Can't close input file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$in_fh->close or carp("Can't close input file: $!") 3 0 3
102 3 ▶ $out_fh->close or carp "Can't close output file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$out_fh->close or carp("Can't close output file: $!") 3 0 3
103  
104 3 return;
105 }
106  
107 sub convert
108 3 ▶{
Subroutine convert: called CC=2 SCAR=6.9
Pod documented
109 3 my $self = shift;
110 3 my ( $rd ) = validate_pos( @_, { type => SCALAR } );
111  
112 2 my $in_fh = IO::String->new( $rd );
113 2 my $out_fh = IO::String->new();
114  
115 2 $self->_parse_robodoc( $in_fh );
116 2 ▶ $self->_write_pod ( $out_fh ) if @{ $self->{_parsed} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_parsed"};} 1 1 2
117  
118 2 return ${ $out_fh->string_ref() };
119 }
120  
121 sub _load_custom_tags
122 8 ▶{
Subroutine _load_custom_tags: called CC=1 SCAR=0.0
123 8 my ( $self ) = @_;
124  
125 8 push @RD_TAGS, grep { / [A-Z0-9\s_-]+ /x } @{ $self->{customtags} };
126  
127 8 $RD_TAG_STRING = join q{|}, @RD_TAGS;
128 8 $RD_TAG_REGEX = qr{\A[ ]*($RD_TAG_STRING)[ ]*\z}o;
129  
130 8 return;
131 }
132  
133 sub _parse_robodoc
134 5 ▶{
Subroutine _parse_robodoc: called CC=15 SCAR=27.7
135 5 my $self = shift;
136 5 my ( $fh ) = validate_pos( @_, { type => HANDLE } );
137  
138 5 my @parsed;
139 my $inrobodoc;
140 5 my $insource;
141 5 my $tag;
142  
143 5 ▶ while ( $_ = $fh->getline() )
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = $fh->getline) 160 5 165
144 {
145 160 chomp;
146 160 my $rawline = $_;
147  
148 160 s/^[ ]*#//;
149 160 ▶ next if not $_ and $self->{skipblanks};
Branch: 1 branch
BranchTrueFalseTotal
if not $_ and $self->{"skipblanks"} 0 160 160
Condition66%2/3
not $_ and $self->{"skipblanks"}
!ll&&!rl&&r
15640
MC/DC0%0/2
not $_ and $self->{"skipblanks"}
not $_ $self->{"skipblanks"}
Truth table66%2/3
not(A) and B
A= not $_B= $self->{"skipblanks"}
ABresult
0X0
100
111
150  
151 160 ▶ $inrobodoc = 0 if /$RD_END_MARK/;
Branch: 1 branch
BranchTrueFalseTotal
if /$RD_END_MARK/ 4 156 160
152  
153 160 ▶ if ( $inrobodoc and /$RD_TAG_REGEX/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($inrobodoc and /$RD_TAG_REGEX/) 36 124 160
Condition100%3/3
$inrobodoc and /$RD_TAG_REGEX/
!ll&&!rl&&r
2010436
MC/DC100%2/2
$inrobodoc and /$RD_TAG_REGEX/
$inrobodoc /$RD_TAG_REGEX/
Truth table100%3/3
A and B
A= $inrobodocB= /$RD_TAG_REGEX/
ABresult
0X0
100
111
154 {
155 36 $tag = $1;
156 36 $insource = $tag eq 'SOURCE';
157  
158 36 ▶ next if $insource and not $self->{keepsource};
Branch: 1 branch
BranchTrueFalseTotal
if $insource and not $self->{"keepsource"} 0 36 36
Condition33%1/3
$insource and not $self->{"keepsource"}
!ll&&!rl&&r
3600
MC/DC0%0/2
$insource and not $self->{"keepsource"}
$insource not $self->{"keepsource"}
Truth table33%1/3
A and not(B)
A= $insourceB= not $self->{"keepsource"}
ABresult
0X0
100
111
159  
160 36 push @{ $parsed[ -1 ]{ tags } }, { tag => $tag, text => [] };
161 36 next;
162 }
163  
164 124 ▶ if ( $inrobodoc and $tag )
Branch: 1 branch
BranchTrueFalseTotal
if ($inrobodoc and $tag) 104 20 124
Condition66%2/3
$inrobodoc and $tag
!ll&&!rl&&r
200104
MC/DC50%1/2
$inrobodoc and $tag
$inrobodoc $tag
Truth table66%2/3
A and B
A= $inrobodocB= $tag
ABresult
0X0
100
111
165 {
166 104 ▶ next if $insource and not $self->{keepsource};
Branch: 1 branch
BranchTrueFalseTotal
if $insource and not $self->{"keepsource"} 0 104 104
Condition33%1/3
$insource and not $self->{"keepsource"}
!ll&&!rl&&r
10400
MC/DC0%0/2
$insource and not $self->{"keepsource"}
$insource not $self->{"keepsource"}
Truth table33%1/3
A and not(B)
A= $insourceB= not $self->{"keepsource"}
ABresult
0X0
100
111
167  
168 104 ▶ push @{ $parsed[ -1 ]{ tags }->[ -1 ]->{ text } },
Branch: 1 branch
BranchTrueFalseTotal
$insource ? : 0 104 104
169 ( $insource ? $rawline : $_ );
170 }
171  
172 124 ▶ if ( /$RD_BEGIN_MARK/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/$RD_BEGIN_MARK/) 4 120 124
173 {
174 4 $inrobodoc = 1;
175 4 $tag = undef;
176  
177 4 push @parsed, { type => $RD_HEADERS{ $1 }, element => $2, tags => [] };
178 }
179 }
180  
181 5 $self->{_parsed} = \@parsed;
182 5 return;
183 }
184  
185 sub _write_pod
186 4 ▶{
Subroutine _write_pod: called CC=6 SCAR=18.0
187 4 my $self = shift;
188 4 my ( $fh ) = validate_pos( @_, { type => HANDLE } );
189  
190 4 my @pod;
191  
192 4 push @pod, sprintf '# Generated by %s version %s', __PACKAGE__, $VERSION;
193 4 push @pod, '=pod';
194  
195 4 for my $doc ( @{ $self->{_parsed} } )
196 {
197 4 push @pod, $EMPTY, "=head1 $doc->{type} $doc->{element}", $EMPTY;
198  
199 4 for my $tag ( @{ $doc->{tags} } )
200 {
201 36 ▶ if ( $tag->{tag} eq 'SOURCE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->{'tag'} eq 'SOURCE') { } 0 36 36
202 0 push @pod, $EMPTY, '=cut', $EMPTY;
203 }
204 else {
205 36 push @pod, $EMPTY, "=head2 $tag->{tag}", $EMPTY;
206 }
207  
208 36 push @pod, $_ for @{ $tag->{text} };
209 }
210 }
211  
212 4 push @pod, $EMPTY, '=cut', $EMPTY;
213  
214 4 print $fh "$_\n" for @pod;
215 4 return;
216 }
217  
218 8 1;
219  
220 __END__
↑ Summary