Debian Codegraph @

lib/Pod/POM/View/Restructured.pm

raw symbols SCIP
1 # AUTHOR: Don Owens <[email protected]>
2 # AUTHOR: Jeff Fearn <[email protected]>
3 # AUTHOR: Alex Muntada <[email protected]>
4 # OWNER: 2010 Don Owens <[email protected]>
5 # OWNER: 2016 Jeff Fearn <[email protected]>
6 # OWNER: 2016-2026 Alex Muntada <[email protected]>
7 # LICENSE: Perl_5
8
9
10 use strict;
11 use warnings;
12 use Data::Dumper ();
13
14 use Pod::POM;
15
16 package Pod::POM::View::Restructured;
17
18 our $VERSION = '1.000004'; # VERSION
19
20 use base 'Pod::POM::View::Text';
21
22
23 sub new {
24 my ($class, $params) = @_;
25 $params = { } unless $params and UNIVERSAL::isa($params, 'HASH');
26
27 my $self = bless { seen_something => 0, title_set => 0, params => { } }, ref($class) || $class;
28
29 my $callbacks = $params->{callbacks};
30 $callbacks = { } unless $callbacks;
31 $self->{callbacks} = $callbacks;
32 $self->{namespace} = delete($params->{namespace});
33
34 return $self;
35 }
36
37 sub convert_file {
38 my ($self, $source_file, $title, $dest_file, $callbacks) = @_;
39
40 my $cb;
41 if ($callbacks) {
42 $cb = { %{ $self->{callbacks} }, %$callbacks };
43 }
44 else {
45 $cb = $self->{callbacks};
46 }
47
48 my $view = Pod::POM::View::Restructured->new({ callbacks => $cb, namespace => $self->{namespace} });
49 my $parser = Pod::POM->new;
50
51 unless (-r $source_file) {
52 warn "can't read source file $source_file";
53 return;
54 }
55
56 my $pom = $parser->parse_file($source_file);
57
58 $view->{title_set} = 1 if defined($title);
59 my $out = $pom->present($view);
60
61 if (defined($title)) {
62 $out = $self->_build_header($title, '#', 1) . "\n" . $out;
63 }
64 else {
65 $title = $view->{title};
66 }
67
68 if (defined($dest_file) and $dest_file ne '') {
69 my $out_fh;
70 if (UNIVERSAL::isa($dest_file, 'GLOB')) {
71 $out_fh = $dest_file;
72 }
73 else {
74 unless (open($out_fh, '>', $dest_file)) {
75 warn "couldn't open output file $dest_file";
76 return;
77 }
78 }
79
80 print $out_fh $out;
81 close $out_fh;
82 }
83
84 my $rv = { content => $out, title => $title };
85
86 return $rv;
87 }
88
89 sub convert_files {
90 my ($self, $file_spec, $index_file, $index_title, $out_dir) = @_;
91
92 my $index_fh = $self->_get_file_handle($index_file, '>');
93
94 if ($index_fh and defined($index_title) and $index_title ne '') {
95 my $header = $self->_build_header($index_title, '#', 1);
96 # my $line = '#' x length($index_title);
97 # my $header = $line . "\n" . $index_title . "\n" . $line . "\n\n";
98
99 print $index_fh $header;
100
101 print $index_fh "\nContents:\n\n";
102 print $index_fh ".. toctree::\n";
103 print $index_fh " :maxdepth: 1\n\n";
104 }
105
106 my $count = 0;
107 my $toc = '';
108 foreach my $spec (@$file_spec) {
109 $count++;
110 my $data = $self->convert_file($spec->{source_file}, $spec->{title},
111 $spec->{dest_file}, $spec->{callbacks});
112
113 my $this_title = $data->{title};
114 # print STDERR Data::Dumper->Dump([ $this_title ], [ 'this_title' ]) . "\n\n";
115
116 unless (defined($this_title) and $this_title !~ /\A\s*\Z/) {
117 $this_title = 'section_' . $count;
118 }
119
120 my $name = $spec->{dest_file};
121 if (defined($name)) {
122 $name =~ s/\.rst\Z//;
123 }
124 else {
125 ($name = $this_title) =~ s/\W/_/g;
126 my $dest_file = $out_dir . '/' . $name . '.rst';
127 my $out_fh;
128
129 unless (open($out_fh, '>', $dest_file)) {
130 warn "couldn't open output file $dest_file";
131 return;
132 }
133
134 print $out_fh $data->{content};
135 close $out_fh;
136 }
137
138 unless ($spec->{no_toc}) {
139 $toc .= ' ' . $name . "\n";
140 }
141
142 if ($index_fh and not $spec->{no_toc}) {
143 print $index_fh " " . $name . "\n";
144 }
145 }
146
147 if ($index_fh) {
148 print $index_fh "\n";
149 }
150
151 return { toc => $toc };
152 }
153
154 sub _get_file_handle {
155 my ($self, $file, $mode) = @_;
156
157 return unless defined $file;
158
159 if (ref($file) and UNIVERSAL::isa($file, 'GLOB')) {
160 return $file;
161 }
162
163 $mode = '<' unless $mode;
164
165 my $fh;
166 if ($file ne '') {
167 unless (open($fh, $mode, $file)) {
168 warn "couldn't open input file $file: $!";
169 return;
170 }
171 }
172
173 return $fh;
174 }
175
176 sub view_pod {
177 my ($self, $node) = @_;
178
179 return $node->content()->present($self);
180 }
181
182 sub _generic_head {
183 my ($self, $node, $marker, $do_overline) = @_;
184
185 return scalar($self->_generic_head_multi($node, $marker, $do_overline));
186 }
187
188 sub _generic_head_multi {
189 my ($self, $node, $marker, $do_overline) = @_;
190
191 my $title = $node->title()->present($self);
192 my $content = $node->content()->present($self);
193
194 $title = ' ' if $title eq '';
195 # my $section_line = $marker x length($title);
196
197 my $section = $self->_build_header($title, $marker, $do_overline) . "\n" . $content;
198
199 # my $section = $title . "\n" . $section_line . "\n\n" . $content;
200 # if ($do_overline) {
201 # $section = $section_line . "\n" . $section;
202 # }
203
204 $section .= "\n";
205
206 return wantarray ? ($section, $content, $title) : $section;
207 }
208
209 sub _build_header {
210 my ($self, $text, $marker, $do_overline) = @_;
211
212 my $line = $marker x length($text);
213 my $header = $text . "\n" . $line . "\n";
214
215 if ($do_overline) {
216 $header = $line . "\n" . $header;
217 }
218
219 my $namespace = $self->{namespace};
220 if($namespace) {
221 my $a = $text;
222 # prepend the namesspace to guarantee document wide unique names
223 $a = "$namespace\:\:$a" unless($text =~ /^$namespace/);
224 $a =~ s/(?:\s)/-/g;
225
226 $header = qq{.. _$a:\n\n} . $header;
227 }
228
229 return "\n" . $header;
230 }
231
232 sub _do_indent {
233 my ($self, $text, $indent_amount, $dbg) = @_;
234
235 my $indent = ' ' x $indent_amount;
236
237 # $indent = "'$dbg" . $indent . "'";
238
239 my @lines = split /\n/, $text, -1;
240 foreach my $line (@lines) {
241 $line = $indent . $line;
242 }
243
244 return join("\n", @lines);
245 }
246
247 sub view_head1 {
248 my ($self, $node) = @_;
249
250 my ($section, $content, $title) = $self->_generic_head_multi($node, '*', 1);
251
252 unless ($self->{seen_something} or $self->{title_set}) {
253 if ($title eq 'NAME') {
254 $self->{seen_something} = 1;
255
256 if ($content =~ /\A\s*(\w+(?:::\w+)+)\s+-\s+/s) {
257 my $mod_name = $1;
258 $self->{module_name} = $mod_name;
259 $self->{title} = $mod_name;
260 $self->{title_set} = 1;
261
262 $section = $self->_build_header($mod_name, '#', 1) . $section;
263
264 # my $line = '#' x length($mod_name);
265 # $section = $line . "\n" . $mod_name . "\n" . $line . "\n\n" . $section;
266 }
267
268 return $section;
269 }
270 }
271
272 $self->{seen_something} = 1;
273 return $section;
274 }
275
276 sub view_head2 {
277 my ($self, $node) = @_;
278
279 $self->{seen_something} = 1;
280 return $self->_generic_head($node, '=');
281 }
282
283 sub view_head3 {
284 my ($self, $node) = @_;
285
286 $self->{seen_something} = 1;
287 return $self->_generic_head($node, '-');
288 }
289
290 sub view_head4 {
291 my ($self, $node) = @_;
292
293 $self->{seen_something} = 1;
294 return $self->_generic_head($node, '^');
295 }
296
297 sub view_item {
298 my ($self, $node) = @_;
299
300 $self->{seen_something} = 1;
301
302 my $title = $node->title()->present($self);
303 my $content = $node->content()->present($self);
304
305 $title =~ s/\A\s+//;
306 $title =~ s/\n/ /;
307 $title = "- $title"
308 unless $title =~ /
309 # the line starts with
310 \A
311 # single unordered bullet,
312 (?:(?:[-+] | \\[*])
313 # or ordered bullet followed by dot,
314 | [1AaIi] \.
315 # or ordered bullet within parens (first optional),
316 | \(? [1AaIi] \)
317 # then finally followed by whitespace.
318 )\s
319 /xms;
320 # Make asterisk an actual bullet
321 $title =~ s/ \A \\ [*]/*/xms;
322 # $content =~ s/\n/\n /g;
323 # $content = ' ' . $content;
324
325 $self->{view_item_count}++;
326 $content = $self->_do_indent($content, 1, "[[view_item_$self->{view_item_count}]]");
327
328 return "\n" . $title . "\n" . $content . "\n\n";
329 }
330
331 sub view_over {
332 my ($self, $node) = @_;
333
334 my $content = $node->content()->present($self);
335 # my $indent = $node->indent();
336
337 return "\n" . $content;
338 }
339
340 sub view_text {
341 my ($self, $node) = @_;
342
343 my @lines = split /\n/, $node;
344 foreach my $line (@lines) {
345 $line =~ s/\A\s+//;
346 }
347
348 return join("\n", @lines);
349 }
350
351 sub view_textblock {
352 my ($self, $text) = @_;
353
354 return "\n" . $text . "\n";
355 }
356
357
358 sub view_verbatim {
359 my ($self, $node) = @_;
360
361 # (my $node_part = ' ' . $node) =~ s/\n/\n /g;
362 my $node_part = $self->_do_indent($node . '', 1, '[[view_verbatim]]');
363
364 my $block_part = ".. code-block:: perl\n\n";
365 if (defined($self->{next_code_block})) {
366 my $lang = $self->{next_code_block};
367 delete $self->{next_code_block};
368
369 if ($lang eq 'none') {
370 # FIXME: need to output a preformatted paragraph here, but no highlighting
371 $block_part = '';
372 }
373 else {
374 $block_part = ".. code-block:: $lang\n\n";
375 }
376 }
377
378 my $content = $block_part . $node_part;
379
380 return "\n\n" . $content . "\n\n";
381 }
382
383 sub view_for {
384 my ($self, $node) = @_;
385
386 my $fmt = $node->format();
387
388 # print STDERR "got for: fmt='$fmt', text='" . $node->text() . "'\n";
389
390 if ($fmt eq 'pod2rst') {
391 my $text = $node->text();
392 if ($text =~ /\A\s*next-code-block\s*:\s*(\S+)/) {
393 my $lang = $1;
394 $self->{next_code_block} = $lang;
395 return '';
396 }
397
398 return "\n". $node->text() . "\n\n";
399 }
400
401 return $self->SUPER::view_for($node);
402 }
403
404 sub view_seq_code {
405 my ($self, $text) = @_;
406
407 return '\ ``' . $text . '``\ ';
408 }
409
410 sub view_seq_bold {
411 my ($self, $text) = @_;
412
413 $text =~ s/\*/\\*/g;
414 $text =~ s/\`/\\`/g;
415
416 return '\ **' . $text . '**\ ';
417 }
418
419 sub view_seq_italic {
420 my ($self, $text) = @_;
421
422 $text =~ s/\*/\\*/g;
423 $text =~ s/\`/\\`/g;
424
425 return '\ *' . $text . '*\ ';
426 }
427
428 sub view_seq_file {
429 my ($self, $text) = @_;
430
431 $text =~ s/\*/\\*/g;
432 $text =~ s/\`/\\`/g;
433
434 return '\ *' . $text . '*\ ';
435 }
436
437 sub view_seq_text {
438 my ($self, $node) = @_;
439
440 my $text = $node . '';
441
442 $text =~ s/\*/\\*/g;
443 $text =~ s/\`/\\`/g;
444
445 return $text;
446 }
447
448 sub view_seq_zero {
449 return '';
450 }
451
452 sub view_seq_link {
453 my ($self, $text) = @_;
454
455 # FIXME: determine if has label, if manpage, etc., and pass that info along to the callback,
456 # instead of just the text, e.g.,
457 # $link_cb->($label, $name, $sec, $url);
458 my $link_cb = $self->{callbacks}{link};
459 if ($link_cb) {
460 my ($url, $label) = $link_cb->($text);
461
462 if (defined($url)) {
463 if ($url eq '' and defined($label) and $label ne '') {
464 $text = $label;
465 }
466 elsif (defined($label) and $label ne '') {
467 $text = qq{`$label <$url>`_};
468 }
469 else {
470 $text = qq{`$url <$url>`_};
471 }
472
473 return $text;
474 }
475 }
476
477 my $url = '';
478 my $label = '';
479 my $module = $text;
480 my $namespace = $self->{namespace};
481
482 if ($text =~ m{\A/(.+)}) {
483 (my $section = $1) =~ s/\A"(.+)"/$1/;
484 $text = qq{`$section`_};
485 }
486 elsif ($text =~ m{\Ahttps?://}) {
487 $text = qq{`$text <$text>`_};
488 }
489 elsif ($text =~ /::/) {
490 $label = $text;
491 if ($text =~ /\A(.+?)\|(.+::.+)/) {
492 $label = $1;
493 $module = $2;
494 }
495
496 # Links in this namespace are cross references
497 if (($namespace) and ($text =~ /^$namespace/)) {
498 $module = qq{$namespace\:\:$module} unless($module =~ /^$namespace/);
499 $module =~ s/(?:\s)/-/g;
500 $text = qq{:ref:`$label <$module>`};
501 }
502 else {
503 $module = $self->_url_encode($module);
504 my $url = "https://metacpan.org/search?q=$module";
505 $text = qq{`$label <$url>`_};
506 }
507 }
508
509 return $text;
510 }
511
512 sub _url_encode {
513 my ($self, $str) = @_;
514
515 use bytes;
516 $str =~ s{([^A-Za-z0-9_])}{sprintf("%%%02x", ord($1))}eg;
517 return $str;
518 }
519
520
521
522 1;
523
524 __END__
525
526 =pod
527
528 =head1 NAME
529
530 Pod::POM::View::Restructured - View for Pod::POM that outputs reStructuredText
531
532 =head1 SYNOPSIS
533
534 use Pod::POM::View::Restructured;
535
536 my $view = Pod::POM::View::Restructured->new;
537 my $parser = Pod::POM->new;
538 my $pom = $parser->parse_file("$top_dir/lib/Pod/POM/View/Restructured.pm");
539 my $out = $pom->present($view);
540
541 =head1 DESCRIPTION
542
543 This module outputs reStructuredText that is expected to be used
544 with Sphinx. Verbatim sections (indented paragraphs) in the POD
545 will be output with syntax highlighting for Perl code by default.
546 See L</"POD commands specifically for reStructuredText"> for how
547 to change this for a particular block.
548
549 This module can be downloaded from L<https://metacpan.org/release/Pod-POM-View-Restructured>.
550
551 =head1 METHODS
552
553 =head2 C<new(\%params)>
554
555 Constructor. \%params is optional. If present, the following keys are valid:
556
557 =over 4
558
559 =item C<callbacks>
560
561 See documentation below for C<convert_file()>.
562
563 =item C<namespace>
564
565 If a namespace is declared then links to that namespace are
566 converted to cross references and an anchor is added for each
567 head tag.
568
569 =back
570
571 =head2 C<convert_file($source_file, $title, $dest_file, $callbacks)>
572
573 Converts the POD in C<$source_file> to reStructuredText. If
574 C<$dest_file> is defined, it writes the output there. If
575 C<$title> is defined, it is used for the title of the document.
576 Otherwise, an attempt is made to infer the title from the NAME
577 section (checks if the body looks like C</\A\s*(\w+(?:::\w+)+)\s+-\s+/s>).
578
579 Returns the output as a string.
580
581 C<$source_file> and C<$dest_file> can be either file names or file
582 handles.
583
584 =head2 C<convert_files($file_spec, $index_file, $index_title, $out_dir)>
585
586 Converts the files given in C<$file_spec> to reStructuredText.
587 If C<$index_file> is provided, it is the path to the index file
588 to be created (with a table of contents pointing to all of the
589 files created). If C<$index_title> is provided, it is used as
590 the section title for the index file. C<$out_dir> is the
591 directory the generated files will be written to.
592
593 C<$file_spec> is a reference to an array of hashes specifying
594 attributes for each file to be converted. The valid keys are:
595
596 =over 4
597
598 =item C<source_file>
599
600 File to convert.
601
602 =item C<dest_file>
603
604 File to output the reStructuredText. If not provided, a file
605 name will be generated based on the title.
606
607 =item C<title>
608
609 Section title for the generated reStructuredText. If not
610 provided, an attempt will be made to infer the title from the
611 NAME section in the POD, if it exists. As a last resort, a title
612 will be generated that looks like "section_(\d+)".
613
614 =item C<callbacks>
615
616 A reference to a hash containing names and the corresponding callbacks.
617
618 Currently the only valid callback is C<link>. It is given the
619 text inside a LE<lt>E<gt> section from the POD, and is expected to return a
620 tuple C<($url, $label)>. If the value returned for C<$label> is
621 undefined, the value of C<$url> is used as the label.
622
623 =item C<no_toc>
624
625 Causes the item to not be printed to the index or return in the C<toc> field.
626
627 =back
628
629 This method returns a hash ref with a table of contents (the
630 C<toc> field) suitable for a reStructuredText table of contents.
631
632 E.g.,
633
634 my $conv = Pod::POM::View::Restructured->new;
635
636 my $files = [
637 { source_file => "$base_dir/Restructured.pm" },
638 { source_file => "$base_dir/DWIW.pm" },
639 { source_file => "$base_dir/Wrapper.pm" },
640 ];
641
642 my $rv = $conv->convert_files($files, "$dest_dir/index.rst", 'My Big Test', $dest_dir);
643
644 =head1 POD commands specifically for reStructuredText
645
646 The following sequences can be used in POD to request actions specifically for this module.
647
648 =head2 =Z<>for pod2rst next-code-block: I<lang>
649
650 This sets up the next verbatim section, i.e., the next indented
651 paragraph to be highlighted according to the syntax of the
652 programming/markup/config language I<lang>. Verbatim sections
653 are assumed to be Perl code by default. Sphinx uses Pygments to
654 do syntax highlighting in these sections, so you can use any value
655 for I<lang> that Pygments supports, e.g., Python, C, C++,
656 Javascript, SQL, etc.
657
658 =head1 EXAMPLES
659
660 =over 4
661
662 =item Converting a single file using C<pod2rst>
663
664 =for pod2rst next-code-block: bash
665
666 pod2rst --infile=Restructured.pm --outfile=restructured.rst
667
668 =back
669
670 B<Need to document:>
671
672 =over 4
673
674 =item B<Document example of setting up sphinx build, generating rst from pod, and building>
675
676 =back
677
678 =head1 TODO
679
680 =over 4
681
682 =item code highlighting
683
684 Currently, a verbatim block (indented paragraph) gets output as a
685 Perl code block by default in reStructuredText. There should be
686 an option (e.g., in the constructor) to change the language for
687 highlighting purposes (for all verbatim blocks), or disable syntax
688 highlighting and just make it a preformatted paragraph. There is a
689 way to do this in POD (see L</"POD commands specifically for reStructuredText">),
690 but there should also be an option in the constructor.
691
692 =item improve escaping
693
694 Text blocks are not escaped properly, so it is currently possible
695 to invoke a command in reStructuredText by accident.
696
697 =back
698
699 =head1 DEPENDENCIES
700
701 Inherits from L<Pod::POM::View::Text> that comes with the Pod::POM distribution.
702
703 =head1 SEE ALSO
704
705 L<Pod::POM>
706
707 L<Pod::POM::View::HTML>
708
709 L<pod2rst> (distributed with Pod::POM::View::HTML)
710
711 reStructuredText: L<http://docutils.sourceforge.net/rst.html>
712
713 Sphinx (uses reStructuredText): L<https://www.sphinx-doc.org/>
714
715 Pygments (used by Sphinx for syntax highlighting): L<https://pygments.org/>
716
717 =head1 AUTHORS
718
719 Don Owens <[email protected]>
720
721 Jeff Fearn <[email protected]>
722
723 Alex Muntada <[email protected]>
724
725 =head1 COPYRIGHT
726
727 This software is copyright (c) 2010 by Don Owens <[email protected]>, 2016 by Jeff Fearn <[email protected]>, and 2016-2026 by Alex Muntada <[email protected]>.
728
729 This software is available under the same terms as the perl 5 programming language system itself.
730
731 =head1 VERSION
732
733 1.000004
734
735 =cut