Mercurial > hgbook
annotate contrib/latex-to-docbook @ 796:5cb09396dc35
add auto-snippets.xml
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Thu, 18 Jun 2009 16:41:21 +0900 |
parents | 2180358c32c4 |
children |
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', |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
19 'figure': 'informalfigure', |
651
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): |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
26 print >> ofp, '<!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->\n' |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
27 stack = [] |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 para = True |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
29 inlist = 0 |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
30 for line in ifp: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
31 if line.startswith('%%% Local Variables:'): |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 break |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
33 line = (line.rstrip() |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
34 .replace('~', ' ') |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
35 .replace('&', '&') |
658
b90b024729f1
WIP DocBook snapshot that all compiles. Mirabile dictu!
Bryan O'Sullivan <bos@serpentine.com>
parents:
655
diff
changeset
|
36 .replace('---', '&emdash;') |
651
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('\#', '#') |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
43 .replace('<', '<') |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
44 .replace('>', '>') |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
45 .replace('``', '<quote>') |
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
46 .replace("''", '</quote>') |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
47 .replace('\\', '\\')) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
48 line = re.sub(r'\s*\\(?:centering|small)\b\s*', '', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
49 line = re.sub(r'\\(?:hgrc\\|hgrc)\b', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
50 r'<filename role="special"> /.hgrc</filename>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
51 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
|
52 line = re.sub(r'\\bug{(?P<id>\d+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 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
|
54 line = re.sub(r'\\cite{([^}]+)}', r'<citation>\1</citation>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
55 line = re.sub(r'\\hggopt{(?P<opt>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 r'<option role="hg-opt-global">\g<opt></option>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
57 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
|
58 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
|
59 line = re.sub(r'\\hgxcmd{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
60 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
|
61 line = re.sub(r'\\hgext{(?P<ext>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
62 r'<literal role="hg-ext">\g<ext></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
63 line = re.sub(r'\\hgopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
64 r'<option role="hg-opt-\g<cmd>">\g<opt></option>', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
65 line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
66 line = re.sub(r'\\cmdopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
67 r'<option role="cmd-opt-\g<cmd>">\g<opt></option>', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
68 line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
69 line = re.sub(r'\\hgcmd{(?P<cmd>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
70 r'<command role="hg-cmd">hg \g<cmd></command>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
71 line = re.sub(r'\\caption{(?P<text>[^}]+?)}', |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
72 r'<caption><para>\g<text></para></caption>', line) |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
73 line = re.sub(r'\\grafix{(?P<name>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
74 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
|
75 line = re.sub(r'\\envar{(?P<name>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
76 r'<envar>\g<name></envar>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
77 line = re.sub(r'\\rcsection{(?P<sect>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
78 r'<literal role="rc-\g<sect>">\g<sect></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 line = re.sub(r'\\rcitem{(?P<sect>[^}]+)}{(?P<name>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
80 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
|
81 line = re.sub(r'\\dirname{(?P<dir>[^}]+?)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
82 r'<filename class="directory">\g<dir></filename>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
83 line = re.sub(r'\\filename{(?P<file>[^}]+?)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
84 r'<filename>\g<file></filename>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
85 line = re.sub(r'\\tildefile{(?P<file>[^}]+)}', |
658
b90b024729f1
WIP DocBook snapshot that all compiles. Mirabile dictu!
Bryan O'Sullivan <bos@serpentine.com>
parents:
655
diff
changeset
|
86 r'<filename role="home">~/\g<file></filename>', line) |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
87 line = re.sub(r'\\sfilename{(?P<file>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
88 r'<filename role="special">\g<file></filename>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
89 line = re.sub(r'\\sdirname{(?P<dir>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
90 r'<filename role="special" class="directory">\g<dir></filename>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
91 line = re.sub(r'\\interaction{(?P<id>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
92 r'<!-- &interaction.\g<id>; -->', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
93 line = re.sub(r'\\excode{(?P<id>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
94 r'<!-- &example.\g<id>; -->', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
95 line = re.sub(r'\\pymod{(?P<mod>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
96 r'<literal role="py-mod">\g<mod></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
97 line = re.sub(r'\\pymodclass{(?P<mod>[^}]+)}{(?P<class>[^}]+)}', |
658
b90b024729f1
WIP DocBook snapshot that all compiles. Mirabile dictu!
Bryan O'Sullivan <bos@serpentine.com>
parents:
655
diff
changeset
|
98 r'<literal role="py-mod-\g<mod>">\g<class></literal>', line) |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
99 line = re.sub(r'\\url{(?P<url>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
100 r'<ulink url="\g<url>">\g<url></ulink>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
101 line = re.sub(r'\\href{(?P<url>[^}]+)}{(?P<text>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
102 r'<ulink url="\g<url>">\g<text></ulink>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
103 line = re.sub(r'\\command{(?P<cmd>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
104 r'<command>\g<cmd></command>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
105 line = re.sub(r'\\option{(?P<opt>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
106 r'<option>\g<opt></option>', line) |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
107 line = re.sub(r'\\ref{(?P<id>[^}]+)}', r'<xref linkend="\g<id>"/>', line) |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
108 line = re.sub(r'\\emph{(?P<txt>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
109 r'<emphasis>\g<txt></emphasis>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
110 line = re.sub(r'\\texttt{(?P<txt>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
111 r'<literal>\g<txt></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
112 line = re.sub(r'\\textbf{(?P<txt>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
113 r'<emphasis role="bold">\g<txt></emphasis>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
114 line = re.sub(r'\\hook{(?P<name>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
115 r'<literal role="hook">\g<name></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
116 line = re.sub(r'\\tplfilter{(?P<name>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
117 r'<literal role="template-filter">\g<name></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
118 line = re.sub(r'\\tplkword{(?P<name>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
119 r'<literal role="template-keyword">\g<name></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
120 line = re.sub(r'\\tplkwfilt{(?P<tpl>[^}]+)}{(?P<name>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
121 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
|
122 line = re.sub(r'\\[vV]erb(.)(?P<txt>[^\1]+?)\1', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
123 r'<literal>\g<txt></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
124 line = re.sub(r'\\package{(?P<name>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
125 r'<literal role="package">\g<name></literal>', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
126 line = re.sub(r'\\hgcmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
127 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
|
128 line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
129 line = re.sub(r'\\cmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
130 r'<command>\g<cmd> \g<args></command>', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
131 line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
132 m = re.match(r'\\(chapter|section|subsection|subsubsection){(.*)}', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
133 if m: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
134 kind, content = m.groups() |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
135 sec = sections[kind] |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
136 while stack and stack[-1] >= sec: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
137 close = stack.pop() |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
138 print >> ofp, '</%s>' % close |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
139 stack.append(sec) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
140 print >> ofp, '<%s>\n<title>%s</title>' % (sec, content) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
141 else: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
142 m = re.match(r'\s*\\(begin|end){(?P<sect>[^}]+)}', line) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
143 if m: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
144 if not para: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
145 print >> ofp, '</para>' |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
146 if inlist: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
147 ofp.write('</listitem>') |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
148 para = True |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
149 state, env = m.groups() |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
150 env = envs[env] |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
151 if state == 'begin': |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
152 ofp.write('<') |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
153 if env in ('itemizedlist', 'orderedlist'): |
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
154 inlist = 1 |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
155 else: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
156 ofp.write('</') |
658
b90b024729f1
WIP DocBook snapshot that all compiles. Mirabile dictu!
Bryan O'Sullivan <bos@serpentine.com>
parents:
655
diff
changeset
|
157 if env in ('itemizedlist', 'orderedlist'): |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
158 inlist = 0 |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
159 print >> ofp, env + '>' |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
160 else: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
161 if line.startswith('\\item '): |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
162 if inlist > 1: |
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
163 print >> ofp, '</para>' |
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
164 print >> ofp, '</listitem>' |
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
165 else: |
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
166 inlist = 2 |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
167 para = True |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
168 line = line[6:] |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
169 if line and 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 ofp.write('<para>') |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
173 para = False |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
174 if not line and not para: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
175 print >> ofp, '</para>' |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
176 if inlist: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
177 ofp.write('</listitem>') |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
178 para = True |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
179 print >> ofp, line |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
180 while stack: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
181 print >> ofp, '</%s>' % stack.pop() |
655
dbb4c40e2609
Fix some awful infelicities by replacing them with worse ones
Bryan O'Sullivan <bos@serpentine.com>
parents:
651
diff
changeset
|
182 ofp.write('\n'.join(['\n<!--', |
651
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
183 'local variables: ', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
184 'sgml-parent-document: ("00book.xml" "book" "chapter")', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
185 'end:', |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
186 '-->'])) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
187 |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
188 |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
189 if __name__ == '__main__': |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
190 for name in sys.argv[1:]: |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
191 if not name.endswith('.tex'): |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
192 continue |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
193 newname = name[:-3] + 'xml' |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
194 ofp = StringIO.StringIO() |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
195 process(open(name), ofp) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
196 s = ofp.getvalue() |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
197 s = re.sub('\n+</para>', '</para>', s, re.M) |
cf006cabe489
Snapshot of conversion script
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
198 open(newname, 'w').write(s) |