diff options
Diffstat (limited to 'Master/texmf-dist/scripts/latexindent/LatexIndent/Heading.pm')
-rw-r--r-- | Master/texmf-dist/scripts/latexindent/LatexIndent/Heading.pm | 143 |
1 files changed, 83 insertions, 60 deletions
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Heading.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Heading.pm index 24d6865c4d5..0a6366f8fa2 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Heading.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Heading.pm @@ -1,4 +1,5 @@ package LatexIndent::Heading; + # 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 @@ -23,34 +24,38 @@ use LatexIndent::GetYamlSettings qw/%mainSettings/; use LatexIndent::LogFile qw/$logger/; use LatexIndent::Special qw/$specialBeginBasicRegExp/; 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/find_heading construct_headings_levels $allHeadingsRegexp/; our $headingCounter; our @headingsRegexpArray; our $allHeadingsRegexp = q(); -sub construct_headings_levels{ +sub construct_headings_levels { my $self = shift; # grab the heading levels - my %headingsLevels = %{$mainSettings{indentAfterHeadings}}; + my %headingsLevels = %{ $mainSettings{indentAfterHeadings} }; # output to log file - $logger->trace("*Constructing headings reg exp for example, chapter, section, etc (see indentAfterThisHeading)") if $is_t_switch_active ; + $logger->trace("*Constructing headings reg exp for example, chapter, section, etc (see indentAfterThisHeading)") + if $is_t_switch_active; # delete the values that have indentAfterThisHeading set to 0 - while( my ($headingName,$headingInfo)= each %headingsLevels){ - if(!${$headingsLevels{$headingName}}{indentAfterThisHeading}){ + while ( my ( $headingName, $headingInfo ) = each %headingsLevels ) { + if ( !${ $headingsLevels{$headingName} }{indentAfterThisHeading} ) { $logger->trace("Not indenting after $headingName (see indentAfterThisHeading)") if $is_t_switch_active; delete $headingsLevels{$headingName}; - } else { + } + else { # *all heading* regexp, remembering to put starred headings at the front of the regexp - if($headingName =~ m/\*/){ - $logger->trace("Putting $headingName at the beginning of the allHeadings regexp, as it contains a *") if $is_t_switch_active ; - $allHeadingsRegexp = $headingName.($allHeadingsRegexp eq '' ?q():"|$allHeadingsRegexp"); - } else { - $logger->trace("Putting $headingName at the END of the allHeadings regexp") if $is_t_switch_active ; - $allHeadingsRegexp .= ($allHeadingsRegexp eq '' ?q():"|").$headingName ; + if ( $headingName =~ m/\*/ ) { + $logger->trace("Putting $headingName at the beginning of the allHeadings regexp, as it contains a *") + if $is_t_switch_active; + $allHeadingsRegexp = $headingName . ( $allHeadingsRegexp eq '' ? q() : "|$allHeadingsRegexp" ); + } + else { + $logger->trace("Putting $headingName at the END of the allHeadings regexp") if $is_t_switch_active; + $allHeadingsRegexp .= ( $allHeadingsRegexp eq '' ? q() : "|" ) . $headingName; } } } @@ -58,54 +63,63 @@ sub construct_headings_levels{ # check for a * in the name $allHeadingsRegexp =~ s/\*/\\\*/g; - # sort the file extensions by preference - my @sortedByLevels = sort { ${$headingsLevels{$a}}{level} <=> $headingsLevels{$b}{level} } keys(%headingsLevels); + # sort the file extensions by preference + my @sortedByLevels = sort { ${ $headingsLevels{$a} }{level} <=> $headingsLevels{$b}{level} } keys(%headingsLevels); # it could be that @sortedByLevels is empty; return if !@sortedByLevels; - $logger->trace("*All headings regexp: $allHeadingsRegexp") if $is_tt_switch_active; - $logger->trace("*Now to construct headings regexp for each level:") if $is_t_switch_active; + $logger->trace("*All headings regexp: $allHeadingsRegexp") if $is_tt_switch_active; + $logger->trace("*Now to construct headings regexp for each level:") if $is_t_switch_active; - # loop through the levels, and create a regexp for each (min and max values are the first and last values respectively from sortedByLevels) - for(my $i = ${$headingsLevels{$sortedByLevels[0]}}{level}; $i <= ${$headingsLevels{$sortedByLevels[-1]}}{level}; $i++ ){ +# loop through the levels, and create a regexp for each (min and max values are the first and last values respectively from sortedByLevels) + for ( + my $i = ${ $headingsLevels{ $sortedByLevels[0] } }{level}; + $i <= ${ $headingsLevels{ $sortedByLevels[-1] } }{level}; + $i++ + ) + { # level regexp - my @tmp = grep { ${$headingsLevels{$_}}{level} == $i } keys %headingsLevels; - if(@tmp){ + my @tmp = grep { ${ $headingsLevels{$_} }{level} == $i } keys %headingsLevels; + if (@tmp) { my $headingsAtThisLevel = q(); - foreach(@tmp){ - # put starred headings at the front of the regexp - if($_ =~ m/\*/){ - $logger->trace("Putting $_ at the beginning of this regexp (level $i), as it contains a *") if $is_t_switch_active; - $headingsAtThisLevel = $_.($headingsAtThisLevel eq '' ?q():"|$headingsAtThisLevel"); - } else { + foreach (@tmp) { + + # put starred headings at the front of the regexp + if ( $_ =~ m/\*/ ) { + $logger->trace("Putting $_ at the beginning of this regexp (level $i), as it contains a *") + if $is_t_switch_active; + $headingsAtThisLevel = $_ . ( $headingsAtThisLevel eq '' ? q() : "|$headingsAtThisLevel" ); + } + else { $logger->trace("Putting $_ at the END of this regexp (level $i)") if $is_t_switch_active; - $headingsAtThisLevel .= ($headingsAtThisLevel eq '' ?q():"|").$_ ; - } + $headingsAtThisLevel .= ( $headingsAtThisLevel eq '' ? q() : "|" ) . $_; + } } # make the stars escaped correctly $headingsAtThisLevel =~ s/\*/\\\*/g; - push(@headingsRegexpArray,$headingsAtThisLevel); - $logger->trace("Heading level regexp for level $i will contain: $headingsAtThisLevel") if $is_t_switch_active; + push( @headingsRegexpArray, $headingsAtThisLevel ); + $logger->trace("Heading level regexp for level $i will contain: $headingsAtThisLevel") + if $is_t_switch_active; } } - } +} -sub find_heading{ +sub find_heading { # if there are no headings regexps, there's no point going any further - return if!@headingsRegexpArray; + return if !@headingsRegexpArray; my $self = shift; # otherwise loop through the headings regexp $logger->trace("*Searching ${$self}{name} for headings ") if $is_t_switch_active; - # loop through each headings match; note that we need to + # loop through each headings match; note that we need to # do it in *reverse* so as to ensure that the lower level headings get matched first of all - foreach(reverse(@headingsRegexpArray)){ - + foreach ( reverse(@headingsRegexpArray) ) { + # the regexp my $headingRegExp = qr/ ( @@ -117,8 +131,8 @@ sub find_heading{ (\R*)? # linebreaks at end of body into $4 ((?:\\(?:$allHeadingsRegexp))|$) # up to another heading, or else the end of the file /sx; - - while(${$self}{body} =~ m/$headingRegExp/){ + + while ( ${$self}{body} =~ m/$headingRegExp/ ) { # log file output $logger->trace("heading found: $2") if $is_t_switch_active; @@ -148,19 +162,19 @@ sub find_heading{ ${@{${$self}{children}}[-1]}{replacementText}; /xse; } - } + } } -sub get_replacement_text{ +sub get_replacement_text { my $self = shift; # the replacement text for a heading (chapter, section, etc) needs to put the trailing part back in $logger->trace("Custom replacement text routine for ${$self}{name}") if $is_t_switch_active; - ${$self}{replacementText} = ${$self}{id}.${$self}{afterbit}; + ${$self}{replacementText} = ${$self}{id} . ${$self}{afterbit}; delete ${$self}{afterbit}; } -sub create_unique_id{ +sub create_unique_id { my $self = shift; $headingCounter++; @@ -169,11 +183,12 @@ sub create_unique_id{ return; } -sub adjust_replacement_text_line_breaks_at_end{ +sub adjust_replacement_text_line_breaks_at_end { return; } -sub yaml_get_object_attribute_for_indentation_settings{ +sub yaml_get_object_attribute_for_indentation_settings { + # when looking for noAdditionalIndent or indentRules, we may need to determine # which thing we're looking for, e.g # @@ -186,11 +201,11 @@ sub yaml_get_object_attribute_for_indentation_settings{ # this method returns 'body' by default, but the other objects (optionalArgument, mandatoryArgument, afterHeading) # return their appropriate identifier. my $self = shift; - + return ${$self}{modifyLineBreaksYamlName}; } -sub tasks_particular_to_each_object{ +sub tasks_particular_to_each_object { my $self = shift; # search for commands, keys, named grouping braces @@ -209,16 +224,21 @@ sub tasks_particular_to_each_object{ # CommandStartsOnOwnLine: 1 # # then we need to transfer this information to the heading object - if($is_m_switch_active){ - $logger->trace("Searching for linebreak preferences immediately infront of ${$self}{parent}") if $is_t_switch_active; - foreach(@{${$self}{children}}){ - if(${$_}{name} eq ${$self}{parent}){ + if ($is_m_switch_active) { + $logger->trace("Searching for linebreak preferences immediately infront of ${$self}{parent}") + if $is_t_switch_active; + foreach ( @{ ${$self}{children} } ) { + if ( ${$_}{name} eq ${$self}{parent} ) { $logger->trace("Named child found: ${$_}{name}") if $is_t_switch_active; - if(defined ${$_}{BeginStartsOnOwnLine}){ - $logger->trace("Transferring information from ${$_}{id} (${$_}{name}) to ${$self}{id} (${$self}{name}) for BeginStartsOnOwnLine") if $is_t_switch_active; + if ( defined ${$_}{BeginStartsOnOwnLine} ) { + $logger->trace( + "Transferring information from ${$_}{id} (${$_}{name}) to ${$self}{id} (${$self}{name}) for BeginStartsOnOwnLine" + ) if $is_t_switch_active; ${$self}{BeginStartsOnOwnLine} = ${$_}{BeginStartsOnOwnLine}; - } else { - $logger->trace("No information found in ${$_}{name} for BeginStartsOnOwnLine") if $is_t_switch_active; + } + else { + $logger->trace("No information found in ${$_}{name} for BeginStartsOnOwnLine") + if $is_t_switch_active; } last; } @@ -231,13 +251,16 @@ sub tasks_particular_to_each_object{ return; } -sub add_surrounding_indentation_to_begin_statement{ - # almost all of the objects add surrounding indentation to the 'begin' statements, +sub add_surrounding_indentation_to_begin_statement { + + # almost all of the objects add surrounding indentation to the 'begin' statements, # but some (e.g HEADING) have their own method my $self = shift; - - $logger->trace("Adding surrounding indentation after (empty, by design!) begin statement of ${$self}{name} (${$self}{id})") if $is_t_switch_active; - ${$self}{begin} .= ${$self}{surroundingIndentation}; # add indentation + + $logger->trace( + "Adding surrounding indentation after (empty, by design!) begin statement of ${$self}{name} (${$self}{id})") + if $is_t_switch_active; + ${$self}{begin} .= ${$self}{surroundingIndentation}; # add indentation } |