MetaCurator/Export.pm untested

filter: Statementstmt 0.0% 0 / 222 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 339 CC 36 SCAR 49.7
CC
36
Cov
0%
build_dag_tree
240.0
export_tree
240.0
gather_statistics
72.0
CRAP 144.6
1 package CPAN::MetaCurator::Export;
2  
3 use boolean;
4 use feature 'say';
5 use parent 'CPAN::MetaCurator::HTML';
6 use warnings qw(FATAL utf8); # Fatalize encoding glitches.
7  
8 use Data::Dumper::Concise; # For Dumper().
9 use DateTime::Tiny;
10  
11 use File::Slurper qw/read_lines write_text/;
12  
13 use File::Spec;
14  
15 use HTML::Escape 'escape_html';
16  
17 use Mew;
18  
19 use Switch::Declare; # For switch.
20 use Syntax::Keyword::Match; # For match.
21  
22 use Tree::DAG_Node;
23  
24 has -dag_nodetree_path => (Str, default => sub{return ''}, chained => 1);
25  
26 has jstree_html_path => (Str, default => sub{return ''}, chained => 1);
27  
28 our $leaf_id;
29 our %seen;
30  
31 our $VERSION = '1.29';
32  
33 # --------------------------------------------------
34  
35 sub build_dag_tree
36 {
37 my($self, $daughter, $pad, $topic) = @_;
38 my(@lines) = split(/\n/, $$topic{text});
39 @lines = grep{length} map{s/^\s+//; s/:\s*$//; $_} @lines;
40 my($index) = -1;
41  
42 my(@components);
43 my($entry);
44 my(%inside, $item);
45 my($leaf, $line, $line_count);
46 my($module);
47 my(%node_type, $note, $note_count);
48 my(@pre_pre);
49 my($see_also_root);
50 my($text, $token, $type);
51  
52 $inside{pre_pre} = false;
53 $inside{see_also} = false;
54 $item = {description => '', href => '', id => 0, text => '', uri => ''};
55  
56 while ($index < $#lines)
57 {
58 $index++;
59  
60 $line = $lines[$index];
61 $token = ($line =~ /^o (.+)/) ? $1 : '';
62  
63 $self -> logger -> debug("Processing line $index: <$line>. token: $token");
64  
65 # $token ne '':
66 # a. See also
67 # b. An acronym
68 # Otherwise:
69 # c. A description
70 # d. A href
71 # e. <pre>
72 # f. </pre>
73  
74 if ($token eq 'See also')
75 {
76 $inside{see_also} = true;
77 $see_also_root = Tree::DAG_Node -> new({name => 'See also', attributes => {id => ++$leaf_id, description => '', uri => ''} });
78  
79 $daughter -> add_daughter($see_also_root);
80 }
81 elsif ($token)
82 {
83 $inside{see_also} = false;
84 $line_count = 0;
85 $module = $token;
86 $note_count = 0;
87  
88 $self -> gather_statistics(\%node_type, $pad, $module, $topic);
89  
90 if (! $seen{$module} && ($$topic{title} ne 'Acronyms') )
91 {
92 $seen{$module} = $self -> insert_hashref('modules', {name => $module});
93 }
94 }
95 elsif ($line =~ /<pre>/)
96 {
97 # Fix me. What happens if there are 2 sets of <pre>...</pre> within 1 topic?
98  
99 $inside{pre_pre} = true;
100 }
101 elsif ($line =~ m|</pre>|)
102 {
103 $inside{pre_pre} = false;
104 }
105 elsif ($inside{pre_pre})
106 {
107 }
108 else
109 {
110 $line_count++;
111  
112 $token = ($line =~ /^- (.+)/) ? $1 : '';
113  
114 if ($inside{see_also})
115 {
116 # Fix me. References to topics can be forward references.
117  
118 @components = split(' - ', $token);
119 $text = ($#components < 1) ? $components[0] : $components[1];
120 $type = switch ($components[0])
121 {
122 case /^\[?\[?[A-Za-z]+\d?\d?\]?\]?$/ {'topic'}
123 case /^http/ {'uri'}
124 default {'text'}
125 };
126  
127 match ($type : eq)
128 {
129 case('topic') {
130 $$item{text} = ($components[0] =~ /^\[?\[?([A-Za-z]+\d?\d?)\]?\]?$/) ? $1 : $components[0];
131 $$item{text} = "[Topic] <button class='btn btn-info'>$$item{text}</button>"
132 }
133 case('uri') {$$item{text} = "<a href = '" . escape_html($components[0]) . "' target = '_blank'>$text</a>"}
134 case('text') {$$item{text} = $token}
135 }
136  
137 $leaf = Tree::DAG_Node -> new({name => $$item{text}, attributes => {id => ++$leaf_id, description => '', uri => $$item{text}} });
138  
139 $see_also_root -> add_daughter($leaf);
140 }
141 elsif ($line_count == 1)
142 {
143 $$item{description} = $token;
144 }
145 elsif ($line_count == 2)
146 {
147 $$item{text} = $token;
148 $leaf = Tree::DAG_Node -> new({name => $module, attributes => {id => ++$leaf_id, description => $$item{description}, uri => $$item{text}} });
149  
150 $daughter -> add_daughter($leaf);
151 }
152 else
153 {
154 $note_count++;
155  
156 if ($note_count == 1)
157 {
158 $note = Tree::DAG_Node -> new({name => "Notes for: $module", attributes => {id => ++$leaf_id, description => '', uri => ''} });
159  
160 $daughter -> add_daughter($note);
161 }
162  
163 $entry = Tree::DAG_Node -> new({name => $token, attributes => {id => ++$leaf_id, description => '', uri => ''} });
164  
165 $note -> add_daughter($entry);
166 }
167 }
168 }
169  
170 } # End of build_dag_tree.
171  
172 # -----------------------------------------------
173  
174 sub build_tree
175 {
176 my($self, $pad) = @_;
177 my($origin) = shift @{$$pad{topics} }; # I.e.: {id => 1, parent_id => 1, text => 'Root', title => 'MetaCurator'}.
178 $leaf_id = 0;
179 my($root) = Tree::DAG_Node -> new({name => $$origin{title}, attributes => {id => $leaf_id, description => 'Root'} });
180  
181 my($daughter);
182  
183 for my $topic (@{$$pad{topics} })
184 {
185 $daughter = Tree::DAG_Node -> new({name => $$topic{title}, attributes => {id => ++$leaf_id, description => 'Topic'} });
186  
187 $root -> add_daughter($daughter);
188  
189 $self -> build_dag_tree($daughter, $pad, $topic);
190 }
191  
192 return $root;
193  
194 } # End of build_tree.
195  
196 # -----------------------------------------------
197  
198 sub export_tree
199 {
200 my($self) = @_;
201  
202 $self -> init_config;
203 $self -> init_db;
204  
205 # Phase 1: Build the DAG_Node tree.
206  
207 my($pad) = $self -> build_pad;
208 my($root) = $self -> build_tree($pad);
209  
210 # Phase 2: Save the DAG_Node tree to disk.
211  
212 if ($self -> dag_nodetree_path)
213 {
214 write_text($self -> dag_nodetree_path, join("\n", @{$root -> tree2string}) . "\n");
215 }
216  
217 # Phase 3: Scan the DAG_Node tree to get the topic ids.
218 # These will be used to resolve forward references of topics.
219  
220 my($attributes);
221 my($id);
222 my($name);
223 my(%topic_id_map);
224  
225 $root -> walk_down
226 ({
227 callbackback => sub
228 {
229 my($node, $options) = @_;
230 $attributes = $node -> attributes;
231 $name = $node -> name;
232  
233 if ($$options{_depth} == 1) # Topics.
234 {
235 $topic_id_map{$name} = $$attributes{id};
236 }
237  
238 }, # End of callbackback.
239 _depth => 0,
240 });
241  
242 # Phase 4; Build the JS Tree.
243 # New style.
244  
245 my($description);
246 my(@list);
247 my($previous_depth);
248 my($uri);
249  
250 $root -> walk_down
251 ({
252 callback => sub
253 {
254 my($node, $options) = @_;
255 $attributes = $node -> attributes;
256 $name = $node -> name;
257  
258 if ($$options{_depth} == 0) # Root.
259 {
260 push @list, '<ul>'; # Open global li.
261 push @list, qq|<li data-jstree='{"opened": true}' id = '$$attributes{id}'>$name|;
262 push @list, '<ul>'; # Open ul for subtree below root.
263 }
264 elsif ($$options{_depth} == 1) # Topics.
265 {
266 push @list, '</li>' if ($previous_depth == 1); # Close li opened at this depth.
267 push @list, '</ul></li>' if ($previous_depth == 2); # Close ul & li opened in subtree below.
268 push @list, '</ul></li></ul>' if ($previous_depth == 3); # Close ul & li opened in subtree below. Eg: CssStuff with no entries below See also.
269 push @list, qq|\t<li data-jstree='{"opened": false}' id = '$$attributes{id}'>$name|;
270 }
271 elsif ($$options{_depth} == 2) # Module name || 'Notes for ...' || 'See also'.
272 {
273 $$pad{count}{leaf}++;
274  
275 $description = $$attributes{description};
276 $uri = $$attributes{uri} || '#';
277 $uri = ($name =~ qr/Notes for|See also/) ? "&#8853; $name" : "<a href = '" . escape_html($uri) . "' target = '_blank'>$name - $description</a>";
278  
279 push @list, '<ul>' if ($previous_depth == 1); # Open ul for subtree at this level.
280 push @list, '</li>' if ($previous_depth == 2); # Close li opened at this depth.
281 push @list, '</ul></li>' if ($previous_depth == 3); # Close ul & li opened in subtree below.
282 push @list, qq|\t<li data-jstree='{"opened": false}' id = '$$attributes{id}'>$uri|;
283 }
284 elsif ($$options{_depth} == 3) # 'Notes for ...' || 'See also' entries.
285 {
286 push @list, '<ul>' if ($previous_depth == 2); # Open ul for subtree at this level.
287 push @list, qq|\t<li data-jstree='{"opened": false}' id = '$$attributes{id}'>$name</li>|;
288 }
289  
290 $previous_depth = $$options{_depth};
291  
292 return 1;
293  
294 }, # End of callback.
295 _depth => 0,
296 });
297  
298 push @list, '</ul>'; # Close ul for subtree opened at root.
299 push @list, '</li>'; # CLose li opened at root.
300 push @list, '</ul>'; # Close global li.
301  
302 # Phase 5: Build the web page which uses JSTree.
303 # And save it to html/cpan.metacurator.tree.html
304  
305 $$pad{jstree_html_path} = $self -> jstree_html_path;
306 my($header, $body, $footer) = $self -> build_html($pad); # Returns templates.
307 my($list) = join("\n", @list);
308 $body =~ s/!list!/$list/;
309  
310 for $_ (keys %{$$pad{count} })
311 {
312 $header =~ s/!$_!/$$pad{count}{$_}/;
313 }
314  
315 $self -> write_file($header, $body, $footer, $pad);
316 $self -> logger -> info("$_ count: $$pad{count}{$_}") for (sort keys %{$$pad{count} });
317  
318 return 0;
319  
320 } # End of export_tree.
321  
322 # --------------------------------------------------
323  
324 sub export_modules_table
325 {
326 my($self) = @_;
327  
328 $self -> init_config;
329 $self -> init_db;
330  
331 my($database_path) = File::Spec -> catfile($self -> home_path, $self -> database_path);
332 my($modules_csv_path) = File::Spec -> catfile($self -> home_path, $self -> output_path);
333  
334 $self -> logger -> info("Exporting modules table");
335 $self -> logger -> info("Reading: $database_path");
336 $self -> logger -> info("Writing: $modules_csv_path");
337  
338 my($command) = `echo ".h on\n.mode csv\nselect * from modules" | sqlite3 $database_path > $modules_csv_path`;
339 my($line_count) = `wc -l $modules_csv_path`;
340 my($module_count, $name) = split(' ', $line_count);
341 $module_count--; # Allow for header record.
342  
343 $self -> logger -> info("Output record count (excluding header): $module_count");
344  
345 } # End of export_modules_table.
346  
347 # --------------------------------------------------
348 # Some names might be acronyms & module names & topic names.
349 # Example: RSS.
350  
351 sub gather_statistics
352 {
353 my($self, $node_type, $pad, $token, $topic) = @_;
354  
355 $$node_type{acronym} = $$topic{title} eq 'Acronyms' ? true : false;
356 $$node_type{topic} = exists($$pad{topic_names}{$token}) ? true : false;
357 $$node_type{known} = exists($$pad{module_names}{$token}) ? true : false;
358 $$node_type{unknown} = ( (! $$node_type{acronym}) && (! $$node_type{known}) && (! $$node_type{topic}) ) ? true : false;
359  
360 $$pad{count}{acronym}++ if ($$node_type{acronym});
361 $$pad{count}{known}++ if ($$node_type{known});
362 $$pad{count}{unknown}++ if ($$node_type{unknown} && ($token ne 'See also') );
363  
364 } # End of gather_statistics;
365  
366 # --------------------------------------------------
367  
368 sub write_file
369 {
370 my($self, $header, $body, $footer, $pad) = @_;
371 my($output_path) = File::Spec -> catfile($self -> home_path, $self -> jstree_html_path);
372  
373 # $$pad{encoding} has a ':' prefix, & the value is from the constants table, which is from
374 # /home/ron/perl.modules/CPAN-MetaCurator/data/cpan.metacurator.constants.csv.
375  
376 open(my $fh, ">$$pad{encoding}", $output_path);
377 print $fh $header, $body, $footer;
378 close $fh;
379  
380 $self -> logger -> info("Created $output_path. Encoding: $$pad{encoding}");
381  
382 } # End of write_file.
383  
384 # --------------------------------------------------
385  
386 1;
387  
388 =pod
389  
390 =head1 Machine-Readable Change Log
391  
392 The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.
393  
394 =head1 Version Numbers
395  
396 Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
397  
398 =head1 Test data
399  
400 There is a mechanism to restrict processing to a tiny number of topics.
401  
402 To this end there is an option called test_topics_path, which takes a file name.
403 If this file is present it is read, & each line in it is assumed to be a topic name.
404  
405 Topics listed are wanted, & so the program skips processing any other topics.
406  
407 There is a special case. If the file is present but empty, or absent, all topics are deemed
408 to appear in the file & hence are processed.
409  
410 Default: /tmp/test.topics.txt.
411  
412 =head1 Support
413  
414 Email the author.
415  
416 =head1 Author
417  
418 L<CPAN::MetaCurator> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2025.
419  
420 My homepage: L<https://savage.net.au/>.
421  
422 =head1 Copyright
423  
424 Australian copyright (c) 2025, Ron Savage.
425  
426 All Programs of mine are 'OSI Certified Open Source Software';
427 you can redistribute them and/or modify them under the terms of
428 The Perl License, a copy of which is available at:
429 http://dev.perl.org/licenses/
430  
431 =cut