| 1 | package Perl::Tidy::Sweetened::Pluggable; |
| 2 | |
| 3 | # ABSTRACT: Simple object to facilitate a pluggable filter architecture |
| 4 | |
| 5 | use strict; |
| 6 | use warnings; |
| 7 | |
| 8 | our $VERSION = '1.20'; |
| 9 | |
| 10 | sub new { |
| 11 | my ( $class, %args ) = @_; |
| 12 | return bless {%args}, $class; |
| 13 | } |
| 14 | |
| 15 | sub filters { return ( $_[0]->{filters} ||= [] ) } |
| 16 | |
| 17 | sub args { return ( $_[0]->{args} ||= {} ) } |
| 18 | |
| 19 | sub add_filter { |
| 20 | my $self = shift; |
| 21 | push @{ $self->filters }, @_; |
| 22 | } |
| 23 | |
| 24 | sub prefilter { |
| 25 | my ( $self, $code ) = @_; |
| 26 | for my $filter ( @{ $self->filters } ) { |
| 27 | $code = $filter->prefilter($code, $self->args); |
| 28 | } |
| 29 | |
| 30 | # warn "After prefilter, before tidy\n"; |
| 31 | # warn $code; |
| 32 | |
| 33 | return $code; |
| 34 | } |
| 35 | |
| 36 | sub postfilter { |
| 37 | my ( $self, $code ) = @_; |
| 38 | |
| 39 | # warn "After tidy, before postfilter\n"; |
| 40 | # warn $code; |
| 41 | |
| 42 | for my $filter ( @{ $self->filters } ) { |
| 43 | $code = $filter->postfilter($code, $self->args); |
| 44 | } |
| 45 | return $code; |
| 46 | } |
| 47 | |
| 48 | sub add_args { |
| 49 | my ( $self, $args ) = @_; |
| 50 | my ( $body_parts, $msg ) = Perl::Tidy::parse_args($args); |
| 51 | $self->{args} = |
| 52 | { map { my ( $k, $v ) = split /=/, $_, 2; ( $k => $v ) } @$body_parts }; |
| 53 | } |
| 54 | |
| 55 | 1; |
| 56 | |
| 57 | __END__ |
| 58 | |
| 59 | =pod |
| 60 | |
| 61 | =head1 NAME |
| 62 | |
| 63 | Perl::Tidy::Sweetened::Pluggable - Simple object to facilitate a pluggable filter architecture |
| 64 | |
| 65 | =head1 VERSION |
| 66 | |
| 67 | version 1.20 |
| 68 | |
| 69 | =head1 SYNOPSIS |
| 70 | |
| 71 | our $plugins = Perl::Tidy::Sweetened::Pluggable->new(); |
| 72 | |
| 73 | $plugins->add_filter( |
| 74 | Perl::Tidy::Sweetened::Keyword::SubSignature->new( |
| 75 | keyword => 'method', |
| 76 | marker => 'METHOD', |
| 77 | ) ); |
| 78 | |
| 79 | $plugins->prefilter( $code ); |
| 80 | $plugins->postfilter( $code ); |
| 81 | |
| 82 | =head1 DESCRIPTION |
| 83 | |
| 84 | Builds a pluggable, chainable list of filters. |
| 85 | |
| 86 | =head1 AUTHOR |
| 87 | |
| 88 | Mark Grimes <[email protected]> |
| 89 | |
| 90 | =head1 SOURCE |
| 91 | |
| 92 | Source repository is at L<https://github.com/mvgrimes/Perl-Tidy-Sweetened>. |
| 93 | |
| 94 | =head1 BUGS |
| 95 | |
| 96 | Please report any bugs or feature requests on the bugtracker website L<https://github.com/mvgrimes/Perl-Tidy-Sweetened/issues> |
| 97 | |
| 98 | When submitting a bug or request, please include a test-file or a |
| 99 | patch to an existing test-file that illustrates the bug or desired |
| 100 | feature. |
| 101 | |
| 102 | =head1 COPYRIGHT AND LICENSE |
| 103 | |
| 104 | This software is copyright (c) 2023 by Mark Grimes <[email protected]>. |
| 105 | |
| 106 | This is free software; you can redistribute it and/or modify it under |
| 107 | the same terms as the Perl 5 programming language system itself. |
| 108 | |
| 109 | =cut |