# Author: David Goodger # Contact: goodger@users.sourceforge.net # Revision: $Revision$ # Date: $Date$ # Copyright: This module has been placed in the public domain. """ Transforms for resolving references. """ __docformat__ = 'reStructuredText' import sys import re from docutils import nodes, utils from docutils.transforms import TransformError, Transform indices = xrange(sys.maxint) class ChainedTargets(Transform): """ Attributes "refuri" and "refname" are migrated from the final direct target up the chain of contiguous adjacent internal targets, using `ChainedTargetResolver`. """ default_priority = 420 def apply(self): visitor = ChainedTargetResolver(self.document) self.document.walk(visitor) class ChainedTargetResolver(nodes.SparseNodeVisitor): """ Copy reference attributes up the length of a hyperlink target chain. "Chained targets" are multiple adjacent internal hyperlink targets which "point to" an external or indirect target. After the transform, all chained targets will effectively point to the same place. Given the following ``document`` as input:: I'm known as "d". ``ChainedTargetResolver(document).walk()`` will transform the above into:: I'm known as "d". """ def unknown_visit(self, node): pass def visit_target(self, node): if node.hasattr('refuri'): attname = 'refuri' call_if_named = self.document.note_external_target elif node.hasattr('refname'): attname = 'refname' call_if_named = self.document.note_indirect_target elif node.hasattr('refid'): attname = 'refid' call_if_named = None else: return attval = node[attname] index = node.parent.index(node) for i in range(index - 1, -1, -1): sibling = node.parent[i] if not isinstance(sibling, nodes.target) \ or sibling.hasattr('refuri') \ or sibling.hasattr('refname') \ or sibling.hasattr('refid'): break sibling[attname] = attval if sibling.hasattr('name') and call_if_named: call_if_named(sibling) class AnonymousHyperlinks(Transform): """ Link anonymous references to targets. Given:: internal external Corresponding references are linked via "refid" or resolved via "refuri":: text external """ default_priority = 440 def apply(self): if len(self.document.anonymous_refs) \ != len(self.document.anonymous_targets): msg = self.document.reporter.error( 'Anonymous hyperlink mismatch: %s references but %s ' 'targets.\nSee "backrefs" attribute for IDs.' % (len(self.document.anonymous_refs), len(self.document.anonymous_targets))) msgid = self.document.set_id(msg) for ref in self.document.anonymous_refs: prb = nodes.problematic( ref.rawsource, ref.rawsource, refid=msgid) prbid = self.document.set_id(prb) msg.add_backref(prbid) ref.parent.replace(ref, prb) return for ref, target in zip(self.document.anonymous_refs, self.document.anonymous_targets): if target.hasattr('refuri'): ref['refuri'] = target['refuri'] ref.resolved = 1 else: ref['refid'] = target['id'] self.document.note_refid(ref) target.referenced = 1 class IndirectHyperlinks(Transform): """ a) Indirect external references:: indirect external The "refuri" attribute is migrated back to all indirect targets from the final direct target (i.e. a target not referring to another indirect target):: indirect external Once the attribute is migrated, the preexisting "refname" attribute is dropped. b) Indirect internal references:: indirect internal Targets which indirectly refer to an internal target become one-hop indirect (their "refid" attributes are directly set to the internal target's "id"). References which indirectly refer to an internal target become direct internal references:: indirect internal """ default_priority = 460 def apply(self): for target in self.document.indirect_targets: if not target.resolved: self.resolve_indirect_target(target) self.resolve_indirect_references(target) def resolve_indirect_target(self, target): refname = target['refname'] reftarget_id = self.document.nameids.get(refname) if not reftarget_id: self.nonexistent_indirect_target(target) return reftarget = self.document.ids[reftarget_id] if isinstance(reftarget, nodes.target) \ and not reftarget.resolved and reftarget.hasattr('refname'): self.one_indirect_target(reftarget) # multiply indirect if reftarget.hasattr('refuri'): target['refuri'] = reftarget['refuri'] if target.hasattr('name'): self.document.note_external_target(target) elif reftarget.hasattr('refid'): target['refid'] = reftarget['refid'] self.document.note_refid(target) else: try: target['refid'] = reftarget['id'] self.document.note_refid(target) except KeyError: self.nonexistent_indirect_target(target) return del target['refname'] target.resolved = 1 reftarget.referenced = 1 def nonexistent_indirect_target(self, target): naming = '' if target.hasattr('name'): naming = '"%s" ' % target['name'] reflist = self.document.refnames.get(target['name'], []) else: reflist = self.document.refids.get(target['id'], []) naming += '(id="%s")' % target['id'] msg = self.document.reporter.warning( 'Indirect hyperlink target %s refers to target "%s", ' 'which does not exist.' % (naming, target['refname']), base_node=target) msgid = self.document.set_id(msg) for ref in reflist: prb = nodes.problematic( ref.rawsource, ref.rawsource, refid=msgid) prbid = self.document.set_id(prb) msg.add_backref(prbid) ref.parent.replace(ref, prb) target.resolved = 1 def resolve_indirect_references(self, target): if target.hasattr('refid'): attname = 'refid' call_if_named = 0 call_method = self.document.note_refid elif target.hasattr('refuri'): attname = 'refuri' call_if_named = 1 call_method = self.document.note_external_target else: return attval = target[attname] if target.hasattr('name'): name = target['name'] try: reflist = self.document.refnames[name] except KeyError, instance: if target.referenced: return msg = self.document.reporter.info( 'Indirect hyperlink target "%s" is not referenced.' % name, base_node=target) target.referenced = 1 return delatt = 'refname' else: id = target['id'] try: reflist = self.document.refids[id] except KeyError, instance: if target.referenced: return msg = self.document.reporter.info( 'Indirect hyperlink target id="%s" is not referenced.' % id, base_node=target) target.referenced = 1 return delatt = 'refid' for ref in reflist: if ref.resolved: continue del ref[delatt] ref[attname] = attval if not call_if_named or ref.hasattr('name'): call_method(ref) ref.resolved = 1 if isinstance(ref, nodes.target): self.resolve_indirect_references(ref) target.referenced = 1 class ExternalTargets(Transform): """ Given:: direct external The "refname" attribute is replaced by the direct "refuri" attribute:: direct external """ default_priority = 640 def apply(self): for target in self.document.external_targets: if target.hasattr('refuri') and target.hasattr('name'): name = target['name'] refuri = target['refuri'] try: reflist = self.document.refnames[name] except KeyError, instance: if target.referenced: continue msg = self.document.reporter.info( 'External hyperlink target "%s" is not referenced.' % name, base_node=target) target.referenced = 1 continue for ref in reflist: if ref.resolved: continue del ref['refname'] ref['refuri'] = refuri ref.resolved = 1 target.referenced = 1 class InternalTargets(Transform): """ Given:: direct internal The "refname" attribute is replaced by "refid" linking to the target's "id":: direct internal """ default_priority = 660 def apply(self): for target in self.document.internal_targets: if target.hasattr('refuri') or target.hasattr('refid') \ or not target.hasattr('name'): continue name = target['name'] refid = target['id'] try: reflist = self.document.refnames[name] except KeyError, instance: if target.referenced: continue msg = self.document.reporter.info( 'Internal hyperlink target "%s" is not referenced.' % name, base_node=target) target.referenced = 1 continue for ref in reflist: if ref.resolved: continue del ref['refname'] ref['refid'] = refid ref.resolved = 1 target.referenced = 1 class Footnotes(Transform): """ Assign numbers to autonumbered footnotes, and resolve links to footnotes, citations, and their references. Given the following ``document`` as input:: A labeled autonumbered footnote referece: An unlabeled autonumbered footnote referece: Unlabeled autonumbered footnote. Labeled autonumbered footnote. Auto-numbered footnotes have attribute ``auto="1"`` and no label. Auto-numbered footnote_references have no reference text (they're empty elements). When resolving the numbering, a ``label`` element is added to the beginning of the ``footnote``, and reference text to the ``footnote_reference``. The transformed result will be:: A labeled autonumbered footnote referece: 2 An unlabeled autonumbered footnote referece: 1