Fred Drake | 3cae66b | 2001-07-17 16:46:14 +0000 | [diff] [blame] | 1 | """Simple script to replace @DATE@ and friends with real information. |
| 2 | |
| 3 | Usage: rewrite.py boilerplate.tex [VAR=value] ... <template >output |
| 4 | """ |
| 5 | |
| 6 | import string |
| 7 | import sys |
| 8 | import time |
| 9 | |
| 10 | |
| 11 | def get_info(fp): |
| 12 | s = fp.read() |
| 13 | |
| 14 | d = {} |
| 15 | start = string.find(s, r"\date{") |
| 16 | if start >= 0: |
| 17 | end = string.find(s, "}", start) |
| 18 | date = s[start+6:end] |
| 19 | if date == r"\today": |
| 20 | date = time.strftime("%B %d, %Y", time.localtime(time.time())) |
| 21 | d["DATE"] = date |
| 22 | return d |
| 23 | |
| 24 | |
| 25 | def main(): |
| 26 | s = sys.stdin.read() |
| 27 | if "@" in s: |
| 28 | # yes, we actully need to load the replacement values |
| 29 | d = get_info(open(sys.argv[1])) |
| 30 | for arg in sys.argv[2:]: |
| 31 | name, value = string.split(arg, "=", 1) |
| 32 | d[name] = value |
| 33 | start = 0 |
| 34 | while 1: |
| 35 | start = string.find(s, "@", start) |
| 36 | if start < 0: |
| 37 | break |
| 38 | end = string.find(s, "@", start+1) |
| 39 | name = s[start+1:end] |
| 40 | if name: |
| 41 | value = d.get(name) |
| 42 | if value is None: |
| 43 | start = end + 1 |
| 44 | else: |
| 45 | s = s[:start] + value + s[end+1:] |
| 46 | start = start + len(value) |
| 47 | else: |
| 48 | # "@@" --> "@" |
| 49 | s = s[:start] + s[end:] |
| 50 | start = end |
| 51 | sys.stdout.write(s) |
| 52 | |
| 53 | |
| 54 | if __name__ == "__main__": |
| 55 | main() |