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
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package LatexIndent::Environment;
# 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::GetYamlSettings qw/%mainSettings/;
use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active $is_m_switch_active/;
use LatexIndent::LogFile qw/$logger/;
use LatexIndent::Braces qw/$braceBracketRegExpBasic/;
use LatexIndent::IfElseFi qw/$ifElseFiBasicRegExp/;
use LatexIndent::Heading qw/$allHeadingsRegexp/;
use LatexIndent::Special qw/$specialBeginAndBracesBracketsBasicRegExp/;
use Exporter qw/import/;
our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321
our @EXPORT_OK = qw/find_environments $environmentBasicRegExp construct_environments_regexp/;
our $environmentCounter;
our $environmentBasicRegExp = qr/\\begin\{/;
our $environmentRegExp;
# store the regular expresssion for matching and replacing the \begin{}...\end{} statements
sub construct_environments_regexp{
# read from fine tuning
my $environmentNameRegExp = qr/${${$mainSettings{fineTuning}}{environments}}{name}/;
$environmentRegExp = qr/
(
\\begin\{
(
$environmentNameRegExp # lowercase|uppercase letters, @, *, numbers
) # environment name captured into $2
\} # \begin{<something>} statement
\h* # horizontal space
(\R*)? # possible line breaks (into $3)
) # begin statement captured into $1
(
(?: # cluster-only (), don't capture
(?! # don't include \begin in the body
(?:\\begin\{) # cluster-only (), don't capture
). # any character, but not \\begin
)*? # non-greedy
(\R*)? # possible line breaks (into $5)
) # environment body captured into $4
(
\\end\{\2\} # \end{<something>} statement
) # captured into $6
(\h*)? # possibly followed by horizontal space
(\R)? # possibly followed by a line break
/sx;
}
sub find_environments{
my $self = shift;
while( ${$self}{body} =~ m/$environmentRegExp\h*($trailingCommentRegExp)?/){
# global substitution
${$self}{body} =~ s/
$environmentRegExp(\h*)($trailingCommentRegExp)?
/
# create a new Environment object
my $env = LatexIndent::Environment->new(begin=>$1,
name=>$2,
body=>$4,
end=>$6,
linebreaksAtEnd=>{
begin=>$3?1:0,
body=>$5?1:0,
end=>$8?1:0,
},
modifyLineBreaksYamlName=>"environments",
endImmediatelyFollowedByComment=>$8?0:($10?1:0),
horizontalTrailingSpace=>$7?$7:q(),
);
# log file output
$logger->trace("*environment found: $2") if $is_t_switch_active;
# the settings and storage of most objects has a lot in common
$self->get_settings_and_store_new_object($env);
${@{${$self}{children}}[-1]}{replacementText}.($9?$9:q()).($10?$10:q());
/xseg;
$self->adjust_line_breaks_end_parent if $is_m_switch_active;
}
return;
}
sub tasks_particular_to_each_object{
my $self = shift;
# if the environment is empty, we may need to update linebreaksAtEnd{body}
if(${$self}{body} =~ m/^\h*$/s and ${${$self}{linebreaksAtEnd}}{begin}){
$logger->trace("empty environment body (${$self}{name}), updating linebreaksAtEnd{body} to be 1") if($is_t_switch_active);
${${$self}{linebreaksAtEnd}}{body} = 1;
}
# lookForAlignDelims: lookForChildCodeBlocks set to 0 means no child objects searched for
# see: test-cases/alignment/issue-308.tex
#
if( defined ${$self}{lookForChildCodeBlocks} and !${$self}{lookForChildCodeBlocks} ){
$logger->trace("lookForAlignDelims: lookForChildCodeBlocks set to 0, so child objects will *NOT* be searched for") if($is_t_switch_active);
return;
}
# search for items as the first order of business
$self->find_items if ${$mainSettings{indentAfterItems}}{${$self}{name}};
# 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;
# search for ifElseFi blocks
$self->find_ifelsefi if ${$self}{body} =~ m/$ifElseFiBasicRegExp/s;
}
sub create_unique_id{
my $self = shift;
$environmentCounter++;
${$self}{id} = "$tokens{environments}$environmentCounter";
return;
}
1;
|