improve treatment of multi-line replies, ignore empty lines
[python/dscho.git] / Doc / text2latex.py
blob93d0587d30487a936904c6d8924e21a1621b10f4
1 import os
2 import sys
3 import regex
4 import regsub
5 import string
6 import getopt
8 def main():
9 process(sys.stdin, sys.stdout)
11 dashes = regex.compile('^-+[ \t]*$')
12 equals = regex.compile('^=+[ \t]*$')
13 stars = regex.compile('^\*+[ \t]*$')
14 blank = regex.compile('^[ \t]*$')
15 indented = regex.compile('^\( *\t\| \)[ \t]*[^ \t]')
17 def process(fi, fo):
18 inverbatim = 0
19 line = '\n'
20 nextline = fi.readline()
21 while nextline:
22 prevline = line
23 line = nextline
24 nextline = fi.readline()
25 fmt = None
26 if dashes.match(nextline) >= 0:
27 fmt = '\\subsection{%s}\n'
28 elif equals.match(nextline) >= 0:
29 fmt = '\\section{%s}\n'
30 elif stars.match(nextline) >= 0:
31 fmt = '\\chapter{%s}\n'
32 if fmt:
33 nextline = '\n'
34 line = fmt % string.strip(line)
35 if '(' in line:
36 line = regsub.gsub('[a-zA-Z0-9_]+()',
37 '{\\\\tt \\0}', line)
38 elif inverbatim:
39 if blank.match(line) >= 0 and \
40 indented.match(nextline) < 0:
41 inverbatim = 0
42 fo.write('\\end{verbatim}\n')
43 else:
44 if indented.match(line) >= 0 and \
45 blank.match(prevline) >= 0:
46 inverbatim = 1
47 fo.write('\\begin{verbatim}\n')
48 if inverbatim:
49 line = string.expandtabs(line, 4)
50 elif not fmt and '(' in line:
51 line = regsub.gsub('[a-zA-Z0-9_]+()',
52 '\\\\code{\\0}', line)
53 fo.write(line)
55 main()