-
Notifications
You must be signed in to change notification settings - Fork 64
/
texmate.py
executable file
·1236 lines (965 loc) · 42.7 KB
/
texmate.py
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env -S "${TM_BUNDLE_SUPPORT}/bin/python"
# encoding: utf-8
# -----------------------------------------------------------------------------
# Goals:
#
# 1. Modularize the processing of a latex run to better capture and parse
# errors
# 2. Provide a nice pushbutton interface for manually running
# latex, bibtex, makeindex, and viewing
#
# Overview:
#
# Each tex command has its own class that parses the output from that
# program. Each of these classes extends the TexParser class which provides
# default methods:
#
# parse_stream
# error
# warning
# info
#
# The parse_stream method reads each line from the input stream matches
# against a set of regular expressions defined in the patterns dictionary. If
# one of these patterns matches then the corresponding method is called. This
# method is also stored in the dictionary. Pattern matching callback methods
# must each take the match object as well as the current line as a parameter.
#
# Original Author: Brad Miller
# -----------------------------------------------------------------------------
# -- Imports ------------------------------------------------------------------
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from os import sys, path
sys.path.insert(
1,
path.dirname(path.dirname(path.abspath(__file__))) + "/lib/Python")
from argparse import ArgumentParser, ArgumentTypeError
from glob import glob
from io import open
from os import chdir, getenv, putenv, remove
from os.path import (basename, dirname, exists, getmtime, isfile, normpath,
realpath, splitext)
from pickle import load, dump
from pipes import quote as shellquote
from re import match, search
from subprocess import (call, CalledProcessError, check_output, Popen, PIPE,
STDOUT)
from sys import exit, version_info
from textwrap import dedent
try:
from urllib.parse import quote # Python 3
except ImportError:
from urllib import quote # Python 2
from auxiliary import remove_auxiliary_files
from gutter import update_marks
from parsing import (BibTexParser, BiberParser, ChkTexParser, LaTexParser,
MakeGlossariesParser, MakeIndexParser, LaTexMkParser)
from tex import (find_file_to_typeset, find_tex_directives, find_tex_packages)
from tmprefs import Preferences
# -- Module Import ------------------------------------------------------------
try:
# Python 2
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
except NameError:
# Python 3
pass
# -- Exit Codes ---------------------------------------------------------------
EXIT_SUCCESS = 0
EXIT_TEX_ENGINE_NOT_FOUND = 1
EXIT_DISCARD = 200
EXIT_SHOW_TOOL_TIP = 206
# -- Functions ----------------------------------------------------------------
def run_bibtex(filename, verbose=False):
"""Run bibtex for a certain file.
Run bibtex for ``filename`` and return the following values:
- The return value of the bibtex runs done by this function: This value
will be ``0`` after a successful run. Any other value indicates that
there were some kind of problems.
- Fatal error: Specifies if there was a fatal error while processing the
bibliography.
- Errors: The number of non-fatal errors encountered while processing the
bibliography
- Warnings: The number of warnings found while running this function
Arguments:
filename
Specifies the name of the tex file without its extension. This
information will be used to find the bibliography.
verbose
Specifies if the output by this function should be verbose.
Returns: ``(int, bool, int, int)``
Examples:
>>> chdir('Tests/TeX')
>>> run_bibtex('external_bibliography') # doctest:+ELLIPSIS
<h4>Processing: ...
...
(0, False, 0, 0)
>>> chdir('../..')
"""
directory = dirname(filename) if dirname(filename) else '.'
regex_auxfiles = (r'.*/({}|bu\d+)\.aux$'.format(filename))
auxfiles = [
f for f in glob("{}/*.aux".format(directory))
if match(regex_auxfiles, f)
]
stat, fatal, errors, warnings = 0, False, 0, 0
for bib in auxfiles:
print('<h4>Processing: {} </h4>'.format(bib))
run_object = Popen("bibtex {}".format(shellquote(bib)),
shell=True,
stdout=PIPE,
stdin=PIPE,
stderr=STDOUT,
close_fds=True,
universal_newlines=True)
bp = BibTexParser(run_object.stdout, verbose)
f, e, w = bp.parse_stream()
fatal |= f
errors += e
warnings += w
stat |= run_object.wait()
return stat, fatal, errors, warnings
def run_biber(filename, verbose=False):
"""Run biber for a certain file.
The interface for this function is exactly the same as the one for
``run_bibtex``. For the list of arguments and return values please take a
look at the doc string of ``run_bibtex``.
Examples:
>>> chdir('Tests/TeX')
>>> # Generate files for biber
>>> call('pdflatex external_bibliography_biber.tex > /dev/null',
... shell=True)
0
>>> run_biber('external_bibliography_biber') # doctest:+ELLIPSIS
<...
...
(0, False, 0, 0)
>>> chdir('../..')
"""
run_object = Popen("biber {}".format(shellquote(filename)),
shell=True,
stdout=PIPE,
stdin=PIPE,
stderr=STDOUT,
close_fds=True,
universal_newlines=True)
bp = BiberParser(run_object.stdout, verbose)
fatal, errors, warnings = bp.parse_stream()
stat = run_object.wait()
return stat, fatal, errors, warnings
def run_latex(ltxcmd, texfile, cache_filename, verbose=False):
"""Run the flavor of latex specified by ltxcmd on texfile.
This function returns:
- the return value of ``ltxcmd``,
- a value specifying if there were any fatal flaws (``True``) or not
(``False``), and
- the number of errors and
- the number of warnings encountered while processing ``texfile``.
Arguments:
cache_filename
The path to the cache file for the current tex project. This file
is used to store information about gutter marks between runs of
``texmate``.
ltxcmd
The latex command which should be used translate ``texfile``.
texfile
The path of the tex file which should be translated by ``ltxcmd``.
Returns: ``(int, bool, int, int)``
Examples:
>>> chdir('Tests/TeX')
>>> run_latex(ltxcmd='pdflatex',
... cache_filename='.external_bibliography.lb',
... texfile='external_bibliography.tex') # doctest:+ELLIPSIS
<h4>...
...
(0, False, 0, 0)
>>> chdir('../..')
"""
run_object = Popen("{} {}".format(ltxcmd, shellquote(texfile)),
shell=True,
stdout=PIPE,
stdin=PIPE,
stderr=STDOUT,
close_fds=True,
universal_newlines=True)
lp = LaTexParser(run_object.stdout, verbose, texfile)
fatal, errors, warnings = lp.parse_stream()
stat = run_object.wait()
update_marks(cache_filename, lp.marks)
return stat, fatal, errors, warnings
def run_makeindex(filename, verbose=False):
"""Run the makeindex command.
Generate the index for the given file returning
- the return value of ``makeindex``,
- a value specifying if there were any fatal flaws (``True``) or not
(``False``), and
- the number of errors and
- the number of warnings encountered while processing ``filename``.
Arguments:
filename
The name of the tex file for which we want to generate an index.
Returns: ``(int, bool, int, int)``
Examples:
>>> chdir('Tests/TeX')
>>> run_makeindex('makeindex.tex') # doctest:+ELLIPSIS
<p class="info">Run...Makeindex...
(0, False, 0, 0)
>>> chdir('../..')
"""
run_object = Popen("makeindex {}".format(
shellquote("{}.idx".format(splitext(filename)[0]))),
shell=True,
stdout=PIPE,
stdin=PIPE,
stderr=STDOUT,
close_fds=True,
universal_newlines=True)
ip = MakeIndexParser(run_object.stdout, verbose)
fatal, errors, warnings = ip.parse_stream()
stat = run_object.wait()
return stat, fatal, errors, warnings
def run_makeglossaries(filename, verbose=False):
"""Run makeglossaries for the given file.
The interface of this function is exactly the same as the one for
``run_makeindex``. For the list of arguments and return values, please
take a look at ``run_makeindex``.
Arguments:
filename
The name of the tex file for which we want to generate an index.
verbose
This value specifies if all output should be printed
(``verbose=True``) or if only significant messages should be
printed.
Examples:
>>> chdir('Tests/TeX')
>>> call('pdflatex makeglossaries.tex > /dev/null', shell=True)
0
>>> run_makeglossaries('makeglossaries.tex') # doctest:+ELLIPSIS
<h2>Make Glossaries...
...
(0, False, 0, 0)
>>> chdir('../..')
"""
run_object = Popen("makeglossaries {}".format(
shellquote(splitext(filename)[0])),
shell=True,
stdout=PIPE,
stdin=PIPE,
stderr=STDOUT,
close_fds=True,
universal_newlines=True)
bp = MakeGlossariesParser(run_object.stdout, verbose)
fatal, errors, warnings = bp.parse_stream()
stat = run_object.wait()
return stat, fatal, errors, warnings
def get_app_path(application, tm_support_path=getenv("TM_SUPPORT_PATH")):
"""Get the absolute path of the specified application.
This function returns either the path to ``application`` or ``None`` if
the specified application was not found.
Arguments:
application
The application for which this function should return the path
tm_support_path
The path to the “Bundle Support” bundle
Returns: ``str``
# We assume that Skim is installed in the ``/Applications`` folder
>>> print(get_app_path('Skim'))
/Applications/Skim.app
>>> get_app_path('NonExistentApp') # Returns ``None``
"""
try:
return check_output("'{}/bin/find_app' '{}.app'".format(
tm_support_path, application),
shell=True,
universal_newlines=True).strip()
except CalledProcessError:
return None
def get_app_path_and_sync_command(viewer, path_pdf, path_tex_file,
line_number):
"""Get the path and pdfsync command for the specified viewer.
This function returns a tuple containing
- the full path to the application, and
- a command which can be used to show the PDF output corresponding to
``line_number`` inside tex file.
If one of these two variables could not be determined, then the
corresponding value will be set to ``None``.
Arguments:
viewer:
The name of the PDF viewer application.
path_pdf:
The path to the generated PDF file.
path_tex_file
The path to the tex file for which we want to generate the pdfsync
command.
line_number
The line in the tex file for which we want to get the
synchronization command.
Examples:
# We assume that Skim is installed
>>> app_path, sync_command = get_app_path_and_sync_command(
... 'Skim', 'test.pdf', 'test.tex', 1)
>>> print('({}, {})'.format(app_path,
... sync_command)) # doctest:+ELLIPSIS
(.../Skim.app, .../Skim.app/.../displayline' 1 test.pdf test.tex)
# Preview has no pdfsync support
>>> app_path, sync_command = get_app_path_and_sync_command(
... 'Preview', 'test.pdf', 'test.tex', 1)
>>> print('({}, {})'.format(app_path,
... sync_command)) # doctest:+ELLIPSIS
(...Applications/Preview.app, None)
"""
sync_command = None
path_to_viewer = get_app_path(viewer)
if path_to_viewer and viewer == 'Skim':
sync_command = (
"'{}/Contents/SharedSupport/displayline' ".format(path_to_viewer) +
"{} {} {}".format(line_number, shellquote(path_pdf),
shellquote(path_tex_file)))
return path_to_viewer, sync_command
def refresh_viewer(viewer,
pdf_path,
tm_bundle_support=getenv('TM_BUNDLE_SUPPORT')):
"""Tell the specified PDF viewer to refresh the PDF output.
If the viewer does not support refreshing PDFs (e.g. “Preview”) then this
command will do nothing. This command will return a non-zero value if the
the viewer could not be found or the PDF viewer does not support a
“manual” refresh. For this method to work correctly ``viewer`` needs to be
open beforehand.
Arguments:
viewer
The viewer for which we want to refresh the output of the PDF file
specified in ``pdf_path``.
pdf_path
The path to the PDF file for which we want to refresh the output.
tm_bundle_support
The location of the “LaTeX Bundle” support folder
Returns: ``int``
Examples:
>>> # The viewer application needs to be open before we call the
>>> # function
>>> call('open -a Skim', shell=True)
0
>>> refresh_viewer('Skim', 'test.pdf',
... tm_bundle_support=realpath('Support'))
<p class="info">Tell Skim to refresh 'test.pdf'</p>
0
"""
print('<p class="info">Tell {} to refresh \'{}\'</p>'.format(
viewer, pdf_path))
if viewer in ['Skim', 'TeXShop']:
return call("osascript '{}/bin/refresh_viewer.scpt' {} {} ".format(
tm_bundle_support, viewer, shellquote(pdf_path)),
shell=True)
return 1
def run_viewer(viewer,
texfile_path,
pdffile_path,
suppress_pdf_output_textmate,
use_pdfsync,
line_number,
tm_bundle_support=getenv('TM_BUNDLE_SUPPORT')):
"""Open the PDF viewer containing the PDF generated from ``file_name``.
If ``use_pdfsync`` is set to ``True`` and the ``viewer`` supports pdfsync
then the part of the PDF corresponding to ``line_number`` will be opened.
The function returns the exit value of the shell command used to display
the PDF file.
Arguments:
viewer
Specifies which PDF viewer should be used to display the PDF
tex_file_path
The location of the tex file.
suppress_pdf_output_textmate
This variable is only used when ``viewer`` is set to ``TextMate``.
If it is set to ``True`` then TextMate will not try to display the
generated PDF.
tm_bundle_support
The location of the “LaTeX Bundle” support folder
Returns: ``int``
Examples:
>>> chdir('Tests/TeX')
>>> call("xelatex ünicöde.tex > /dev/null", shell=True)
0
>>> for viewer in ['Skim', 'TextMate']: # doctest: +ELLIPSIS
... run_viewer(viewer, './ünicöde.tex', './ünicöde.pdf',
... suppress_pdf_output_textmate=None,
... use_pdfsync=True, line_number=10,
... tm_bundle_support=realpath('../../Support'))
0
<script type="text/javascript">
...
...
0
>>> chdir('../..')
"""
status = 0
if viewer == 'TextMate':
if not suppress_pdf_output_textmate:
if isfile(pdffile_path):
print('''<script type="text/javascript">
window.location="file://{}"
</script>'''.format(quote(
pdffile_path.encode('utf8'))))
else:
print("File does not exist: {}".format(pdffile_path))
else:
path_to_viewer, sync_command = get_app_path_and_sync_command(
viewer, pdffile_path, texfile_path, line_number)
# PDF viewer is installed
if path_to_viewer:
if version_info <= (3, 0):
# If this is not done, the next line will thrown an encoding
# exception when the PDF file contains non-ASCII characters.
viewer = viewer.encode('utf-8')
pdf_already_open = not (bool(
call("'{}/bin/check_open' '{}' {} > /dev/null".format(
tm_bundle_support, viewer, shellquote(pdffile_path)),
shell=True)))
if pdf_already_open:
refresh_viewer(viewer, pdffile_path)
else:
status = call("open -a '{}.app' {}".format(
viewer, shellquote(pdffile_path)),
shell=True)
# PDF viewer supports pdfsync
if sync_command and use_pdfsync:
call(sync_command, shell=True)
elif not sync_command and use_pdfsync:
print("{} does not supported pdfsync".format(viewer))
# PDF viewer could not be found
else:
print('<strong class="error"> {} does not appear '.format(viewer) +
'to be installed on your system.</strong>')
return status
def construct_engine_options(ts_directives, tm_engine_options, synctex=True):
"""Construct a string of command line options.
The options come from two different sources:
- %!TEX TS-options directive in the file
- Options specified in the preferences of the LaTeX bundle
In any case ``nonstopmode`` is set as is ``file-line-error-style``.
Arguments:
ts_directives
A dictionary containing typesetting directives. If it contains the
key ``TS-options`` then this value will be used to construct the
options.
tm_engine_options
A string containing the default typesetting options set inside
TextMate. This string will be used to extend the options only if
ts_directives does not contain typesetting options. Otherwise the
settings specified in this item will be ignored.
synctex
Specifies if synctex should be used for typesetting or not.
Returns: ``str``
Examples:
>>> print(construct_engine_options({}, '', True))
-interaction=nonstopmode -file-line-error-style -synctex=1
>>> print(construct_engine_options({'TS-options': '-draftmode'},
... '', False))
-interaction=nonstopmode -file-line-error-style -draftmode
>>> print(construct_engine_options({'TS-options': '-draftmode'},
... '-8bit', False))
-interaction=nonstopmode -file-line-error-style -draftmode
>>> print(construct_engine_options({}, '-8bit'))
-interaction=nonstopmode -file-line-error-style -synctex=1 -8bit
"""
options = "-interaction=nonstopmode -file-line-error-style{}".format(
' -synctex=1' if synctex else '')
if 'TS-options' in ts_directives:
options += ' {}'.format(ts_directives['TS-options'])
else:
options += ' {}'.format(tm_engine_options) if tm_engine_options else ''
return options
def construct_engine_command(ts_directives, tm_engine, packages):
"""Decide which tex engine to use according to the given arguments.
The value of the engine is calculated according to the first applicable
setting in the following list:
1. The value of ``TS-program`` specified inside the tex file.
2. The list of included packages. If one of the used packages only works
with a special typesetting engine, then this engine will be returned.
3. The value of the latex engine specified inside the preferences of
the LaTeX bundle.
Arguments:
ts_directives
A dictionary containing typesetting directives. If it contains the
key ``TS-program`` then this value will be used as the typesetting
engine.
tm_engine
A sting containing the default tex engine used in TextMate. The
default engine will be used if ``TS-program`` or ``program`` is
not set and none of the specified packages contain engine-specific
code.
packages
The packages included in the tex file, which should be typeset.
Returns: ``str``
Examples:
>>> print(construct_engine_command({'TS-program': 'pdflatex'},
... 'latex', set()))
pdflatex
>>> print(construct_engine_command({}, 'latex', {'xunicode'}))
xelatex
>>> print(construct_engine_command({}, 'latex', set()))
latex
"""
latex_indicators = {'xyling', 'pst-asr', 'OTtablx'}
xelatex_indicators = {'xunicode'}
lualatex_indicators = {'luacode'}
if 'TS-program' in ts_directives:
engine = ts_directives['TS-program']
elif 'program' in ts_directives:
engine = ts_directives['program']
elif packages.intersection(latex_indicators):
engine = 'latex'
elif packages.intersection(xelatex_indicators):
engine = 'xelatex'
elif packages.intersection(lualatex_indicators):
engine = 'lualatex'
else:
engine = tm_engine
if call("type {} > /dev/null".format(engine), shell=True) != 0:
print('''<p class="error">Error: {} was not found,
Please make sure that LaTeX is installed and your PATH is
setup properly.</p>'''.format(engine))
exit(EXIT_TEX_ENGINE_NOT_FOUND)
return engine
def write_latexmkrc(engine, options, location='/tmp/latexmkrc'):
"""Create a “latexmkrc” file that uses the proper engine and arguments.
Arguments:
engine
A string specifying the engine which should be used by ``latexmk``.
options
A string specifying the arguments which should be used by
``engine``.
location
The path to the location where the ``latexmkrc`` file should be
saved.
Examples:
>>> write_latexmkrc(engine='latex', options='8bit')
>>> with open('/tmp/latexmkrc') as latexmkrc_file:
... print(latexmkrc_file.read()) # doctest:+ELLIPSIS
$latex = '...8bit';
...
"""
with open("/tmp/latexmkrc", 'w', encoding='utf-8') as latexmkrc:
latexmkrc.write(
dedent("""\
$latex = 'latex -interaction=nonstopmode -file-line-error-style {0}';
$pdflatex = '{1} -interaction=nonstopmode -file-line-error-style {0}';
""".format(options, engine)))
def get_typesetting_data(filepath,
tm_engine,
tm_bundle_support=getenv('TM_BUNDLE_SUPPORT'),
ignore_warnings=False):
"""Return a dictionary containing up-to-date typesetting data.
This function changes the current directory to the location of
``filepath``!
Arguments:
filepath
The filepath of the file we want to typeset.
tm_engine
A string representing the current default latex engine.
tm_bundle_support
The location of the “LaTeX Bundle” support folder.
ignore_warnings
Specifies if this function exits with an error status if there are
any problems.
Returns: ``{str: str}``
Examples:
>>> from os import getcwd
>>> current_directory = getcwd()
>>> data = get_typesetting_data('Tests/TeX/lualatex.tex', 'pdflatex')
>>> print(data['engine'])
lualatex
>>> data['synctex']
True
>>> chdir(current_directory)
"""
def get_cached_data():
"""Get current data and update cache."""
cache_read = False
typesetting_data = {}
try:
with open(cache_filename, 'rb') as storage:
typesetting_data = load(storage)
cache_read = True
cache_data_outdated = (getmtime(file_path) <
getmtime(cache_filename) >
getmtime(filepath))
# Write new cache data if the current data does not contain
# the necessary up to date information - This might be the case if
# only `texparser` has written to the cache file
if 'engine' not in typesetting_data or cache_data_outdated:
raise Exception()
except Exception:
# Get data and save it in the cache
packages = find_tex_packages(filename, ignore_warnings)
engine = construct_engine_command(typesetting_directives,
tm_engine, packages)
synctex = not (bool(
call("{} --help | grep -q synctex".format(engine),
shell=True)))
typesetting_data.update({
'engine': engine,
'packages': packages,
'synctex': synctex
})
if not cache_read:
typesetting_data['files_with_guttermarks'] = {filename}
try:
with open(cache_filename, 'wb') as storage:
dump(typesetting_data, storage)
except IOError:
print('<p class="warning"> Could not write cache file!</p>')
return typesetting_data
filepath = normpath(realpath(filepath))
typesetting_directives = find_tex_directives(filepath, ignore_warnings)
filename, file_path = find_file_to_typeset(typesetting_directives,
tex_file=filepath)
file_without_suffix = splitext(filename)[0]
chdir(file_path)
cache_filename = '.{}.lb'.format(file_without_suffix)
typesetting_data = get_cached_data()
# We add the tex files in the bundle directory to the possible input
# files. If `TEXINPUTS` was not set before then we also add the current
# directory `.` and the central default repository `::` to the start
# of `TEXINPUTS`
texinputs = "{}:{}/tex//".format(
getenv('TEXINPUTS') if getenv('TEXINPUTS') else '.::',
tm_bundle_support)
putenv('TEXINPUTS', texinputs)
typesetting_data.update({
'cache_filename': cache_filename,
'filename': filename,
'file_path': file_path,
'file_without_suffix': file_without_suffix,
'typesetting_directives': typesetting_directives
})
return typesetting_data
def get_command_line_arguments():
"""Specify and get command line arguments.
This function returns a ``Namespace`` containing the arguments specified on
the command line. The most important arguments in the ``Namespace`` are:
addoutput
A boolean that tells us if we should generate HTML output for an
already existing HTML output window.
command
The tex command which should be run by this script.
filename
The path to the tex file we want to process.
Returns: ``Namespace``
"""
def file_exists(filename):
if not exists(filename):
raise ArgumentTypeError(
"No such file or directory: {0}".format(filename))
return filename
parser_file = ArgumentParser(add_help=False)
parser_file.add_argument(
'filepath',
type=file_exists,
nargs='?',
default=getenv('TM_FILEPATH'),
help='Specify the file which should be processed.')
parser_latex = ArgumentParser(add_help=False)
parser_latex.add_argument(
'-latexmk',
default=None,
choices={'yes', 'no'},
help='''Specify if latexmk should be used to translate the document.
If you do not set this option, then value set inside
TextMate will be used.''')
parser_latex.add_argument(
'-engine',
default=None,
choices={'latex', 'lualatex', 'pdflatex', 'xelatex', 'texexec'},
help='''Set the default engine for tex documents. If you do not set
this option explicitly, then the value currently set inside the
TextMate preferences will be used.''')
parser_latex.add_argument(
'-options',
default=None,
dest='engine_options',
help='''Set the default engine options for tex documents. If you do
not set this option explicitly, then the engine options set
inside the TextMate preferences will be used.''')
parser = ArgumentParser(description='Execute common TeX commands.')
parser.add_argument(
'-addoutput',
action='store_true',
default=False,
help=('Tell %(prog)s to generate HTML output for an existing HTML ' +
'output window'))
parser.add_argument(
'-suppressview',
action='store_true',
default=False,
help=('''Tell %(prog)s to not open the PDF viewer application.'''))
subparsers = parser.add_subparsers(title="Commands", dest='command')
subparsers.add_parser('bibtex',
parents=[parser_file],
help='Run bibtex/biber for the specified file.')
subparsers.add_parser('clean',
parents=[parser_file],
help='Remove auxiliary files')
subparsers.add_parser('chktex',
parents=[parser_file],
help='Check the specified file with chktex.')
subparsers.add_parser(
'index',
parents=[parser_file],
help='''Create a index for the specified file using either
makeglossaries or makeindex.''')
subparsers.add_parser('latex',
parents=[parser_file, parser_latex],
help='Typeset the specified file using latex.')
subparsers.add_parser(
'sync',
parents=[parser_file],
help='''Open the specified PDF file at the position corresponding to
the currently selected tex code. Instead of providing the
path to the PDF it is also possible to just use the path of
the tex file. This command assumes that path to the PDF and
the tex file only differ in their extension!''')
subparsers.add_parser(
'view',
parents=[parser_file],
help='''View the PDF corresponding to the specified tex file using
either the currently selected viewer or the viewer specified
as argument.''')
subparsers.add_parser(
'version',
parents=[parser_file, parser_latex],
help='Return a version string for the currently selected engine.')
return parser.parse_args()
# -- Main ---------------------------------------------------------------------
if __name__ == '__main__':
# Get preferences from TextMate
tm_preferences = Preferences()
# Parse command line parameters...
arguments = get_command_line_arguments()
command = arguments.command
viewer_status = 0
filepath = arguments.filepath
first_run = not arguments.addoutput
line_number = match(r'^\d+', getenv('TM_SELECTION')).group(0)
number_errors = 0
number_runs = 0
number_warnings = 0
suppress_viewer = arguments.suppressview
synctex = False
tex_status = 0
tm_autoview = tm_preferences['latexAutoView']
tm_bundle_support = getenv('TM_BUNDLE_SUPPORT')
tm_engine = tm_preferences['latexEngine']
tm_engine_options = tm_preferences['latexEngineOptions'].strip()
use_latexmk = False
verbose = True if tm_preferences['latexVerbose'] == 1 else False
viewer = tm_preferences['latexViewer']
if command == 'latex' or command == 'version':