summaryrefslogtreecommitdiff
path: root/support/easylatex/easylatex.pl
blob: a440195f75761c2c78391dfc60f0bc2e4866ce7c (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/perl

use English;
use Getopt::Declare; 



######################
# ARGUMENT PROCESSING
######################
#$inFilename = $ARGV[0];

use vars qw ($inFilename $POSTSCRIPT_OUTPUT $PDF_OUTPUT $PDFLATEX_OUTPUT $ALLOW_EMBEDDED_CODE $PARTIAL_DOCUMENT);

use vars qw ($EASYLATEXSTORE_MAIN_RESERVED_WORD $StorageIndex);

$StorageIndex = 0;

$EASYLATEXSTORE_MAIN_RESERVED_WORD = 'EASYLATEXSTORE-MAIN-RESERVED-WORD';

#test4


$argSpecification = q(
		      -ps	              Create a postscript output file (by calling latex, and then dvips)
		      {$POSTSCRIPT_OUTPUT = 1;} 

		      -pdf	              Create a pdf output file (by calling latex, and then dvipdf)
		      {$PDF_OUTPUT = 1;}

		      -pdflatex	              Create a pdf output file (by calling pdflatex -- use this if there's screwy errors in the file generated by -pdf)
		      {$PDFLATEX_OUTPUT = 1;}

		      --partial	              This file is not a full LaTeX document, just a partial one (to be \\include'd, for instance)
		      {$PARTIAL_DOCUMENT = 1;}

		      -e		 Allow execution of embedded code (advanced feature)
		      {$ALLOW_EMBEDDED_CODE = 1;}

		      <input>	     The input file
		      {$inFilename = $input;}
		      );



$args = new Getopt::Declare($argSpecification);

#print "ARG: $inFilename\n $j\nPO: $POSTSCRIPT_OUTPUT, PDF: $PDF_OUTPUT";

######################
# DEFINE GLOBAL CONSTANTS
######################

use vars qw($EASYLATEX_DIR @TRANSFORM_PATHS $PS_OUTPUT_COMMANDS $PDF_OUTPUT_COMMANDS $PDFLATEX_OUTPUT_COMMANDS $outFilename $stripFilename);

# doing both ways; todo; clean up global var declr

$PS_OUTPUT_COMMANDS = <<'EOF' ;
system('latex',$outFilename);
system('dvips',"${stripFilename}.dvi", "-o${stripFilename}.ps");
EOF

$PDF_OUTPUT_COMMANDS = <<'EOF' ;
system('latex',$outFilename);
system('dvipdf',"${stripFilename}.dvi");
EOF

$PDFLATEX_OUTPUT_COMMANDS = <<'EOF' ;


while ($out =~ /\{([^}]+?\.eps)\}/g) {
  push(@eps_files_to_convert, $1);
}

$out =~ s/\{([^}]+?)\.eps\}/{\1.pdf}/g;
foreach $file (@eps_files_to_convert) {
  system('epstopdf', $file);
}

open(OUT, '>'.$outFilename);
print OUT $out;
close(OUT);

system('pdflatex',$outFilename);
EOF


#
# I'll assume most distos have psfrag and leave this out for now...
#
#    use Cwd;
#$cwd = cwd();
#$ENV{TEXINPUTS} .= ":$cwd/psfrag";
#$ENV{DVIPSHEADERS} .= ":$cwd/psfrag";


######################
# MAIN
######################

init();

$in = readFile($inFilename);
$stripFilename = stripFilename($inFilename); 
$outFilename = $stripFilename . '.tex';

### preprocess the file

###   1) if it doesn't end with a newline, add one
if (substr($in,-1) ne "\n") {$in = $in . "\n";}

###   2) convert DOS-style newlines to UNIX-style (strip linefeeds)
while ($in =~ s/\r$//g) {}

###   3) store verbatims
$in = storeVerbatims($in);


## first, make a copy of the input
open(OUT, '>'.$outFilename);
print OUT $in;
close(OUT);

## now, run the transforms on it
foreach $transform (@TRANSFORM_PATHS) {
    if (-x $transform)
    {
	system("$transform", "$outFilename", "$EASYLATEX_DIR", @TRANSFORM_ARGS);

	unlink "$outFilename.bak";
	unlink "$outFilename.bak.bak";
    }
    elsif ($transform =~ /.*\.pl$/)
    {
	#print STDERR "about to do $transform\n";
	#$ENV{'PERL5OPT'} = 'i';
	use English;
        #print STDERR "$EXECUTABLE_NAME -i.bak $transform $outFilename $EASYLATEX_DIR $TRANSFORM_ARGS[0]\n";
	system("$EXECUTABLE_NAME -i.bak $transform", "$outFilename", "$EASYLATEX_DIR", @TRANSFORM_ARGS);
	
	unlink "$outFilename.bak";
	unlink "$outFilename.bak.bak";
    }
    else {
	print STDERR "Skipping $transform -- can't figure out how to execute it\
n";
    } 
    #print "\n\nJUST DID $transform\n\n";

    #print "\n\nJUST DID $transform on $outFilename\n\n";
#    system('cat', "$outFilename");
}

## first, make a copy of the input
open(OUT, $outFilename);
local undef $/;
my $out = <OUT>; 
close(OUT);


###   3) unstore verbatims
$out = unstoreVerbatims($out);

open(OUT, '>'.$outFilename);
print OUT $out;
close(OUT);


## do post-processing if requested
postProcess($out);


######################
# end of MAIN
######################



sub init
{
    findEasylatexDir();
    initListOfTransforms();
    if ($PARTIAL_DOCUMENT) {
	push(@TRANSFORM_ARGS, '--partial');
    }
    $TRANSFORM_ARGS = join(' ', @TRANSFORM_ARGS);
    #print "DBG: #$TRANSFORM_ARGS#\n";
}

sub postProcess {
    my ($out) = @_;


    open(FILE, $outFilename);
    local undef $/;
    my $file = <FILE>; 
    close(FILE);

    #TODO: may not work with pdflatex
    if ($file =~ /\\bibliography{(.*)\.bib}/) {
	system('latex',$outFilename);
	system('bibtex',$1);
    }

    system('latex',$outFilename);


    if ($POSTSCRIPT_OUTPUT)
    {
	eval $PS_OUTPUT_COMMANDS;
    }

    if ($PDF_OUTPUT)
    {
	eval $PDF_OUTPUT_COMMANDS;
    }

    if ($PDFLATEX_OUTPUT)
    {
	eval $PDFLATEX_OUTPUT_COMMANDS;
    }
}


###########################################################################
# findEasylatexDir
#   looks through a searchpath for the easylatex directory.
#   die if it can't find it.
###########################################################################
sub findEasylatexDir {
   local @easylatex_dir_searchpath;

    push(@easylatex_dir_searchpath,"./easylatex");
    push(@easylatex_dir_searchpath,"$ENV{HOME}/.easylatex");
    push(@easylatex_dir_searchpath,'/usr/lib/easylatex');

    $EASYLATEX_DIR = '';
    foreach $path (@easylatex_dir_searchpath)
    {
	if (( -e $path && -d $path)) {
	    $EASYLATEX_DIR = $path;
	}
    }

    errorCheckEasylatexDir();
}

####################################################
# errorCheckEasylatexDir
#    if the easylatex or the transform directories
#    don't exist or aren't directories, then die
####################################################
sub errorCheckEasylatexDir {    
    if (! $EASYLATEX_DIR)
    {
	die "Can't find easylatex directory. The program probably isn't installed correctly; please see documentation or run quickInstall. Aborting (looked for the directory in " . join(', ', @easylatex_dir_searchpath) . ")";
	
    }
    
    if (! ( -e "$EASYLATEX_DIR/transforms" && -d "$EASYLATEX_DIR/transforms"))
    {
	die "I found the easylatex directory at $EASYLATEX_DIR, but didn't find the directory $EASYLATEX_DIR/transforms. The program probably isn't installed correctly; please see documentation. Aborting.";
	
    }

# TODO: add to docs
}

###########################################################################
# initListOfTransforms
#   makes a list of the paths of the transforms to run
#     (1) lists the files in the transform directory,
#     (2) filters out files starting with '.' or '#' or ending with '~',
#     (3) sorts the resulting list,
#     (4) prepends the path to each file,
#     (5) leaves the result in the global variable @TRANSFORM_PATHS
###########################################################################
sub initListOfTransforms {

    my %fullPath;

##### list the files in the transform directory
    opendir(TRANSFORMDIR, "$EASYLATEX_DIR/transforms") || die "can't opendir $EASYLATEX_DIR/transforms. The program probably isn't installed correctly; please see documentation. Aborting.";
    @TRANSFORM_PATHS = readdir(TRANSFORMDIR);
    closedir(TRANSFORMDIR);

#### remember the path
    foreach $path  (@TRANSFORM_PATHS)
    {
	$fullPath{$path} = "$EASYLATEX_DIR/transforms/"  . $path;
    }


##### if allowed, add the embedded code transforms
    if ($ALLOW_EMBEDDED_CODE) {
	opendir(EMBEDDED_CODE_TRANSFORMDIR,"$EASYLATEX_DIR/embed_transforms");
	@EMBEDDED_CODE_TRANSFORM_PATHS = readdir(EMBEDDED_CODE_TRANSFORMDIR);
	closedir(EMBEDDED_CODE_TRANSFORMDIR);

	foreach $path  (@EMBEDDED_CODE_TRANSFORM_PATHS)
	{
	    $fullPath{$path} = "$EASYLATEX_DIR/embed_transforms/"  . $path;
	}

	push(@TRANSFORM_PATHS, @EMBEDDED_CODE_TRANSFORM_PATHS); 
	# add this to the @TRANSFORM_PATHS list
}



##### filter
    @TRANSFORM_PATHS = grep(!/^\./,@TRANSFORM_PATHS);
    @TRANSFORM_PATHS = grep(!/^\#/,@TRANSFORM_PATHS);
    @TRANSFORM_PATHS = grep(!/~$/,@TRANSFORM_PATHS);
    @TRANSFORM_PATHS = grep(!/math_mode_symbol_list.txt/,@TRANSFORM_PATHS);
    @TRANSFORM_PATHS = grep(!/config.txt/,@TRANSFORM_PATHS);

#### sort
    @TRANSFORM_PATHS = sort(@TRANSFORM_PATHS);


#### prepend the path
    foreach $path  (@TRANSFORM_PATHS)
    {
	$path = $fullPath{$path};
    }


#    use Data::Dumper; print Dumper(@TRANSFORM_PATHS);



}

######################
# UTILITY SUBROUTINES
######################

######################
# string readFile($fileName)
#   reads in and returns the contents of file $fileName
######################

sub readFile {
    my ($fileName) = @_;
    my $file;

    local undef $INPUT_RECORD_SEPARATOR;
    open(INFILE, $fileName);
    $file = <INFILE>;
    close INFILE;

    return $file;
}

######################
# string calculateOutFilename($inFilename)
######################

sub stripFilepath {
        my ($inFilename) = @_;
	my $stripFilepath;

	$stripFilename = $inFilename;
	
	$stripFilename =~ s/.txt//;

	return $stripFilepath;
	
}


sub stripFilename {
        my ($inFilename) = @_;
	my $stripFilename;

	$stripFilename = $inFilename;
	
	$stripFilename =~ s/.txt//;
	
	$stripFilename =~ s/.*\///;

	return $stripFilename;
	
}


######################
# "store" subroutines
######################

sub storeVerbatims {
    my ($file) = @_;

    ${notSlashRE_begin} = '(^|(?!\\).)';
    ${notSlashRE} = '(?!\\).';

    $file =~ s/(\\begin{verbatim}.*?\\end{verbatim})/store($1)/egs;
    $file =~ s/\\begin{latex}(.*?)\\end{latex}/store($1)/egs;
    $file =~ s/(\\newcommand.*)/store($1)/eg;
    $file =~ s/(\\includegraphics(?:\[[^\]]*\])?{[^}]+})/store($1)/egs;
     # TODO: there's a bug in the last line (i think, maybe)
    $file =~ s/(%%.*)/store($1)/eg;


    return $file;
}

sub unstoreVerbatims {
    my ($file) = @_;

#    print STDERR "UNSTORE:\n\n".$file;

    while ($file =~ s/ ?${EASYLATEXSTORE_MAIN_RESERVED_WORD}-(\d+)-${EASYLATEXSTORE_MAIN_RESERVED_WORD} ?/$Stored{$1}/egs) {}

    $file =~ s/\\begin{latex}//g;
    $file =~ s/\\end{latex}//g;

    return $file;
}



sub store {
    # got this idea from UseMod
  my ($toBeStored) = @_;

  $Stored{$StorageIndex} = $toBeStored;
#  print STDERR "STORED: $toBeStored\n";

#  print STDERR "REPLACEDW: ".${EASYLATEXSTORE_RESERVED_WORD}.'_' . $StorageIndex . '_'.${EASYLATEXSTORE_RESERVED_WORD};

  return ' '.${EASYLATEXSTORE_MAIN_RESERVED_WORD}.'-' . $StorageIndex++ . '-'.${EASYLATEXSTORE_MAIN_RESERVED_WORD}.' ';
}