summaryrefslogtreecommitdiff
path: root/support/latexindent/LatexIndent/HiddenChildren.pm
blob: a402a7cbce9a42b1b2aa0e60f57bc4a9d033cc63 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package LatexIndent::HiddenChildren;
#	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::Switches qw/$is_t_switch_active $is_tt_switch_active $is_m_switch_active /;
use LatexIndent::Tokens qw/%tokens/;
use LatexIndent::LogFile qw/$logger/;
use Data::Dumper;
use Exporter qw/import/;
our @EXPORT_OK = qw/find_surrounding_indentation_for_children update_family_tree get_family_tree check_for_hidden_children %familyTree hidden_children_preparation_for_alignment unpack_children_into_body/;

# hiddenChildren can be stored in a global array, it doesn't matter what level they're at
our %familyTree;
our %allChildren;


#----------------------------------------------------------
#   Discussion surrounding hidden children
#
#   Consider the following latex code
#
#   \begin{one}
#       body of one 
#       body of one 
#       body of one 
#       \begin{two}
#          body of two
#          body of two
#          body of two
#          body of two
#       \end{two}
#   \end{one}
#
#   From the visual perspective, we might say that <one> and <two> are *nested* children;
#   from the persepective of latexindent.pl, however, they actually have *the same level*.
#
#   Graphically, you might represent it as follows
#
#                     *
#                   /  \
#                  /    \
#                 /      \ 
#                O        O
#
#   where * represents the 'root' document object, and each 'O' is an environment object; the
#   first one, on the left, represents <two> and the second one, on the right, represents <one>. 
#   (Remember that the environment regexp does not allow \begin within its body.)
#
#   When processing the document, <one> will be processed *before* <two>. Furthermore, because
#   <one> and <two> are at the same level, they are not *natural* ancestors of each other; as such, 
#   we say that <two> is a *hidden* child, and that its 'adopted' ancestor is <one>. 
#
#   We need to go to a lot of effort to make sure that <two> knows about its ancestors and its 
#   surrounding indentation (<one> in this case). The subroutines in this file do that effort.
#----------------------------------------------------------

sub find_surrounding_indentation_for_children{
    my $self = shift;

    # output to logfile
    $logger->trace("*FamilyTree before update:") if $is_tt_switch_active;
    $logger->trace(Dumper(\%familyTree)) if($is_tt_switch_active);

    # update the family tree with ancestors
    $self->update_family_tree;

    # output information to the logfile
    $logger->trace("*FamilyTree after update:") if $is_tt_switch_active;
    $logger->trace(Dumper(\%familyTree)) if($is_tt_switch_active);

    while( my ($idToSearch,$ancestorToSearch) = each %familyTree){
          $logger->trace("*Hidden child ID: ,$idToSearch, here are its ancestors:") if $is_t_switch_active;
          foreach(@{${$ancestorToSearch}{ancestors}}){
              $logger->trace("ID: ${$_}{ancestorID}") if($is_t_switch_active);
              my $tmpIndentation = ref(${$_}{ancestorIndentation}) eq 'SCALAR'?${${$_}{ancestorIndentation}}:${$_}{ancestorIndentation};
              $tmpIndentation = $tmpIndentation ? $tmpIndentation : q(); 
              $logger->trace("indentation: '$tmpIndentation'") if($is_t_switch_active);
              }
          }

    return;
}

sub update_family_tree{
    my $self = shift;

    # loop through the hash
    $logger->trace("*Updating FamilyTree...") if $is_t_switch_active;
    while( my ($idToSearch,$ancestorToSearch)= each %familyTree){
          $logger->trace("*current ID: $idToSearch") if($is_t_switch_active);
          foreach(@{${$ancestorToSearch}{ancestors}}){
              my $ancestorID = ${$_}{ancestorID};

              # construct the natural ancestors
              my $naturalAncestors = q();
              foreach(@{${$familyTree{$idToSearch}}{ancestors}}){
                  $naturalAncestors .= "---".${$_}{ancestorID} if(${$_}{type} eq "natural");
              }
              
              # we only need to update the family tree if the $ancestorID is *not* a natural
              # ancestor, otherwise everything will be taken care of by the natural ancestor
              if($naturalAncestors !~ m/$ancestorID/){
                    $logger->trace("ancestor: $ancestorID") if($is_t_switch_active);
                    if($familyTree{$ancestorID}){
                        $logger->trace("$ancestorID is a key within familyTree, grabbing its ancestors") if($is_t_switch_active);
                        foreach(@{${$familyTree{$ancestorID}}{ancestors}}){
                            $logger->trace("ancestor of *hidden* child: ${$_}{ancestorID}") if($is_t_switch_active);
                            my $newAncestorId = ${$_}{ancestorID};
                            my $type;
                            if($naturalAncestors =~ m/$ancestorID/){
                                  $type = "natural";
                            } else {
                                  $type = "adopted";
                            }
                            my $matched = grep { $_->{ancestorID} eq $newAncestorId } @{${$familyTree{$idToSearch}}{ancestors}};
                            push(@{${$familyTree{$idToSearch}}{ancestors}},{ancestorID=>${$_}{ancestorID},ancestorIndentation=>${$_}{ancestorIndentation},type=>$type}) unless($matched);
                        }
                    } else {
                          $logger->trace("natural ancestors of $ancestorID: $naturalAncestors") if($is_t_switch_active);
                          foreach(@{${$allChildren{$ancestorID}}{ancestors}}){
                              my $newAncestorId = ${$_}{ancestorID};
                              my $type;
                              if($naturalAncestors =~ m/$newAncestorId/){
                                  $type = "natural";
                              } else {
                                  $type = "adopted";
                              }
                              my $matched = grep { $_->{ancestorID} eq $newAncestorId } @{${$familyTree{$idToSearch}}{ancestors}};
                              unless($matched){
                                  $logger->trace("ancestor of UNHIDDEN child: ${$_}{ancestorID}") if($is_t_switch_active);
                                  push(@{${$familyTree{$idToSearch}}{ancestors}},{ancestorID=>${$_}{ancestorID},ancestorIndentation=>${$_}{ancestorIndentation},type=>$type});
                              }
                          }
                    } 
               }
          }
    }

}

sub check_for_hidden_children{

    my $self = shift;

    my @matched;
    # grab the matches
    if($is_m_switch_active){
        # if modifyLineBreaks is active, then the IDS can be split across lines
        my $ifElseFiSpecialBegin = join("\\R?\\h*",split(//,$tokens{ifelsefiSpecial}));
        my $BeginwithLineBreaks = join("\\R?\\h*",split(//,$tokens{beginOfToken}));
        my $EndwithLineBreaks = join("\\R?\\h*",split(//,$tokens{endOfToken}));
        my $BlankLinesWithLineBreaks = join("\\R?\\h*",split(//,$tokens{blanklines}));
        @matched = (${$self}{body} =~ /(?!$BlankLinesWithLineBreaks)((?:$ifElseFiSpecialBegin)?$BeginwithLineBreaks[-a-z0-9\n]+?$EndwithLineBreaks)/ig);

        # remove line breaks and horizontal space from matches
        $_ =~ s/\R|\h//gs foreach (@matched);
    } else {
        @matched = (${$self}{body} =~ /((?:$tokens{ifelsefiSpecial})?$tokens{beginOfToken}.[-a-z0-9]+?$tokens{endOfToken})/igs);
    }

    # log file
    $logger->trace("*Hidden children check for ${$self}{name}") if $is_t_switch_active;
    $logger->trace(join("|",@matched)) if $is_t_switch_active;

    my $naturalAncestors = ${$self}{naturalAncestors}; 

    # loop through the hidden children
    foreach my $match (@matched){
        next if $match =~ m/$tokens{verbatim}/;

        # update the family tree with ancestors of self
        if(${$self}{ancestors}){
            foreach(@{${$self}{ancestors}}){
                my $newAncestorId = ${$_}{ancestorID};
                unless (grep { $_->{ancestorID} eq $newAncestorId } @{${$familyTree{$match}}{ancestors}}){
                    my $type = ($naturalAncestors =~ m/${$_}{ancestorID}/ ) ? "natural" : "adopted";
                    $logger->trace("Adding ${$_}{ancestorID} to the $type family tree of $match") if($is_t_switch_active);
                    push(@{$familyTree{$match}{ancestors}},{ancestorID=>${$_}{ancestorID},ancestorIndentation=>${$_}{ancestorIndentation},type=>$type});
                }
            }
        }

        # update the family tree with self
        unless (grep { $_->{ancestorID} eq ${$self}{id}} @{${$familyTree{$match}}{ancestors}}){
                    my $type = ($naturalAncestors =~ m/${$self}{id}/ ) ? "natural" : "adopted";
                    $logger->trace("Adding ${$self}{id} to the $type family tree of hiddenChild $match") if($is_t_switch_active);
                    push(@{$familyTree{$match}{ancestors}},{ancestorID=>${$self}{id},ancestorIndentation=>${$self}{indentation},type=>$type});

                    if(${$self}{lookForAlignDelims}){
                        $logger->trace("$match needs measuring for ${$self}{name} (see lookForAlignDelims)") if($is_t_switch_active);
                        push(@{${$self}{measureHiddenChildren}},$match);
                    }
        }
    }

}

#
# AlignmentAtAmpersand routine calculations are below
#
# PURPOSE:
#
# Consider the following example, which contains hidden children
#
#     \begin{align}
#     	A & =\begin{array}{cc}      % <!--- Hidden child
#     		     BBB & CCC \\       % <!--- Hidden child
#     		     E   & F            % <!--- Hidden child
#     	     \end{array} \\         % <!--- Hidden child
#
#     	Z & =\begin{array}{cc}      % <!--- Hidden child
#     		     Y & X \\           % <!--- Hidden child
#     		     W & V              % <!--- Hidden child
#     	     \end{array}            % <!--- Hidden child
#     \end{align}
#
# the approach that we adopt is:
#
#   1. for the *original* object  (align in the above), we loop through
#       its hidden children (array in the above) and unpack their contents
#       for measuring
#
#       see hidden_children_preparation_for_alignment
#           unpack_children_into_body
#
#   2. we store the unpacked body in the familyTree hash, using 'bodyForMeasure'
#
#       see $familyTree{${$_}{id}}{bodyForMeasure} = $bodyForMeasure;
#
#   3. during the alignment routine, we use 'bodyForMeasure' for measuring the cells
#
sub hidden_children_preparation_for_alignment{

    my $self = shift;
    my $latexIndentObject = shift;
    for my $hiddenChildToMeasure (@{${$latexIndentObject}{measureHiddenChildren}}){
        for (@{${$self}{children}}){
            if(${$_}{id} eq $hiddenChildToMeasure){

                my $bodyForMeasure = ${$_}{begin}.${$_}{body}.${$_}{end};
                for my $child (${$_}{children}){
                    $bodyForMeasure = &unpack_children_into_body(\@{$child},$bodyForMeasure);
                }
                $familyTree{${$_}{id}}{bodyForMeasure} = $bodyForMeasure;
            }
        }
    }
    return;
}

sub unpack_children_into_body{
    my $child = shift;
    my $body  = shift;
    for my $individualChild (@{$child}){
          $body =~ s/${$individualChild}{id}/${$individualChild}{begin}${$individualChild}{body}${$individualChild}{end}/s;

          if(${$individualChild}{children}){ 
            for my $nextlevelchild (${$individualChild}{children}){
                $body = &unpack_children_into_body(\@{$nextlevelchild},$body);
            }
          }
    }
    return $body;
}

1;