-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Am 14.12.2010 00:21, schrieb Václav Šmilauer:
> Hello everybody,
>
> I am struggling with a simple thing (but I want to do it properly)...
> I want to create a custom internal hyperlinking role :yref:`target`,
> where target is transformed to yield name of an existing label
> (target2, created either manually or automatically) somewhere else in
> the document (class name or such), so in the end it will be as if
> writing :ref:`target <target2>`. This target2 should be then resolved
> automatically.
>
> So far, I do the following in conf.py:
>
> 1. I create a class inheriting from XRefRole, overriding process_link:
>
> class YXRefRole(XRefRole):
> def process_link(self, env, refnode, has_explicit_title, title,
> target):
> # the logic here would be more complex; for now, we want to
> simulate :ref:`[[target]] <prefix.target>`
> return '[['+title+']]','prefix.'+target
>
> 2. Then I create a thin wrapper function to use that class:
>
> def yref_role(type,rawtext,text,lineno,inliner,options={},content=[]):
> # I know this has to instantiate the class over and over, but that
> is not an issue now
> return YXRefRole()
> ('yref',rawtext,text,lineno,inliner,options,content)
>
> 3. Finally, the role is registered:
>
> from docutils.parsers.rst import roles
> roles.register_canonical_role('yref', yref_role)
>
> The result is that the title is really replaced (I see [[target]] in
> bold in the resulting HTML), but the hyperlink is not created and I
> don't see any warning about unresolvable reference...
>
> What is the last piece that I am missing?
You will have to tell Sphinx what role you want to replace: in yref_role,
call YXRefRole()('std:ref', ...) instead of ...'yref'...
You can also set these properties in process_link() and remove the
indirection via another role function. My suggestion:
from sphinx.roles import XRefRole
class YXRefRole(XRefRole):
def process_link(self, env, refnode, has_explicit_title, title, target):
refnode['reftype'] = 'ref'
refnode['refexplicit'] = True # to prevent that Sphinx overwrites
# the title you set
return '[['+title+']]','prefix.'+target
def setup(app):
app.add_role_to_domain('std', 'yref', YXRefRole())
cheers & HTH,
Georg
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)
iEYEARECAAYFAk0o8H4ACgkQN9GcIYhpnLA7kACgqPB/h8Rpdu50bq5i/sEsq3jE
qaMAnjFO/FbT9i8gOonnwHTtnNFs5uLY
=Y77h
-----END PGP SIGNATURE-----
--
You received this message because you are subscribed to the Google Groups
"sphinx-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sphinx-dev?hl=en.