annotate Doc/text2latex.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 1691ea5d9d4b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1401
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 import os
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 import sys
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import regex
1402
af4e271d07b1 * ext.tex: did most of the tedious changes from plain ASCII text to LaTeX.
Guido van Rossum <guido@python.org>
parents: 1401
diff changeset
4 import regsub
1401
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import string
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import getopt
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 def main():
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 process(sys.stdin, sys.stdout)
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 dashes = regex.compile('^-+[ \t]*$')
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 equals = regex.compile('^=+[ \t]*$')
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 stars = regex.compile('^\*+[ \t]*$')
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 blank = regex.compile('^[ \t]*$')
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 indented = regex.compile('^\( *\t\| \)[ \t]*[^ \t]')
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 def process(fi, fo):
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 inverbatim = 0
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 line = '\n'
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 nextline = fi.readline()
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 while nextline:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 prevline = line
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 line = nextline
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 nextline = fi.readline()
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 fmt = None
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 if dashes.match(nextline) >= 0:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 fmt = '\\subsection{%s}\n'
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 elif equals.match(nextline) >= 0:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 fmt = '\\section{%s}\n'
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 elif stars.match(nextline) >= 0:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 fmt = '\\chapter{%s}\n'
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 if fmt:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 nextline = '\n'
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 line = fmt % string.strip(line)
1402
af4e271d07b1 * ext.tex: did most of the tedious changes from plain ASCII text to LaTeX.
Guido van Rossum <guido@python.org>
parents: 1401
diff changeset
35 if '(' in line:
af4e271d07b1 * ext.tex: did most of the tedious changes from plain ASCII text to LaTeX.
Guido van Rossum <guido@python.org>
parents: 1401
diff changeset
36 line = regsub.gsub('[a-zA-Z0-9_]+()',
af4e271d07b1 * ext.tex: did most of the tedious changes from plain ASCII text to LaTeX.
Guido van Rossum <guido@python.org>
parents: 1401
diff changeset
37 '{\\\\tt \\0}', line)
1401
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 elif inverbatim:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 if blank.match(line) >= 0 and \
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 indented.match(nextline) < 0:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 inverbatim = 0
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 fo.write('\\end{verbatim}\n')
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 else:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 if indented.match(line) >= 0 and \
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 blank.match(prevline) >= 0:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 inverbatim = 1
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 fo.write('\\begin{verbatim}\n')
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 if inverbatim:
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 line = string.expandtabs(line, 4)
1402
af4e271d07b1 * ext.tex: did most of the tedious changes from plain ASCII text to LaTeX.
Guido van Rossum <guido@python.org>
parents: 1401
diff changeset
50 elif not fmt and '(' in line:
af4e271d07b1 * ext.tex: did most of the tedious changes from plain ASCII text to LaTeX.
Guido van Rossum <guido@python.org>
parents: 1401
diff changeset
51 line = regsub.gsub('[a-zA-Z0-9_]+()',
af4e271d07b1 * ext.tex: did most of the tedious changes from plain ASCII text to LaTeX.
Guido van Rossum <guido@python.org>
parents: 1401
diff changeset
52 '\\\\code{\\0}', line)
1401
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 fo.write(line)
cd5989a58bcf * ext.tex: documentation for extending, reference counts, and embedding
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
1411
1691ea5d9d4b * text2latex.py: call main() instead of always processing ext.tex.
Guido van Rossum <guido@python.org>
parents: 1402
diff changeset
55 main()