diff options
| author | Björn B. Brandenburg <bbb@cs.unc.edu> | 2010-05-25 23:05:14 -0400 |
|---|---|---|
| committer | Björn B. Brandenburg <bbb@cs.unc.edu> | 2010-05-25 23:05:14 -0400 |
| commit | ade7a3224c59a7a460ac1cb75e4b522f0d44282c (patch) | |
| tree | bdbc0869a161d97131782202a103f2915dcb672f | |
| parent | 1b326051dd975a736c74353712dc9171f36dad0e (diff) | |
add little utility to avoid latex includes
| -rwxr-xr-x | tex-un-include | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tex-un-include b/tex-un-include new file mode 100755 index 0000000..72d7998 --- /dev/null +++ b/tex-un-include | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | |||
| 3 | import sys | ||
| 4 | import re | ||
| 5 | |||
| 6 | comment_re = re.compile(r'([^\\]|^)%') | ||
| 7 | input_re = re.compile(r'\\input\{([^}]*)\}') | ||
| 8 | |||
| 9 | def strip_comment(line): | ||
| 10 | g = comment_re.search(line) | ||
| 11 | if g: | ||
| 12 | # we matched some non-\ character | ||
| 13 | return line[0:g.start() + 1] | ||
| 14 | else: | ||
| 15 | return line | ||
| 16 | |||
| 17 | def chomp(line): | ||
| 18 | if line and line[-1] == '\n': | ||
| 19 | return line[:-1] | ||
| 20 | else: | ||
| 21 | return line | ||
| 22 | |||
| 23 | def open_tex_file(fname, mode='r'): | ||
| 24 | try: | ||
| 25 | f = open(fname, mode) | ||
| 26 | except IOError: | ||
| 27 | # try again with .tex appended | ||
| 28 | f = open(fname + '.tex', mode) | ||
| 29 | return f | ||
| 30 | |||
| 31 | def process_file(fname, out=sys.stdout): | ||
| 32 | for line in open_tex_file(fname): | ||
| 33 | line = chomp(strip_comment(line)) | ||
| 34 | idx = 0 | ||
| 35 | for g in input_re.finditer(line): | ||
| 36 | out.write(line[idx:g.start()]) | ||
| 37 | fname = g.group(1) | ||
| 38 | # recurse into file | ||
| 39 | out.write("%%!!!!! processing %s !!!!!\n" % fname) | ||
| 40 | process_file(fname, out) | ||
| 41 | idx = g.end() | ||
| 42 | out.write(line[idx:]) | ||
| 43 | out.write('\n') | ||
| 44 | |||
| 45 | |||
| 46 | def main(args=sys.argv[1:]): | ||
| 47 | for fname in args: | ||
| 48 | process_file(fname) | ||
| 49 | |||
| 50 | if __name__ == '__main__': | ||
| 51 | main() | ||
