package LatexIndent::GetYamlSettings; # 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-2025 # # For all communication, please visit: https://github.com/cmhughes/latexindent.pl use strict; use warnings; use Data::Dumper; use LatexIndent::Switches qw/%switch $is_m_switch_active $is_t_switch_active $is_tt_switch_active $is_check_switch_active $is_check_verbose_switch_active $is_r_switch_active $is_rr_switch_active $is_rv_switch_active /; use YAML::Tiny; # interpret defaultSettings.yaml and other potential settings files use File::Basename; # to get the filename and directory path use File::HomeDir; use Cwd; use Exporter qw/import/; use LatexIndent::LogFile qw/$logger/; our @EXPORT_OK = qw/yaml_obsolete_checks yaml_get_alignment_at_ampersand_from_parent yaml_read_settings yaml_modify_line_breaks_settings yaml_get_indentation_settings_for_this_object yaml_poly_switch_get_every_or_custom_value yaml_get_indentation_information yaml_get_object_attribute_for_indentation_settings yaml_alignment_at_ampersand_settings %mainSetting %previouslyFoundSetting $argumentsBetweenCommands $commaPolySwitchExists $equalsPolySwitchExists %polySwitchNames/; # Read in defaultSettings.YAML file our $defaultSettings; # master yaml settings is a hash, global to this module our %mainSetting; use LatexIndent::UTF8CmdLineArgsFileOperation qw/copy_with_encode exist_with_encode open_with_encode zero_with_encode read_yaml_with_encode/; use utf8; # previously found settings is a hash, global to this module our %previouslyFoundSetting; # default values for align at ampersand routine our @alignAtAmpersandInformation; our $argumentsBetweenCommands; our $commaPolySwitchExists = 0; our $equalsPolySwitchExists = 0; our %polySwitchNames; sub yaml_read_settings { my $self = shift; # read the default settings $defaultSettings = YAML::Tiny->read("$FindBin::RealBin/defaultSettings.yaml") if ( -e "$FindBin::RealBin/defaultSettings.yaml" ); # grab the logger object $logger->info("*YAML settings read: defaultSettings.yaml"); $logger->info("Reading defaultSettings.yaml from $FindBin::RealBin/defaultSettings.yaml"); my $myLibDir = dirname(__FILE__); my ( $name, $dir, $ext ) = fileparse( $INC{"LatexIndent/GetYamlSettings.pm"}, "pm" ); $dir =~ s/\/$//; # if latexindent.exe is invoked from TeXLive, then defaultSettings.yaml won't be in # the same directory as it; we need to navigate to it if ( !$defaultSettings ) { $logger->info( "Reading defaultSettings.yaml (2nd attempt) from $FindBin::RealBin/../../texmf-dist/scripts/latexindent/defaultSettings.yaml" ); $logger->info("and then, if necessary, $FindBin::RealBin/LatexIndent/defaultSettings.yaml"); if ( -e "$FindBin::RealBin/../../texmf-dist/scripts/latexindent/defaultSettings.yaml" ) { $defaultSettings = YAML::Tiny->read("$FindBin::RealBin/../../texmf-dist/scripts/latexindent/defaultSettings.yaml"); } elsif ( -e "$FindBin::RealBin/LatexIndent/defaultSettings.yaml" ) { $defaultSettings = YAML::Tiny->read("$FindBin::RealBin/LatexIndent/defaultSettings.yaml"); } elsif ( -e "$dir/defaultSettings.yaml" ) { $defaultSettings = YAML::Tiny->read("$dir/defaultSettings.yaml"); } elsif ( -e "$myLibDir/defaultSettings.yaml" ) { +$defaultSettings = YAML::Tiny->read("$myLibDir/defaultSettings.yaml"); } else { $logger->fatal("*Could not open defaultSettings.yaml"); $self->output_logfile(); exit(2); } } # need to exit if we can't get defaultSettings.yaml if ( !$defaultSettings ) { $logger->fatal("*Could not open defaultSettings.yaml"); $self->output_logfile(); exit(2); } # master yaml settings is a hash, global to this module our %mainSetting = %{ $defaultSettings->[0] }; &yaml_update_dumper_settings(); # scalar to read user settings my $userSettings; # array to store the paths to user settings my @absPaths; # we'll need the home directory a lot in what follows my $homeDir = File::HomeDir->my_home; $logger->info("*YAML reading settings") unless $switch{onlyDefault}; my $indentconfig = undef; if ( defined $ENV{LATEXINDENT_CONFIG} && !$switch{onlyDefault} ) { if ( -f $ENV{LATEXINDENT_CONFIG} ) { $indentconfig = $ENV{LATEXINDENT_CONFIG}; $logger->info('The $LATEXINDENT_CONFIG variable was detected.'); $logger->info( 'The value of $LATEXINDENT_CONFIG is: "' . $ENV{LATEXINDENT_CONFIG} . '"' ); } else { $logger->warn('*The $LATEXINDENT_CONFIG variable is assigned, but does not point to a file!'); $logger->warn( 'The value of $LATEXINDENT_CONFIG is: "' . $ENV{LATEXINDENT_CONFIG} . '"' ); } } if ( !defined $indentconfig && !$switch{onlyDefault} ) { # see all possible values of $^O here: https://perldoc.perl.org/perlport#Unix and https://perldoc.perl.org/perlport#DOS-and-Derivatives if ( $^O eq "linux" ) { if ( defined $ENV{XDG_CONFIG_HOME} && -f "$ENV{XDG_CONFIG_HOME}/latexindent/indentconfig.yaml" ) { $indentconfig = "$ENV{XDG_CONFIG_HOME}/latexindent/indentconfig.yaml"; $logger->info( 'The $XDG_CONFIG_HOME variable and the config file in "' . "$ENV{XDG_CONFIG_HOME}/latexindent/indentconfig.yaml" . '" were recognized' ); $logger->info( 'The value of $XDG_CONFIG_HOME is: "' . $ENV{XDG_CONFIG_HOME} . '"' ); } elsif ( -f "$homeDir/.config/latexindent/indentconfig.yaml" ) { $indentconfig = "$homeDir/.config/latexindent/indentconfig.yaml"; $logger->info( 'The config file in "' . "$homeDir/.config/latexindent/indentconfig.yaml" . '" will be read' ); } } elsif ( $^O eq "darwin" ) { if ( -f "$homeDir/Library/Preferences/latexindent/indentconfig.yaml" ) { $indentconfig = "$homeDir/Library/Preferences/latexindent/indentconfig.yaml"; $logger->info( 'The config file in "' . "$homeDir/Library/Preferences/latexindent/indentconfig.yaml" . '" will be read' ); } } elsif ( $^O eq "MSWin32" || $^O eq "cygwin" ) { if ( defined $ENV{LOCALAPPDATA} && -f "$ENV{LOCALAPPDATA}/latexindent/indentconfig.yaml" ) { $indentconfig = "$ENV{LOCALAPPDATA}/latexindent/indentconfig.yaml"; $logger->info( 'The $LOCALAPPDATA variable and the config file in "' . "$ENV{LOCALAPPDATA}" . '\latexindent\indentconfig.yaml" were recognized' ); $logger->info( 'The value of $LOCALAPPDATA is: "' . $ENV{LOCALAPPDATA} . '"' ); } elsif ( -f "$homeDir/AppData/Local/latexindent/indentconfig.yaml" ) { $indentconfig = "$homeDir/AppData/Local/latexindent/indentconfig.yaml"; $logger->info( 'The config file in "' . "$homeDir" . '\AppData\Local\latexindent\indentconfig.yaml" will be read' ); } } # if $indentconfig is still not defined, fallback to the location in $homeDir if ( !defined $indentconfig ) { # if all of these don't exist check home directly, with the non hidden file $indentconfig = ( -f "$homeDir/indentconfig.yaml" ) ? "$homeDir/indentconfig.yaml" : undef; # if indentconfig.yaml doesn't exist, check for the hidden file, .indentconfig.yaml if ( !defined $indentconfig ) { $indentconfig = ( -f "$homeDir/.indentconfig.yaml" ) ? "$homeDir/.indentconfig.yaml" : undef; } $logger->info( 'The config file in "' . "$indentconfig" . '" will be read' ) if defined $indentconfig; } } # messages for indentconfig.yaml and/or .indentconfig.yaml if ( defined $indentconfig and -f $indentconfig and !$switch{onlyDefault} ) { # read the absolute paths from indentconfig.yaml $userSettings = YAML::Tiny->read("$indentconfig"); # update the absolute paths if ( $userSettings and ( ref( $userSettings->[0] ) eq 'HASH' ) and $userSettings->[0]->{paths} ) { $logger->info("Reading path information from $indentconfig"); # output the contents of indentconfig to the log file $logger->info( Dump \%{ $userSettings->[0] } ); # change the encoding of the paths according to the field `encoding` if ( $userSettings and ( ref( $userSettings->[0] ) eq 'HASH' ) and $userSettings->[0]->{encoding} ) { use Encode; my $encoding = $userSettings->[0]->{encoding}; my $encodingObject = find_encoding($encoding); # Check if the encoding is valid. if ( ref($encodingObject) ) { $logger->info("*Encoding of the paths is $encoding"); foreach ( @{ $userSettings->[0]->{paths} } ) { my $temp = $encodingObject->encode("$_"); $logger->info("Transform file encoding: $_ -> $temp"); push( @absPaths, $temp ); } } else { $logger->warn("*encoding \"$encoding\" not found"); $logger->warn("Ignore this setting and will take the default encoding."); @absPaths = @{ $userSettings->[0]->{paths} }; } } else # No such setting, and will take the default { # $logger->info("*Encoding of the paths takes the default."); @absPaths = @{ $userSettings->[0]->{paths} }; } } else { $logger->warn( "*The paths field cannot be read from $indentconfig; this means it is either empty or contains invalid YAML" ); $logger->warn( "See https://latexindentpl.readthedocs.io/en/latest/sec-indent-config-and-settings.html for an example" ); } } else { if ( $switch{onlyDefault} ) { $logger->info("*-d switch active: only default settings requested"); $logger->info("not reading USER settings from $indentconfig") if ( defined $indentconfig && -e $indentconfig ); $logger->info("Ignoring the -l switch: $switch{readLocalSettings} (you used the -d switch)") if ( $switch{readLocalSettings} ); $logger->info("Ignoring the -y switch: $switch{yaml} (you used the -d switch)") if ( $switch{yaml} ); $switch{readLocalSettings} = 0; $switch{yaml} = 0; } else { # give the user instructions on where to put the config file $logger->info("Home directory is $homeDir"); $logger->info("latexindent.pl didn't find indentconfig.yaml or .indentconfig.yaml"); $logger->info( "see all possible locations: https://latexindentpl.readthedocs.io/en/latest/sec-appendices.html#indentconfig-options" ); } } # default value of readLocalSettings # # latexindent -l myfile.tex # # means that we wish to use localSettings.yaml if ( defined( $switch{readLocalSettings} ) and ( $switch{readLocalSettings} eq '' ) ) { $logger->info('*-l switch used without filename, will search for the following files in turn:'); $logger->info('localSettings.yaml,latexindent.yaml,.localSettings.yaml,.latexindent.yaml'); $switch{readLocalSettings} = 'localSettings.yaml,latexindent.yaml,.localSettings.yaml,.latexindent.yaml'; } # local settings can be called with a + symbol, for example # -l=+myfile.yaml # -l "+ myfile.yaml" # -l=myfile.yaml+ # which translates to, respectively # -l=localSettings.yaml,myfile.yaml # -l=myfile.yaml,localSettings.yaml # Note: the following is *not allowed*: # -l+myfile.yaml # and # -l + myfile.yaml # will *only* load localSettings.yaml, and myfile.yaml will be ignored my @localSettings; $logger->info("*YAML settings read: -l switch") if $switch{readLocalSettings}; # remove leading, trailing, and intermediate space $switch{readLocalSettings} =~ s/^\h*//g; $switch{readLocalSettings} =~ s/\h*$//g; $switch{readLocalSettings} =~ s/\h*,\h*/,/g; if ( $switch{readLocalSettings} =~ m/\+/ ) { $logger->info( "+ found in call for -l switch: will add localSettings.yaml,latexindent.yaml,.localSettings.yaml,.latexindent.yaml" ); # + can be either at the beginning or the end, which determines if where the comma should go my $commaAtBeginning = ( $switch{readLocalSettings} =~ m/^\h*\+/ ? q() : "," ); my $commaAtEnd = ( $switch{readLocalSettings} =~ m/^\h*\+/ ? "," : q() ); $switch{readLocalSettings} =~ s/\h*\+\h*/$commaAtBeginning ."localSettings.yaml,latexindent.yaml,.localSettings.yaml,.latexindent.yaml" .$commaAtEnd/ex; $logger->info("New value of -l switch: $switch{readLocalSettings}"); } # local settings can be separated by , # e.g # -l = myyaml1.yaml,myyaml2.yaml # and in which case, we need to read them all if ( $switch{readLocalSettings} =~ m/,/ ) { $logger->info("Multiple localSettings found, separated by commas:"); @localSettings = split( /,/, $switch{readLocalSettings} ); $logger->info( join( ', ', @localSettings ) ); } else { push( @localSettings, $switch{readLocalSettings} ) if ( $switch{readLocalSettings} ); } my $workingFileLocation = dirname( ${$self}{fileName} ); # add local settings to the paths, if appropriate foreach (@localSettings) { # check for an extension (.yaml) my ( $name, $dir, $ext ) = fileparse( $_, "yaml" ); # if no extension is found, append the current localSetting with .yaml $_ = $_ . ( $_ =~ m/\.\z/ ? q() : "." ) . "yaml" if ( !$ext ); # if the -l switch is called on its own, or else with + # and latexindent.pl is called from a different directory, then # we need to account for this if ( $_ =~ m/^[.]?(localSettings|latexindent)\.yaml$/ ) { # check for existence in the directory of the file. if ( ( -e $workingFileLocation . "/" . $_ ) ) { $_ = $workingFileLocation . "/" . $_; # otherwise we fallback to the current directory } elsif ( ( -e cwd() . "/" . $_ ) ) { $_ = cwd() . "/" . $_; } } # diacritics in YAML names (highlighted in https://github.com/cmhughes/latexindent.pl/pull/439) #$_ = decode( "utf-8", $_ ); $_ = $_; # check for existence and non-emptiness if ( exist_with_encode($_) and !( zero_with_encode($_) ) ) { $logger->info("Adding $_ to YAML read paths"); push( @absPaths, "$_" ); } elsif ( !( exist_with_encode($_) ) ) { if (( $_ =~ m/localSettings|latexindent/s and !( -e 'localSettings.yaml' ) and !( -e '.localSettings.yaml' ) and !( -e 'latexindent.yaml' ) and !( -e '.latexindent.yaml' ) ) or $_ !~ m/localSettings|latexindent/s ) { $logger->warn("*yaml file not found: $_ not found. Proceeding without it."); } } } # heading for the log file $logger->info("*YAML settings, reading from the following files:") if @absPaths; # read in the settings from each file foreach my $settings (@absPaths) { # check that the settings file exists and that it isn't empty if ( exist_with_encode($settings) and !( zero_with_encode($settings) ) ) { $logger->info("Reading USER settings from $settings"); $userSettings = read_yaml_with_encode("$settings"); # update the absolute paths if ( $userSettings and ( ref( $userSettings->[0] ) eq 'HASH' ) and $userSettings->[0]->{paths} ) { $logger->info("Reading path information from $settings"); # output the contents of indentconfig to the log file $logger->info( Dump \%{ $userSettings->[0] } ); foreach ( @{ $userSettings->[0]->{paths} } ) { push( @absPaths, $_ ); } } # if we can read userSettings if ($userSettings) { # specialBeginEnd backwards compatibility # # old: HASH # new: ARRAY # if ( ${ $userSettings->[0] }{specialBeginEnd} and ( ref( ${ $userSettings->[0] }{specialBeginEnd} ) eq 'HASH' ) ) { my @newSpecialBeginEnd; while ( my ( $specialKey, $specialValue ) = each %{ ${ $userSettings->[0] }{specialBeginEnd} } ) { my %newIndvSpecialBeginEnd; if ( ref($specialValue) eq 'HASH' ) { $newIndvSpecialBeginEnd{name} = $specialKey; $newIndvSpecialBeginEnd{begin} = ${$specialValue}{begin} if defined ${$specialValue}{begin}; $newIndvSpecialBeginEnd{end} = ${$specialValue}{end} if defined ${$specialValue}{end}; $newIndvSpecialBeginEnd{nested} = ${$specialValue}{nested} if defined ${$specialValue}{nested}; $newIndvSpecialBeginEnd{middle} = ${$specialValue}{middle} if defined ${$specialValue}{middle}; $newIndvSpecialBeginEnd{lookForThis} = ${$specialValue}{lookForThis} if defined ${$specialValue}{lookForThis}; push( @newSpecialBeginEnd, \%newIndvSpecialBeginEnd ); } } ${ $userSettings->[0] }{specialBeginEnd} = \@newSpecialBeginEnd; $logger->warn( "*specialBeginEnd should be specified as list, but has been converted to the appropriate format, see below" ); } # update the MASTER settings to include updates from the userSettings while ( my ( $firstLevelKey, $firstLevelValue ) = each %{ $userSettings->[0] } ) { # the update approach is slightly different for hashes vs scalars/arrays if ( ref($firstLevelValue) eq "HASH" ) { while ( my ( $secondLevelKey, $secondLevelValue ) = each %{ $userSettings->[0]{$firstLevelKey} } ) { if ( ref $secondLevelValue eq "HASH" ) { # if mainSetting already contains a *scalar* value in secondLevelKey # then we need to delete it (test-cases/headings-first.tex with indentRules1.yaml first demonstrated this) if ( defined $mainSetting{$firstLevelKey}{$secondLevelKey} and ref $mainSetting{$firstLevelKey}{$secondLevelKey} ne "HASH" ) { # tabular: 0/1 needs to be translated into # lookForAlignDelims: # tabular: # delims: 0/1 # see, for example, latexindent.pl -t -s table1 -o=+-mod3 -l=tabular3,multiColumnGrouping2 if ( $firstLevelKey eq 'lookForAlignDelims' ) { my $delims = $mainSetting{$firstLevelKey}{$secondLevelKey}; delete $mainSetting{$firstLevelKey}{$secondLevelKey}; ${ $mainSetting{$firstLevelKey}{$secondLevelKey} }{delims} = $delims; } else { delete $mainSetting{$firstLevelKey}{$secondLevelKey}; } $logger->trace( "*mainSetting{$firstLevelKey}{$secondLevelKey} currently contains a *scalar* value, but it needs to be updated with a hash (see $settings); deleting the scalar" ) if ($is_t_switch_active); } while ( my ( $thirdLevelKey, $thirdLevelValue ) = each %{$secondLevelValue} ) { if ( ref $thirdLevelValue eq "HASH" ) { # similarly for third level if ( defined $mainSetting{$firstLevelKey}{$secondLevelKey}{$thirdLevelKey} and ref $mainSetting{$firstLevelKey}{$secondLevelKey}{$thirdLevelKey} ne "HASH" ) { $logger->trace( "*mainSetting{$firstLevelKey}{$secondLevelKey}{$thirdLevelKey} currently contains a *scalar* value, but it needs to be updated with a hash (see $settings); deleting the scalar" ) if ($is_t_switch_active); delete $mainSetting{$firstLevelKey}{$secondLevelKey}{$thirdLevelKey}; } while ( my ( $fourthLevelKey, $fourthLevelValue ) = each %{$thirdLevelValue} ) { $mainSetting{$firstLevelKey}{$secondLevelKey}{$thirdLevelKey} {$fourthLevelKey} = $fourthLevelValue; } } else { $mainSetting{$firstLevelKey}{$secondLevelKey}{$thirdLevelKey} = $thirdLevelValue; } } } else { # settings such as commandCodeBlocks can have arrays, which may wish # to be amalgamated, rather than overwritten if ( ref($secondLevelValue) eq "ARRAY" and ${ ${ $mainSetting{$firstLevelKey}{$secondLevelKey} }[0] }{amalgamate} and !( ref( ${$secondLevelValue}[0] ) eq "HASH" and defined ${$secondLevelValue}[0]{amalgamate} and !${$secondLevelValue}[0]{amalgamate} ) ) { $logger->trace("*$firstLevelKey -> $secondLevelKey, amalgamate: 1") if ($is_t_switch_active); foreach ( @{$secondLevelValue} ) { $logger->trace("$_") if ($is_t_switch_active); push( @{ $mainSetting{$firstLevelKey}{$secondLevelKey} }, $_ ) unless ( ref($_) eq "HASH" ); } # remove duplicated entries, https://stackoverflow.com/questions/7651/how-do-i-remove-duplicate-items-from-an-array-in-perl my %seen = (); my @unique = grep { !$seen{$_}++ } @{ $mainSetting{$firstLevelKey}{$secondLevelKey} }; @{ $mainSetting{$firstLevelKey}{$secondLevelKey} } = @unique; $logger->trace( "*main settings for $firstLevelKey -> $secondLevelKey now look like:") if $is_t_switch_active; foreach ( @{ $mainSetting{$firstLevelKey}{$secondLevelKey} } ) { $logger->trace("$_") if ($is_t_switch_active); } } else { $mainSetting{$firstLevelKey}{$secondLevelKey} = $secondLevelValue; } } } } elsif ( ref($firstLevelValue) eq "ARRAY" ) { # update amalgamate in master settings if ( ref( ${$firstLevelValue}[0] ) eq "HASH" and defined ${$firstLevelValue}[0]{amalgamate} ) { ${ $mainSetting{$firstLevelKey}[0] }{amalgamate} = ${$firstLevelValue}[0]{amalgamate}; shift @{$firstLevelValue} if ${ $mainSetting{$firstLevelKey}[0] }{amalgamate}; } if ( ref( ${$firstLevelValue}[0] ) eq "HASH" and defined ${$firstLevelValue}[0]{specialBeforeCommand} ) { ${ $mainSetting{$firstLevelKey}[0] }{specialBeforeCommand} = ${$firstLevelValue}[0]{specialBeforeCommand}; } # if amalgamate is set to 1, then append if ( ref( $mainSetting{$firstLevelKey}[0] ) eq "HASH" and ${ $mainSetting{$firstLevelKey}[0] }{amalgamate} ) { # loop through the other settings foreach ( @{$firstLevelValue} ) { push( @{ $mainSetting{$firstLevelKey} }, $_ ); } } else { # otherwise overwrite $mainSetting{$firstLevelKey} = $firstLevelValue; } } else { $mainSetting{$firstLevelKey} = $firstLevelValue; } } # output settings to $logfile if ( $mainSetting{logFilePreferences}{showEveryYamlRead} ) { $logger->info( Dump \%{ $userSettings->[0] } ); } else { $logger->info( "Not showing settings in the log file (see showEveryYamlRead and showAmalgamatedSettings)."); } # warning to log file if modifyLineBreaks specified and m switch not active if ( ${ $userSettings->[0] }{modifyLineBreaks} and !$is_m_switch_active ) { $logger->warn("*modifyLineBreaks specified and m switch is *not* active"); $logger->warn("perhaps you intended to call"); $logger->warn(" latexindent.pl -m -l $settings ${$self}{fileName}"); } } else { # otherwise print a warning that we can not read userSettings.yaml $logger->warn("*$settings contains invalid yaml format- not reading from it"); } } else { # otherwise keep going, but put a warning in the log file $logger->warn("*$homeDir/indentconfig.yaml"); if ( zero_with_encode($settings) ) { $logger->info("specifies $settings but this file is EMPTY -- not reading from it"); } else { $logger->info( "specifies $settings but this file does not exist - unable to read settings from this file"); } } &yaml_update_dumper_settings(); } # read settings from -y|--yaml switch if ( $switch{yaml} ) { # report to log file $logger->info("*YAML settings read: -y switch"); # remove any horizontal space before or after , OR : OR ; or at the beginning or end of the switch value $switch{yaml} =~ s/\h*(,|(?info( "YAML setting: " . $_ ); } # it is possible to specify, for example, # # -y=indentAfterHeadings:paragraph:lookForThis:1;level:1 # -y=specialBeginEnd:displayMath:begin:'\\\[';end: '\\\]';lookForThis: 1 # # which should be translated into # # indentAfterHeadings: # paragraph: # lookForThis:1 # level:1 # # so we need to loop through the comma separated list and search # for semi-colons my $settingsCounter = 0; my @originalYamlSettings = @yamlSettings; foreach (@originalYamlSettings) { # increment the counter $settingsCounter++; # need to be careful in splitting at ';' # # motivation as detailed in https://github.com/cmhughes/latexindent.pl/issues/243 # # latexindent.pl -m -y='modifyLineBreaks:oneSentencePerLine:manipulateSentences: 1, # modifyLineBreaks:oneSentencePerLine:sentencesBeginWith:a-z: 1, # fineTuning:modifyLineBreaks:betterFullStop: "(?:\.|;|:(?![a-z]))|(?:(?trace("*Sub-field detected (; present) and the root is: $root") if $is_t_switch_active; # now we need to attach the $root back together with any subfields foreach (@subfield) { # splice the new field into @yamlSettings (reference: https://perlmaven.com/splice-to-slice-and-dice-arrays-in-perl) splice @yamlSettings, $settingsCounter, 0, $root . ":" . $_; # increment the counter $settingsCounter++; } $logger->info( "-y switch value interpreted as: " . join( ',', @yamlSettings ) ); } } # loop through each of the settings specified in the -y switch foreach (@yamlSettings) { my @keysValues; # as above, need to be careful in splitting at ':' # # motivation as detailed in https://github.com/cmhughes/latexindent.pl/issues/243 # # latexindent.pl -m -y='modifyLineBreaks:oneSentencePerLine:manipulateSentences: 1, # modifyLineBreaks:oneSentencePerLine:sentencesBeginWith:a-z: 1, # fineTuning:modifyLineBreaks:betterFullStop: "(?:\.|;|:(?![a-z]))|(?:(?info("quote found in -y switch"); $logger->info( "key: " . $splitAtQuote[0] ); # definition check $splitAtQuote[1] = '' if not defined $splitAtQuote[1]; # then log the value $logger->info( "value: " . $splitAtQuote[1] ); # split at : (@keysValues) = split( /(?info("double-quoted string found in -y switch: $raw_value, substitute to $value"); } elsif ( $value =~ m/^'(.*)'$/ ) { # single-quoted string my $raw_value = $value; $value = $1; # special treatment for tabs and newlines # translate: '\t', '\n' # only translate string starts with an odd number of escape characters '\' $value =~ s/(?info("single-quoted string found in -y switch: $raw_value, substitute to $value"); } if ( scalar(@keysValues) == 2 ) { # for example, -y="defaultIndent: ' '" my $key = $keysValues[0]; $logger->info("Updating mainSetting with $key: $value"); $mainSetting{$key} = $value; } elsif ( scalar(@keysValues) == 3 ) { # for example, -y="indentRules: one: '\t\t\t\t'" my $parent = $keysValues[0]; my $child = $keysValues[1]; $logger->info("Updating mainSetting with $parent: $child: $value"); $mainSetting{$parent}{$child} = $value; } elsif ( scalar(@keysValues) == 4 ) { # for example, -y='modifyLineBreaks : environments: EndStartsOnOwnLine:3' -m my $parent = $keysValues[0]; my $child = $keysValues[1]; my $grandchild = $keysValues[2]; if ( ref $mainSetting{$parent} eq 'HASH' ) { delete $mainSetting{$parent}{$child} if ( defined $mainSetting{$parent}{$child} and ref $mainSetting{$parent}{$child} ne "HASH" ); $logger->info("Updating mainSetting with $parent: $child: $grandchild: $value"); $mainSetting{$parent}{$child}{$grandchild} = $value; } else { $logger->warn( "*-y:$parent:$child:$grandchild:$value ignored, as mainSetting{$parent} should be specified as a list" ); } } elsif ( scalar(@keysValues) == 5 ) { # for example, -y='modifyLineBreaks : environments: one: EndStartsOnOwnLine:3' -m my $parent = $keysValues[0]; my $child = $keysValues[1]; my $grandchild = $keysValues[2]; my $greatgrandchild = $keysValues[3]; $logger->info("Updating mainSetting with $parent: $child: $grandchild: $greatgrandchild: $value"); $mainSetting{$parent}{$child}{$grandchild}{$greatgrandchild} = $value; } &yaml_update_dumper_settings(); } } # switches via YAML, alphabetically foreach my $switch ( sort keys %{ $mainSetting{switchesViaYaml} } ) { next unless ${ $mainSetting{switchesViaYaml} }{$switch}; if ( $switch eq 'kSwitch' and !$is_check_switch_active ) { $logger->info("*Switch activated via YAML: check mode (-k)"); $is_check_switch_active = 1; } if ( $switch eq 'kvSwitch' and !$is_check_switch_active ) { $logger->info("*Switch activated via YAML: verbose check mode (-kv)"); $is_check_switch_active = 1; $is_check_verbose_switch_active = 1; } if ( $switch eq 'mSwitch' and !$is_m_switch_active ) { $logger->info("*Switch activated via YAML: modifyLineBreaks (-m)"); $is_m_switch_active = 1; } if ( $switch eq 'rSwitch' and !$is_r_switch_active ) { $logger->info("*Switch activated via YAML: replacement mode (-r)"); $is_r_switch_active = 1; } if ( $switch eq 'rrSwitch' and !$is_rr_switch_active ) { $logger->info("*Switch activated via YAML: replacement ONLY mode (-rr)"); $is_r_switch_active = 1; $is_rr_switch_active = 1; } if ( $switch eq 'rvSwitch' and !$is_rv_switch_active ) { $logger->info("*Switch activated via YAML: replacement respect verbatim mode (-rv)"); $is_r_switch_active = 1; $is_rv_switch_active = 1; } if ( $switch eq 'sSwitch' and !$switch{silentMode} ) { $logger->info("*Switch activated via YAML: silent mode (-s)"); $switch{silentMode} = 1; } if ( $switch eq 'slSwitch' and !$switch{screenlog} ) { $logger->info("*Switch activated via YAML: screen log (-sl)"); $switch{screenlog} = 1; } if ( $switch eq 'tSwitch' and !$is_t_switch_active ) { $logger->info("*Switch activated via YAML: trace mode (-t)"); $is_t_switch_active = 1; } if ( $switch eq 'ttSwitch' and !$is_tt_switch_active ) { $logger->info("*Switch activated via YAML: ttrace mode (-tt)"); $is_t_switch_active = 1; $is_tt_switch_active = 1; } } # the following are incompatible: # # modifyLineBreaks: # oneSentencePerLine: # manipulateSentences: 1 # textWrapSentences: 1 # sentenceIndent: " "