forked from hercynium/dist-zilla-plugin-readmefrompod
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ReadmeAnyFromPod.pm
509 lines (393 loc) · 13.8 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use strict;
use warnings;
package Dist::Zilla::Plugin::ReadmeAnyFromPod;
# ABSTRACT: Automatically convert POD to a README in any format for Dist::Zilla
use List::Util 1.33 qw( none first );
use Moose::Util::TypeConstraints qw(enum);
use Moose;
use MooseX::Has::Sugar;
use Path::Tiny 0.004;
use Scalar::Util 'blessed';
with 'Dist::Zilla::Role::AfterBuild',
'Dist::Zilla::Role::AfterRelease',
'Dist::Zilla::Role::FileGatherer',
'Dist::Zilla::Role::FileMunger',
'Dist::Zilla::Role::FilePruner',
'Dist::Zilla::Role::FileWatcher',
'Dist::Zilla::Role::PPI',
;
# TODO: Should these be separate modules?
our $_types = {
pod => {
filename => 'README.pod',
parser => sub {
return $_[0];
},
},
text => {
filename => 'README',
parser => sub {
my $pod = $_[0];
require Pod::Simple::Text;
Pod::Simple::Text->VERSION('3.23');
my $parser = Pod::Simple::Text->new;
$parser->output_string( \my $content );
$parser->parse_characters(1);
$parser->parse_string_document($pod);
return $content;
},
},
markdown => {
filename => 'README.mkdn',
parser => sub {
my $pod = $_[0];
require Pod::Markdown;
Pod::Markdown->VERSION('2.000');
my $parser = Pod::Markdown->new();
$parser->output_string( \my $content );
$parser->parse_characters(1);
$parser->parse_string_document($pod);
return $content;
},
},
gfm => {
filename => 'README.md',
parser => sub {
my $pod = $_[0];
require Pod::Markdown::Github;
Pod::Markdown->VERSION('0.01');
my $parser = Pod::Markdown::Github->new();
$parser->output_string( \my $content );
$parser->parse_characters(1);
$parser->parse_string_document($pod);
return $content;
},
},
html => {
filename => 'README.html',
parser => sub {
my $pod = $_[0];
require Pod::Simple::HTML;
Pod::Simple::HTML->VERSION('3.23');
my $parser = Pod::Simple::HTML->new;
$parser->output_string( \my $content );
$parser->parse_characters(1);
$parser->parse_string_document($pod);
return $content;
}
}
};
=attr type
The file format for the readme. Supported types are "text",
"markdown", "gfm" (Github-flavored markdown), "pod", and "html". Note
that you are not advised to create a F<.pod> file in the dist itself,
as L<ExtUtils::MakeMaker> will install that, both into C<PERL5LIB> and
C<MAN3DIR>.
=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. If the main module
has a companion ".pod" file with the same basename, that is used as
the default instead.
=cut
has source_filename => (
ro, lazy,
isa => 'Str',
builder => '_build_source_filename',
);
sub _build_source_filename {
my $self = shift;
my $pm = $self->zilla->main_module->name;
(my $pod = $pm) =~ s/\.pm$/\.pod/;
return -e $pod ? $pod : $pm;
}
=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
If you want to generate the same README file in both the build
directory and the root directory, simply generate it in the build
directory and use the
L<C<[CopyFilesFromBuild]>|Dist::Zilla::Plugin::CopyFilesFromBuild>
plugin to copy it to the dist root.
=cut
has location => (
ro, lazy,
isa => enum([qw(build root)]),
default => sub { $_[0]->__from_name()->[1] || 'build' },
);
=attr phase
At what phase to generate the README file. Choices are:
=over 4
=item build
(Default) This generates the README at 'after build' time. A new
README will be generated each time you build the dist.
=item release
This generates the README at 'after release' time. Note that this is
too late to get the file into the generated tarball, and is therefore
incompatible with C<location = build>. However, this is ideal if you
are using C<location = root> and only want to update the README upon
each release of your module.
=back
=cut
has phase => (
ro, lazy,
isa => enum([qw(build release)]),
default => 'build',
);
=for Pod::Coverage BUILD
=cut
sub BUILD {
my $self = shift;
$self->log_fatal('You cannot use location=build with phase=release!')
if $self->location eq 'build' and $self->phase eq 'release';
$self->log('You are creating a .pod directly in the build - be aware that this will be installed like a .pm file and as a manpage')
if $self->location eq 'build' and $self->type eq 'pod';
}
=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 none { $_->name eq $filename } @{ $self->zilla->files }
) {
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 }) {
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 = first { $_->name eq $filename } @{ $self->zilla->files };
if ($file) {
$self->munge_file($file);
}
else {
$self->log_fatal(
"Could not find a $filename file during the build"
. ' - did you prune it away with a PruneFiles block?' );
}
}
return;
}
=method munge_file
Edits the content into the requested README file in the dist.
=cut
my %watching;
sub munge_file {
my ($self, $target_file) = @_;
# Ensure that we repeat the munging if the source file is modified
# after we run.
my $source_file = $self->_source_file();
$self->watch_file($source_file, sub {
my ($self, $watched_file) = @_;
# recalculate the content based on the updates
$self->log('someone tried to munge ' . $watched_file->name . ' after we read from it. Making modifications again...');
$self->munge_file($target_file);
}) if not $watching{$source_file->name}++;
$self->log_debug([ 'ReadmeAnyFromPod updating contents of %s in dist', $target_file->name ]);
$target_file->content($self->get_readme_content);
return;
}
=method after_build
Create the requested README file at build time, if requested.
=cut
sub after_build {
my $self = shift;
$self->_create_readme if $self->phase eq 'build';
}
=method after_release
Create the requested README file at release time, if requested.
=cut
sub after_release {
my $self = shift;
$self->_create_readme if $self->phase eq 'release';
}
sub _create_readme {
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();
my $destination_file = path($self->zilla->root)->child($filename);
if (-e $destination_file) {
$self->log("overriding $filename in root");
}
my $encoding = $self->_get_source_encoding();
$destination_file->spew_raw(
$encoding eq 'raw'
? $content
: do { require Encode; Encode::encode($encoding, $content) }
);
}
return;
}
sub _source_file {
my ($self) = shift;
my $filename = $self->source_filename;
first { $_->name eq $filename } @{ $self->zilla->files };
}
# Holds the contents of the source file as of the last time we
# generated a readme from it. We use this to detect when the source
# file is modified so we can update the README file again.
has _last_source_content => (
is => 'rw', isa => 'Str',
default => '',
);
sub _get_source_pod {
my ($self) = shift;
my $source_file = $self->_source_file;
# cache contents before we alter it, for later comparison
$self->_last_source_content($source_file->content);
require PPI::Document; # for Dist::Zilla::Role::PPI < 5.009
my $doc = $self->ppi_document_for_file($source_file);
my $pod_elems = $doc->find('PPI::Token::Pod');
my $pod_content = "";
if ($pod_elems) {
# Concatenation should stringify it
$pod_content .= PPI::Token::Pod->merge(@$pod_elems);
}
if ((my $encoding = $self->_get_source_encoding) ne 'raw'
and not eval { Dist::Zilla::Role::PPI->VERSION('6.003') }
) {
# older Dist::Zilla::Role::PPI passes encoded content to PPI
require Encode;
$pod_content = Encode::decode($encoding, $pod_content);
}
return $pod_content;
}
sub _get_source_encoding {
my ($self) = shift;
my $source_file = $self->_source_file;
return
$source_file->can('encoding')
? $source_file->encoding
: 'raw'; # Dist::Zilla pre-5.0
}
=method get_readme_content
Get the content of the README in the desired format.
=cut
sub get_readme_content {
my ($self) = shift;
my $source_pod = $self->_get_source_pod();
my $parser = $_types->{$self->type}->{parser};
# Save the POD text used to generate the README.
return $parser->($source_pod);
}
{
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