diff options
Diffstat (limited to 'Master/texmf-dist/scripts/latexindent/LatexIndent/Verbatim.pm')
-rw-r--r-- | Master/texmf-dist/scripts/latexindent/LatexIndent/Verbatim.pm | 272 |
1 files changed, 168 insertions, 104 deletions
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/Verbatim.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/Verbatim.pm index f283eb0661c..d115bf078f7 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/Verbatim.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/Verbatim.pm @@ -22,7 +22,7 @@ use LatexIndent::Tokens qw/%tokens/; use LatexIndent::GetYamlSettings qw/%masterSettings/; use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active $is_m_switch_active/; use LatexIndent::LogFile qw/$logger/; -our @EXPORT_OK = qw/put_verbatim_back_in find_verbatim_environments find_noindent_block find_verbatim_commands put_verbatim_commands_back_in find_verbatim_special/; +our @EXPORT_OK = qw/put_verbatim_back_in find_verbatim_environments find_noindent_block find_verbatim_commands find_verbatim_special verbatim_common_tasks/; our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321 our $verbatimCounter; @@ -59,11 +59,13 @@ sub find_noindent_block{ while( ${$self}{body} =~ m/$noIndentRegExp/sx){ - # create a new Environment object + # create a new Verbatim object my $noIndentBlockObj = LatexIndent::Verbatim->new( begin=>$1, body=>$2, end=>$3, name=>$noIndentBlock, + type=>"noindentblock", + modifyLineBreaksYamlName=>"verbatim", ); # give unique id @@ -113,19 +115,34 @@ sub find_verbatim_environments{ ( \\end\{$verbEnvSpec\} # \end{<something>} statement ) + (\h*)? # possibly followed by horizontal space + (\R)? # possibly followed by a line break /sx; while( ${$self}{body} =~ m/$verbatimRegExp/sx){ - # create a new Environment object + # create a new Verbatim object my $verbatimBlock = LatexIndent::Verbatim->new( begin=>$1, body=>$2, end=>$3, name=>$verbEnv, + type=>"environment", + modifyLineBreaksYamlName=>"verbatim", + linebreaksAtEnd=>{ + end=>$5?1:0, + }, + horizontalTrailingSpace=>$4?$4:q(), + aliases=>{ + # begin statements + BeginStartsOnOwnLine=>"VerbatimBeginStartsOnOwnLine", + # after end statements + EndFinishesWithLineBreak=>"VerbatimEndFinishesWithLineBreak", + }, ); - # give unique id - $verbatimBlock->create_unique_id; + # there are common tasks for each of the verbatim objects + $verbatimBlock->verbatim_common_tasks; + # verbatim children go in special hash ${$self}{verbatim}{${$verbatimBlock}{id}}=$verbatimBlock; @@ -133,7 +150,7 @@ sub find_verbatim_environments{ $logger->trace("*VERBATIM environment found: $verbEnv") if $is_t_switch_active; # remove the environment block, and replace with unique ID - ${$self}{body} =~ s/$verbatimRegExp/${$verbatimBlock}{id}/sx; + ${$self}{body} =~ s/$verbatimRegExp/${$verbatimBlock}{replacementText}/sx; $logger->trace("replaced with ID: ${$verbatimBlock}{id}") if $is_t_switch_active; @@ -161,7 +178,7 @@ sub find_verbatim_commands{ if($yesno){ $logger->trace("looking for $verbCommand:$yesno Commands") if $is_t_switch_active; - my $verbatimCommandRegExp = qr/ + my $verbatimRegExp = qr/ ( \\$verbCommand \h* @@ -176,39 +193,53 @@ sub find_verbatim_commands{ (?<!\\) # not immediately pre-ceeded by \ \] # [optional arguments] \h* - )? # opt arg into $2 + )? # opt arg into $2 ( . - ) # delimiter into $3 + ) # delimiter into $3 ( .*? - ) # body into $4 + ) # body into $4 \3 + (\h*)? # possibly followed by horizontal space + (\R)? # possibly followed by a line break /mx; - while( ${$self}{body} =~ m/$verbatimCommandRegExp/){ + while( ${$self}{body} =~ m/$verbatimRegExp/){ - # create a new Environment object + # create a new Verbatim object my $verbatimCommand = LatexIndent::Verbatim->new( begin=>$1.($2?$2:q()).$3, body=>$4, end=>$3, name=>$verbCommand, + type=>"command", + modifyLineBreaksYamlName=>"verbatim", + linebreaksAtEnd=>{ + end=>$6?1:0, + }, + horizontalTrailingSpace=>$5?$5:q(), + aliases=>{ + # begin statements + BeginStartsOnOwnLine=>"VerbatimBeginStartsOnOwnLine", + # after end statements + EndFinishesWithLineBreak=>"VerbatimEndFinishesWithLineBreak", + }, optArg=>$2?$2:q(), ); - # give unique id - $verbatimCommand->create_unique_id; + # there are common tasks for each of the verbatim objects + $verbatimCommand->verbatim_common_tasks; # output, if desired $logger->trace(Dumper($verbatimCommand),'ttrace') if($is_tt_switch_active); # verbatim children go in special hash - ${$self}{verbatimCommands}{${$verbatimCommand}{id}}=$verbatimCommand; + ${$self}{verbatim}{${$verbatimCommand}{id}}=$verbatimCommand; # log file output $logger->trace("*VERBATIM command found: $verbCommand") if $is_t_switch_active; # remove the environment block, and replace with unique ID - ${$self}{body} =~ s/$verbatimCommandRegExp/${$verbatimCommand}{id}/sx; + ${$self}{body} =~ s/$verbatimRegExp/${$verbatimCommand}{replacementText}/sx; $logger->trace("replaced with ID: ${$verbatimCommand}{id}") if $is_t_switch_active; @@ -223,73 +254,127 @@ sub find_verbatim_commands{ } -sub put_verbatim_back_in { +sub find_verbatim_special{ my $self = shift; - # if there are no verbatim children, return - return unless(${$self}{verbatim}); + # loop through specialBeginEnd + while( my ($specialName,$BeginEnd)= each %{$masterSettings{specialBeginEnd}}){ - # search for environments/commands - $logger->trace('*Putting verbatim back in, here is the pre-processed body:') if $is_tt_switch_active; - $logger->trace(${$self}{body}) if($is_tt_switch_active); + # only classify special Verbatim if lookForThis is 'verbatim' + if( (ref($BeginEnd) eq "HASH") and ${$BeginEnd}{lookForThis}=~m/v/s and ${$BeginEnd}{lookForThis} eq 'verbatim'){ + $logger->trace('*Searching for VERBATIM special (see specialBeginEnd)') if $is_t_switch_active; - # loop through document children hash - while( (scalar keys %{${$self}{verbatim}})>0 ){ - while( my ($key,$child)= each %{${$self}{verbatim}}){ - if(${$self}{body} =~ m/${$child}{id}/mx){ + my $verbatimRegExp = qr/ + ( + ${$BeginEnd}{begin} + ) + ( + .*? + ) + ( + ${$BeginEnd}{end} + ) + (\h*)? # possibly followed by horizontal space + (\R)? # possibly followed by a line break + /sx; - # replace ids with body - ${$self}{body} =~ s/${$child}{id}/${$child}{begin}${$child}{body}${$child}{end}/; + while( ${$self}{body} =~ m/$verbatimRegExp/sx){ - # log file info - $logger->trace('Body now looks like:') if $is_tt_switch_active; - $logger->trace(${$self}{body},'ttrace') if($is_tt_switch_active); + # create a new Verbatim object + my $verbatimBlock = LatexIndent::Verbatim->new( begin=>$1, + body=>$2, + end=>$3, + name=>$specialName, + modifyLineBreaksYamlName=>"specialBeginEnd", + linebreaksAtEnd=>{ + end=>$5?1:0, + }, + horizontalTrailingSpace=>$4?$4:q(), + type=>"special", + aliases=>{ + # begin statements + BeginStartsOnOwnLine=>"SpecialBeginStartsOnOwnLine", + # after end statements + EndFinishesWithLineBreak=>"SpecialEndFinishesWithLineBreak", + }, + ); + # there are common tasks for each of the verbatim objects + $verbatimBlock->verbatim_common_tasks; - # delete the hash so it won't be operated upon again - delete ${$self}{verbatim}{${$child}{id}}; - } elsif ($is_m_switch_active and ${$masterSettings{modifyLineBreaks}{textWrapOptions}}{columns}>1 and ${$self}{body} !~ m/${$child}{id}/){ - $logger->trace("${$child}{id} not found in body using /m matching, it may have been split across line (see modifyLineBreaks: textWrapOptions)") if($is_t_switch_active); + # verbatim children go in special hash + ${$self}{verbatim}{${$verbatimBlock}{id}}=$verbatimBlock; - # search for a version of the verbatim ID that may have line breaks - my $verbatimIDwithLineBreaks = join("\\R?",split(//,${$child}{id})); - my $verbatimIDwithLineBreaksRegExp = qr/$verbatimIDwithLineBreaks/s; + # log file output + $logger->trace("*VERBATIM special found: $specialName") if $is_t_switch_active; - # replace the line-broken verbatim ID with a non-broken verbatim ID - ${$self}{body} =~ s/$verbatimIDwithLineBreaksRegExp/${$child}{id}/s; - } - } - } + # remove the special block, and replace with unique ID + ${$self}{body} =~ s/$verbatimRegExp/${$verbatimBlock}{replacementText}/sx; - # logfile info - $logger->trace('*Post-processed body:') if $is_tt_switch_active; - $logger->trace(${$self}{body}) if($is_tt_switch_active); - return; + $logger->trace("replaced with ID: ${$verbatimBlock}{id}") if $is_t_switch_active; + + # possible decoration in log file + $logger->trace(${$masterSettings{logFilePreferences}}{showDecorationFinishCodeBlockTrace}) if ${$masterSettings{logFilePreferences}}{showDecorationFinishCodeBlockTrace}; + } + } + } } -sub put_verbatim_commands_back_in { +sub put_verbatim_back_in { my $self = shift; + my %input = @_; # if there are no verbatim children, return - return unless(${$self}{verbatimCommands}); + return unless(${$self}{verbatim}); + my $verbatimCount=0; + my $toMatch = q(); + if($input{match} eq "everything-except-commands"){ + $toMatch = "noindentblockenvironmentspecial"; + + # count the number of non-command verbatim objects + while( my ($key,$child)= each %{${$self}{verbatim}}){ + ${$child}{type} = "environment" if !(defined ${$child}{type}); + $verbatimCount++ if($toMatch =~ m/${$child}{type}/); + } + return unless($verbatimCount>0); + } else { + $toMatch = "command"; + # count the number of command verbatim objects + while( my ($key,$child)= each %{${$self}{verbatim}}){ + ${$child}{type} = "environment" if !(defined ${$child}{type}); + $verbatimCount++ if($toMatch =~ m/${$child}{type}/); + } + return unless($verbatimCount>0); + } + # search for environments/commands - $logger->trace('*Putting verbatim commands back in, here is the pre-processed body:') if $is_tt_switch_active; + $logger->trace('*Putting verbatim back in') if $is_t_switch_active; + $logger->trace('pre-processed body:') if $is_tt_switch_active; $logger->trace(${$self}{body}) if($is_tt_switch_active); # loop through document children hash - while( (scalar keys %{${$self}{verbatimCommands}})>0 ){ - while( my ($key,$child)= each %{${$self}{verbatimCommands}}){ + my $verbatimFound=0; + while($verbatimFound < $verbatimCount){ + while( my ($key,$child)= each %{${$self}{verbatim}}){ + if($toMatch =~ m/${$child}{type}/){ if(${$self}{body} =~ m/${$child}{id}/mx){ - + # possibly remove trailing line break + if(defined ${$child}{EndFinishesWithLineBreak} + and ${$child}{EndFinishesWithLineBreak}==-1 + and ${$self}{body} =~ m/${$child}{id}\h*\R/s){ + $logger->trace("m-switch active, removing trailing line breaks from ${$child}{name}") if $is_t_switch_active; + ${$self}{body} =~ s/${$child}{id}(\h*)?(\R|\h)*/${$child}{id} /s; + } # replace ids with body - ${$self}{body} =~ s/${$child}{id}/${$child}{begin}${$child}{body}${$child}{end}/; + ${$self}{body} =~ s/${$child}{id}/${$child}{begin}${$child}{body}${$child}{end}/s; # log file info $logger->trace('Body now looks like:') if $is_tt_switch_active; $logger->trace(${$self}{body},'ttrace') if($is_tt_switch_active); - # delete the hash so it won't be operated upon again - delete ${$self}{verbatimCommands}{${$child}{id}}; + # delete the child so it won't be operated upon again + delete ${$self}{verbatim}{${$child}{id}}; + $verbatimFound++; } elsif ($is_m_switch_active and ${$masterSettings{modifyLineBreaks}{textWrapOptions}}{columns}>1 and ${$self}{body} !~ m/${$child}{id}/){ $logger->trace("${$child}{id} not found in body using /m matching, it may have been split across line (see modifyLineBreaks: textWrapOptions)") if($is_t_switch_active); @@ -299,65 +384,39 @@ sub put_verbatim_commands_back_in { # replace the line-broken verbatim ID with a non-broken verbatim ID ${$self}{body} =~ s/$verbatimIDwithLineBreaksRegExp/${$child}{id}/s; + + # note: we do *not* label this as found, as we need to go back through + # and search for the newly modified ID } } - } - # logfile info - $logger->trace('*Post-processed body:') if $is_tt_switch_active; - $logger->trace(${$self}{body}) if($is_tt_switch_active); + # logfile info + $logger->trace('*Post-processed body:') if $is_tt_switch_active; + $logger->trace(${$self}{body}) if($is_tt_switch_active); + } + } return; } -sub find_verbatim_special{ - my $self = shift; - - # loop through specialBeginEnd - while( my ($specialName,$BeginEnd)= each %{$masterSettings{specialBeginEnd}}){ - - # only classify special Verbatim if lookForThis is 'verbatim' - if( (ref($BeginEnd) eq "HASH") and ${$BeginEnd}{lookForThis}=~m/v/s and ${$BeginEnd}{lookForThis} eq 'verbatim'){ - $logger->trace('*Searching for VERBATIM special (see specialBeginEnd)') if $is_t_switch_active; - - my $verbatimRegExp = qr/ - ( - ${$BeginEnd}{begin} - ) - ( - .*? - ) - ( - ${$BeginEnd}{end} - ) - /sx; - - while( ${$self}{body} =~ m/$verbatimRegExp/sx){ +sub verbatim_common_tasks{ - # create a new Environment object - my $verbatimBlock = LatexIndent::Verbatim->new( begin=>$1, - body=>$2, - end=>$3, - name=>$specialName, - ); - # give unique id - $verbatimBlock->create_unique_id; + my $self = shift; - # verbatim children go in special hash - ${$self}{verbatim}{${$verbatimBlock}{id}}=$verbatimBlock; + # get yaml settings + $self->yaml_modify_line_breaks_settings if $is_m_switch_active; - # log file output - $logger->trace("*VERBATIM special found: $specialName") if $is_t_switch_active; + # give unique id + $self->create_unique_id; - # remove the special block, and replace with unique ID - ${$self}{body} =~ s/$verbatimRegExp/${$verbatimBlock}{id}/sx; + # the replacement text can be just the ID, but the ID might have a line break at the end of it + $self->get_replacement_text; - $logger->trace("replaced with ID: ${$verbatimBlock}{id}") if $is_t_switch_active; - - # possible decoration in log file - $logger->trace(${$masterSettings{logFilePreferences}}{showDecorationFinishCodeBlockTrace}) if ${$masterSettings{logFilePreferences}}{showDecorationFinishCodeBlockTrace}; - } - } - } + # the above regexp, when used below, will remove the trailing linebreak in ${$self}{linebreaksAtEnd}{end} + # so we compensate for it here + $self->adjust_replacement_text_line_breaks_at_end; + + # modify line breaks end statements + $self->modify_line_breaks_end if $is_m_switch_active; } sub create_unique_id{ @@ -368,4 +427,9 @@ sub create_unique_id{ return; } +sub yaml_get_textwrap_removeparagraphline_breaks{ + my $self = shift; + $logger->trace("No text wrap or remove paragraph line breaks for verbatim code blocks, ${$self}{name}") if ${$masterSettings{logFilePreferences}}{showDecorationFinishCodeBlockTrace}; +} + 1; |