Skip to content

Commit 5aaa0c8

Browse files
committed
extractsagecode: add option for extracting only environments
Also, use a different comment prefix for comments output by the extraction tool.
1 parent dd379aa commit 5aaa0c8

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

scripts.dtx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ def argparser():
235235
p.add_argument('outputfile', nargs='?', default=None, help="Output file name")
236236
p.add_argument('-o', '--overwrite', action="store_true", default=False,
237237
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")
238240
return p
239241
240242
def run(args):
@@ -246,9 +248,9 @@ def run(args):
246248
sys.exit(1)
247249
248250
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"
252254
"" % (src, ext, time.strftime('%a %d %b %Y %H:%M:%S', time.localtime())))
253255
254256
if dst is not None:
@@ -467,7 +469,7 @@ class DeSageTex():
467469
% Sage.
468470
% \begin{macrocode}
469471
class SageCodeExtractor():
470-
def __init__(self, texfn):
472+
def __init__(self, texfn, inline=True):
471473
smacro = sagemacroparser
472474
smacro.setParseAction(self.macroout)
473475
@@ -494,7 +496,10 @@ class SageCodeExtractor():
494496
sunpause = sagetexunpause
495497
sunpause.setParseAction(self.unpause)
496498
497-
doit = smacro | splot | senv | spause | sunpause
499+
if inline:
500+
doit = smacro | splot | senv | spause | sunpause
501+
else:
502+
doit = senv | spause | sunpause
498503
doit.ignore('%' + restOfLine)
499504
500505
str = ''.join(open(texfn, 'r').readlines())
@@ -503,26 +508,26 @@ class SageCodeExtractor():
503508
doit.transformString(str)
504509
505510
def macroout(self, s, l, t):
506-
self.result += '# \\sage{} from line %s\n' % lineno(l, s)
511+
self.result += '#> \\sage{} from line %s\n' % lineno(l, s)
507512
self.result += t.code[1:-1] + '\n\n'
508513
509514
def plotout(self, s, l, t):
510-
self.result += '# \\sageplot{} from line %s:\n' % lineno(l, s)
515+
self.result += '#> \\sageplot{} from line %s:\n' % lineno(l, s)
511516
if t.format != '':
512-
self.result += '# format: %s' % t.format[0][1:-1] + '\n'
517+
self.result += '#> format: %s' % t.format[0][1:-1] + '\n'
513518
self.result += t.code[1:-1] + '\n\n'
514519
515520
def envout(self, s, l, t):
516-
self.result += '# %s environment from line %s:' % (t.env,
521+
self.result += '#> %s environment from line %s:' % (t.env,
517522
lineno(l, s))
518523
self.result += ''.join(t.code) + '\n'
519524
520525
def pause(self, s, l, t):
521-
self.result += ('# SageTeX (probably) paused on input line %s.\n\n' %
526+
self.result += ('#> SageTeX (probably) paused on input line %s.\n\n' %
522527
(lineno(l, s)))
523528
524529
def unpause(self, s, l, t):
525-
self.result += ('# SageTeX (probably) unpaused on input line %s.\n\n' %
530+
self.result += ('#> SageTeX (probably) unpaused on input line %s.\n\n' %
526531
(lineno(l, s)))
527532
% \end{macrocode}
528533
% \end{macro}

0 commit comments

Comments
 (0)