summaryrefslogtreecommitdiff
path: root/support/latexindent/LatexIndent/FileExtension.pm
blob: a9ff031493ee032afa54e3e47298a47a97da1e75 (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
package LatexIndent::FileExtension;
#	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 utf8;
use PerlIO::encoding;
use open ':std', ':encoding(UTF-8)';
use File::Basename; # to get the filename and directory path
use Exporter qw/import/;
use LatexIndent::GetYamlSettings qw/%mainSettings/;
use LatexIndent::Switches qw/%switches $is_check_switch_active/;
use LatexIndent::LogFile qw/$logger/;
our @EXPORT_OK = qw/file_extension_check/;

sub file_extension_check{
    my $self = shift;

    # grab the filename
    my $fileName = ${$self}{fileName};

    # see if an extension exists for the fileName
    my ($name,$dir,$ext) = fileparse($fileName,qr/\..[^.]*$/);
    
    # grab the file extension preferences
    my %fileExtensionPreference= %{$mainSettings{fileExtensionPreference}};

    # sort the file extensions by preference 
    my @fileExtensions = sort { $fileExtensionPreference{$a} <=> $fileExtensionPreference{$b} } keys(%fileExtensionPreference);
    
    # store the base name
    ${$self}{baseName} = $name;

    # if no extension, search according to fileExtensionPreference
    if ($fileName ne "-"){
        if (!$ext) {
            $logger->info("*File extension work:");
            $logger->info("latexindent called to act upon $fileName without a file extension;\nsearching for files in the following order (see fileExtensionPreference):");
            $logger->info($fileName.join("\n$fileName",@fileExtensions));

            my $fileFound = 0;
            # loop through the known file extensions (see @fileExtensions)
            foreach (@fileExtensions ){
                if ( -e $fileName.$_ ) {
                   $logger->info("$fileName$_ found!");
                   $fileName .= $_;
                   $logger->info("Updated fileName to $fileName");
                   ${$self}{fileName} = $fileName ;
                   $fileFound = 1;
                   $ext = $_;
                   last;
                }
            }
            unless($fileFound){
              $logger->fatal("*I couldn't find a match for $fileName in fileExtensionPreference (see defaultSettings.yaml)");
              foreach (@fileExtensions ){
                $logger->fatal("I searched for $fileName$_");
              }
              $logger->fatal("but couldn't find any of them.\nConsider updating fileExtensionPreference.");
              $logger->fatal("*Exiting, no indendation done."); 
              $self->output_logfile();
              exit(3);
            }
          } else {
            # if the file has a recognised extension, check that the file exists
            unless( -e $fileName ){
              $logger->fatal("*I couldn't find $fileName, are you sure it exists?");
              $logger->fatal("Exiting, no indendation done."); 
              $self->output_logfile();
              exit(3);
            }
          }
     }

    # store the file extension
    ${$self}{fileExtension} = $ext;

    # check to see if -o switch is active
    if($switches{outputToFile}){
        
        $logger->info("*-o switch active: output file check");

        if ($fileName eq "-" and $switches{outputToFile} =~ m/^\+/){
            $logger->info("STDIN input mode active, -o switch is removing all + symbols");
            $switches{outputToFile} =~ s/\+//g;
        }
        # the -o file name might begin with a + symbol
        if($switches{outputToFile} =~ m/^\+(.*)/ and $1 ne "+"){
            $logger->info("-o switch called with + symbol at the beginning: $switches{outputToFile}");
            $switches{outputToFile} = ${$self}{baseName}.$1;
            $logger->info("output file is now: $switches{outputToFile}");
        }

        my $strippedFileExtension = ${$self}{fileExtension};
        $strippedFileExtension =~ s/\.//; 
        $strippedFileExtension = "tex" if ($strippedFileExtension eq "");

        # grab the name, directory, and extension of the output file
        my ($name, $dir, $ext) = fileparse($switches{outputToFile}, $strippedFileExtension);

        # if there is no extension, then add the extension from the file to be operated upon
        if(!$ext){
            $logger->info("-o switch called with file name without extension: $switches{outputToFile}");
            $switches{outputToFile} = $name.($name=~m/\.\z/ ? q() : ".").$strippedFileExtension;
            $logger->info("Updated to $switches{outputToFile} as the file extension of the input file is $strippedFileExtension");
        }

        # the -o file name might end with ++ in which case we wish to search for existence, 
        # and then increment accordingly
        $name =~ s/\.$//;
        if($name =~ m/\+\+$/){
            $logger->info("-o switch called with file name ending with ++: $switches{outputToFile}");
            $name =~ s/\+\+$//;
            $name = ${$self}{baseName} if ($name eq "");
            my $outputFileCounter = 0;
            my $fileName = $name.$outputFileCounter.".".$strippedFileExtension; 
            $logger->info("will search for existence and increment counter, starting with $fileName");
            while( -e $fileName ){
                $logger->info("$fileName exists, incrementing counter");
                $outputFileCounter++;
                $fileName = $name.$outputFileCounter.".".$strippedFileExtension; 
            }
            $logger->info("$fileName does not exist, and will be the output file");
            $switches{outputToFile} = $fileName;
        }
    }

    # read the file into the Document body
    my @lines;
    if($fileName ne "-"){
        my $openFilePossible=1;
        open(MAINFILE, $fileName) or ($openFilePossible=0);
        if($openFilePossible==0){
            $logger->fatal("*$fileName exists, but could not open it");
            $logger->fatal("Exiting, no indendation done."); 
            $self->output_logfile();
            exit(4);
        }
        push(@lines,$_) while(<MAINFILE>);
        close(MAINFILE);
    } else {
            push(@lines,$_) while (<>)
    }

    # -n, --lines mode active
    if($switches{lines}){
      $self->lines_body_selected_lines(\@lines); 
    } else {
      # the all-important step: update the body
      ${$self}{body} = join("",@lines);
    }

    # necessary extra storage if check switch is active
    if ($is_check_switch_active){
        ${$self}{originalBody} = ${$self}{body};
    }
}
1;