blib/lib/Pod/Simple/Pandoc.pm

filter: Statementstmt 97.6% 122 / 125 Branchbran 84.2% 32 / 38 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.1% 32 / 34 Podpod 50.0% 2 / 4 totaltotal 91.7% 189 / 206 CC 24 SCAR 11.0
CC
24
Cov
93%
_pod_link
7.0
_pod_data
5.1
_pod_list
5.0
CRAP 3.0
« blib/lib/Pandoc/Elements.pm ↑ Summary blib/lib/Pandoc/Filter.pm »
1 package Pod::Simple::Pandoc;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.17';
7  
8 1 ▶use Pod::Simple::SimpleTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Pandoc::Elements;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub new {
12 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 1 bless {}, $class;
14 }
15  
16 sub _parser {
17 2 ▶ my $self = shift;
Subroutine _parser: called CC=1 SCAR=0.0
18  
19 2 my $parser = Pod::Simple::SimpleTree->new;
20 2 $parser->nix_X_codes(1); # ignore X<...> codes
21 2 $parser->nbsp_for_S(1); # map S<...> to U+00A0 (non-breaking space)
22 2 $parser->merge_text(1); # emit text nodes combined
23 2 $parser->no_errata_section(1); # omit errata section
24 2 $parser->complain_stderr(1); # TODO: configure
25 2 $parser->accept_targets( 'html', 'HTML', 'tex', 'latex', 'TeX', 'LaTeX' );
26  
27 # remove shortest leading whitespace string from verbatim sections
28 $parser->strip_verbatim_indent(
29 sub {
30 3 ▶ my $indent = length $_[0][1];
Subroutine __ANON__: called CC=3 SCAR=11.0
31 3 for ( @{ $_[0] } ) {
32 13 $_ =~ /^(\s*)/;
33 13 ▶ $indent = length($1) if length($1) < $indent;
Branch: 1 branch
BranchTrueFalseTotal
if length $1 < $indent 1 12 13
34 }
35 3 ' ' x $indent;
36 }
37 2 );
38  
39 2 return $parser;
40 }
41  
42 sub parse_file {
43 1 ▶ my ( $self, $file ) = @_;
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
44 1 $self->parse_tree( $self->_parser->parse_file($file)->root );
45 }
46  
47 sub parse_string {
48 1 ▶ my ( $self, $string ) = @_;
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
49 1 $self->parse_tree( $self->_parser->parse_string_document($string)->root );
50 }
51  
52 sub parse_tree {
53 2 ▶ my ( $self, $tree ) = @_;
Subroutine parse_tree: called CC=1 SCAR=0.0
Pod undocumented
54 2 _pod_element($tree);
55 }
56  
57 my %POD_ELEMENT_TYPES = (
58 Document => sub {
59 2 ▶ Document {}, [ _pod_content( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
60 },
61 Para => sub {
62 27 ▶ Para [ _pod_content( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
63 },
64 I => sub {
65 4 ▶ Emph [ _pod_content( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
66 },
67 B => sub {
68 1 ▶ Strong [ _pod_content( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
69 },
70 L => \&_pod_link,
71 C => sub {
72 30 ▶ Code attributes {}, _pod_flatten( $_[0] );
Subroutine __ANON__: called CC=1 SCAR=0.0
73 },
74 F => sub {
75 2 ▶ Code attributes { classes => ['filename'] }, _pod_flatten( $_[0] );
Subroutine __ANON__: called CC=1 SCAR=0.0
76 },
77 head1 => sub {
78 7 ▶ Header 1, attributes {}, [ _pod_content( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
79 },
80 head2 => sub {
81 9 ▶ Header 2, attributes {}, [ _pod_content( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
82 },
83 head3 => sub {
84 0 ▶ Header 3, attributes {}, [ _pod_content( $_[0] ) ];
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 },
86 head4 => sub {
87 0 ▶ Header 4, attributes {}, [ _pod_content( $_[0] ) ];
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 },
89 Verbatim => sub {
90 3 ▶ CodeBlock attributes {}, _pod_flatten( $_[0] );
Subroutine __ANON__: called CC=1 SCAR=0.0
91 },
92 'over-bullet' => sub {
93 1 ▶ BulletList [ _pod_list( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
94 },
95 'over-number' => sub {
96 1 ▶ OrderedList [ 1, DefaultStyle, DefaultDelim ], [ _pod_list( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
97 },
98 'over-text' => sub {
99 1 ▶ DefinitionList [ _pod_list( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
100 },
101 'over-block' => sub {
102 2 ▶ BlockQuote [ _pod_content( $_[0] ) ];
Subroutine __ANON__: called CC=1 SCAR=0.0
103 },
104 1 'for' => \&_pod_data,
105 );
106  
107 # map a single element or text to a list of Pandoc elements
108 sub _pod_element {
109 242 ▶ my ($element) = @_;
Subroutine _pod_element: called CC=4 SCAR=13.9
110  
111 242 ▶ if ( ref $element ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $element) { } 121 121 242
112 121 ▶ my $type = $POD_ELEMENT_TYPES{ $element->[0] } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $POD_ELEMENT_TYPES{$element->[0]} or return 0 121 121
113 121 $type->($element);
114 }
115 else {
116 121 my $n = 0;
117 121 ▶ map { $n++ ? ( Space, Str $_) : Str $_ } split( /\s+/, $element, -1 );
Branch: 1 branch
BranchTrueFalseTotal
$n++ ? : 285 121 406
118 }
119 }
120  
121 # map the content of a Pod element to a list of Pandoc elements
122 sub _pod_content {
123 86 ▶ my ($element) = @_;
Subroutine _pod_content: called CC=1 SCAR=0.0
124 86 my $length = scalar @$element;
125 86 map { _pod_element($_) } @$element[ 2 .. ( $length - 1 ) ];
126 }
127  
128 # stringify the content of an element
129 sub _pod_flatten {
130 35 ▶ my $string = '';
Subroutine _pod_flatten: called CC=1 SCAR=0.0
131 35 my $walk;
132 $walk = sub {
133 37 ▶ my ($element) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
134 37 my $n = scalar @$element;
135 37 for ( @$element[ 2 .. $n - 1 ] ) {
136 38 ▶ if ( ref $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 2 36 38
137 2 $walk->($_);
138 }
139 else {
140 36 $string .= $_;
141 }
142 }
143 35 };
144 35 $walk->( $_[0] );
145  
146 35 return $string;
147 }
148  
149 # map link
150 sub _pod_link {
151 27 ▶ my $type = $_[0][1]{type};
Subroutine _pod_link: called CC=7 SCAR=19.5
152 27 my $to = $_[0][1]{to};
153 27 my $section = $_[0][1]{section};
154 27 my $url = '';
155  
156 27 ▶ if ( $type eq 'url' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'url') { } 3 24 27
elsif ($type eq 'man') { } 3 21 24
elsif ($type eq 'pod') { } 21 0 21
157 3 $url = "$to";
158 }
159 elsif ( $type eq 'man' ) {
160 3 ▶ if ( $to =~ /^([^(]+)(?:[(](\d+)[)])?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to =~ /^([^(]+)(?:[(](\d+)[)])?$/) 3 0 3
161  
162 # TODO: configure MAN_URL, e.g.
163 # http://man7.org/linux/man-pages/man{section}/{name}.{section}.html
164 3 $url = "http://linux.die.net/man/$2/$1";
165  
166 # TODO: add section to URL if given
167 }
168 }
169 elsif ( $type eq 'pod' ) {
170 21 ▶ if ($to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to) 19 2 21
171  
172 # TODO: configure PERLDOC_URL
173 19 $url = "https://metacpan.org/pod/$to";
174 }
175 21 ▶ if ($section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section) 4 17 21
176  
177 # TODO: further escaping
178 4 $section =~ s/ /-/g;
179 4 $url .= "#$section";
180 }
181 }
182  
183 27 return Link attributes { }, [ _pod_content( $_[0] ) ], [ $url, '' ];
184 }
185  
186 # map data section
187 sub _pod_data {
188 4 ▶ my ($element) = @_;
Subroutine _pod_data: called CC=5 SCAR=16.3
189 4 my $target = lc( $element->[1]{target} );
190  
191 4 my $length = scalar @$element;
192 5 my $content = join "\n\n", map { $_->[2] }
193 4 grep { $_->[0] eq 'Data' } @$element[ 2 .. $length - 1 ];
194  
195 4 ▶ if ( $target eq 'html' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($target eq 'html') { } 2 2 4
elsif ($target =~ /^(la)?tex$/) { } 2 0 2
196 2 ▶ $content = "<div>$content</div>" if $content !~ /^<.+>$/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /^<.+>$/s 1 1 2
197 2 RawBlock 'html', $content . "\n";
198 }
199 elsif ( $target =~ /^(la)?tex$/ ) {
200  
201 # TODO: more intelligent check & grouping, especiall at the end
202 2 ▶ $content = "\\begingroup $content \\endgroup" if $content !~ /^[\\{]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /^[\\{]/ 0 2 2
203 2 RawBlock 'tex', "$content\n";
204 }
205 else {
206 0 undef;
207 }
208 }
209  
210 # map a list (any kind)
211 sub _pod_list {
212 3 ▶ my ($element) = @_;
Subroutine _pod_list: called CC=5 SCAR=16.1
213 3 my $length = scalar @$element;
214  
215 3 my $deflist = $element->[2][0] eq 'item-text';
216 3 my @list;
217 3 my $item = [];
218  
219 my $push_item = sub {
220 10 ▶ return unless @$item;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless @$item 3 7 10
221 7 ▶ if ($deflist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deflist) { } 3 4 7
222 3 my $term = shift @$item;
223 3 push @list, [ $term->content, [$item] ];
224 }
225 else {
226 4 push @list, $item;
227 }
228 3 };
229  
230 3 foreach my $e ( @$element[ 2 .. $length - 1 ] ) {
231 10 my $type = $e->[0];
232 10 ▶ if ( $type =~ /^item-(number|bullet|text)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^item-(number|bullet|text)$/) { } 7 3 10
233 7 $push_item->();
234 7 $item = [ Plain [ _pod_content($e) ] ];
235 }
236 else {
237 3 ▶ if ( @$item == 1 and $item->[0]->name eq 'Plain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$item == 1 and $item->[0]->name eq "Plain") 3 0 3
Condition33%1/3
@$item == 1 and $item->[0]->name eq "Plain"
!ll&&!rl&&r
003
MC/DC0%0/2
@$item == 1 and $item->[0]->name eq "Plain"
@$item == 1 $item->[0]->name eq "Plain"
Truth table33%1/3
A and B
A= @$item == 1B= $item->[0]->name eq "Plain"
ABresult
0X0
100
111
238  
239 # first block element in item should better be Paragraph
240 3 $item->[0] = Para $item->[0]->content;
241 }
242 3 push @$item, _pod_element($e);
243 }
244 }
245 3 $push_item->();
246  
247 # BulletList/OrderedList: [ @blocks ], ...
248 # DefinitionList: [ [ @inlines ], [ @blocks ] ], ...
249 3 return @list;
250 }
251  
252 1 1;
253 __END__
« blib/lib/Pandoc/Elements.pm ↑ Summary blib/lib/Pandoc/Filter.pm »