hgbook
diff contrib/latex-to-docbook @ 679:06458701453c
Fix up some links to example URLs that aren't actually real.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Tue Apr 21 21:07:20 2009 -0700 (2009-04-21) |
parents | b90b024729f1 |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/contrib/latex-to-docbook Tue Apr 21 21:07:20 2009 -0700 1.3 @@ -0,0 +1,198 @@ 1.4 +#!/usr/bin/python 1.5 +# 1.6 +# This is the most horrible of hacks. Pretend you're not looking.</para> 1.7 + 1.8 +import cStringIO as StringIO 1.9 +import re, sys 1.10 + 1.11 +sections = { 1.12 + 'chapter': 'chapter', 1.13 + 'section': 'sect1', 1.14 + 'subsection': 'sect2', 1.15 + 'subsubsection': 'sect3', 1.16 + } 1.17 + 1.18 +envs = { 1.19 + 'codesample2': 'programlisting', 1.20 + 'codesample4': 'programlisting', 1.21 + 'enumerate': 'orderedlist', 1.22 + 'figure': 'informalfigure', 1.23 + 'itemize': 'itemizedlist', 1.24 + 'note': 'note', 1.25 + 'quote': 'blockquote', 1.26 + } 1.27 + 1.28 +def process(ifp, ofp): 1.29 + print >> ofp, '<!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->\n' 1.30 + stack = [] 1.31 + para = True 1.32 + inlist = 0 1.33 + for line in ifp: 1.34 + if line.startswith('%%% Local Variables:'): 1.35 + break 1.36 + line = (line.rstrip() 1.37 + .replace('~', ' ') 1.38 + .replace('&', '&') 1.39 + .replace('---', '&emdash;') 1.40 + .replace('\_', '_') 1.41 + .replace('\{', '{') 1.42 + .replace('\}', '}') 1.43 + .replace('\$', '$') 1.44 + .replace('\%', '%') 1.45 + .replace('\#', '#') 1.46 + .replace('<', '<') 1.47 + .replace('>', '>') 1.48 + .replace('``', '<quote>') 1.49 + .replace("''", '</quote>') 1.50 + .replace('\\', '\\')) 1.51 + line = re.sub(r'\s*\\(?:centering|small)\b\s*', '', line) 1.52 + line = re.sub(r'\\(?:hgrc\\|hgrc)\b', 1.53 + r'<filename role="special"> /.hgrc</filename>', line) 1.54 + line = re.sub(r'\\item\[(?P<key>[^]]+)\]', r'\item \g<key>:', line) 1.55 + line = re.sub(r'\\bug{(?P<id>\d+)}', 1.56 + r'<ulink role="hg-bug" url="http://www.selenic.com/mercurial/bts/issue\g<id>">issue \g<id></ulink>', line) 1.57 + line = re.sub(r'\\cite{([^}]+)}', r'<citation>\1</citation>', line) 1.58 + line = re.sub(r'\\hggopt{(?P<opt>[^}]+)}', 1.59 + r'<option role="hg-opt-global">\g<opt></option>', line) 1.60 + line = re.sub(r'\\hgxopt{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', 1.61 + r'<option role="hg-ext-\g<ext>-cmd-\g<cmd>-opt">\g<opt></option>', line) 1.62 + line = re.sub(r'\\hgxcmd{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}', 1.63 + r'<command role="hg-ext-\g<ext>">\g<cmd></command>', line) 1.64 + line = re.sub(r'\\hgext{(?P<ext>[^}]+)}', 1.65 + r'<literal role="hg-ext">\g<ext></literal>', line) 1.66 + line = re.sub(r'\\hgopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', 1.67 + r'<option role="hg-opt-\g<cmd>">\g<opt></option>', 1.68 + line) 1.69 + line = re.sub(r'\\cmdopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', 1.70 + r'<option role="cmd-opt-\g<cmd>">\g<opt></option>', 1.71 + line) 1.72 + line = re.sub(r'\\hgcmd{(?P<cmd>[^}]+)}', 1.73 + r'<command role="hg-cmd">hg \g<cmd></command>', line) 1.74 + line = re.sub(r'\\caption{(?P<text>[^}]+?)}', 1.75 + r'<caption><para>\g<text></para></caption>', line) 1.76 + line = re.sub(r'\\grafix{(?P<name>[^}]+)}', 1.77 + r'<mediaobject><imageobject><imagedata fileref="\g<name>"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>', line) 1.78 + line = re.sub(r'\\envar{(?P<name>[^}]+)}', 1.79 + r'<envar>\g<name></envar>', line) 1.80 + line = re.sub(r'\\rcsection{(?P<sect>[^}]+)}', 1.81 + r'<literal role="rc-\g<sect>">\g<sect></literal>', line) 1.82 + line = re.sub(r'\\rcitem{(?P<sect>[^}]+)}{(?P<name>[^}]+)}', 1.83 + r'<envar role="rc-item-\g<sect>">\g<name></envar>', line) 1.84 + line = re.sub(r'\\dirname{(?P<dir>[^}]+?)}', 1.85 + r'<filename class="directory">\g<dir></filename>', line) 1.86 + line = re.sub(r'\\filename{(?P<file>[^}]+?)}', 1.87 + r'<filename>\g<file></filename>', line) 1.88 + line = re.sub(r'\\tildefile{(?P<file>[^}]+)}', 1.89 + r'<filename role="home">~/\g<file></filename>', line) 1.90 + line = re.sub(r'\\sfilename{(?P<file>[^}]+)}', 1.91 + r'<filename role="special">\g<file></filename>', line) 1.92 + line = re.sub(r'\\sdirname{(?P<dir>[^}]+)}', 1.93 + r'<filename role="special" class="directory">\g<dir></filename>', line) 1.94 + line = re.sub(r'\\interaction{(?P<id>[^}]+)}', 1.95 + r'<!-- &interaction.\g<id>; -->', line) 1.96 + line = re.sub(r'\\excode{(?P<id>[^}]+)}', 1.97 + r'<!-- &example.\g<id>; -->', line) 1.98 + line = re.sub(r'\\pymod{(?P<mod>[^}]+)}', 1.99 + r'<literal role="py-mod">\g<mod></literal>', line) 1.100 + line = re.sub(r'\\pymodclass{(?P<mod>[^}]+)}{(?P<class>[^}]+)}', 1.101 + r'<literal role="py-mod-\g<mod>">\g<class></literal>', line) 1.102 + line = re.sub(r'\\url{(?P<url>[^}]+)}', 1.103 + r'<ulink url="\g<url>">\g<url></ulink>', line) 1.104 + line = re.sub(r'\\href{(?P<url>[^}]+)}{(?P<text>[^}]+)}', 1.105 + r'<ulink url="\g<url>">\g<text></ulink>', line) 1.106 + line = re.sub(r'\\command{(?P<cmd>[^}]+)}', 1.107 + r'<command>\g<cmd></command>', line) 1.108 + line = re.sub(r'\\option{(?P<opt>[^}]+)}', 1.109 + r'<option>\g<opt></option>', line) 1.110 + line = re.sub(r'\\ref{(?P<id>[^}]+)}', r'<xref linkend="\g<id>"/>', line) 1.111 + line = re.sub(r'\\emph{(?P<txt>[^}]+)}', 1.112 + r'<emphasis>\g<txt></emphasis>', line) 1.113 + line = re.sub(r'\\texttt{(?P<txt>[^}]+)}', 1.114 + r'<literal>\g<txt></literal>', line) 1.115 + line = re.sub(r'\\textbf{(?P<txt>[^}]+)}', 1.116 + r'<emphasis role="bold">\g<txt></emphasis>', line) 1.117 + line = re.sub(r'\\hook{(?P<name>[^}]+)}', 1.118 + r'<literal role="hook">\g<name></literal>', line) 1.119 + line = re.sub(r'\\tplfilter{(?P<name>[^}]+)}', 1.120 + r'<literal role="template-filter">\g<name></literal>', line) 1.121 + line = re.sub(r'\\tplkword{(?P<name>[^}]+)}', 1.122 + r'<literal role="template-keyword">\g<name></literal>', line) 1.123 + line = re.sub(r'\\tplkwfilt{(?P<tpl>[^}]+)}{(?P<name>[^}]+)}', 1.124 + r'<literal role="template-kw-filt-\g<tpl>">\g<name></literal>', line) 1.125 + line = re.sub(r'\\[vV]erb(.)(?P<txt>[^\1]+?)\1', 1.126 + r'<literal>\g<txt></literal>', line) 1.127 + line = re.sub(r'\\package{(?P<name>[^}]+)}', 1.128 + r'<literal role="package">\g<name></literal>', line) 1.129 + line = re.sub(r'\\hgcmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', 1.130 + r'<command role="hg-cmd">hg \g<cmd> \g<args></command>', 1.131 + line) 1.132 + line = re.sub(r'\\cmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', 1.133 + r'<command>\g<cmd> \g<args></command>', 1.134 + line) 1.135 + m = re.match(r'\\(chapter|section|subsection|subsubsection){(.*)}', line) 1.136 + if m: 1.137 + kind, content = m.groups() 1.138 + sec = sections[kind] 1.139 + while stack and stack[-1] >= sec: 1.140 + close = stack.pop() 1.141 + print >> ofp, '</%s>' % close 1.142 + stack.append(sec) 1.143 + print >> ofp, '<%s>\n<title>%s</title>' % (sec, content) 1.144 + else: 1.145 + m = re.match(r'\s*\\(begin|end){(?P<sect>[^}]+)}', line) 1.146 + if m: 1.147 + if not para: 1.148 + print >> ofp, '</para>' 1.149 + if inlist: 1.150 + ofp.write('</listitem>') 1.151 + para = True 1.152 + state, env = m.groups() 1.153 + env = envs[env] 1.154 + if state == 'begin': 1.155 + ofp.write('<') 1.156 + if env in ('itemizedlist', 'orderedlist'): 1.157 + inlist = 1 1.158 + else: 1.159 + ofp.write('</') 1.160 + if env in ('itemizedlist', 'orderedlist'): 1.161 + inlist = 0 1.162 + print >> ofp, env + '>' 1.163 + else: 1.164 + if line.startswith('\\item '): 1.165 + if inlist > 1: 1.166 + print >> ofp, '</para>' 1.167 + print >> ofp, '</listitem>' 1.168 + else: 1.169 + inlist = 2 1.170 + para = True 1.171 + line = line[6:] 1.172 + if line and para: 1.173 + if inlist: 1.174 + ofp.write('<listitem>') 1.175 + ofp.write('<para>') 1.176 + para = False 1.177 + if not line and not para: 1.178 + print >> ofp, '</para>' 1.179 + if inlist: 1.180 + ofp.write('</listitem>') 1.181 + para = True 1.182 + print >> ofp, line 1.183 + while stack: 1.184 + print >> ofp, '</%s>' % stack.pop() 1.185 + ofp.write('\n'.join(['\n<!--', 1.186 + 'local variables: ', 1.187 + 'sgml-parent-document: ("00book.xml" "book" "chapter")', 1.188 + 'end:', 1.189 + '-->'])) 1.190 + 1.191 + 1.192 +if __name__ == '__main__': 1.193 + for name in sys.argv[1:]: 1.194 + if not name.endswith('.tex'): 1.195 + continue 1.196 + newname = name[:-3] + 'xml' 1.197 + ofp = StringIO.StringIO() 1.198 + process(open(name), ofp) 1.199 + s = ofp.getvalue() 1.200 + s = re.sub('\n+</para>', '</para>', s, re.M) 1.201 + open(newname, 'w').write(s)