blib/lib/Docbook/Convert/Base.pm untested

filter: Statementstmt 0.0% 0 / 168 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 297 CC 22 SCAR 34.0
CC
22
Cov
0%
find_node_tag_text_recurse
90.0
find_node_tag_text
56.0
find_parent
20.0
CRAP 30.0
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 #
17 package Docbook::Convert::Base;
18  
19  
20 # Pragma
21 #
22 use strict qw(vars);
23 use vars qw($VERSION $AUTOLOAD);
24 use warnings;
25 no warnings qw(uninitialized once);
26  
27  
28 # External modules
29 #
30 use Docbook::Convert::Constant;
31 use Docbook::Convert::Util;
32 use Data::Dumper;
33  
34  
35 # Version information in a format suitable for CPAN etc. Must be
36 # all on one line
37 #
38 $VERSION='1.012';
39  
40  
41 #===================================================================================================
42  
43  
44 sub new { ## no subsort
45  
46 # New instance
47 #
48 my ($class, $param_hr)=@_;
49 my %self=(
50 meta_display_top => $META_DISPLAY_TOP,
51 meta_display_bottom => $META_DISPLAY_BOTTOM,
52 meta_display_title => $META_DISPLAY_TITLE,
53 meta_display_title_h_style => $META_DISPLAY_TITLE_H_STYLE,
54 no_html => $NO_HTML,
55 no_image_fetch => $NO_IMAGE_FETCH,
56 no_warn_unhandled => $NO_WARN_UNHANDLED,
57 table_wrap_columns => $TABLE_WRAP_COLUMNS,
58 table_wrap_huge => $TABLE_WRAP_HUGE,
59 %{$param_hr}
60 );
61 return bless(\%self, ref($class) || $class);
62  
63 }
64  
65  
66 sub create_tag_synonym {
67  
68 # Create tag equivalents
69 #
70 my ($tag_synonym_hr, $package)=@_;
71 $package ||= (caller())[0];
72 while (my ($tag, $tag_synonym_ar)=each %{$tag_synonym_hr}) {
73 foreach my $tag_synonym (@{$tag_synonym_ar}) {
74 *{"${package}::${tag_synonym}"}=sub {shift()->$tag(@_)}
75 }
76 }
77 }
78  
79  
80 sub find_node {
81  
82 # Find a node in the data tree
83 #
84 my ($self, $data_ar, $tag)=@_;
85 $tag ||= $data_ar->[$NODE_IX];
86 my @result;
87 $self->find_node_recurse(\@result, $data_ar, $tag);
88 return \@result;
89  
90 }
91  
92  
93 sub find_node_recurse {
94  
95 # Do the recursive searching. If we find remove tag from autoload hash - as we obviously knew
96 # enough about it to do the search for it.
97 #
98 my ($self, $result_ar, $data_ar, $tag)=@_;
99 if ($data_ar->[$NODE_IX] eq $tag) {
100 push @{$result_ar}, $data_ar;
101 delete $self->{'_autoload'}{$data_ar};
102 }
103 else {
104 foreach my $data_child_ar (@{$data_ar->[$CHLD_IX]}) {
105 $self->find_node_recurse($result_ar, $data_child_ar, $tag);
106 }
107 }
108 return undef;
109 }
110  
111  
112 sub pull_node_tag_text {
113  
114 # Same as find_node_tag_text but destructive (pulls text out and collapses data_ar to remove text node)
115 #
116 my $self=shift();
117  
118 # Set flag so find_node_tag_text will destroy node after finding. Bit hacky.
119 $self->{'_pull_node_tag_text'}++;
120 my $text=$self->find_node_tag_text(@_);
121 debug($text);
122 delete $self->{'_pull_node_tag_text'};
123 return $text;
124  
125 }
126  
127  
128 sub find_node_tag_text {
129  
130 my ($self, $data_ar, $tag_ar, $join)=@_;
131 $tag_ar ||= [$data_ar->[$NODE_IX]];
132 unless (ref($tag_ar)) {
133 $tag_ar=[split('\|', $tag_ar)];
134 }
135 debug("tag_ar %s, join *$join*", Dumper($tag_ar));
136 my @text;
137 foreach my $tag (@{$tag_ar}) {
138 my @tag;
139  
140 #print "find tag $tag\n";
141 $self->find_node_tag_text_recurse($data_ar, $tag, \@tag) ||
142 return err();
143 debug('about to join %s with %s', Dumper(\@tag), Dumper($join));
144 if (@tag) {
145 if (ref($join) eq 'SCALAR') {
146 push @text, join(${$join}, @tag);
147 }
148 elsif (ref($join) eq 'CODE') {
149 push @text, $join->(\@tag);
150 }
151 elsif ($join) {
152 push @text, join($join, @tag);
153 }
154 else {
155 push @text, \@tag
156 }
157 }
158 }
159  
160 #elsif (wantarray() && (@{$tag_ar} > 1)) {
161 # return @text;
162 #}
163 #print Dumper(\@text);
164 if (wantarray()) {
165 debug('return %s', Dumper(\@text));
166 return @text;
167 }
168 else {
169 debug("return *$text[0]*");
170 return $text[0];
171 }
172  
173 }
174  
175  
176 sub find_node_tag_text_recurse {
177  
178 # Do the recursive searching for all text and push into array
179 #
180 my ($self, $data_ar, $tag, $text_ar, $tag_found)=@_;
181  
182 #print "find $data_ar", Dumper([[caller()]->[0..2]]), "\n";;
183 if ((my $node_tag=$data_ar->[$NODE_IX]) eq $tag) {
184  
185 # Only return first hit. If already text in array just
186 # return;
187 #
188 return $text_ar if @{$text_ar};
189  
190 # Else proceed
191 #
192 $tag_found++;
193 $self->{'_autotext'}{$data_ar}=undef;
194 delete $self->{'_autoload'}{$data_ar};
195 }
196 foreach my $data_child_ar (@{$data_ar->[$CHLD_IX]}) {
197 if (ref($data_child_ar)) {
198 $self->find_node_tag_text_recurse($data_child_ar, $tag, $text_ar, $tag_found);
199 }
200 elsif ($tag_found) {
201 push @{$text_ar}, $data_child_ar;
202 }
203 if ($tag_found && ref($data_child_ar)) {
204 delete $self->{'_autoload'}{$data_child_ar};
205 $self->{'_autotext'}{$data_child_ar}=$data_child_ar unless
206 exists $self->{'_autotext'}{$data_child_ar}
207 }
208 }
209 if ($tag_found) {
210 delete $data_ar->[$CHLD_IX] if $self->{'_pull_node_tag_text'};
211 }
212 return $text_ar;
213 }
214  
215  
216 sub pull_node_text {
217  
218 # Same as find_node_text but destructive
219 my ($self, $data_ar, $join)=@_;
220 my $tag=$data_ar->[$NODE_IX];
221 return $self->pull_node_tag_text($data_ar, $tag, $join);
222  
223 }
224  
225  
226 sub find_node_text {
227  
228 my ($self, $data_ar, $join)=@_;
229 my $tag=$data_ar->[$NODE_IX];
230 return $self->find_node_tag_text($data_ar, $tag, $join);
231  
232 }
233  
234  
235 sub find_parent {
236  
237 my ($self, $data_ar, $tag_ar)=@_;
238 unless (ref($tag_ar)) {
239 $tag_ar=[split('\|', $tag_ar)];
240 }
241 if (my $data_parent_ar=$data_ar->[$PRNT_IX]) {
242 foreach my $tag (@{$tag_ar}) {
243 debug("look for tag $tag");
244 if ((my $t=$data_parent_ar->[$NODE_IX]) eq $tag) {
245 return $data_parent_ar;
246 }
247 }
248 return $self->find_parent($data_parent_ar, $tag_ar);
249 }
250 return undef;
251  
252 }
253  
254  
255 sub image_getwidth {
256  
257 my ($self, $url)=@_;
258 $self->load_imagemagick() ||
259 return err();
260 my $width;
261  
262 my $ua=LWP::UserAgent->new;
263 $ua->timeout($LWP_TIMEOUT);
264 $ua->env_proxy;
265 my $code_or=$ua->get($url);
266 my $image;
267 if ($code_or->is_success) {
268 $image=$code_or->content;
269 }
270 else {
271 die err("failed to get URL '$url', %s", $code_or->status_line);
272 }
273 if ($image) {
274 my $magick_or=Image::Magick->new(magick => 'jpg');
275 $magick_or->BlobToImage($image);
276 $width=$magick_or->Get('width');
277 }
278 debug("width $width");
279 return $width;
280  
281 }
282  
283  
284 sub load_imagemagick {
285  
286 # Load ImageMagic module
287 #
288 eval {
289 require Image::Magick;
290 1;
291 } || do {
292 return err("unable to load Image::Magick module, $@");
293 };
294 eval {
295 require LWP::UserAgent;
296 } || do {
297 return err("unable to load LWP::UserAgent module, $@");
298 };
299  
300 }
301  
302  
303 sub text_wrap {
304  
305 my ($self, $text)=@_;
306 eval {
307 require Text::Wrap;
308 1;
309 } || return err("unable to load Text::Wrap module, $@");
310 $self->{'_wrap_init'} ||= do {
311 $Text::Wrap::column=$self->{'table_wrap_columns'};
312 $Text::Wrap::huge=$self->{'table_wrap_huge'};
313 1;
314 };
315 $text=~s/\R//g;
316 $text=~s/\t/ /g;
317 $text=Text::Wrap::wrap(undef, undef, $text);
318 return $text;
319  
320 }
321  
322  
323 sub AUTOLOAD {
324  
325 # Catch tags we are not rendering yet - output them as text only
326 #
327 my ($self, $data_ar)=@_;
328 unless (ref($data_ar)) {
329 warn "unhandled internal method $AUTOLOAD, $data_ar\n"
330 }
331 $self->{'_autoload'}{$data_ar}=$data_ar;
332 return $data_ar;
333 }
334  
335  
336 sub DESTROY {
337  
338 1;
339  
340 }
341  
342 1;
343 __END__