summaryrefslogtreecommitdiff
path: root/support/latexindent/LatexIndent/Else.pm
blob: 26d8014f5a4d37f6dfbcc88c0a93cb0340e8fadd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package LatexIndent::Else;
#	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::Switches qw/$is_m_switch_active $is_t_switch_active $is_tt_switch_active/;
use LatexIndent::LogFile qw/$logger/;
use LatexIndent::Braces qw/$braceBracketRegExpBasic/;
use LatexIndent::Special qw/$specialBeginAndBracesBracketsBasicRegExp/;
use LatexIndent::Heading qw/$allHeadingsRegexp/;
use Exporter qw/import/;
our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321
our @EXPORT_OK = qw/check_for_else_statement/;
our $elseCounter;

sub check_for_else_statement{
    my $self = shift;

    # we call the else routine from different places; see IfElseFi.pm and Special.pm
    my %input = @_;

    # store the regular expresssion for matching and replacing the \else statements
    my $elseRegExp = qr/
                      (
                        $input{elseNameRegExp}
                        \h*                             # possible horizontal space
                        (\R*)                           # possible line breaks after \else statement
                      )
                      (
                        (?: 
                            (?!$input{elseNameRegExp}).
                        )*?                             # body, which can't include another \else
                      )
                      $
                /sx;

    $logger->trace("*Looking for $input{elseNameRegExp} statement (${$self}{name})") if $is_t_switch_active;

    while(${$self}{body} =~ m/$elseRegExp(\h*)($trailingCommentRegExp)?/){
        ${$self}{body} =~ s/$elseRegExp(\h*)($trailingCommentRegExp)?
                           /   
                          # create a new Else object
                          my $else = LatexIndent::Else->new(begin=>$1,
                                            name=>${$self}{name},
                                            storageNameAppend=>$input{storageNameAppend},
                                            body=>$3,
                                            end=>q(),
                                            linebreaksAtEnd=>{
                                              begin=>$2?1:0,
                                              body=>0,
                                              end=>0,
                                            },
                                            aliases=>{
                                              # begin statements
                                              BeginStartsOnOwnLine=>$input{ElseStartsOnOwnLine},
                                              # end statements
                                              BodyStartsOnOwnLine=>$input{ElseFinishesWithLineBreak},
                                            },
                                            modifyLineBreaksYamlName=>${$self}{modifyLineBreaksYamlName},
                                            endImmediatelyFollowedByComment=>0,
                                            horizontalTrailingSpace=>q(),
                                            # mandatory and optional arguments have a parent, which we need
                                            # to detail for double back slash poly-switches 
                                            # (see test-cases alignment command-align.tex, for example)
                                            parent=>(${$self}{parent}?${$self}{parent}:"none"),
                                          );

                          # log file output
                          $logger->trace("*$input{logName} found: ${$self}{name}")if $is_t_switch_active;
             
                          # the settings and storage of most objects has a lot in common
                          $self->get_settings_and_store_new_object($else);
                          ${@{${$self}{children}}[-1]}{replacementText};
                          /xse;
    }
    return;
}

sub remove_line_breaks_begin{
    # the \else command can need a trailing white space if the line breaks have been removed after it and
    # there is no white space
    my $self = shift;
    my $BodyStringLogFile = ${$self}{aliases}{BodyStartsOnOwnLine}||"BodyStartsOnOwnLine";
    $logger->trace("Removing linebreak at the end of begin (see $BodyStringLogFile)");
    ${$self}{begin} =~ s/\R*$//sx;
    ${$self}{begin} .= " " unless(${$self}{begin} =~ m/\h$/s or ${$self}{body} =~ m/^\h/s or ${$self}{body} =~ m/^\R/s );
    ${$self}{linebreaksAtEnd}{begin} = 0;
}

sub tasks_particular_to_each_object{
    my $self = shift;

    # search for headings (important to do this before looking for commands!)
    $self->find_heading if ${$self}{body} =~ m/$allHeadingsRegexp/s;

    # search for commands and special code blocks
    $self->find_commands_or_key_equals_values_braces_and_special if ${$self}{body} =~ m/$specialBeginAndBracesBracketsBasicRegExp/s;
    
    # search for arguments
    $self->find_opt_mand_arguments if ${$self}{body} =~ m/$braceBracketRegExpBasic/s;

    return;
}

sub yaml_get_indentation_information{
    return q();
}

sub create_unique_id{
    my $self = shift;

    $elseCounter++;

    ${$self}{id} = "$tokens{else}$elseCounter";
    return;
}

1;