How to add rel attribute to sphinx reference in HTML output?

81 views
Skip to first unread message

Axial

unread,
Oct 31, 2012, 9:46:21 PM10/31/12
to sphin...@googlegroups.com
I have a simple extension for Sphinx (my version in use is Sphinx-1.1.3-py2.6). Very much like this excellent example by Doug Hellmann.
My question is: How can I add a rel='bar' attribute to the final HTML for the tag?

There seems to be a few ways to do this, but I couldn't find a simple one. Advise and tips are appreciated.

The reference nodes are created in this fashion:

node = nodes.reference(rawtext, utils.unescape(text),
            internal=False,
            refuri=ref,
            classes=['foocss'],
            rel='bar',
            **options)

However, the rel='bar' attribute gets stripped out from the final HTML markup (note: it make is fine into the starttag). Hunting through the source got me to sphinx/writers/html.py and the HTMLTranslator class. Here is part of the visit_reference method:

# overwritten
def visit_reference(self, node):
    atts = {'class': 'reference'}

    <snip>

    if 'reftitle' in node:
        atts['title'] = node['reftitle']
    self.body.append(self.starttag(node, 'a', '', **atts))

Additional attributes are not handled. Maybe they could be replaced in others parts. I couldn't find anything useful in that respect.

So, I could:

  • create a custom node which reimplements all the functionality of the reference node. A fair bit of work for a small addition.
  • Overwrite the visit_reference method in sphinx/writers/html.py. Quicker, but bad in terms of future Sphinx updates.
  • Add the rel attribute with jQuery to the link tag after the fact. Well, not pretty either.

I'm sure I'm missing the obvious and elegant solution.

Note that I've posted the same question on stackoverflow.com with a bit more information.
Just casting the net a bit wider since this seems to be the appropriate forum.

Thanks for reading.

Axial

unread,
Nov 1, 2012, 6:02:40 AM11/1/12
to sphin...@googlegroups.com
Reposted in sphinx-users forum. Seems more appropriate. Sorry for the noise.

Reply all
Reply to author
Forward
0 new messages