| 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
| |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | $in_fh = IO::File->new( $params{input}, '<' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 91 | 3 | ▶ | $out_fh = IO::File->new( $params{output}, '>' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | ▶ | $in_fh ->close or carp "Can't close input file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 3 | ▶ | $out_fh->close or carp "Can't close output file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 160 | chomp; | |||||||||||||||||||||||||||
| 146 | 160 | my $rawline = $_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 160 | s/^[ ]*#//; | |||||||||||||||||||||||||||
| 149 | 160 | ▶ | next if not $_ and $self->{skipblanks}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $_ and $self->{"skipblanks"}
MC/DC0%0/2
not $_ and $self->{"skipblanks"}
not $_
$self->{"skipblanks"}
Truth table66%2/3
not(A) and B
A= not $_B= $self->{"skipblanks"}
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 160 | ▶ | $inrobodoc = 0 if /$RD_END_MARK/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 160 | ▶ | if ( $inrobodoc and /$RD_TAG_REGEX/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$inrobodoc and /$RD_TAG_REGEX/
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/
| |||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 36 | $tag = $1; | |||||||||||||||||||||||||||
| 156 | 36 | $insource = $tag eq 'SOURCE'; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 36 | ▶ | next if $insource and not $self->{keepsource}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$insource and not $self->{"keepsource"}
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"}
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 36 | push @{ $parsed[ -1 ]{ tags } }, { tag => $tag, text => [] }; | |||||||||||||||||||||||||||
| 161 | 36 | next; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 124 | ▶ | if ( $inrobodoc and $tag ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$inrobodoc and $tag
MC/DC50%1/2
$inrobodoc and $tag
$inrobodoc
$tag
Truth table66%2/3
A and B
A= $inrobodocB= $tag
| |||||||||||||||||||||||||||||
| 165 | { | ||||||||||||||||||||||||||||
| 166 | 104 | ▶ | next if $insource and not $self->{keepsource}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$insource and not $self->{"keepsource"}
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"}
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 104 | ▶ | push @{ $parsed[ -1 ]{ tags }->[ -1 ]->{ text } }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | ( $insource ? $rawline : $_ ); | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 124 | ▶ | if ( /$RD_BEGIN_MARK/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||