| 1 | # Copyrights 2003-2021 by [Mark Overmeer]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of perl distribution OODoc. It is licensed under the | ||
| 6 | # same terms as Perl itself: https://spdx.org/licenses/Artistic-2.0.html | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | package OODoc::Format::Pod3; | ||
| 12 | use vars '$VERSION'; | ||
| 13 | $VERSION = '2.02'; | ||
| 14 | |||
| 15 | use base 'OODoc::Format::Pod'; | ||
| 16 | |||
| 17 | use Log::Report 'oodoc'; | ||
| 18 | |||
| 19 | use OODoc::Template (); | ||
| 20 | use List::Util qw/first/; | ||
| 21 | |||
| 22 | |||
| 23 | my $default_template; | ||
| 24 | { local $/; | ||
| 25 | $default_template = <DATA>; | ||
| 26 | close DATA; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub createManual(@) | ||
| 30 | { my ($self, %args) = @_; | ||
| 31 | $self->{O_template} = delete $args{template} || \$default_template; | ||
| 32 | $self->SUPER::createManual(%args); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub formatManual(@) | ||
| 36 | { my ($self, %args) = @_; | ||
| 37 | my $output = delete $args{output}; | ||
| 38 | |||
| 39 | my $template = OODoc::Template->new | ||
| 40 | ( markers => [ '<{', '}>' ] | ||
| 41 | , manual_obj => delete $args{manual} | ||
| 42 | , chapter_order => | ||
| 43 | [ qw/NAME INHERITANCE SYNOPSIS DESCRIPTION OVERLOADED METHODS | ||
| 44 | FUNCTIONS CONSTANTS EXPORTS DIAGNOSTICS DETAILS REFERENCES | ||
| 45 | COPYRIGHTS/ | ||
| 46 | ] | ||
| 47 | , %args | ||
| 48 | ); | ||
| 49 | |||
| 50 | $output->print | ||
| 51 | ( scalar $template->process | ||
| 52 | ( $self->{O_template} | ||
| 53 | , manual => sub { shift; ( {}, @_ ) } | ||
| 54 | , chapters => sub { $self->chapters($template, @_) } | ||
| 55 | , sections => sub { $self->sections($template, @_) } | ||
| 56 | , subsections => sub { $self->subsections($template, @_) } | ||
| 57 | , subsubsections => sub { $self->subsubsections($template, @_) } | ||
| 58 | , subroutines => sub { $self->subroutines($template, @_) } | ||
| 59 | , diagnostics => sub { $self->diagnostics($template, @_) } | ||
| 60 | ) | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub structure($$$) | ||
| 66 | { my ($self, $template, $type, $object) = @_; | ||
| 67 | |||
| 68 | my $manual = $template->valueFor('manual_obj'); | ||
| 69 | my $descr = $self->cleanup($manual, $object->description); | ||
| 70 | my $name = $object->name; | ||
| 71 | |||
| 72 | $descr =~ s/\n*$/\n\n/ | ||
| 73 | if defined $descr && length $descr; | ||
| 74 | |||
| 75 | my @examples; | ||
| 76 | foreach my $example ($object->examples) | ||
| 77 | { my $title = $example->name || 'Example'; | ||
| 78 | $title = "Example: $example" if $title !~ /example/i; | ||
| 79 | $title =~ s/\s+$//; | ||
| 80 | |||
| 81 | push @examples, | ||
| 82 | +{ title => $title | ||
| 83 | , descr => $self->cleanup($manual, $example->description) | ||
| 84 | }; | ||
| 85 | } | ||
| 86 | |||
| 87 | my @extends; | ||
| 88 | |||
| 89 | unless($name eq 'NAME' || $name eq 'SYNOPSIS') | ||
| 90 | { @extends = map +{manual => $_->manual, header => $name} | ||
| 91 | , $object->extends; | ||
| 92 | } | ||
| 93 | |||
| 94 | +{ $type => $name | ||
| 95 | , $type.'_obj' => $object | ||
| 96 | , description => $descr | ||
| 97 | , examples => \@examples | ||
| 98 | , extends => \@extends | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub chapters($$$$$) | ||
| 103 | { my ($self, $template, $tag, $attrs, $then, $else) = @_; | ||
| 104 | my $manual = $template->valueFor('manual_obj'); | ||
| 105 | |||
| 106 | my @chapters | ||
| 107 | = map $self->structure($template, chapter => $_) | ||
| 108 | , $manual->chapters; | ||
| 109 | |||
| 110 | if(my $order = $attrs->{order}) | ||
| 111 | { my @order = ref $order eq 'ARRAY' ? @$order : split( /\,\s*/, $order); | ||
| 112 | my %order; | ||
| 113 | |||
| 114 | # first the pre-defined names, then the other | ||
| 115 | my $count = 1; | ||
| 116 | $order{$_} = $count++ for @order; | ||
| 117 | $order{$_->{chapter}} ||= $count++ for @chapters; | ||
| 118 | |||
| 119 | @chapters = sort { $order{$a->{chapter}} <=> $order{$b->{chapter}} } | ||
| 120 | @chapters; | ||
| 121 | } | ||
| 122 | |||
| 123 | ( \@chapters, $attrs, $then, $else ); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub sections($$$$$) | ||
| 127 | { my ($self, $template, $tag, $attrs, $then, $else) = @_; | ||
| 128 | my $chapter = $template->valueFor('chapter_obj'); | ||
| 129 | |||
| 130 | return ([], $attrs, $then, $else) | ||
| 131 | unless first {!$_->isEmpty} $chapter->sections; | ||
| 132 | |||
| 133 | my @sections | ||
| 134 | = map { $self->structure($template, section => $_) } | ||
| 135 | $chapter->sections; | ||
| 136 | |||
| 137 | ( \@sections, $attrs, $then, $else ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub subsections($$$$$) | ||
| 141 | { my ($self, $template, $tag, $attrs, $then, $else) = @_; | ||
| 142 | my $section = $template->valueFor('section_obj'); | ||
| 143 | |||
| 144 | return ([], $attrs, $then, $else) | ||
| 145 | unless first {!$_->isEmpty} $section->subsections; | ||
| 146 | |||
| 147 | my @subsections | ||
| 148 | = map { $self->structure($template, subsection => $_) } | ||
| 149 | $section->subsections; | ||
| 150 | |||
| 151 | ( \@subsections, $attrs, $then, $else ); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub subsubsections($$$$$) | ||
| 155 | { my ($self, $template, $tag, $attrs, $then, $else) = @_; | ||
| 156 | my $subsection = $template->valueFor('subsection_obj'); | ||
| 157 | |||
| 158 | return ([], $attrs, $then, $else) | ||
| 159 | unless first {!$_->isEmpty} $subsection->subsubsections; | ||
| 160 | |||
| 161 | my @subsubsections | ||
| 162 | = map { $self->structure($template, subsubsection => $_) } | ||
| 163 | $subsection->subsubsections; | ||
| 164 | |||
| 165 | ( \@subsubsections, $attrs, $then, $else ); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub subroutines($$$$$$) | ||
| 169 | { my ($self, $template, $tag, $attrs, $then, $else) = @_; | ||
| 170 | |||
| 171 | my $parent | ||
| 172 | = $template->valueFor('subsubsection_obj') | ||
| 173 | || $template->valueFor('subsection_obj') | ||
| 174 | || $template->valueFor('section_obj') | ||
| 175 | || $template->valueFor('chapter_obj'); | ||
| 176 | |||
| 177 | defined $parent | ||
| 178 | or return (); | ||
| 179 | |||
| 180 | my $out = ''; | ||
| 181 | open OUT, '>',\$out; | ||
| 182 | |||
| 183 | my @show = map +($_ => scalar $template->valueFor($_)), | ||
| 184 | qw/show_described_options show_described_subs show_diagnostics | ||
| 185 | show_examples show_inherited_options show_inherited_subs | ||
| 186 | show_option_table show_subs_index/; | ||
| 187 | |||
| 188 | # This is quite weak: the whole POD section for a sub description | ||
| 189 | # is produced outside the template. In the future, this may get | ||
| 190 | # changed: if there is a need for it: of course, we can do everything | ||
| 191 | # in the template system. | ||
| 192 | |||
| 193 | $self->showSubroutines | ||
| 194 | ( subroutines => [ $parent->subroutines ] | ||
| 195 | , manual => $parent->manual | ||
| 196 | , output => \*OUT | ||
| 197 | , @show | ||
| 198 | ); | ||
| 199 | |||
| 200 | close OUT; | ||
| 201 | length $out or return; | ||
| 202 | |||
| 203 | $out =~ s/\n*$/\n\n/; | ||
| 204 | ($out); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub diagnostics($$$$$$) | ||
| 208 | { my ($self, $template, $tag, $attrs, $then, $else) = @_; | ||
| 209 | my $manual = $template->valueFor('manual_obj'); | ||
| 210 | |||
| 211 | my $out = ''; | ||
| 212 | open OUT, '>',\$out; | ||
| 213 | $self->chapterDiagnostics(%$attrs, manual => $manual, output => \*OUT); | ||
| 214 | close OUT; | ||
| 215 | |||
| 216 | $out =~ s/\n*$/\n\n/; | ||
| 217 | ($out); | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; | ||
| 221 | |||
| 222 | __DATA__ |