forked from avar/dist-zilla-plugin-readmefrompod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadmeAnyFromPod.pm
390 lines (297 loc) · 10.5 KB
/
ReadmeAnyFromPod.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use strict;
use warnings;
package Dist::Zilla::Plugin::ReadmeAnyFromPod;
# ABSTRACT: Automatically convert POD to a README in any format for Dist::Zilla
use Moose;
use Moose::Autobox;
use MooseX::Has::Sugar;
use Moose::Util::TypeConstraints qw(enum);
use IO::Handle;
use Encode qw( encode );
use Scalar::Util 'blessed';
with 'Dist::Zilla::Role::FileGatherer';
with 'Dist::Zilla::Role::FileMunger';
with 'Dist::Zilla::Role::FilePruner';
with 'Dist::Zilla::Role::AfterBuild';
our $_types = {
text => {
filename => 'README',
parser => sub {
my $mmcontent = $_[0];
require Pod::Text;
my $parser = Pod::Text->new();
$parser->output_string( \my $input_content );
$parser->parse_string_document( $mmcontent );
my $content;
if( defined $parser->{encoding} ){
$content = encode( $parser->{encoding} , $input_content );
} else {
$content = $input_content;
}
return $content;
},
},
markdown => {
filename => 'README.mkdn',
parser => sub {
my $mmcontent = $_[0];
require Pod::Markdown;
my $parser = Pod::Markdown->new();
require IO::Scalar;
my $input_handle = IO::Scalar->new(\$mmcontent);
$parser->parse_from_filehandle($input_handle);
my $content = $parser->as_markdown();
return $content;
},
},
pod => {
filename => 'README.pod',
parser => sub {
my $mmcontent = $_[0];
require Pod::Select;
require IO::Scalar;
my $input_handle = IO::Scalar->new(\$mmcontent);
my $content = '';
my $output_handle = IO::Scalar->new(\$content);
my $parser = Pod::Select->new();
$parser->parse_from_filehandle($input_handle, $output_handle);
return $content;
},
},
html => {
filename => 'README.html',
parser => sub {
my $mmcontent = $_[0];
require Pod::Simple::HTML;
my $parser = Pod::Simple::HTML->new;
my $content;
$parser->output_string( \$content );
$parser->parse_string_document($mmcontent);
return $content;
}
}
};
=attr type
The file format for the readme. Supported types are "text", "markdown", "pod", and "html".
=cut
has type => (
ro, lazy,
isa => enum([keys %$_types]),
default => sub { $_[0]->__from_name()->[0] || 'text' },
);
=attr filename
The file name of the README file to produce. The default depends on the selected format.
=cut
has filename => (
ro, lazy,
isa => 'Str',
default => sub { $_types->{$_[0]->type}->{filename}; }
);
=attr source_filename
The file from which to extract POD for the content of the README.
The default is the file of the main module of the dist.
=cut
has source_filename => (
ro, lazy,
isa => 'Str',
default => sub { shift->zilla->main_module->name; },
);
=attr location
Where to put the generated README file. Choices are:
=over 4
=item build
This puts the README in the directory where the dist is currently
being built, where it will be incorporated into the dist.
=item root
This puts the README in the root directory (the same directory that
contains F<dist.ini>). The README will not be incorporated into the
built dist.
=back
=cut
has location => (
ro, lazy,
isa => enum([qw(build root)]),
default => sub { $_[0]->__from_name()->[1] || 'build' },
);
=method gather_files
We create the file early, so other plugins that need to have the full list of
files are aware of what we will be generating.
=cut
sub gather_files {
my ($self) = @_;
my $filename = $self->filename;
if ( $self->location eq 'build'
# allow for the file to also exist in the dist
and not @{$self->zilla->files->grep( sub { $_->name eq $filename })}
) {
require Dist::Zilla::File::InMemory;
my $file = Dist::Zilla::File::InMemory->new({
content => 'this will be overwritten',
name => $self->filename,
});
$self->add_file($file);
}
return;
}
=method prune_files
Files with C<location = root> must also be pruned, so that they don't
sneak into the I<next> build by virtue of already existing in the root
dir. (The alternative is that the user doesn't add them to the build in the
first place, with an option to their C<GatherDir> plugin.)
=cut
sub prune_files {
my ($self) = @_;
# leave the file in the dist if another instance of us is adding it there.
if ($self->location eq 'root'
and not grep {
blessed($self) eq blessed($_)
and $_->location eq 'build'
and $_->filename eq $self->filename
} @{$self->zilla->plugins}) {
for my $file ($self->zilla->files->flatten) {
next unless $file->name eq $self->filename;
$self->log_debug([ 'pruning %s', $file->name ]);
$self->zilla->prune_file($file);
}
}
return;
}
=method munge_files
=cut
sub munge_files {
my $self = shift;
if ( $self->location eq 'build' ) {
my $filename = $self->filename;
my $file = $self->zilla->files->grep( sub { $_->name eq $filename } )->head;
$self->munge_file($file);
}
return;
}
=method munge_file
Edits the content into the requested README file in the dist.
=cut
sub munge_file {
my ($self, $file) = @_;
$self->log_debug([ 'ReadmeAnyFromPod updating contents of %s in dist', $file->name ]);
$file->content($self->get_readme_content);
return;
}
=method after_build
Create the requested README file in the root.
=cut
sub after_build {
my $self = shift;
if ( $self->location eq 'root' ) {
my $filename = $self->filename;
$self->log_debug([ 'ReadmeAnyFromPod updating contents of %s in root', $filename ]);
my $content = $self->get_readme_content();
require File::Slurp;
my $file = $self->zilla->root->file($filename);
if (-e $file) {
$self->log("overriding $filename in root");
}
File::Slurp::write_file("$file", {binmode => ':raw'}, $content);
}
return;
}
sub _file_from_filename {
my ($self, $filename) = @_;
for my $file ($self->zilla->files->flatten) {
return $file if $file->name eq $filename;
}
die 'no README found (place [ReadmeAnyFromPod] below [Readme] in dist.ini)!';
}
# possibly set more than once, as other plugins modify the source content
has _readme_content => (
is => 'rw', isa => 'Str',
default => '',
);
=method get_readme_content
Get the content of the README in the desired format.
=cut
sub get_readme_content {
my ($self) = shift;
my $source_file = $self->_file_from_filename($self->source_filename);
my $callcount = 0;
if (not $source_file->does('Dist::Zilla::Role::File::ChangeNotification'))
{
require Dist::Zilla::Role::File::ChangeNotification;
Dist::Zilla::Role::File::ChangeNotification->meta->apply($source_file);
my $plugin = $self;
$source_file->on_changed(sub {
my ($self, $newcontent) = @_;
# recalculate the content based on the updates, provided it isn't
# ourselves that triggered this call
if ($newcontent ne $plugin->_readme_content)
{
$plugin->log('someone tried to munge ' . $source_file->name . ' after we read from it. Making modifications again...');
$plugin->munge_file($self);
}
});
$source_file->watch_file;
}
my $mmcontent = $source_file->content;
my $parser = $_types->{$self->type}->{parser};
$self->_readme_content($parser->($mmcontent));
}
{
my %cache;
sub __from_name {
my ($self) = @_;
my $name = $self->plugin_name;
# Use cached values if available
if ($cache{$name}) {
return $cache{$name};
}
# qr{TYPE1|TYPE2|...}
my $type_regex = join('|', map {quotemeta} keys %$_types);
# qr{LOC1|LOC2|...}
my $location_regex = join('|', map {quotemeta} qw(build root));
# qr{(?:Readme)? (TYPE1|TYPE2|...) (?:In)? (LOC1|LOC2|...) }x
my $complete_regex = qr{ (?:Readme)? ($type_regex) (?:(?:In)? ($location_regex))? }ix;
my ($type, $location) = (lc $name) =~ m{(?:\A|/) \s* $complete_regex \s* \Z}ix;
$cache{$name} = [$type, $location];
return $cache{$name};
}
}
__PACKAGE__->meta->make_immutable;
__END__
=head1 SYNOPSIS
In your F<dist.ini>
[ReadmeAnyFromPod]
; Default is plaintext README in build dir
; Using non-default options: POD format with custom filename in
; dist root, outside of build. Including this README in version
; control makes Github happy.
[ReadmeAnyFromPod / ReadmePodInRoot]
type = pod
filename = README.pod
location = root
; Using plugin name autodetection: Produces README.html in root
[ ReadmeAnyFromPod / HtmlInRoot ]
=head1 DESCRIPTION
Generates a README for your L<Dist::Zilla> powered dist from its
C<main_module> in any of several formats. The generated README can be
included in the build or created in the root of your dist for e.g.
inclusion into version control.
=head2 PLUGIN NAME AUTODETECTION
If you give the plugin an appropriate name (a string after the slash)
in your dist.ini, it will can parse the C<type> and C<location>
attributes from it. The format is "Readme[TYPE]In[LOCATION]". The
words "Readme" and "In" are optional, and the whole name is
case-insensitive. The SYNOPSIS section above gives one example.
When run with C<location = dist>, this plugin runs in the C<FileMunger> phase
to create the new file. If it runs before another C<FileMunger> plugin does,
that happens to modify the input pod (like, say,
L<C<[PodWeaver]>|Dist::Zilla::Plugin::PodWeaver>), the README file contents
will be recalculated, along with a warning that you should modify your
F<dist.ini> by referencing C<[ReadmeAnyFromPod]> lower down in the file (the
build still works, but is less efficient).
=head1 BUGS AND LIMITATIONS
Please report any bugs or feature requests to
C<rct+perlbug@thompsonclan.org>.
=head1 SEE ALSO
=for :list
* L<Dist::Zilla::Plugin::ReadmeFromPod> - The base for this module
* L<Dist::Zilla::Plugin::ReadmeMarkdownFromPod> - Functionality subsumed by this module
* L<Dist::Zilla::Plugin::CopyReadmeFromBuild> - Functionality partly subsumed by this module