hgbook

view tools/latex-to-docbook @ 555:b08f6a61bf15

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