Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame^] | 1 | import os |
| 2 | import sys |
| 3 | import regex |
| 4 | import string |
| 5 | import getopt |
| 6 | |
| 7 | def main(): |
| 8 | process(sys.stdin, sys.stdout) |
| 9 | |
| 10 | dashes = regex.compile('^-+[ \t]*$') |
| 11 | equals = regex.compile('^=+[ \t]*$') |
| 12 | stars = regex.compile('^\*+[ \t]*$') |
| 13 | blank = regex.compile('^[ \t]*$') |
| 14 | indented = regex.compile('^\( *\t\| \)[ \t]*[^ \t]') |
| 15 | |
| 16 | def process(fi, fo): |
| 17 | inverbatim = 0 |
| 18 | line = '\n' |
| 19 | nextline = fi.readline() |
| 20 | while nextline: |
| 21 | prevline = line |
| 22 | line = nextline |
| 23 | nextline = fi.readline() |
| 24 | fmt = None |
| 25 | if dashes.match(nextline) >= 0: |
| 26 | fmt = '\\subsection{%s}\n' |
| 27 | elif equals.match(nextline) >= 0: |
| 28 | fmt = '\\section{%s}\n' |
| 29 | elif stars.match(nextline) >= 0: |
| 30 | fmt = '\\chapter{%s}\n' |
| 31 | if fmt: |
| 32 | nextline = '\n' |
| 33 | line = fmt % string.strip(line) |
| 34 | elif inverbatim: |
| 35 | if blank.match(line) >= 0 and \ |
| 36 | indented.match(nextline) < 0: |
| 37 | inverbatim = 0 |
| 38 | fo.write('\\end{verbatim}\n') |
| 39 | else: |
| 40 | if indented.match(line) >= 0 and \ |
| 41 | blank.match(prevline) >= 0: |
| 42 | inverbatim = 1 |
| 43 | fo.write('\\begin{verbatim}\n') |
| 44 | if inverbatim: |
| 45 | line = string.expandtabs(line, 4) |
| 46 | fo.write(line) |
| 47 | |
| 48 | #main() |
| 49 | process(open('ext.tex', 'r'), sys.stdout) |