@@ -64,15 +64,24 @@ def run(args):
64
64
else:
65
65
src = os.path.splitext(src)[0]
66
66
67
+ # Ensure results are output in the same directory as the source files
68
+ os.chdir(os.path.dirname(src))
69
+ src = os.path.basename(src)
70
+
67
71
usepackage = r'usepackage(\[ .*\] )?{sagetex}'
68
72
uses_sagetex = False
69
73
70
- # if it does not use sagetex, obviously running sage is unnecessary
71
- with open(src + '.tex' ) as texf:
72
- for line in texf:
73
- if line.strip().startswith(r'\usepackage ') and re.search(usepackage, line):
74
- uses_sagetex = True
75
- break
74
+ # If it does not use sagetex, obviously running sage is unnecessary.
75
+ if os.path.isfile(src + '.tex' ):
76
+ with open(src + '.tex' ) as texf:
77
+ for line in texf:
78
+ if line.strip().startswith(r'\usepackage ') and re.search(usepackage, line):
79
+ uses_sagetex = True
80
+ break
81
+ else:
82
+ # The .tex file might not exist if LaTeX output was put to a different
83
+ # directory, so in that case just assume we need to build.
84
+ uses_sagetex = True
76
85
77
86
if not uses_sagetex:
78
87
print(src + ".tex doesn't seem to use SageTeX, exiting." , file=sys.stderr)
@@ -150,6 +159,8 @@ def argparser():
150
159
p.add_argument('outputfile', nargs='?', default=None, help="Output file name")
151
160
p.add_argument('-o', '--overwrite' , action="store_true", default=False,
152
161
help="Overwrite output file if it exists")
162
+ p.add_argument('-s', '--sout' , action="store", default=None,
163
+ help="Location of the .sagetex.sout file")
153
164
return p
154
165
155
166
def run(args):
@@ -161,11 +172,13 @@ def run(args):
161
172
sys.exit(1)
162
173
163
174
src, ext = os.path.splitext(src)
175
+ texfn = src + '.tex'
176
+ soutfn = args.sout if args.sout is not None else src + '.sagetex.sout'
164
177
% \end{macrocode}
165
178
% All the real work gets done in the line below. Sorry it's not more
166
179
% exciting-looking.
167
180
% \begin{macrocode}
168
- desagetexed = DeSageTex(src )
181
+ desagetexed = DeSageTex(texfn, soutfn )
169
182
% \end{macrocode}
170
183
% This part is cool: we need double percent signs at the beginning of
171
184
% the line because Python needs them (so they get turned into single
@@ -233,7 +246,7 @@ def run(args):
233
246
sys.exit(1)
234
247
235
248
src, ext = os.path.splitext(src)
236
- sagecode = SageCodeExtractor(src)
249
+ sagecode = SageCodeExtractor(src + '.tex' )
237
250
header = ("# This file contains Sage code extracted from % s%s.\n"
238
251
"# Processed %s.\n"
239
252
"" % (src, ext, time.strftime('%a %d %b %Y %H:%M:%S', time.localtime())))
@@ -266,6 +279,7 @@ if __name__ == "__main__":
266
279
% over the screen.
267
280
% \begin{macrocode}
268
281
import sys
282
+ import os
269
283
from pyparsing import *
270
284
% \end{macrocode}
271
285
% First, we define this very helpful parser: it finds the matching
@@ -351,11 +365,11 @@ class SoutParser():
351
365
% that the provided |fn| is just a basename.
352
366
% \begin{macrocode}
353
367
class DeSageTex():
354
- def __init__(self, fn ):
368
+ def __init__(self, texfn, soutfn ):
355
369
self.sagen = 0
356
370
self.plotn = 0
357
- self.fn = fn
358
- self.sout = SoutParser(fn + '.sagetex.sout' )
371
+ self.fn = os.path.basename(texfn)
372
+ self.sout = SoutParser(soutfn )
359
373
% \end{macrocode}
360
374
% Parse |\sage| macros. We just need to pull in the result from the
361
375
% |.sout| file and increment the counter---that's what |self.sage| does.
@@ -417,7 +431,7 @@ class DeSageTex():
417
431
% |transformString| on it, since that will just pick out the interesting
418
432
% bits and munge them according to the above definitions.
419
433
% \begin{macrocode}
420
- str = '' .join(open(fn + '.tex' , 'r' ).readlines())
434
+ str = '' .join(open(texfn , 'r' ).readlines())
421
435
self.result = doit.transformString(str)
422
436
% \end{macrocode}
423
437
% That's the end of the class constructor, and it's all we need to do
@@ -451,7 +465,7 @@ class DeSageTex():
451
465
% Sage.
452
466
% \begin{macrocode}
453
467
class SageCodeExtractor():
454
- def __init__(self, fn ):
468
+ def __init__(self, texfn ):
455
469
smacro = sagemacroparser
456
470
smacro.setParseAction(self.macroout)
457
471
@@ -480,7 +494,7 @@ class SageCodeExtractor():
480
494
481
495
doit = smacro | splot | senv | spause | sunpause
482
496
483
- str = '' .join(open(fn + '.tex' , 'r' ).readlines())
497
+ str = '' .join(open(texfn , 'r' ).readlines())
484
498
self.result = ''
485
499
486
500
doit.transformString(str)
0 commit comments