|
From: Chris L. <cli...@gm...> - 2005-04-14 22:38:01
|
David Goodger wrote:
> [Robert Brewer]
> > I would just prefer to show the .txt file explicitly in the source
> > text and add some other decoration that expresses it is rewritable
> > by the backend.
>
> That may be asking too much.
what about rules?
i think about this:
.. role:: link(rewrite)
:transform: .txt|.html
and then to use it:
::
for more information see :link:`README.txt`
it would be useful if it supported an additional option
:format: html
so that separate rules for each format can be defined.
(like the "raw" role)
below is a simple implementation of "rewrite".
an improved version could allow regexps and not just replace the end of
the string (the one below does not support internal links like
README.txt#section-one
a more generic "rewrite", for not just links, could be useful for other
things too.. maybe...
chris
#-------------- to be placed in a writer or rules.py ---
from docutils.nodes import whitespace_normalize_name
from docutils.parsers.rst import roles
def role_rewrite(name, rawtext, text, lineno, inliner,
options={}, content=[]):
if not options.has_key('transform'):
msg = inliner.reporter.error(
'No transform is associated with this role: "%s".\n'
'The "rewrite" role cannot be used directly.\n'
'Instead, use the "role" directive to create a new role with '
'an associated format.' % role, line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
if '|' not in options['transform']:
msg = inliner.reporter.error(
'The transform is invalid with this role: "%s".\n'
'e.g. ".txt|.html".' % role, line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
rin, rout = options['transform'].split('|')
if text.endswith(rin):
text = text[:-len(rin)] + rout
node = nodes.reference(rawtext, utils.unescape(text),
name=whitespace_normalize_name(text))
return [node], []
role_rewrite.options = {
'transform': directives.unchanged,
'format': directives.unchanged
}
roles.register_local_role('rewrite', role_rewrite)
#------------------------
cr
|