Using :ref: style tags in an html template?

19 views
Skip to first unread message

Ted Drain

unread,
Nov 3, 2014, 5:17:16 PM11/3/14
to sphinx...@googlegroups.com
I have an RST document that I can reference using a tag like this :ref:`foo_bar` and I'd like to have a similar link in an HTML template file like this:

<a href="{{ linkto( 'foo_bar' ) }}">Foo/Bar</a>

I tried using the template function pathto() but that doesn't expand the link out to the document location. Is there any way to use RST link targets in a template file?

Takayuki Shimizukawa

unread,
Nov 4, 2014, 8:18:17 PM11/4/14
to sphinx...@googlegroups.com
Hi,

There is no way officially, however I wrote a simple helper function
for conf.py as:

def update_page_context(app, pagename, templatename, context, doctree):
def linkto(label):
docname, labelid =
app.env.domains['std'].data['anonlabels'].get(label, ('', ''))
if docname == pagename:
url = '#' + labelid
else:
url = app.builder.get_relative_uri(pagename, docname) +
'#' + labelid
return url
context['linkto'] = linkto


def setup(app):
app.connect('html-page-context', update_page_context)


(there is a full version:
https://gist.github.com/shimizukawa/37e26f6353327dad3f9d )

Please try it.
Note that it is tested only with HTML builder and it has no error handling.

Regards,
--
Takayuki SHIMIZUKAWA
http://about.me/shimizukawa
> --
> 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.
> To post to this group, send email to sphinx...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sphinx-users.
> For more options, visit https://groups.google.com/d/optout.

Sandro Dentella

unread,
Nov 8, 2014, 12:49:26 PM11/8/14
to sphinx...@googlegroups.com


Hi Takayuki,

> https://gist.github.com/shimizukawa/37e26f6353327dad3f9d

this is a wanderful function. From my tests it seems to work just for internal refs and not for refs generated when
using intersphinx syntax. Do you have a hing for that case too?

Thanks in advance


sandro

Takayuki Shimizukawa

unread,
Nov 8, 2014, 6:48:00 PM11/8/14
to sphinx...@googlegroups.com
Hi,

I hacked it for the intersphinx.
Try it again.
Note that it supports std ref target only. If you need other types, please try extending it.

Enjoy.

Sandro Dentella

unread,
Nov 9, 2014, 12:54:48 PM11/9/14
to sphinx...@googlegroups.com

Great,

thanks a lot Takayuki


sandro
*:-)

Sandro Dentella

unread,
Nov 9, 2014, 1:05:27 PM11/9/14
to sphinx...@googlegroups.com
This is great,

thank you Takayuki

sandro
*:-)
Reply all
Reply to author
Forward
0 new messages