summaryrefslogtreecommitdiffstats
path: root/lib/Pod/Weaver/Section/Template.pm
blob: 469a396ab47131a3f3bec89001f2da277bc47e5e (plain) (blame)
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 Pod::Weaver::Section::Template;
use Moose;
# ABSTRACT: add pod section from a Text::Template template

with 'Pod::Weaver::Role::Section';

use Text::Template 'fill_in_file';

=head1 SYNOPSIS

  # weaver.ini
  [Template / SUPPORT]
  template = ~/.dzil/pod_templates/support.section
  main_module_only = 1

=head1 DESCRIPTION

This plugin generates a pod section based on the contents of a template file.
The template is parsed using L<Text::Template>, and is then interpreted as pod.
When parsing the template, any options specified in the plugin configuration
which aren't configuration options for this plugin will be provided as
variables for the template. Also, if this is being run as part of a
L<Dist::Zilla> build process, the values of all of the attributes on the
C<zilla> object will be available as variables, and the additional variable
C<$main_module_name> will be defined as the module name for the C<main_module>
file.

=cut

use Moose::Util::TypeConstraints;

subtype 'PWST::File',
    as 'Str',
    where { !m+^~/+ && -r },
    message { "Couldn't read file $_" };
coerce 'PWST::File',
    from 'Str',
    via { s+^~/+$ENV{HOME}/+; $_ };

no Moose::Util::TypeConstraints;

=attr template

The file to be run through Text::Template and added as a pod section. Required.

=cut

has template => (
    is       => 'ro',
    isa      => 'PWST::File',
    required => 1,
    coerce   => 1,
);

=attr header

The section header. Defaults to the plugin name.

=cut

has header => (
    is      => 'ro',
    isa     => 'Str',
    default => sub { shift->plugin_name },
);

=attr main_module_only

If L<Pod::Weaver> is being run through L<Dist::Zilla>, this option determines
whether to add the section to each module in the distribution, or to just the
distribution's main module. Defaults to false.

=cut

has main_module_only => (
    is  => 'ro',
    isa => 'Bool',
);

has delim => (
    is      => 'ro',
    isa     => 'ArrayRef[Str]',
    default => sub { ['{{', '}}'] },
);

has extra_args => (
    is  => 'rw',
    isa => 'HashRef',
);

sub BUILD {
    my $self = shift;
    my ($args) = @_;
    my $copy = {%$args};
    delete $copy->{$_}
        for map { $_->init_arg } $self->meta->get_all_attributes;
    $self->extra_args($copy);
}

sub main_module_name {
    my $self = shift;
    my ($zilla) = @_;
    return unless $zilla;

    my $main_module_name = $zilla->main_module->name;
    my $root = $zilla->root->stringify;
    $main_module_name =~ s:^\Q$root::;
    $main_module_name =~ s:lib/::;
    $main_module_name =~ s+/+::+g;
    $main_module_name =~ s/\.pm//;

    return $main_module_name;
}

sub parse_pod {
    my $self = shift;
    my ($pod) = @_;

    # ensure it's a valid pod document
    $pod = "=pod\n\n$pod\n";

    my $children = Pod::Elemental->read_string($pod)->children;

    # but strip off the bits we had to add
    shift @$children
        while $children->[0]->isa('Pod::Elemental::Element::Generic::Blank')
           || ($children->[0]->isa('Pod::Elemental::Element::Generic::Command')
            && $children->[0]->command eq 'pod');
    my $blank;
    $blank = pop @$children
        while $children->[-1]->isa('Pod::Elemental::Element::Generic::Blank');
    push @$children, $blank
        if $blank;

    return $children;
}

sub get_zilla_hash {
    my $self = shift;
    my ($zilla) = @_;
    my %zilla_hash;
    for my $attr ($zilla->meta->get_all_attributes) {
        next if $attr->get_read_method =~ /^_/;
        my $value = $attr->get_value($zilla);
        $zilla_hash{$attr->name} = blessed($value) ? \$value : $value;
    }
    $zilla_hash{main_module_name} = $self->main_module_name($zilla);
    return %zilla_hash;
}

sub weave_section {
    my $self = shift;
    my ($document, $input) = @_;

    my $zilla = $input->{zilla};

    if ($self->main_module_only) {
        return if $zilla && $zilla->main_module->name ne $input->{filename};
    }

    die "Couldn't find file " . $self->template
        unless -r $self->template;
    my $pod = fill_in_file(
        $self->template,
        DELIMITERS => $self->delim,
        HASH       => {
            $zilla ? ($self->get_zilla_hash($zilla)) : (),
            %{ $self->extra_args },
        },
    );

    push @{ $document->children },
        Pod::Elemental::Element::Nested->new(
            command  => 'head1',
            content  => $self->header,
            children => $self->parse_pod($pod),
        );
}

__PACKAGE__->meta->make_immutable;
no Moose;

=head1 SEE ALSO

L<Text::Template>
L<Pod::Weaver>
L<Dist::Zilla>

=cut

1;