Skip to content

Commit 631e43d

Browse files
committed
extractsagecode: add option for extracting only environments + dedent
Fix pyparsing compatibility, and dedent code appropriately. Also, use a different comment prefix for comments output by the extraction tool.
1 parent afaac84 commit 631e43d

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

scripts.dtx

+20-14
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:
@@ -280,6 +282,7 @@ if __name__ == "__main__":
280282
% \begin{macrocode}
281283
import sys
282284
import os
285+
import textwrap
283286
from pyparsing import *
284287
% \end{macrocode}
285288
% First, we define this very helpful parser: it finds the matching
@@ -467,7 +470,7 @@ class DeSageTex():
467470
% Sage.
468471
% \begin{macrocode}
469472
class SageCodeExtractor():
470-
def __init__(self, texfn):
473+
def __init__(self, texfn, inline=True):
471474
smacro = sagemacroparser
472475
smacro.setParseAction(self.macroout)
473476
@@ -494,7 +497,10 @@ class SageCodeExtractor():
494497
sunpause = sagetexunpause
495498
sunpause.setParseAction(self.unpause)
496499
497-
doit = smacro | splot | senv | spause | sunpause
500+
if inline:
501+
doit = smacro | splot | senv | spause | sunpause
502+
else:
503+
doit = senv | spause | sunpause
498504
doit.ignore('%' + restOfLine)
499505
500506
str = ''.join(open(texfn, 'r').readlines())
@@ -503,26 +509,26 @@ class SageCodeExtractor():
503509
doit.transformString(str)
504510
505511
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'
508514
509515
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)
511517
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'
514520
515521
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,
517523
lineno(l, s))
518-
self.result += ''.join(t.code) + '\n'
524+
self.result += textwrap.dedent(''.join(t.code)) + '\n'
519525
520526
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' %
522528
(lineno(l, s)))
523529
524530
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' %
526532
(lineno(l, s)))
527533
% \end{macrocode}
528534
% \end{macro}

0 commit comments

Comments
 (0)