| 1 | package SWISH::Prog::ReplaceRules; | ||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use base qw( SWISH::Prog::Class ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Data::Dump qw( dump ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use Text::ParseWords; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | our $VERSION = '0.75'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | __PACKAGE__->mk_accessors(qw( rules )); | |||||||||||||
| 13 | |||||||||||||||
| 14 | =pod | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 NAME | ||||||||||||||
| 17 | |||||||||||||||
| 18 | SWISH::Prog::ReplaceRules - filename mangler | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||
| 21 | |||||||||||||||
| 22 | use SWISH::Prog::ReplaceRules; | ||||||||||||||
| 23 | my $rules = SWISH::Prog::ReplaceRules->new( | ||||||||||||||
| 24 | qq(replace "the string you want replaced" "what to change it to"), | ||||||||||||||
| 25 | qq(remove "a string to remove"), | ||||||||||||||
| 26 | qq(prepend "a string to add before the result"), | ||||||||||||||
| 27 | qq(append "a string to add after the result"), | ||||||||||||||
| 28 | qq(regex "/search string/replace string/options"), | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | my $uri = 'foo/bar/baz'; | ||||||||||||||
| 31 | my $modified_uri = $rules->apply($uri); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||
| 34 | |||||||||||||||
| 35 | SWISH::Prog::ReplaceRules is a pure Perl replacement for the ReplaceRules | ||||||||||||||
| 36 | configuration feature in Swish-e. | ||||||||||||||
| 37 | |||||||||||||||
| 38 | This class is typically used internally by SWISH::Prog. The filter() | ||||||||||||||
| 39 | feature of SWISH::Prog is generated to use ReplaceRules if they are defined | ||||||||||||||
| 40 | in a SWISH::Prog::Config object or config file. | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =head1 METHODS | ||||||||||||||
| 43 | |||||||||||||||
| 44 | =head2 new( I<rules> ) | ||||||||||||||
| 45 | |||||||||||||||
| 46 | Constructor for new ReplaceRules object. I<rules> should be an array | ||||||||||||||
| 47 | of strings as defined in | ||||||||||||||
| 48 | L<http://swish-e.org/docs/swish-config.html#replacerules>. | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =head2 init | ||||||||||||||
| 51 | |||||||||||||||
| 52 | Internal method called by new(). Expects an array of rule strings. | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head2 rules | ||||||||||||||
| 55 | |||||||||||||||
| 56 | Get/set the array ref of parsed rules. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =cut | ||||||||||||||
| 59 | |||||||||||||||
| 60 | # override new() to allow for single argument string instead of hashref. | ||||||||||||||
| 61 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 62 | 1 | my $class = shift; | |||||||||||||
| 63 | 1 | my $self = bless {}, $class; | |||||||||||||
| 64 | 1 | $self->init(@_); | |||||||||||||
| 65 | 1 | return $self; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 1 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 69 | 1 | my $self = shift; | |||||||||||||
| 70 | 1 | $self->SUPER::init( rules => $self->_parse_rules(@_) ); | |||||||||||||
| 71 | 1 | return $self; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 1 | ▶ | sub _parse_rules { | ||||||||||||
|
Subroutine
_parse_rules: called CC=4 SCAR=13.9
| |||||||||||||||
| 75 | 1 | my $self = shift; | |||||||||||||
| 76 | 1 | my @rules; | |||||||||||||
| 77 | 1 | for my $r (@_) { | |||||||||||||
| 78 | 5 | my $rule = {}; | |||||||||||||
| 79 | 5 | my ( $action, $target ) | |||||||||||||
| 80 | = ( | ||||||||||||||
| 81 | $r =~ m/^\ *(replace|remove|prepend|append|regex)\s+(.+)$/is ); | ||||||||||||||
| 82 | 5 | $action = lc($action); | |||||||||||||
| 83 | 5 | ▶ | if ( $action eq 'regex' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 84 | 1 | ($target) = shellwords($target); | |||||||||||||
| 85 | 1 | my ( $delim, $before, $after, $opts ) | |||||||||||||
| 86 | = ( $target =~ m!^(.)(.+?)\1(.+?)\1(.+)$! ); | ||||||||||||||
| 87 | |||||||||||||||
| 88 | $rule->{target} = { | ||||||||||||||
| 89 | 1 | delim => $delim, | |||||||||||||
| 90 | before => $before, | ||||||||||||||
| 91 | after => $after, | ||||||||||||||
| 92 | opts => $opts, | ||||||||||||||
| 93 | }; | ||||||||||||||
| 94 | |||||||||||||||
| 95 | } | ||||||||||||||
| 96 | elsif ( $action eq 'replace' ) { | ||||||||||||||
| 97 | 1 | my ( $before, $after ) = shellwords($target); | |||||||||||||
| 98 | |||||||||||||||
| 99 | #warn "before:$before after:$after"; | ||||||||||||||
| 100 | $rule->{target} = { | ||||||||||||||
| 101 | 1 | before => $before, | |||||||||||||
| 102 | after => $after, | ||||||||||||||
| 103 | }; | ||||||||||||||
| 104 | |||||||||||||||
| 105 | } | ||||||||||||||
| 106 | else { | ||||||||||||||
| 107 | 3 | ( $rule->{target} ) = shellwords($target); | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 5 | $rule->{action} = $action; | |||||||||||||
| 111 | 5 | $rule->{orig} = $r; | |||||||||||||
| 112 | 5 | push @rules, $rule; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | #warn "rules: " . dump \@rules; | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 1 | return \@rules; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =head2 apply( I<string> ) | ||||||||||||||
| 121 | |||||||||||||||
| 122 | Apply the rules in the object against I<string>. Returns a modified | ||||||||||||||
| 123 | copy of I<string>. | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =cut | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 1 | ▶ | sub apply { | ||||||||||||
|
Subroutine
apply: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||
| 128 | 1 | my $self = shift; | |||||||||||||
| 129 | 1 | my $str = shift; | |||||||||||||
| 130 | 1 | ▶ | if ( !defined $str ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | croak "string required"; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | #dump $self; | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 1 | for my $rule ( @{ $self->{rules} } ) { | |||||||||||||
| 137 | 5 | my $action = $rule->{action}; | |||||||||||||
| 138 | 5 | my $target = $rule->{target}; | |||||||||||||
| 139 | 5 | my $orig = $rule->{orig}; | |||||||||||||
| 140 | |||||||||||||||
| 141 | #warn "apply '$orig' to '$str'\n"; | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 5 | ▶ | if ( $action eq 'prepend' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | 1 | $str = $target . $str; | |||||||||||||
| 145 | } | ||||||||||||||
| 146 | 5 | ▶ | if ( $action eq 'append' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | 1 | $str .= $target; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | 5 | ▶ | if ( $action eq 'remove' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 1 | $str =~ s/$target//g; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | 5 | ▶ | if ( $action eq 'replace' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 1 | my $b = $target->{before}; | |||||||||||||
| 154 | 1 | my $a = $target->{after}; | |||||||||||||
| 155 | 1 | $str =~ s/$b/$a/g; | |||||||||||||
| 156 | 1 | ▶ | die "Bad rule: $orig ($@)" if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | } | ||||||||||||||
| 158 | 5 | ▶ | if ( $action eq 'regex' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 1 | my $d = $target->{delim}; | |||||||||||||
| 160 | 1 | my $b = quotemeta( $target->{before} ); | |||||||||||||
| 161 | 1 | my $a = quotemeta( $target->{after} ); | |||||||||||||
| 162 | 1 | my $o = $target->{opts}; | |||||||||||||
| 163 | 1 | my $code = "\$str =~ s/$b/$a/$o"; | |||||||||||||
| 164 | |||||||||||||||
| 165 | #warn "code='$code'\n"; | ||||||||||||||
| 166 | 1 | eval "$code"; | |||||||||||||
| 167 | 1 | ▶ | die "Bad rule: $orig ($@)" if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | } | ||||||||||||||
| 169 | |||||||||||||||
| 170 | #warn "$orig applied to '$str'\n"; | ||||||||||||||
| 171 | } | ||||||||||||||
| 172 | 1 | return $str; | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | 1 | 1; | |||||||||||||
| 176 | |||||||||||||||
| 177 | __END__ | ||||||||||||||