Dist-Zilla-Plugin-InsertExample-FromMojoTemplates
view release on metacpan or search on metacpan
lib/Dist/Zilla/Plugin/InsertExample/FromMojoTemplates.pm view on Meta::CPAN
has example_directory => (
is => 'ro',
isa => 'Str',
default => sub { 'examples' },
);
sub gather_files {
my $self = shift;
my $arg = shift;
return if !$self->make_examples;
my $html_template_path = (File::Find::Rule->file->name('template.html')->in($self->directory))[0];
my $html_template = path($html_template_path)->slurp;
my @paths = File::Find::Rule->file->name(qr/@{[ $self->filepattern ]}/)->in($self->directory);
foreach my $path (@paths) {
my $contents = MojoX::CustomTemplateFileParser->new(path => path($path)->absolute->canonpath, output => ['Html'])->to_html;
$contents = $html_template =~ s{\[EXAMPLES\]}{$contents}r;
my $filename = path($path)->basename(qr{\.[^.]+});
my $file = Dist::Zilla::File::InMemory->new(
name => ''.path($self->example_directory)->child("$filename.html"),
content => $contents,
);
$self->add_file($file);
}
return;
}
sub munge_files {
my $self = shift;
$self->munge_file($_) for @{ $self->found_files };
}
sub munge_file {
my $self = shift;
my $file = shift;
my $content = $file->content;
my $re = $self->filepattern;
if($content =~ m{# \s* EXAMPLE: \s* ($re):(.*)}xm) {
my $linere = qr{^\s*#\s*EXAMPLE:\s*([^:]+):(.*)$};
my @lines = grep { m{$linere} } split /\n/ => $content;
my $newcontent = $content;
LINE:
foreach my $line (@lines) {
$line =~ m{$linere};
my $filename = $1;
my $what = $2;
$what =~ s{ }{}g;
$what =~ s{,,+}{,}g;
my @configs = split m/,/ => $what;
my @wanted = ();
my @unwanted = ();
my $all = 0;
my $want_all_examples = 0;
CONFIG:
foreach my $config (@configs) {
if($config eq 'examples') {
$want_all_examples = 1;
}
elsif($config eq 'all') {
$all = 1;
}
elsif($config =~ m{^ (!)? (\d+) (?:-(\d+))? }x) {
my $exclude = defined $1 ? 1 : 0;
my $first = $2;
my $second = $3 || $first;
map { push @wanted => $_ } ($first..$second) if !$exclude;
map { push @unwanted => $_ } ($first..$second) if $exclude;
}
}
my $parser = MojoX::CustomTemplateFileParser->new( path => path($self->directory)->child($filename)->absolute->canonpath, output => ['Pod'] );
my $testcount = $parser->test_count;
@wanted = (1..$testcount) if $all || $want_all_examples;
my %unwanted;
$unwanted{ $_ } = 1 for @unwanted;
@wanted = grep { !exists $unwanted{ $_ } } @wanted;
my $tomunge = '';
foreach my $test (@wanted) {
$tomunge .= $parser->to_pod($test, $want_all_examples);
}
my $success = $newcontent =~ s{$line}{$tomunge};
}
if($newcontent ne $content) {
$file->content($newcontent);
}
}
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dist::Zilla::Plugin::InsertExample::FromMojoTemplates - Creates POD examples from a custom template format (deprecated)
( run in 0.911 second using v1.01-cache-2.11-cpan-71847e10f99 )