forked from avar/dist-zilla-plugin-readmefrompod
/
ReadmeAnyFromPod.pm
270 lines (205 loc) · 6.67 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
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 );
with 'Dist::Zilla::Role::InstallTool';
with 'Dist::Zilla::Role::FilePruner';
my $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 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 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 thr root
dir.
=cut
sub prune_files {
my ($self) = @_;
if ($self->location eq 'root') {
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 setup_installer
Adds the requested README file to the dist.
=cut
sub setup_installer {
my ($self) = @_;
require Dist::Zilla::File::InMemory;
my $content = $self->get_readme_content();
my $filename = $self->filename;
my $file = $self->zilla->files->grep( sub { $_->name eq $filename } )->head;
if ( $self->location eq 'build' ) {
if ( $file ) {
$file->content( $content );
$self->zilla->log("Override $filename in build from [ReadmeAnyFromPod]");
} else {
$file = Dist::Zilla::File::InMemory->new({
content => $content,
name => $filename,
});
$self->add_file($file);
}
}
elsif ( $self->location eq 'root' ) {
require File::Slurp;
my $file = $self->zilla->root->file($filename);
if (-e $file) {
$self->zilla->log("Override $filename in root from [ReadmeAnyFromPod]");
}
File::Slurp::write_file("$file", {binmode => ':raw'}, $content);
}
else {
die "Unknown location specified";
}
return;
}
=method get_readme_content
Get the content of the README in the desired format.
=cut
sub get_readme_content {
my ($self) = shift;
my $mmcontent = $self->zilla->main_module->content;
my $parser = $types->{$self->type}->{parser};
my $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}};
}
my $type_regex = join('|', map {quotemeta} keys %$types);
my $location_regex = join('|', map {quotemeta} qw(build root));
my $complete_regex = qr{($type_regex)(?:(?:In)?($location_regex))?}i;
my ($type, $location) = (lc $name) =~ m{\A $complete_regex \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
=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.
=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