Files
klammertext/tst/editor/editor_driver.py

64 lines
1.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Drive the Sublime Text editor cores over fixture files, outside Sublime.
Usage: editor_driver.py SUBLIME_DIR (MODE INFILE OUTFILE)...
MODE is `indent` or `align`; each triple applies that tool to INFILE and
writes the result to OUTFILE. The Sublime plugin files import without the
`sublime` module (their try/except guard), so the pure cores run under plain
python3. Also asserts the built-in error path (no enclosing table).
Called by editor_test.sh; exits nonzero on an internal error.
"""
import sys
def apply_indent(KI, s):
bols = [0] + [i + 1 for i, ch in enumerate(s)
if ch == '\n' and i + 1 < len(s)]
out = s
for a, b, new in sorted(KI.reindent_lines(s, bols), reverse=True):
out = out[:a] + new + out[b:]
return out
def apply_align(KA, s):
caret = s.index('|') if '|' in s else 0
span = KA.enclosing_span(s, caret, KA.ALIGN_KLAMMERS)
if span is None:
return s
_name, cs, ce = span
edits, _msg = KA.compute_edits(s[cs:ce])
out = s
for a, b, new in sorted(edits, reverse=True):
out = out[:cs + a] + new + out[cs + b:]
return out
def main():
sublime_dir = sys.argv[1]
sys.path.insert(0, sublime_dir)
import Klammertext_indent as KI
import Klammertext_align as KA
args = sys.argv[2:]
for k in range(0, len(args), 3):
mode, infile, outfile = args[k:k + 3]
with open(infile) as f:
s = f.read()
if mode == 'indent':
out = apply_indent(KI, s)
elif mode == 'align':
out = apply_align(KA, s)
else:
sys.exit("editor_driver.py: unknown mode: " + mode)
with open(outfile, 'w') as f:
f.write(out)
# Error path: no enclosing table klammer.
assert KA.enclosing_span("no table here\n", 3, KA.ALIGN_KLAMMERS) is None
if __name__ == '__main__':
main()