/
PodPurler.pm
191 lines (154 loc) · 4.75 KB
/
PodPurler.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package Dist::Zilla::Plugin::PodPurler;
# ABSTRACT: like PodWeaver, but more erratic and amateurish
use Moose;
use Moose::Autobox;
use List::MoreUtils qw(any);
with 'Dist::Zilla::Role::FileMunger';
use namespace::autoclean;
use Pod::Elemental 0.092930;
use Pod::Elemental::Selectors -all;
use Pod::Elemental::Transformer::Pod5;
use Pod::Elemental::Transformer::Nester;
use Pod::Elemental::Transformer::Gatherer;
=head1 WARNING
This code is really, really sketchy. It's crude and brutal and will probably
break whatever it is you were trying to do.
Unlike L<Dist::Zilla::Plugin::PodWeaver|Dist::Zilla::Plugin::PodWeaver>, this
code will not get awesome. In fact, it's just the old PodWeaver code, spun out
(no pun intended) so that RJBS can use it while he fixes PodWeaver-related
things.
=head1 DESCRIPTION
PodPurler ress, which rips apart your kinda-POD and reconstructs it as boring
old real POD.
=cut
sub munge_file {
my ($self, $file) = @_;
return $self->munge_pod($file)
if $file->name =~ /\.(?:pm|pod)$/i
and ($file->name !~ m{/} or $file->name =~ m{^lib/});
return;
}
sub munge_perl_string {
my ($self, $doc, $arg) = @_;
my $document = $doc->{pod};
Pod::Elemental::Transformer::Pod5->new->transform_node($document);
my $nester = Pod::Elemental::Transformer::Nester->new({
top_selector => s_command([ qw(head1 method attr) ]),
content_selectors => [
s_flat,
s_command( [ qw(head2 head3 head4 over item back) ]),
],
});
$nester->transform_node($document);
for my $pair (
[ method => 'METHODS' ],
[ attr => 'ATTRIBUTES' ],
) {
my $sel = s_command($pair->[0]);
if ($document->children->grep($sel)->length) {
my $gatherer = Pod::Elemental::Transformer::Gatherer->new({
gather_selector => $sel,
container => Pod::Elemental::Element::Nested->new({
command => 'head1',
content => "$pair->[1]\n",
}),
});
$gatherer->transform_node($document);
$gatherer->container->children->grep($sel)->each_value(sub {
$_->command('head2');
});
}
}
unless (
$document->children->grep(sub {
s_command('head1', $_) and $_->content eq "VERSION\n"
})->length
) {
my $version_section = Pod::Elemental::Element::Nested->new({
command => 'head1',
content => "VERSION\n",
children => [
Pod::Elemental::Element::Pod5::Ordinary->new({
content => sprintf "version %s\n", $self->zilla->version,
}),
],
});
$document->children->unshift($version_section);
}
unless (
$document->children->grep(sub {
s_command('head1', $_) and $_->content eq "NAME\n"
})->length
) {
Carp::croak "couldn't find package declaration in " . $arg->{filename}
unless my $pkg_node = $doc->{ppi}->find_first('PPI::Statement::Package');
my $package = $pkg_node->namespace;
$self->log("couldn't find abstract in " . $arg->{filename})
unless my ($abstract) = $doc->{ppi} =~ /^\s*#+\s*ABSTRACT:\s*(.+)$/m;
my $name = $package;
$name .= " - $abstract" if $abstract;
my $name_section = Pod::Elemental::Element::Nested->new({
command => 'head1',
content => "NAME\n",
children => [
Pod::Elemental::Element::Pod5::Ordinary->new({
content => "$name\n",
}),
],
});
$document->children->unshift($name_section);
}
unless (
$document->children->grep(sub {
s_command('head1', $_) and $_->content =~ /\AAUTHORS?\n\z/
})->length
) {
my @authors = $self->zilla->authors->flatten;
my $name = @authors > 1 ? 'AUTHORS' : 'AUTHOR';
my $author_section = Pod::Elemental::Element::Nested->new({
command => 'head1',
content => "$name\n",
children => [
Pod::Elemental::Element::Pod5::Ordinary->new({
content => join("\n", @authors) . "\n"
}),
],
});
$document->children->push($author_section);
}
unless (
$document->children->grep(sub {
s_command('head1', $_) and $_->content =~ /\A(?:COPYRIGHT|LICENSE)\n\z/
})->length
) {
my $legal_section = Pod::Elemental::Element::Nested->new({
command => 'head1',
content => "COPYRIGHT AND LICENSE\n",
children => [
Pod::Elemental::Element::Pod5::Ordinary->new({
content => $self->zilla->license->notice
}),
],
});
$document->children->push($legal_section);
}
return {
pod => $document,
ppi => $doc->{ppi},
};
}
sub munge_pod {
my ($self, $file) = @_;
my $content = $file->content;
my $new_content = $self->munge_perl_string(
$content,
{
filename => $file->name,
},
);
$file->content($new_content);
}
with 'Pod::Elemental::PerlMunger';
__PACKAGE__->meta->make_immutable;
no Moose;
1;