annotate tools/latex-to-docbook @ 651:cf006cabe489

Snapshot of conversion script
author Bryan O'Sullivan <bos@serpentine.com>
date Thu, 05 Feb 2009 00:02:00 -0800
parents
children dbb4c40e2609
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
651
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1 #!/usr/bin/python
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
2 #
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
3 # This is the most horrible of hacks. Pretend you're not looking.</para>
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
4
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
5 import cStringIO as StringIO
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
6 import re, sys
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
7
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
8 sections = {
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
9 'chapter': 'chapter',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
10 'section': 'sect1',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
11 'subsection': 'sect2',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
12 'subsubsection': 'sect3',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
13 }
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
14
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
15 envs = {
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
16 'codesample2': 'programlisting',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
17 'codesample4': 'programlisting',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
18 'enumerate': 'orderedlist',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
19 'figure': 'figure',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
20 'itemize': 'itemizedlist',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
21 'note': 'note',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
22 'quote': 'blockquote',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
23 }
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
24
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
25 def process(ifp, ofp):
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
26 stack = []
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
27 para = True
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
28 inlist = False
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
29 for line in ifp:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
30 if line.startswith('%%% Local Variables:'):
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
31 break
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
32 line = (line.rstrip()
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
33 .replace(' ', ' ')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
34 .replace('&', '&amp;')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
35 .replace('&emdash;', '&emdash;')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
36 .replace('\_', '_')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
37 .replace('\{', '{')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
38 .replace('\}', '}')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
39 .replace('\$', '$')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
40 .replace('\%', '%')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
41 .replace('\#', '#')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
42 .replace('<', '&lt;')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
43 .replace('>', '&gt;')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
44 .replace('<quote>', '<quote>')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
45 .replace("</quote>", '</quote>')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
46 .replace('\\', '\\'))
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
47 line = re.sub(r'\s*\\(?:centering|small)\b\s*', '', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
48 line = re.sub(r'\\(?:hgrc\\|hgrc)\b',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
49 r'<filename role="special"> /.hgrc</filename>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
50 line = re.sub(r'\\item\[(?P<key>[^]]+)\]', r'\item \g<key>:', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
51 line = re.sub(r'\\bug{(?P<id>\d+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
52 r'<ulink role="hg-bug" url="http://www.selenic.com/mercurial/bts/issue\g<id>">issue \g<id></ulink>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
53 line = re.sub(r'\\cite{([^}]+)}', r'<citation>\1</citation>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
54 line = re.sub(r'\\hggopt{(?P<opt>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
55 r'<option role="hg-opt-global">\g<opt></option>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
56 line = re.sub(r'\\hgxopt{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
57 r'<option role="hg-ext-\g<ext>-cmd-\g<cmd>-opt">\g<opt></option>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
58 line = re.sub(r'\\hgxcmd{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
59 r'<command role="hg-ext-\g<ext>">\g<cmd></command>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
60 line = re.sub(r'\\hgext{(?P<ext>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
61 r'<literal role="hg-ext">\g<ext></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
62 line = re.sub(r'\\hgopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
63 r'<option role="hg-opt-\g<cmd>">\g<opt></option>',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
64 line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
65 line = re.sub(r'\\cmdopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
66 r'<option role="cmd-opt-\g<cmd>">\g<opt></option>',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
67 line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
68 line = re.sub(r'\\hgcmd{(?P<cmd>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
69 r'<command role="hg-cmd">hg \g<cmd></command>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
70 line = re.sub(r'\\caption{(?P<text>[^}]+?)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
71 r'<caption>\g<text></caption>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
72 line = re.sub(r'\\grafix{(?P<name>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
73 r'<mediaobject><imageobject><imagedata fileref="\g<name>"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
74 line = re.sub(r'\\envar{(?P<name>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
75 r'<envar>\g<name></envar>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
76 line = re.sub(r'\\rcsection{(?P<sect>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
77 r'<literal role="rc-\g<sect>">\g<sect></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
78 line = re.sub(r'\\rcitem{(?P<sect>[^}]+)}{(?P<name>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
79 r'<envar role="rc-item-\g<sect>">\g<name></envar>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
80 line = re.sub(r'\\dirname{(?P<dir>[^}]+?)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
81 r'<filename class="directory">\g<dir></filename>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
82 line = re.sub(r'\\filename{(?P<file>[^}]+?)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
83 r'<filename>\g<file></filename>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
84 line = re.sub(r'\\tildefile{(?P<file>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
85 r'<filename role="home"> /\g<file></filename>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
86 line = re.sub(r'\\sfilename{(?P<file>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
87 r'<filename role="special">\g<file></filename>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
88 line = re.sub(r'\\sdirname{(?P<dir>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
89 r'<filename role="special" class="directory">\g<dir></filename>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
90 line = re.sub(r'\\interaction{(?P<id>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
91 r'<!-- &interaction.\g<id>; -->', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
92 line = re.sub(r'\\excode{(?P<id>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
93 r'<!-- &example.\g<id>; -->', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
94 line = re.sub(r'\\pymod{(?P<mod>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
95 r'<literal role="py-mod">\g<mod></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
96 line = re.sub(r'\\pymodclass{(?P<mod>[^}]+)}{(?P<class>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
97 r'<literal url="py-mod-\g<mod>">\g<class></ulink>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
98 line = re.sub(r'\\url{(?P<url>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
99 r'<ulink url="\g<url>">\g<url></ulink>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
100 line = re.sub(r'\\href{(?P<url>[^}]+)}{(?P<text>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
101 r'<ulink url="\g<url>">\g<text></ulink>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
102 line = re.sub(r'\\command{(?P<cmd>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
103 r'<command>\g<cmd></command>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
104 line = re.sub(r'\\option{(?P<opt>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
105 r'<option>\g<opt></option>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
106 line = re.sub(r'\\ref{(?P<id>[^}]+)}', r'<xref id="\g<id>"/>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
107 line = re.sub(r'\\emph{(?P<txt>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
108 r'<emphasis>\g<txt></emphasis>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
109 line = re.sub(r'\\texttt{(?P<txt>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
110 r'<literal>\g<txt></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
111 line = re.sub(r'\\textbf{(?P<txt>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
112 r'<emphasis role="bold">\g<txt></emphasis>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
113 line = re.sub(r'\\hook{(?P<name>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
114 r'<literal role="hook">\g<name></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
115 line = re.sub(r'\\tplfilter{(?P<name>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
116 r'<literal role="template-filter">\g<name></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
117 line = re.sub(r'\\tplkword{(?P<name>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
118 r'<literal role="template-keyword">\g<name></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
119 line = re.sub(r'\\tplkwfilt{(?P<tpl>[^}]+)}{(?P<name>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
120 r'<literal role="template-kw-filt-\g<tpl>">\g<name></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
121 line = re.sub(r'\\[vV]erb(.)(?P<txt>[^\1]+?)\1',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
122 r'<literal>\g<txt></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
123 line = re.sub(r'\\package{(?P<name>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
124 r'<literal role="package">\g<name></literal>', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
125 line = re.sub(r'\\hgcmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
126 r'<command role="hg-cmd">hg \g<cmd> \g<args></command>',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
127 line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
128 line = re.sub(r'\\cmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
129 r'<command>\g<cmd> \g<args></command>',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
130 line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
131 m = re.match(r'\\(chapter|section|subsection|subsubsection){(.*)}', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
132 if m:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
133 kind, content = m.groups()
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
134 sec = sections[kind]
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
135 while stack and stack[-1] >= sec:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
136 close = stack.pop()
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
137 print >> ofp, '</%s>' % close
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
138 stack.append(sec)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
139 print >> ofp, '<%s>\n<title>%s</title>' % (sec, content)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
140 else:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
141 m = re.match(r'\s*\\(begin|end){(?P<sect>[^}]+)}', line)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
142 if m:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
143 if not para:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
144 print >> ofp, '</para>'
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
145 if inlist:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
146 ofp.write('</listitem>')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
147 para = True
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
148 state, env = m.groups()
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
149 env = envs[env]
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
150 if state == 'begin':
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
151 ofp.write('<')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
152 if env == 'itemizedlist':
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
153 inlist = True
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
154 else:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
155 ofp.write('</')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
156 if env == 'itemizedlist':
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
157 inlist = False
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
158 print >> ofp, env + '>'
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
159 else:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
160 if line.startswith('\\item '):
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
161 para = True
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
162 line = line[6:]
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
163 if line and para:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
164 if inlist:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
165 ofp.write('<listitem>')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
166 ofp.write('<para>')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
167 para = False
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
168 if not line and not para:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
169 print >> ofp, '</para>'
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
170 if inlist:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
171 ofp.write('</listitem>')
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
172 para = True
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
173 print >> ofp, line
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
174 while stack:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
175 print >> ofp, '</%s>' % stack.pop()
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
176 ofp.write('\n'.join(['<!--',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
177 'local variables: ',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
178 'sgml-parent-document: ("00book.xml" "book" "chapter")',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
179 'end:',
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
180 '-->']))
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
181
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
182
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
183 if __name__ == '__main__':
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
184 for name in sys.argv[1:]:
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
185 if not name.endswith('.tex'):
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
186 continue
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
187 newname = name[:-3] + 'xml'
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
188 ofp = StringIO.StringIO()
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
189 process(open(name), ofp)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
190 s = ofp.getvalue()
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
191 s = re.sub('\n+</para>', '</para>', s, re.M)
cf006cabe489 Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
192 open(newname, 'w').write(s)