@@ -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)
@@ -153,6 +162,8 @@ def argparser():
153
162
p.add_argument('outputfile', nargs='?', default=None, help="Output file name")
154
163
p.add_argument('-o', '--overwrite' , action="store_true", default=False,
155
164
help="Overwrite output file if it exists")
165
+ p.add_argument('-s', '--sout' , action="store", default=None,
166
+ help="Location of the .sagetex.sout file")
156
167
return p
157
168
158
169
def run(args):
@@ -164,11 +175,13 @@ def run(args):
164
175
sys.exit(1)
165
176
166
177
src, ext = os.path.splitext(src)
178
+ texfn = src + '.tex'
179
+ soutfn = args.sout if args.sout is not None else src + '.sagetex.sout'
167
180
% \end{macrocode}
168
181
% All the real work gets done in the line below. Sorry it's not more
169
182
% exciting-looking.
170
183
% \begin{macrocode}
171
- desagetexed = DeSageTex(src )
184
+ desagetexed = DeSageTex(texfn, soutfn )
172
185
% \end{macrocode}
173
186
% This part is cool: we need double percent signs at the beginning of
174
187
% the line because Python needs them (so they get turned into single
@@ -236,7 +249,7 @@ def run(args):
236
249
sys.exit(1)
237
250
238
251
src, ext = os.path.splitext(src)
239
- sagecode = SageCodeExtractor(src)
252
+ sagecode = SageCodeExtractor(src + '.tex' )
240
253
header = ("# This file contains Sage code extracted from % s%s.\n"
241
254
"# Processed %s.\n"
242
255
"" % (src, ext, time.strftime('%a %d %b %Y %H:%M:%S', time.localtime())))
@@ -269,6 +282,7 @@ if __name__ == "__main__":
269
282
% over the screen.
270
283
% \begin{macrocode}
271
284
import sys
285
+ import os
272
286
from pyparsing import *
273
287
% \end{macrocode}
274
288
% First, we define this very helpful parser: it finds the matching
@@ -354,11 +368,11 @@ class SoutParser():
354
368
% that the provided |fn| is just a basename.
355
369
% \begin{macrocode}
356
370
class DeSageTex():
357
- def __init__(self, fn ):
371
+ def __init__(self, texfn, soutfn ):
358
372
self.sagen = 0
359
373
self.plotn = 0
360
- self.fn = fn
361
- self.sout = SoutParser(fn + '.sagetex.sout' )
374
+ self.fn = os.path.basename(texfn)
375
+ self.sout = SoutParser(soutfn )
362
376
% \end{macrocode}
363
377
% Parse |\sage| macros. We just need to pull in the result from the
364
378
% |.sout| file and increment the counter---that's what |self.sage| does.
@@ -420,7 +434,7 @@ class DeSageTex():
420
434
% |transformString| on it, since that will just pick out the interesting
421
435
% bits and munge them according to the above definitions.
422
436
% \begin{macrocode}
423
- str = '' .join(open(fn + '.tex' , 'r' ).readlines())
437
+ str = '' .join(open(texfn , 'r' ).readlines())
424
438
self.result = doit.transformString(str)
425
439
% \end{macrocode}
426
440
% That's the end of the class constructor, and it's all we need to do
@@ -454,7 +468,7 @@ class DeSageTex():
454
468
% Sage.
455
469
% \begin{macrocode}
456
470
class SageCodeExtractor():
457
- def __init__(self, fn ):
471
+ def __init__(self, texfn ):
458
472
smacro = sagemacroparser
459
473
smacro.setParseAction(self.macroout)
460
474
@@ -483,7 +497,7 @@ class SageCodeExtractor():
483
497
484
498
doit = smacro | splot | senv | spause | sunpause
485
499
486
- str = '' .join(open(fn + '.tex' , 'r' ).readlines())
500
+ str = '' .join(open(texfn , 'r' ).readlines())
487
501
self.result = ''
488
502
489
503
doit.transformString(str)
0 commit comments