@@ -235,6 +235,8 @@ def argparser():
235
235
p.add_argument('outputfile', nargs='?', default=None, help="Output file name")
236
236
p.add_argument('-o', '--overwrite' , action="store_true", default=False,
237
237
help="Overwrite output file if it exists")
238
+ p.add_argument('--no-inline', action="store_true", default=False,
239
+ help="Extract code only from Sage environments")
238
240
return p
239
241
240
242
def run(args):
@@ -246,9 +248,9 @@ def run(args):
246
248
sys.exit(1)
247
249
248
250
src, ext = os.path.splitext(src)
249
- sagecode = SageCodeExtractor(src + '.tex' )
250
- header = ("# This file contains Sage code extracted from % s%s.\n"
251
- "# Processed %s.\n"
251
+ sagecode = SageCodeExtractor(src + '.tex' , inline=not args.no_inline )
252
+ header = ("#> This file contains Sage code extracted from % s%s.\n"
253
+ "#> Processed %s.\n"
252
254
"" % (src, ext, time.strftime('%a %d %b %Y %H:%M:%S', time.localtime())))
253
255
254
256
if dst is not None:
@@ -280,6 +282,7 @@ if __name__ == "__main__":
280
282
% \begin{macrocode}
281
283
import sys
282
284
import os
285
+ import textwrap
283
286
from pyparsing import *
284
287
% \end{macrocode}
285
288
% First, we define this very helpful parser: it finds the matching
@@ -467,7 +470,7 @@ class DeSageTex():
467
470
% Sage.
468
471
% \begin{macrocode}
469
472
class SageCodeExtractor():
470
- def __init__(self, texfn):
473
+ def __init__(self, texfn, inline=True ):
471
474
smacro = sagemacroparser
472
475
smacro.setParseAction(self.macroout)
473
476
@@ -494,7 +497,10 @@ class SageCodeExtractor():
494
497
sunpause = sagetexunpause
495
498
sunpause.setParseAction(self.unpause)
496
499
497
- doit = smacro | splot | senv | spause | sunpause
500
+ if inline:
501
+ doit = smacro | splot | senv | spause | sunpause
502
+ else:
503
+ doit = senv | spause | sunpause
498
504
doit.ignore('% ' + restOfLine)
499
505
500
506
str = '' .join(open(texfn, 'r' ).readlines())
@@ -503,26 +509,26 @@ class SageCodeExtractor():
503
509
doit.transformString(str)
504
510
505
511
def macroout(self, s, l, t):
506
- self.result += '# \\sage{} from line %s\n' % lineno(l, s)
507
- self.result += t.code[1:-1] + '\n\n'
512
+ self.result += '#> \\sage{} from line %s\n' % lineno(l, s)
513
+ self.result += textwrap.dedent( t.code[1:-1]) + '\n\n'
508
514
509
515
def plotout(self, s, l, t):
510
- self.result += '# \\sageplot{} from line %s:\n' % lineno(l, s)
516
+ self.result += '#> \\sageplot{} from line %s:\n' % lineno(l, s)
511
517
if t.format != '' :
512
- self.result += '# format: %s' % t.format[0][1:-1] + '\n'
513
- self.result += t.code[1:-1] + '\n\n'
518
+ self.result += '#> format: %s' % t.format[0][1:-1] + '\n'
519
+ self.result += textwrap.dedent( t.code[1:-1]) + '\n\n'
514
520
515
521
def envout(self, s, l, t):
516
- self.result += '# %s environment from line %s:' % (t.env,
522
+ self.result += '#> %s environment from line %s:' % (t.env,
517
523
lineno(l, s))
518
- self.result += '' .join(t.code) + '\n'
524
+ self.result += textwrap.dedent( ''.join(t.code) ) + '\n'
519
525
520
526
def pause(self, s, l, t):
521
- self.result += ('# SageTeX (probably) paused on input line % s.\n\n' %
527
+ self.result += ('#> SageTeX (probably) paused on input line % s.\n\n' %
522
528
(lineno(l, s)))
523
529
524
530
def unpause(self, s, l, t):
525
- self.result += ('# SageTeX (probably) unpaused on input line % s.\n\n' %
531
+ self.result += ('#> SageTeX (probably) unpaused on input line % s.\n\n' %
526
532
(lineno(l, s)))
527
533
% \end{macrocode}
528
534
% \end{macro}
0 commit comments