Replace href text in ref node for directive in the standard domain

30 views
Skip to first unread message

Juna Luzi

unread,
Sep 28, 2020, 6:34:16 PM9/28/20
to sphinx-users
I have created a simple directive which can be referenced through the ref role. I register this directive as an explicit target through `self.state.document.note_explicit_target(node)`.

When I reference the directive through the ref role, I want to be able to control the href text displayed depending on a few attributes of the target node. Is there a to override `process_link` for the `ref` role? What's the best way to accomplish this?

Would be happy to provide more info if necessary. :-)

joyoussef67

unread,
Sep 30, 2020, 9:48:04 PM9/30/20
to sphinx-users

Mohamed ghalleb

unread,
Sep 30, 2020, 10:52:08 PM9/30/20
to sphinx...@googlegroups.com

thinks for your atentione.

--
You received this message because you are subscribed to the Google Groups "sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/7fe26bc6-368c-4edb-8ecf-3e8466afbc3dn%40googlegroups.com.

Juna Luzi

unread,
Oct 1, 2020, 8:50:57 AM10/1/20
to sphinx-users
After spending a few days going through sphinx and docutils source code, I was able to find a way to control the text displayed in ref_node when referenced through `ref` and `numref` roles without having to override the ref role.

Just update the `sectname` below to whatever text you want to display by overriding the information in the `domain.labels` and `domain.anonlabels`.

```
def process_docs(app, document):
    ""' traverse through admonition nodes"
    for node in document.traverse(nodes.admonition):
        ....
        domain = cast(StandardDomain, app.env.get_domain('std'))
        domain.anonlabels[name] = docname, labelid
        domain.labels[name] = docname, labelid, sectname
```

`app.connect('doctree-read', process_docs)`

Hope this is helpful to others too. :-)
On Thursday, October 1, 2020 at 4:52:08 AM UTC+2 tipko...@gmail.com wrote:

thinks for your atentione.

Le 1 oct. 2020 02:48, "joyoussef67" <joyou...@gmail.com> a écrit :


في الثلاثاء، 29 سبتمبر 2020 في تمام الساعة 2:34:16 ص UTC+4، كتب juna...@gmail.com رسالة نصها:
I have created a simple directive which can be referenced through the ref role. I register this directive as an explicit target through `self.state.document.note_explicit_target(node)`.

When I reference the directive through the ref role, I want to be able to control the href text displayed depending on a few attributes of the target node. Is there a to override `process_link` for the `ref` role? What's the best way to accomplish this?

Would be happy to provide more info if necessary. :-)

--
You received this message because you are subscribed to the Google Groups "sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages