I need to replace the default intepreted text roles
resolver roles.role() with a custom one. Actually there
is no easy way to do that. I have cooked the small
patch attached to do that. In this way i can do
something like:
class Roles:
def __init__(self):
self.resolvers = [ ]
def register(self, resolver):
self.resolvers.append(resolver)
def role(self, *args, **kwargs):
function, messages = roles.role(*args, **kwargs)
if function is None:
for resolver in self.resolvers:
handler = resolver(*args, **kwargs)
if callable(handler):
return handler, []
return function, messages
class Inliner(rst.states.Inliner):
def __init__(self):
roles = Roles()
rst.states.Inliner.__init__(self, roles=roles)
class Parser(rst.Parser):
def __init__(self):
inliner = Inliner()
rst.Parser.__init__(self, inliner=inliner)
class Reader(standalone.Reader):
def __init__(self, obj, hub):
parser = Parser()
standalone.Reader.__init__(self, parser=parser)
reader = Reader(obj, hub)
reader.parser.inliner.roles.register(CustomRolesResolver(params))
writer = Writer()
parts = publish_parts(source=src,
reader=reader,
writer=writer,
settings_overrides=settings,
config_section='my application')
where CustomRoleResolver() is a class that has a
__call__ method that implements the interface of
roles.role() function.
patch
Logged In: YES
user_id=1014490
I couldn't apply that patch, so I manually changed the code.
A unified
diff against current code is attached. The test suite still
passes.
I'm not sure if we want to check it in, though. David, any
comments?
New patch against current code.
I think this very old patch is superseded by https://sourceforge.net/p/docutils/patches/156/