comparison contrib/latex-to-docbook @ 721:2180358c32c4

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