def doctree_read(app, doctree): # first generate figure numbers for each figure env = app.builder.env docname_figs = getattr(env, 'docname_figs', {}) docnames_by_figname = getattr(env, 'docnames_by_figname', {}) for figure_info in doctree.traverse( lambda n: isinstance(n, nodes.figure) or isinstance( n, subfig.subfigend) or isinstance(n, figtable.figtable)): for id in figure_info['ids']: fig_docname = docnames_by_figname[id] = env.docname if fig_docname not in docname_figs: docname_figs[fig_docname] = OrderedDict() if isinstance(figure_info.parent, subfig.subfig): mainid = figure_info.parent['mainfigid'] else: mainid = id if mainid not in docname_figs[fig_docname]: docname_figs[fig_docname][mainid] = OrderedSet() if isinstance(figure_info.parent, subfig.subfig): docname_figs[fig_docname][mainid].add(id) env.docnames_by_figname = docnames_by_figname env.docname_figs = docname_figs
def sort_references(refs, citations): def sortkey(key): # sort by author last names, but if no author, sort by title citation = citations.get(key) authorsort = u''.join(map(unicode, citation.persons.get('author', ''))) if len(authorsort) > 0: authorsort = authorsort.replace('{', '') authorsort = authorsort.replace('}', '') return authorsort.upper() titlesort = citation.fields.get('title', '') titlesort = titlesort.replace('{', '') titlesort = titlesort.replace('}', '') return titlesort.upper() sortedrefs = sorted(refs, key=sortkey) return OrderedSet(sortedrefs)
class CitationDomain(Domain): name = "cite" label = "citation" object_types = { 'citation': ObjType(l_('citation'), *ROLES, searchprio=-1), } directives = { 'conf': CitationConfDirective, 'refs': CitationReferencesDirective } roles = dict([(r, CitationXRefRole()) for r in ROLES]) initial_data = { 'keys': OrderedSet(), # Holds cite-keys in order of reference 'conf': DEFAULT_CONF, 'refdocs': {} } def __init__(self, env): super(CitationDomain, self).__init__(env) # Update conf env.domaindata['cite']['conf'].update(env.config.natbib) # TODO: warn if citations can't parse bibtex file self.citations = Citations(env) def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode): refdoc = env.domaindata['cite'].get('refdoc') if not refdoc: env.warn( fromdocname, 'no `refs` directive found; citations will have dead links', node.line) refuri = '' else: refuri = builder.get_relative_uri(fromdocname, refdoc) for nd in node.children: if isinstance(nd, nodes.pending): nd.details['refs'] = [] if builder.name == 'latex': cite_keys = nd.details['keys'] cite_keys = ','.join(cite_keys) cite_node = nodes.citation_reference(cite_keys, cite_keys) return cite_node for key in nd.details.pop('keys'): ref = self.citations.get(key) if ref is None: continue nd.details['refs'].append(ref) transform = nd.transform(**nd.details) node = transform.cite( typ, refuri, global_keys=env.domaindata['cite']['keys']) return node