diff options
author | Karl Berry <karl@freefriends.org> | 2022-04-14 19:48:17 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2022-04-14 19:48:17 +0000 |
commit | 96b00a78469d97b34c7e89477725a37ab3fd32a1 (patch) | |
tree | ba28851cb504e6bf89c53af1151df2724b58c991 /Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm | |
parent | d3eb5a117030fe207ad30748d79b0bb0755529fd (diff) |
latexindent (14apr22)
git-svn-id: svn://tug.org/texlive/trunk@63027 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm')
-rw-r--r-- | Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm | 76 |
1 files changed, 41 insertions, 35 deletions
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm index e387d11b4c7..707010e9661 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm @@ -1,4 +1,5 @@ package LatexIndent::Replacement; + # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or @@ -22,65 +23,70 @@ use LatexIndent::GetYamlSettings qw/%mainSettings/; use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active $is_rr_switch_active/; use LatexIndent::LogFile qw/$logger/; use Exporter qw/import/; -our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 +our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 our @EXPORT_OK = qw/make_replacements/; -sub make_replacements{ - my $self = shift; +sub make_replacements { + my $self = shift; my %input = @_; - if ($is_t_switch_active and !$is_rr_switch_active){ - $logger->trace("*Replacement mode *$input{when}* indentation: -r") ; - } elsif ($is_t_switch_active and $is_rr_switch_active) { - $logger->trace("*Replacement mode, -rr switch is active") if $is_t_switch_active ; + if ( $is_t_switch_active and !$is_rr_switch_active ) { + $logger->trace("*Replacement mode *$input{when}* indentation: -r"); + } + elsif ( $is_t_switch_active and $is_rr_switch_active ) { + $logger->trace("*Replacement mode, -rr switch is active") if $is_t_switch_active; } - my @replacements = @{$mainSettings{replacements}}; + my @replacements = @{ $mainSettings{replacements} }; + + foreach (@replacements) { + next if !( ${$_}{this} or ${$_}{substitution} ); - foreach ( @replacements ){ - next if !(${$_}{this} or ${$_}{substitution}); - # default value of "lookForThis" is 1 - ${$_}{lookForThis} = 1 if( !(defined ${$_}{lookForThis})); + ${$_}{lookForThis} = 1 if ( !( defined ${$_}{lookForThis} ) ); # move on if this one shouldn't be looked for - next if(!${$_}{lookForThis}); + next if ( !${$_}{lookForThis} ); # default value of "when" is before - ${$_}{when} = "before" if( !(defined ${$_}{when}) or $is_rr_switch_active); - + ${$_}{when} = "before" if ( !( defined ${$_}{when} ) or $is_rr_switch_active ); + # update to the logging file - if($is_t_switch_active and (${$_}{when} eq $input{when})){ + if ( $is_t_switch_active and ( ${$_}{when} eq $input{when} ) ) { $logger->trace("-"); - $logger->trace("this: ${$_}{this}") if(${$_}{this}); - $logger->trace("that: ${$_}{that}") if(${$_}{that}); - $logger->trace("substitution: ${$_}{substitution}") if(${$_}{substitution}); + $logger->trace("this: ${$_}{this}") if ( ${$_}{this} ); + $logger->trace("that: ${$_}{that}") if ( ${$_}{that} ); + $logger->trace("substitution: ${$_}{substitution}") if ( ${$_}{substitution} ); $logger->trace("when: ${$_}{when}"); } # perform the substitutions - if(${$_}{when} eq $input{when}){ - $logger->warn("*You have specified both 'this' and 'substitution'; the 'substitution' field will be ignored") if(${$_}{this} and ${$_}{substitution}); - if(${$_}{this}){ + if ( ${$_}{when} eq $input{when} ) { + $logger->warn( + "*You have specified both 'this' and 'substitution'; the 'substitution' field will be ignored") + if ( ${$_}{this} and ${$_}{substitution} ); + if ( ${$_}{this} ) { + # *string* replacement # *string* replacement # *string* replacement - my $this = qq{${$_}{this}}; - my $that = (defined ${$_}{that}) ? qq{${$_}{that}} : q(); - my $index_match = index(${$self}{body}, $this); - while ( $index_match != -1 ) { - substr (${$self}{body}, $index_match, length($this), $that ); - $index_match = index(${$self}{body}, $this); - } - } else { + my $this = qq{${$_}{this}}; + my $that = ( defined ${$_}{that} ) ? qq{${$_}{that}} : q(); + my $index_match = index( ${$self}{body}, $this ); + while ( $index_match != -1 ) { + substr( ${$self}{body}, $index_match, length($this), $that ); + $index_match = index( ${$self}{body}, $this ); + } + } + else { # *regex* replacement # *regex* replacement # *regex* replacement - - # https://stackoverflow.com/questions/12423337/how-to-pass-a-replacing-regex-as-a-command-line-argument-to-a-perl-script - my $body = ${$self}{body}; + +# https://stackoverflow.com/questions/12423337/how-to-pass-a-replacing-regex-as-a-command-line-argument-to-a-perl-script + my $body = ${$self}{body}; eval("\$body =~ ${$_}{substitution}"); - ${$self}{body} = $body ; - } + ${$self}{body} = $body; + } } } } |