bos@552: #!/usr/bin/python bos@552: # bos@552: # This is the most horrible of hacks. Pretend you're not looking.</para> bos@552: bos@552: import cStringIO as StringIO bos@552: import re, sys bos@552: bos@552: sections = { bos@552: 'chapter': 'chapter', bos@552: 'section': 'sect1', bos@552: 'subsection': 'sect2', bos@552: 'subsubsection': 'sect3', bos@552: } bos@552: bos@552: envs = { bos@552: 'codesample2': 'programlisting', bos@552: 'codesample4': 'programlisting', bos@552: 'enumerate': 'orderedlist', bos@556: 'figure': 'informalfigure', bos@552: 'itemize': 'itemizedlist', bos@552: 'note': 'note', bos@552: 'quote': 'blockquote', bos@552: } bos@552: bos@552: def process(ifp, ofp): bos@556: print >> ofp, '<!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->\n' bos@552: stack = [] bos@552: para = True bos@556: inlist = 0 bos@552: for line in ifp: bos@552: if line.startswith('%%% Local Variables:'): bos@552: break bos@552: line = (line.rstrip() bos@556: .replace('~', ' ') bos@552: .replace('&', '&') bos@559: .replace('---', '&emdash;') bos@552: .replace('\_', '_') bos@552: .replace('\{', '{') bos@552: .replace('\}', '}') bos@552: .replace('\$', '$') bos@552: .replace('\%', '%') bos@552: .replace('\#', '#') bos@552: .replace('<', '<') bos@552: .replace('>', '>') bos@556: .replace('``', '<quote>') bos@556: .replace("''", '</quote>') bos@552: .replace('\\', '\\')) bos@552: line = re.sub(r'\s*\\(?:centering|small)\b\s*', '', line) bos@552: line = re.sub(r'\\(?:hgrc\\|hgrc)\b', bos@552: r'<filename role="special"> /.hgrc</filename>', line) bos@552: line = re.sub(r'\\item\[(?P<key>[^]]+)\]', r'\item \g<key>:', line) bos@552: line = re.sub(r'\\bug{(?P<id>\d+)}', bos@552: r'<ulink role="hg-bug" url="http://www.selenic.com/mercurial/bts/issue\g<id>">issue \g<id></ulink>', line) bos@552: line = re.sub(r'\\cite{([^}]+)}', r'<citation>\1</citation>', line) bos@552: line = re.sub(r'\\hggopt{(?P<opt>[^}]+)}', bos@552: r'<option role="hg-opt-global">\g<opt></option>', line) bos@552: line = re.sub(r'\\hgxopt{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', bos@552: r'<option role="hg-ext-\g<ext>-cmd-\g<cmd>-opt">\g<opt></option>', line) bos@552: line = re.sub(r'\\hgxcmd{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}', bos@552: r'<command role="hg-ext-\g<ext>">\g<cmd></command>', line) bos@552: line = re.sub(r'\\hgext{(?P<ext>[^}]+)}', bos@552: r'<literal role="hg-ext">\g<ext></literal>', line) bos@552: line = re.sub(r'\\hgopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', bos@552: r'<option role="hg-opt-\g<cmd>">\g<opt></option>', bos@552: line) bos@552: line = re.sub(r'\\cmdopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', bos@552: r'<option role="cmd-opt-\g<cmd>">\g<opt></option>', bos@552: line) bos@552: line = re.sub(r'\\hgcmd{(?P<cmd>[^}]+)}', bos@552: r'<command role="hg-cmd">hg \g<cmd></command>', line) bos@552: line = re.sub(r'\\caption{(?P<text>[^}]+?)}', bos@556: r'<caption><para>\g<text></para></caption>', line) bos@552: line = re.sub(r'\\grafix{(?P<name>[^}]+)}', bos@552: r'<mediaobject><imageobject><imagedata fileref="\g<name>"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>', line) bos@552: line = re.sub(r'\\envar{(?P<name>[^}]+)}', bos@552: r'<envar>\g<name></envar>', line) bos@552: line = re.sub(r'\\rcsection{(?P<sect>[^}]+)}', bos@552: r'<literal role="rc-\g<sect>">\g<sect></literal>', line) bos@552: line = re.sub(r'\\rcitem{(?P<sect>[^}]+)}{(?P<name>[^}]+)}', bos@552: r'<envar role="rc-item-\g<sect>">\g<name></envar>', line) bos@552: line = re.sub(r'\\dirname{(?P<dir>[^}]+?)}', bos@552: r'<filename class="directory">\g<dir></filename>', line) bos@552: line = re.sub(r'\\filename{(?P<file>[^}]+?)}', bos@552: r'<filename>\g<file></filename>', line) bos@552: line = re.sub(r'\\tildefile{(?P<file>[^}]+)}', bos@559: r'<filename role="home">~/\g<file></filename>', line) bos@552: line = re.sub(r'\\sfilename{(?P<file>[^}]+)}', bos@552: r'<filename role="special">\g<file></filename>', line) bos@552: line = re.sub(r'\\sdirname{(?P<dir>[^}]+)}', bos@552: r'<filename role="special" class="directory">\g<dir></filename>', line) bos@552: line = re.sub(r'\\interaction{(?P<id>[^}]+)}', bos@552: r'<!-- &interaction.\g<id>; -->', line) bos@552: line = re.sub(r'\\excode{(?P<id>[^}]+)}', bos@552: r'<!-- &example.\g<id>; -->', line) bos@552: line = re.sub(r'\\pymod{(?P<mod>[^}]+)}', bos@552: r'<literal role="py-mod">\g<mod></literal>', line) bos@552: line = re.sub(r'\\pymodclass{(?P<mod>[^}]+)}{(?P<class>[^}]+)}', bos@559: r'<literal role="py-mod-\g<mod>">\g<class></literal>', line) bos@552: line = re.sub(r'\\url{(?P<url>[^}]+)}', bos@552: r'<ulink url="\g<url>">\g<url></ulink>', line) bos@552: line = re.sub(r'\\href{(?P<url>[^}]+)}{(?P<text>[^}]+)}', bos@552: r'<ulink url="\g<url>">\g<text></ulink>', line) bos@552: line = re.sub(r'\\command{(?P<cmd>[^}]+)}', bos@552: r'<command>\g<cmd></command>', line) bos@552: line = re.sub(r'\\option{(?P<opt>[^}]+)}', bos@552: r'<option>\g<opt></option>', line) bos@556: line = re.sub(r'\\ref{(?P<id>[^}]+)}', r'<xref linkend="\g<id>"/>', line) bos@552: line = re.sub(r'\\emph{(?P<txt>[^}]+)}', bos@552: r'<emphasis>\g<txt></emphasis>', line) bos@552: line = re.sub(r'\\texttt{(?P<txt>[^}]+)}', bos@552: r'<literal>\g<txt></literal>', line) bos@552: line = re.sub(r'\\textbf{(?P<txt>[^}]+)}', bos@552: r'<emphasis role="bold">\g<txt></emphasis>', line) bos@552: line = re.sub(r'\\hook{(?P<name>[^}]+)}', bos@552: r'<literal role="hook">\g<name></literal>', line) bos@552: line = re.sub(r'\\tplfilter{(?P<name>[^}]+)}', bos@552: r'<literal role="template-filter">\g<name></literal>', line) bos@552: line = re.sub(r'\\tplkword{(?P<name>[^}]+)}', bos@552: r'<literal role="template-keyword">\g<name></literal>', line) bos@552: line = re.sub(r'\\tplkwfilt{(?P<tpl>[^}]+)}{(?P<name>[^}]+)}', bos@552: r'<literal role="template-kw-filt-\g<tpl>">\g<name></literal>', line) bos@552: line = re.sub(r'\\[vV]erb(.)(?P<txt>[^\1]+?)\1', bos@552: r'<literal>\g<txt></literal>', line) bos@552: line = re.sub(r'\\package{(?P<name>[^}]+)}', bos@552: r'<literal role="package">\g<name></literal>', line) bos@552: line = re.sub(r'\\hgcmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', bos@552: r'<command role="hg-cmd">hg \g<cmd> \g<args></command>', bos@552: line) bos@552: line = re.sub(r'\\cmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', bos@552: r'<command>\g<cmd> \g<args></command>', bos@552: line) bos@552: m = re.match(r'\\(chapter|section|subsection|subsubsection){(.*)}', line) bos@552: if m: bos@552: kind, content = m.groups() bos@552: sec = sections[kind] bos@552: while stack and stack[-1] >= sec: bos@552: close = stack.pop() bos@552: print >> ofp, '</%s>' % close bos@552: stack.append(sec) bos@552: print >> ofp, '<%s>\n<title>%s</title>' % (sec, content) bos@552: else: bos@552: m = re.match(r'\s*\\(begin|end){(?P<sect>[^}]+)}', line) bos@552: if m: bos@552: if not para: bos@552: print >> ofp, '</para>' bos@552: if inlist: bos@552: ofp.write('</listitem>') bos@552: para = True bos@552: state, env = m.groups() bos@552: env = envs[env] bos@552: if state == 'begin': bos@552: ofp.write('<') bos@556: if env in ('itemizedlist', 'orderedlist'): bos@556: inlist = 1 bos@552: else: bos@552: ofp.write('</') bos@559: if env in ('itemizedlist', 'orderedlist'): bos@556: inlist = 0 bos@552: print >> ofp, env + '>' bos@552: else: bos@552: if line.startswith('\\item '): bos@556: if inlist > 1: bos@556: print >> ofp, '</para>' bos@556: print >> ofp, '</listitem>' bos@556: else: bos@556: inlist = 2 bos@552: para = True bos@552: line = line[6:] bos@552: if line and para: bos@552: if inlist: bos@552: ofp.write('<listitem>') bos@552: ofp.write('<para>') bos@552: para = False bos@552: if not line and not para: bos@552: print >> ofp, '</para>' bos@552: if inlist: bos@552: ofp.write('</listitem>') bos@552: para = True bos@552: print >> ofp, line bos@552: while stack: bos@552: print >> ofp, '</%s>' % stack.pop() bos@556: ofp.write('\n'.join(['\n<!--', bos@552: 'local variables: ', bos@552: 'sgml-parent-document: ("00book.xml" "book" "chapter")', bos@552: 'end:', bos@552: '-->'])) bos@552: bos@552: bos@552: if __name__ == '__main__': bos@552: for name in sys.argv[1:]: bos@552: if not name.endswith('.tex'): bos@552: continue bos@552: newname = name[:-3] + 'xml' bos@552: ofp = StringIO.StringIO() bos@552: process(open(name), ofp) bos@552: s = ofp.getvalue() bos@552: s = re.sub('\n+</para>', '</para>', s, re.M) bos@552: open(newname, 'w').write(s)