blib/lib/Docbook/Convert.pm

filter: Statementstmt 92.0% 23 / 25 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.1% 32 / 34 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Docbook/Convert/Tag.pm ↑ Summary bin/docbook-convert »
1 #
2 # This file is part of Docbook::Convert.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13  
14 #
15 #
16 package Docbook::Convert;
17  
18  
19 # Pragma
20 #
21 4 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use vars qw($VERSION $AUTHORITY $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶no warnings qw(uninitialized utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶sub BEGIN {local $^W=0}
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 # External modules
29 #
30 4 ▶use Docbook::Convert::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Docbook::Convert::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 # External modules
35 #
36 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 0 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
38 use Data::Dumper;
39  
40  
41 # Version information in a format suitable for CPAN etc. Must be
42 # all on one line
43 #
44 $AUTHORITY='cpan:ASPEER';
45 $VERSION='1.012';
46  
47  
48 #===================================================================================================
49  
50  
51 sub data_ar {
52  
53 # Container to hold node tree
54 #
55 my $self=shift();
56 my @data=(
57 shift() || undef, # NODE_IX - tag name
58 shift() || undef, # CHLD_IX - array of child nodes
59 shift() || undef, # ATTR_IX - attributes
60 shift() || undef, # LINE_IX - line no this node was sourced from
61 shift() || undef, # COLM_IX - col no " "
62 shift() || undef # PRNT_IX - link to parent node
63 );
64 return \@data;
65  
66 }
67  
68  
69 sub handler {
70  
71  
72 # Called by twig
73 #
74 my ($self, $data_ar, $twig_or, $elt_or)=@_;
75  
76  
77 # Don't process if not parent (i.e. until we have done all tags).
78 #
79 return if $elt_or->parent();
80  
81  
82 # Call parser now as we are on parent.
83 #
84 return $self->parse($data_ar, $elt_or);
85  
86 }
87  
88  
89 sub parse {
90  
91  
92 # Parse and XML::Twig tree and produce a node tree
93 #
94 my ($self, $data_ar, $elt_or, $data_parent_ar)=@_;
95  
96  
97 # Get tag, any node attributes, line and col number
98 #
99 my $tag=$elt_or->tag();
100 my $attr_hr=$elt_or->atts();
101 my $line_no=delete $attr_hr->{'_line_no'};
102 my $col_no=delete $attr_hr->{'_col_no'};
103 $attr_hr=undef unless keys %{$attr_hr};
104  
105  
106 # Build array data
107 #
108 @{$data_ar}[$NODE_IX, $CHLD_IX, $ATTR_IX, $LINE_IX, $COLM_IX, $PRNT_IX]=
109 ($tag, undef, $attr_hr, $line_no, $col_no, $data_parent_ar); # Name, Child, Attr
110  
111  
112 # Go through children looking for any text nodes
113 #
114 foreach my $elt_child_or ($elt_or->children()) {
115  
116 # Text ?
117 #
118 my $child_tag=$elt_child_or->tag();
119 unless (($child_tag eq '#PCDATA') || ($child_tag eq '#CDATA')) {
120  
121 # No - recurse. Need new data container
122 #
123 debug("recurse for child tag: $child_tag");
124 my $data_child_ar=$self->data_ar();
125 $self->parse($data_child_ar, $elt_child_or, $data_ar);
126 push @{$data_ar->[$CHLD_IX]}, $data_child_ar;
127 }
128 else {
129  
130 # Yes - store as text node. If para cleanup leading whitespace
131 my $text=$elt_child_or->text();
132 if ($tag eq 'para') {
133 $text=&whitespace_clean($text);
134 }
135 debug("$tag: *$text*");
136 my $data_child_ar=
137 $self->data_ar('text', [$text], undef, undef, undef, $data_ar);
138 push @{$data_ar->[$CHLD_IX]}, $data_child_ar;
139 }
140 }
141  
142  
143 # Done - return OK
144 #
145 return \undef;
146  
147 }
148  
149  
150 sub process {
151  
152  
153 # Get self ref, file to process
154 #
155 my ($self, $xml, $param_hr)=@_;
156  
157  
158 # Create a hashed self ref to hold various info
159 #
160 ref($self) || do {
161 $self=bless($param_hr ||= {handler => $HANDLER_DEFAULT}, ref($self) || $self)
162 };
163  
164  
165 # Array to hold parsed data
166 #
167 my $data_ar=$self->data_ar();
168  
169  
170 # Load handler
171 #
172 my $handler=$param_hr->{'handler'} ||
173 return err('no handler supplied');
174 my $handler_module=$HANDLER_HR->{$handler} ||
175 return err("unable to load handler: $handler, no module found");
176 eval("use $handler_module") || do {
177 return err("unable to load handler module: $handler_module, $@")
178 if $@
179 };
180  
181  
182 # Get XML::Twig object
183 #
184 my $xml_or=XML::Twig->new(
185 'twig_handlers' => {
186 '_all_' => sub {$self->handler($data_ar, @_)}
187 },
188 'start_tag_handlers' => {
189 '_all_' => sub {$self->start_tag_handler($data_ar, @_)}
190 },
191 discard_all_spaces => 1,
192 );
193  
194  
195 # Parse file which will fill $data_ar;
196 #
197 $xml_or->parse($xml);
198  
199  
200 # If we are dumping clean up a bit then spit out
201 #
202 if ($param_hr->{'dump'}) {
203 return Dumper(dump_ar($data_ar));
204 }
205  
206  
207 # And render
208 #
209 my $output=$self->render($data_ar, $handler_module);
210  
211  
212 # Done
213 #
214 return $output;
215  
216 }
217  
218  
219 sub process_file {
220  
221 # Open file we want to process
222 #
223 my ($self, $fn, $param_hr)=@_;
224 my $fh=IO::File->new($fn, O_RDONLY) ||
225 return err("unable to open file $fn, $!");
226 return $self->process($fh, $param_hr);
227  
228 }
229  
230  
231 sub render {
232  
233  
234 # Get self ref, node tree
235 #
236 my ($self, $data_ar, $handler)=@_;
237  
238  
239 # Get hander
240 #
241 my $render_or=$handler->new($self) ||
242 return err("unable to initialise handler $handler");
243  
244  
245 # Call recurive render routine
246 #
247 my $output=$self->render_recurse($data_ar, $render_or) ||
248 return err('unable to get ouput from render');
249  
250  
251 # Fix any anchors/links
252 #
253 $output=$render_or->_anchor_fix($output, $self->{'_id'});
254  
255  
256 # Any errors/warnings for unhandled tags ?
257 #
258 if ((my $hr=$render_or->{'_autoload'}) && !$self->{'no_warn_unhandled'}) {
259 my @data_ar=sort {($a->[$NODE_IX] cmp $b->[$NODE_IX]) or ($a->[$LINE_IX] <=> $b->[$LINE_IX])} grep {$_} values(%{$hr});
260 foreach my $data_ar (@data_ar) {
261 my ($tag, $line_no, $col_no)=@{$data_ar}[$NODE_IX, $LINE_IX, $COLM_IX];
262 warn("warning - unrendered tag $tag at line $line_no, column $col_no\n");
263 }
264 }
265 if ((my $hr=$render_or->{'_autotext'}) && !$self->{'no_warn_unhandled'}) {
266 my @data_ar=sort {($a->[$NODE_IX] cmp $b->[$NODE_IX]) or ($a->[$LINE_IX] <=> $b->[$LINE_IX])} grep {$_} values(%{$hr});
267 foreach my $data_ar (@data_ar) {
268 my ($tag, $line_no, $col_no)=@{$data_ar}[$NODE_IX, $LINE_IX, $COLM_IX];
269 warn("warning - autotexted tag '$tag' at line $line_no, column $col_no\n");
270 debug(Dumper($data_ar));
271 }
272 }
273  
274  
275 # Done
276 #
277 return $output;
278  
279 }
280  
281  
282 sub render_recurse {
283  
284  
285 # Get self ref, node
286 #
287 my ($self, $data_ar, $render_or)=@_;
288  
289  
290 # Get tag name
291 #
292 my $tag=$data_ar->[$NODE_IX];
293  
294  
295 # Get attributes and look for anchor
296 #
297 my ($anchor_id, $anchor_title);
298 my $attr_hr=$data_ar->[$ATTR_IX];
299 if ($anchor_id=($attr_hr->{'id'} || $attr_hr->{'xml:id'})) {
300 my ($title, $subtitle)=
301 $render_or->find_node_tag_text($data_ar, 'title|subtitle', $NULL);
302 $anchor_title=$title || $subtitle;
303 $render_or->{'_id'}{$anchor_id}=($anchor_title);
304 debug("anchor found: $anchor_title");
305 }
306  
307  
308 # Does this tag turn on plaintext ? E.g. if withing screen/programlisting/commmand in Markdown no
309 # further markdown is needed
310 #
311 $render_or->{'_plaintext'}++ if
312 $render_or->_plaintext($tag);
313 debug('plaintext flag: %s', $render_or->{'_plaintext'});
314  
315  
316 # Render any children
317 #
318 if ($data_ar->[$CHLD_IX]) {
319 foreach my $data_chld_ix (0..$#{$data_ar->[$CHLD_IX]}) {
320 my $data_chld_ar=$data_ar->[$CHLD_IX][$data_chld_ix];
321 if (ref($data_chld_ar)) {
322 debug("rendering child $data_chld_ar");
323 my $data=$self->render_recurse($data_chld_ar, $render_or);
324 $data_ar->[$CHLD_IX][$data_chld_ix]=$data;
325 }
326 }
327 }
328  
329  
330 # Clear plaintext
331 #
332 delete $render_or->{'_plaintext'}
333 if $render_or->_plaintext($tag);
334  
335  
336 # Render this tag
337 #
338 my $render=$render_or->$tag($data_ar);
339 debug("$tag *$render*") unless ref($render);
340  
341  
342 # Create anchor if needed
343 #
344 if ($anchor_id) {
345 my $anchor=($render_or->_anchor($anchor_id, $anchor_title) . $CR2) unless $NO_HTML;
346 debug("creating anchor: $anchor, render $render");
347 if (ref($render)) {
348 unless ($self->{'no_warn_unhandled'}) {
349 warn("warning - unable to add anchor #${anchor_id} for unhandled tag: $tag\n");
350 }
351 }
352 else {
353 $render=join($CR2, $anchor, $render) unless ref($render);
354 }
355 }
356  
357  
358 # Done
359 #
360 return $render;
361  
362 }
363  
364  
365 sub start_tag_handler {
366  
367 my ($self, $data_ar, $twig_or, $elt_or)=@_;
368 $elt_or->set_att('_line_no', $twig_or->current_line());
369 $elt_or->set_att('_col_no', $twig_or->current_column());
370  
371 }
372  
373  
374 sub AUTOLOAD {
375  
376 # Catchall for handler shortcuts, e.g. Docbook::Convert->markdown();
377 #
378 my ($self, $xml, $param_hr)=@_;
379 my ($handler)=($AUTOLOAD=~/::(\w+)$/);
380 if ($handler=~s/_file$//) {
381 return $self->process_file($xml, {%{$param_hr}, handler => $handler});
382 }
383 else {
384 return $self->process($xml, {%{$param_hr}, handler => $handler});
385 }
386 }
387  
388  
389 sub DESTROY {
390  
391 # Stub so not invoked by AUTOLOAD
392  
393 }
394  
395 1;
396 __END__
« blib/lib/Docbook/Convert/Tag.pm ↑ Summary bin/docbook-convert »