Source

possumpalace_kit / _ext / manualxetex.py

Full commit
"""Disable as moch of ReST's truly overblown citation intervention as possible.
I just want to pump unadorned citaions out and let BibTeX field this one.
"""

import docutils.writers.xetex
from docutils import nodes

class Writer(docutils.writers.xetex.Writer):
    def __init__(self):
        docutils.writers.xetex.Writer.__init__(self)
        self.translator_class = ManualXeLaTeXTranslator

class ManualXeLaTeXTranslator(docutils.writers.xetex.XeLaTeXTranslator):
    def visit_manualcite(self, node):
        if not self.inside_citation_reference_label:
            self.out.append(r'\cite{')
            self.inside_citation_reference_label = 1
        else:
            assert self.body[-1] in (' ', '\n'),\
                    'unexpected non-whitespace while in reference label'
            del self.body[-1]

    def depart_manualcite(self, node):
        followup_citation = False
        # check for a following citation separated by a space or newline
        next_siblings = node.traverse(descend=False, siblings=True,
                                      include_self=False)
        if len(next_siblings) > 1:
            next = next_siblings[0]
            if (isinstance(next, nodes.Text) and
                next.astext() in (' ', '\n')):
                if next_siblings[1].__class__ == node.__class__:
                    followup_citation = True
        if followup_citation:
            self.out.append(',')
        else:
            self.out.append('}')
            self.inside_citation_reference_label = False