hgbook

annotate contrib/latex-to-docbook @ 672:b338f5490029

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