summaryrefslogtreecommitdiff
path: root/support/latexindent/LatexIndent/FileContents.pm
blob: b18feae6bd9cef5266c47c8ba4ec65291f74566a (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
281
282
283
package LatexIndent::FileContents;
#	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::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::Environment qw/$environmentBasicRegExp/;
use LatexIndent::IfElseFi qw/$ifElseFiBasicRegExp/;
use LatexIndent::Special qw/$specialBeginAndBracesBracketsBasicRegExp/;
use LatexIndent::Heading qw/$allHeadingsRegexp/;
use LatexIndent::Verbatim qw/%verbatimStorage/;
use Data::Dumper;
use Exporter qw/import/;
our @EXPORT_OK = qw/find_file_contents_environments_and_preamble/;
our @ISA = "LatexIndent::Document"; # class inheritance, Programming Perl, pg 321
our $fileContentsCounter;

sub find_file_contents_environments_and_preamble{
    my $self = shift;

    # store the file contents blocks in an array which, depending on the value 
    # of indentPreamble, will be put into the verbatim hash, or otherwise 
    # stored as children to be operated upon
    my @fileContentsStorageArray; 

    # fileContents environments
    $logger->trace('*Searching for FILE CONTENTS environments (see fileContentsEnvironments)') if $is_t_switch_active;
    $logger->trace(Dumper(\%{$mainSettings{fileContentsEnvironments}})) if($is_tt_switch_active);
    while( my ($fileContentsEnv,$yesno)= each %{$mainSettings{fileContentsEnvironments}}){

        if(!$yesno){
            $logger->trace(" *not* looking for $fileContentsEnv as $fileContentsEnv:$yesno");
            next;
        }

        $logger->trace("looking for $fileContentsEnv environments") if $is_t_switch_active;

        # the trailing * needs some care
        if($fileContentsEnv =~ m/\*$/){
            $fileContentsEnv =~ s/\*$//;
            $fileContentsEnv .= '\*';
        }

        my $fileContentsRegExp = qr/
                        (
                        \\begin\{
                                $fileContentsEnv       
                               \}                     
                        )
                        (
                            .*?
                        )                            
                        (
                            \\end\{$fileContentsEnv\}  
                            \h*
                        )                    
                        (\R)?  
                    /sx;

        while( ${$self}{body} =~ m/$fileContentsRegExp/sx){

          # create a new Environment object
          my $fileContentsBlock = LatexIndent::FileContents->new( begin=>$1,
                                                body=>$2,
                                                end=>$3,
                                                name=>$fileContentsEnv,
                                                linebreaksAtEnd=>{
                                                  begin=>0,
                                                  body=>0,
                                                  end=>$4?1:0,
                                                },
                                                modifyLineBreaksYamlName=>"filecontents",
                                                );
          # give unique id
          $fileContentsBlock->create_unique_id;
          
          # text wrapping can make the ID split across lines
          ${$fileContentsBlock}{idRegExp} = ${$fileContentsBlock}{id};

          if($is_m_switch_active){
              my $IDwithLineBreaks = join("\\R?\\h*",split(//,${$fileContentsBlock}{id}));
              ${$fileContentsBlock}{idRegExp} = qr/$IDwithLineBreaks/s;  
          }

          # the replacement text can be just the ID, but the ID might have a line break at the end of it
          $fileContentsBlock->get_replacement_text;

          # count body line breaks
          $fileContentsBlock->count_body_line_breaks;

          # the above regexp, when used below, will remove the trailing linebreak in ${$self}{linebreaksAtEnd}{end}
          # so we compensate for it here
          $fileContentsBlock->adjust_replacement_text_line_breaks_at_end;

          # store the fileContentsBlock, and determine location afterwards
          push(@fileContentsStorageArray,$fileContentsBlock);

          # log file output
          $logger->trace("FILECONTENTS environment found: $fileContentsEnv")if $is_t_switch_active;

          # remove the environment block, and replace with unique ID
          ${$self}{body} =~ s/$fileContentsRegExp/${$fileContentsBlock}{replacementText}/sx;

          $logger->trace("replaced with ID: ${$fileContentsBlock}{id}") if $is_tt_switch_active;
        } 
    }

    # determine if body of document contains \begin{document} -- if it does, then assume
    # that the body has a preamble
    my $preambleRegExp = qr/
                        (.*?)
                        (\R*\h*)?            # linebreaks at end of body into $2
                        \\begin\{document\}
                /sx;
    my $preamble = q();

    my $needToStorePreamble = 0;

    # try and find the preamble
    if( ${$self}{body} =~ m/$preambleRegExp/sx and ${$mainSettings{lookForPreamble}}{${$self}{fileExtension}}){

        $logger->trace("\\begin{document} found in body (after searching for filecontents)-- assuming that a preamble exists") if $is_t_switch_active ;

        # create a preamble object
        $preamble = LatexIndent::Preamble->new( begin=>q(),
                                              body=>$1,
                                              end=>q(),
                                              name=>"preamble",
                                              linebreaksAtEnd=>{
                                                begin=>0,
                                                body=>$2?1:0,
                                                end=>0,
                                              },
                                              afterbit=>($2?$2:q())."\\begin{document}",
                                              modifyLineBreaksYamlName=>"preamble",
                                              );

        # give unique id
        $preamble->create_unique_id;
        
        # text wrapping can make the ID split across lines
        ${$preamble}{idRegExp} = ${$preamble}{id};

        if($is_m_switch_active){
            my $IDwithLineBreaks = join("\\R?\\h*",split(//,${$preamble}{id}));
            ${$preamble}{idRegExp} = qr/$IDwithLineBreaks/s;  
        }

        # get the replacement_text
        $preamble->get_replacement_text;

        # log file output
        $logger->trace("preamble found: preamble") if $is_t_switch_active;

        # remove the environment block, and replace with unique ID
        ${$self}{body} =~ s/$preambleRegExp/${$preamble}{replacementText}/sx;

        $logger->trace("replaced with ID: ${$preamble}{replacementText}") if $is_tt_switch_active;
        # indentPreamble set to 1
        if($mainSettings{indentPreamble}){
            $logger->trace("storing ${$preamble}{id} for indentation (see indentPreamble)") if $is_tt_switch_active;
            $needToStorePreamble = 1;
        } else {
            # indentPreamble set to 0
            $logger->trace("NOT storing ${$preamble}{id} for indentation -- will store as VERBATIM object (because indentPreamble:0)") if $is_t_switch_active;
            $preamble->unprotect_blank_lines if( $is_m_switch_active and ${$mainSettings{modifyLineBreaks}}{preserveBlankLines} );
            $verbatimStorage{${$preamble}{id}} = $preamble;
        }
    } else {
        ${$self}{preamblePresent} = 0;
    }

    # loop through the fileContents array, check if it's in the preamble
    foreach(@fileContentsStorageArray){
              my $indentThisChild = 0;
              # verbatim children go in special hash
              if($preamble ne '' and ${$preamble}{body} =~ m/${$_}{id}/){
                $logger->trace("filecontents (${$_}{id}) is within preamble") if $is_t_switch_active;
                # indentPreamble set to 1
                if($mainSettings{indentPreamble}){
                    $logger->trace("storing ${$_}{id} for indentation (indentPreamble is 1)") if $is_t_switch_active;
                    $indentThisChild = 1;
                } else {
                    # indentPreamble set to 0
                    $logger->trace("Storing ${$_}{id} as a VERBATIM object (indentPreamble is 0)") if $is_t_switch_active;
                    $verbatimStorage{${$_}{id}} = $_;
                }
              } else {
                    $logger->trace("storing ${$_}{id} for indentation (${$_}{name} found outside of preamble)") if $is_t_switch_active;
                    $indentThisChild = 1;
              }
              # store the child, if necessary
              if($indentThisChild){
                    $_->remove_leading_space;
                    $_->yaml_get_indentation_settings_for_this_object;
                    $_->tasks_particular_to_each_object;
                    push(@{${$self}{children}},$_);
              
                    # possible decoration in log file 
                    $logger->trace(${$mainSettings{logFilePreferences}}{showDecorationFinishCodeBlockTrace}) if ${$mainSettings{logFilePreferences}}{showDecorationFinishCodeBlockTrace};
              }
    }

    if($needToStorePreamble){
        $preamble->dodge_double_backslash;
        $preamble->remove_leading_space;
        $preamble->find_commands_or_key_equals_values_braces if($mainSettings{preambleCommandsBeforeEnvironments});
        $preamble->tasks_particular_to_each_object;
        push(@{${$self}{children}},$preamble);
    }
    return;
}

sub create_unique_id{
    my $self = shift;

    $fileContentsCounter++;
    ${$self}{id} = "$tokens{filecontents}$fileContentsCounter$tokens{endOfToken}";
    return;
}

sub tasks_particular_to_each_object{
    my $self = shift;

    # text wrapping, remove paragraph line breaks
    if ($is_m_switch_active){
        $self->yaml_get_textwrap_removeparagraphline_breaks;
    }

    # option to textWrap beforeFindingChildCodeBlocks
    if( $is_m_switch_active and ${$mainSettings{modifyLineBreaks}{textWrapOptions}}{beforeFindingChildCodeBlocks} == 1){ 
        # call the remove_paragraph_line_breaks and text_wrap routines
        if(${$mainSettings{modifyLineBreaks}{removeParagraphLineBreaks}}{beforeTextWrap}){
            $self->remove_paragraph_line_breaks if ${$self}{removeParagraphLineBreaks};
            $self->text_wrap if (${$self}{textWrapOptions} and ${$mainSettings{modifyLineBreaks}{textWrapOptions}}{perCodeBlockBasis});
        } else {
            $self->text_wrap if (${$self}{textWrapOptions} and ${$mainSettings{modifyLineBreaks}{textWrapOptions}}{perCodeBlockBasis});
            $self->remove_paragraph_line_breaks if ${$self}{removeParagraphLineBreaks};
        }
    }

    # search for environments
    $self->find_environments if ${$self}{body} =~ m/$environmentBasicRegExp/s;

    # search for ifElseFi blocks
    $self->find_ifelsefi if ${$self}{body} =~ m/$ifElseFiBasicRegExp/s;

    # search for headings (part, chapter, section, setc)
    $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;
    
    # text wrapping, remove paragraph line breaks
    if ($is_m_switch_active and ${$mainSettings{modifyLineBreaks}{textWrapOptions}}{beforeFindingChildCodeBlocks} == 0){
        # call the remove_paragraph_line_breaks and text_wrap routines
        if(${$mainSettings{modifyLineBreaks}{removeParagraphLineBreaks}}{beforeTextWrap}){
            $self->remove_paragraph_line_breaks if ${$self}{removeParagraphLineBreaks};
            $self->text_wrap if (${$self}{textWrapOptions} and ${$mainSettings{modifyLineBreaks}{textWrapOptions}}{perCodeBlockBasis});
        } else {
            $self->text_wrap if (${$self}{textWrapOptions} and ${$mainSettings{modifyLineBreaks}{textWrapOptions}}{perCodeBlockBasis});
            $self->remove_paragraph_line_breaks if ${$self}{removeParagraphLineBreaks};
        }
    }
}

1;