From: Edward L. <ed...@gr...> - 2004-04-16 17:30:32
|
David Goodger wrote: > Done; checked in to CVS. Looks good. One other question I had was where pep_url and rfc_url should live. Currently, they're still in Inliner, but that doesn't seem appropriate. Some other options: - Put them in module-level variables in roles.py - Put them in attributes on the functions that use them. (This is a little odd, but would work). - Implement pep_reference_role and rfc_reference_role as class instances that implement __call__. Something like this: #---------------------------------------------------------------------- class URIReferenceRole: """Abstract base class for specialized reference roles.""" def __call__(self, role, rawtext, text, lineno, inliner): try: self.verify(text) except ValueError, e: msg = inliner.reporter.error(str(e), line=lineno) prb = inliner.problematic(text, text, msg) return [prb, msg] label = self.label(text) uri = self.uri(text) return [nodes.referencenode(rawtext, label, refuri=uri)], [] def verify(self, text): """Raise a ValueError if `text` is invalid.""" def label(self, text): """Return the reference label corresponding to `text`""" def uri(self, text): """Return the reference target corresponding to `text`""" class PEPReferenceRole(ReferenceRole): def __init__(self, base_url): pep_url = base_url+'pep-%04d.html' def verify(self, text): if not re.match(r'\d{1,4}'): raise ValueError('PEP number must be a number from 0 '+ 'to 9999; %r is invalid.' % text) def label(self, text): return 'PEP %s' % text def uri(self, text): return self.pep_url % text pep_reference_role = PEPReferenceRole('http://www.python.org/peps/') local_pep_reference_role = PEPReferenceRole('') register_canonical_role('pep-reference', pep_reference_role) #---------------------------------------------------------------------- Then a user could override the pep location with: register_canonical_role('pep-reference', local_pep_reference_role) or: my_pep_reference_role = PEPReferenceRole('http://somewhere/else/') register_canonical_role('pep-reference', my_pep_reference_role) -Edward |