| 1 | package Pod::Weaver::Plugin::Transformer 4.022; | ||
| 2 | # ABSTRACT: apply arbitrary transformers | ||
| 3 | |||
| 4 | use Moose; | ||
| 5 | with 'Pod::Weaver::Role::Dialect'; | ||
| 6 | |||
| 7 | # BEGIN BOILERPLATE | ||
| 8 | use v5.22.0; | ||
| 9 | use warnings; | ||
| 10 | use utf8; | ||
| 11 | no feature 'switch'; | ||
| 12 | use experimental qw(postderef postderef_qq); # This experiment gets mainlined. | ||
| 13 | # END BOILERPLATE | ||
| 14 | |||
| 15 | use namespace::autoclean; | ||
| 16 | |||
| 17 | use Module::Runtime qw(use_module); | ||
| 18 | use List::MoreUtils qw(part); | ||
| 19 | use String::RewritePrefix; | ||
| 20 | |||
| 21 | #pod =head1 OVERVIEW | ||
| 22 | #pod | ||
| 23 | #pod This plugin acts as a L<Pod::Weaver::Role::Dialect> that applies an arbitrary | ||
| 24 | #pod L<Pod::Elemental::Transformer> to your input document. It is configured like | ||
| 25 | #pod this: | ||
| 26 | #pod | ||
| 27 | #pod [-Transformer / Lists] | ||
| 28 | #pod transformer = List | ||
| 29 | #pod format_name = outline | ||
| 30 | #pod | ||
| 31 | #pod This will end up creating a transformer like this: | ||
| 32 | #pod | ||
| 33 | #pod my $xform = Pod::Elemental::Transformer::List->new({ | ||
| 34 | #pod format_name => 'outline', | ||
| 35 | #pod }); | ||
| 36 | #pod | ||
| 37 | #pod and that transformer will then be handed the entire input Pod document. | ||
| 38 | #pod | ||
| 39 | #pod =cut | ||
| 40 | |||
| 41 | has transformer => (is => 'ro', required => 1); | ||
| 42 | |||
| 43 | sub BUILDARGS { | ||
| 44 | my ($class, @arg) = @_; | ||
| 45 | my %copy = ref $arg[0] ? $arg[0]->%* : @arg; | ||
| 46 | |||
| 47 | my @part = part { /\A\./ ? 0 : 1 } keys %copy; | ||
| 48 | |||
| 49 | my %class_args = map { s/\A\.//; $_ => $copy{ ".$_" } } $part[0]->@*; | ||
| 50 | my %xform_args = map { $_ => $copy{ $_ } } $part[1]->@*; | ||
| 51 | |||
| 52 | my $xform_class = String::RewritePrefix->rewrite( | ||
| 53 | { '' => 'Pod::Elemental::Transformer::', '=' => '' }, | ||
| 54 | delete $xform_args{transformer}, | ||
| 55 | ); | ||
| 56 | |||
| 57 | use_module($xform_class); | ||
| 58 | |||
| 59 | my $plugin_name = delete $xform_args{plugin_name}; | ||
| 60 | my $weaver = delete $xform_args{weaver}; | ||
| 61 | |||
| 62 | my $xform = $xform_class->new(\%xform_args); | ||
| 63 | |||
| 64 | return { | ||
| 65 | %class_args, | ||
| 66 | plugin_name => $plugin_name, | ||
| 67 | weaver => $weaver, | ||
| 68 | transformer => $xform, | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | sub translate_dialect { | ||
| 73 | my ($self, $pod_document) = @_; | ||
| 74 | |||
| 75 | $self->log_debug('applying transform'); | ||
| 76 | $self->transformer->transform_node( $pod_document ); | ||
| 77 | } | ||
| 78 | |||
| 79 | __PACKAGE__->meta->make_immutable; | ||
| 80 | 1; | ||
| 81 | |||
| 82 | __END__ |