diff options
Diffstat (limited to 'Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm')
-rw-r--r-- | Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm | 97 |
1 files changed, 95 insertions, 2 deletions
diff --git a/Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm b/Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm index bcfb2336264..3f0148d4670 100644 --- a/Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm +++ b/Master/texmf-dist/scripts/latexindent/LatexIndent/ModifyLineBreaks.pm @@ -18,11 +18,15 @@ use strict; use warnings; use Data::Dumper; use Exporter qw/import/; +use Text::Wrap; +use LatexIndent::GetYamlSettings qw/%masterSettings/; use LatexIndent::Tokens qw/%tokens/; use LatexIndent::TrailingComments qw/$trailingCommentRegExp/; use LatexIndent::Switches qw/$is_m_switch_active $is_t_switch_active $is_tt_switch_active/; -our @EXPORT_OK = qw/modify_line_breaks_body modify_line_breaks_end adjust_line_breaks_end_parent remove_line_breaks_begin/; -our @allObjects; +use LatexIndent::Item qw/$listOfItems/; +our @EXPORT_OK = qw/modify_line_breaks_body modify_line_breaks_end adjust_line_breaks_end_parent remove_line_breaks_begin max_char_per_line paragraphs_on_one_line construct_paragraph_reg_exp/; +our $paragraphRegExp = q(); + sub modify_line_breaks_body{ my $self = shift; @@ -171,4 +175,93 @@ sub adjust_line_breaks_end_parent{ } +sub max_char_per_line{ + return unless $is_m_switch_active; + + my $self = shift; + return unless ${$masterSettings{modifyLineBreaks}{textWrapOptions}}{columns}>1; + + # call the text wrapping routine + $Text::Wrap::columns=${$masterSettings{modifyLineBreaks}{textWrapOptions}}{columns}; + if(${$masterSettings{modifyLineBreaks}{textWrapOptions}}{separator} ne ''){ + $Text::Wrap::separator=${$masterSettings{modifyLineBreaks}{textWrapOptions}}{separator}; + } + ${$self}{body} = wrap('','',${$self}{body}); +} + +sub construct_paragraph_reg_exp{ + my $self = shift; + + my $stopAtRegExp = q(); + while( my ($paragraphStopAt,$yesNo)= each %{${$masterSettings{modifyLineBreaks}{removeParagraphLineBreaks}}{paragraphsStopAt}}){ + if($yesNo){ + # the headings (chapter, section, etc) need a slightly special treatment + $paragraphStopAt = "afterHeading" if($paragraphStopAt eq "heading"); + + # the comment need a slightly special treatment + $paragraphStopAt = "trailingComment" if($paragraphStopAt eq "comments"); + + # output to log file + $self->logger("The paragraph-stop regexp WILL include $tokens{$paragraphStopAt} (see paragraphsStopAt)",'heading') if $is_t_switch_active ; + + # update the regexp + if($paragraphStopAt eq "items"){ + $stopAtRegExp .= "|(?:\\\\(?:".$listOfItems."))"; + } else { + $stopAtRegExp .= "|(?:".($paragraphStopAt eq "trailingComment" ? "%" : q() ).$tokens{$paragraphStopAt}."\\d+)"; + } + } else { + $self->logger("The paragraph-stop regexp won't include $tokens{$paragraphStopAt} (see paragraphsStopAt)",'heading') if $is_t_switch_active ; + } + } + + $paragraphRegExp = qr/ + ^ + (?!$tokens{beginOfToken}) + (\w + (?: + (?! + (?:$tokens{blanklines}|\\par) + ). + )*? + ) + ( + (?: + ^(?:(\h*\R)|\\par|$tokens{blanklines}$stopAtRegExp) + ) + | + \z # end of string + )/sxm; + + $self->logger("The paragraph-stop-regexp is:",'heading') if $is_tt_switch_active ; + $self->logger($paragraphRegExp) if $is_tt_switch_active ; +} + +sub paragraphs_on_one_line{ + my $self = shift; + return unless ${$self}{removeParagraphLineBreaks}; + + # alignment at ampersand can take priority + return if(${$self}{lookForAlignDelims} and ${$masterSettings{modifyLineBreaks}{removeParagraphLineBreaks}}{alignAtAmpersandTakesPriority}); + + $self->logger("Checking ${$self}{name} for paragraphs (see removeParagraphLineBreaks)") if $is_t_switch_active; + + my $paragraphCounter; + my @paragraphStorage; + + ${$self}{body} =~ s/$paragraphRegExp/ + $paragraphCounter++; + push(@paragraphStorage,{id=>$tokens{paragraph}.$paragraphCounter.$tokens{endOfToken},value=>$1}); + + # replace comment with dummy text + $tokens{paragraph}.$paragraphCounter.$tokens{endOfToken}.$2; + /xsmeg; + + while( my $paragraph = pop @paragraphStorage){ + # remove all line breaks from paragraph, except for any at the very end + ${$paragraph}{value} =~ s/\R(?!\z)/ /sg; + ${$self}{body} =~ s/${$paragraph}{id}/${$paragraph}{value}/; + } +} + 1; |