summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2019-07-13 21:40:12 +0000
committerKarl Berry <karl@freefriends.org>2019-07-13 21:40:12 +0000
commit757d85974d6b7911f766ae371404144ec3bdce34 (patch)
tree795c61e735cf8826289bb477020f3e50ec5aa914 /Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm
parent8e8c05aa6d6db2c28633a8924de4a0b941b52030 (diff)
latexindent (13jul19)
git-svn-id: svn://tug.org/texlive/trunk@51635 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.pm89
1 files changed, 89 insertions, 0 deletions
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm
new file mode 100644
index 00000000000..1bc51caed2d
--- /dev/null
+++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Replacement.pm
@@ -0,0 +1,89 @@
+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
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# See http://www.gnu.org/licenses/.
+#
+# Chris Hughes, 2017
+#
+# For all communication, please visit: https://github.com/cmhughes/latexindent.pl
+use strict;
+use warnings;
+use LatexIndent::Tokens qw/%tokens/;
+use LatexIndent::TrailingComments qw/$trailingCommentRegExp/;
+use LatexIndent::GetYamlSettings qw/%masterSettings/;
+use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active $is_rr_switch_active/;
+use LatexIndent::LogFile qw/$logger/;
+use Data::Dumper;
+use Exporter qw/import/;
+our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321
+our @EXPORT_OK = qw/make_replacements/;
+
+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 ;
+ }
+
+ my @replacements = @{$masterSettings{replacements}};
+
+ foreach ( @replacements ){
+ next if !(${$_}{this} or ${$_}{substitution});
+
+ # default value of "lookForThis" is 1
+ ${$_}{lookForThis} = 1 if( !(defined ${$_}{lookForThis}));
+
+ # move on if this one shouldn't be looked for
+ next if(!${$_}{lookForThis});
+
+ # default value of "when" is before
+ ${$_}{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})){
+ $logger->trace("-");
+ $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}){
+ # *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 {
+ # *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};
+ eval("\$body =~ ${$_}{substitution}");
+ ${$self}{body} = $body ;
+ }
+ }
+ }
+}
+
+1;