如何在HTML输出中将rel属性添加到docutils sphinx引用? [英] How to add rel attribute to docutils sphinx reference in HTML output?

查看:181
本文介绍了如何在HTML输出中将rel属性添加到docutils sphinx引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Sphinx文档实用程序的简单扩展(我使用的版本是Sphinx-1.1.3-py2.6)。非常喜欢Doug Hellmann的这个优秀的例子。如何将 rel ='bar'属性添加到 标记的最终HTML中?

I have a simple extension for the Sphinx documentation utility (my version in use isSphinx-1.1.3-py2.6). Very much like this excellent example by Doug Hellmann. 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)

然而, rel ='bar'属性从最终的HTML标记中删除。通过源代码搜索,我得到了 sphinx / writers / html.py HTMLTranslator 类。以下是visit_reference方法的一部分:

However, the rel='bar' attribute gets stripped out from the final HTML markup. 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.

所以,我可以:


  • 创建一个自定义节点,重新实现参考节点的所有功能。一小部分工作需要做一些工作。

  • sphinx / writers / html.py 中覆盖* visit_reference *方法。在未来的Sphinx更新方面更快,但更糟糕。

  • 事后,将带有jQuery的rel属性添加到链接标记中。好吧,也不漂亮。

  • 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.

谢谢!

推荐答案

我设法用 download_reference 。使用 app.add_node 我覆盖访问_... 方法:

I managed to do this with download_reference. Using app.add_node I override the visit_... method:

import posixpath
from sphinx.writers.html import HTMLTranslator
from sphinx.addnodes import download_reference

def visit_download_reference(self, node):
    if node.hasattr('filename'):
        self.body.append(
            '<a class="reference download internal" href="%s" %s>' %
            (posixpath.join(self.builder.dlpath, node['filename']), 'rel="%s"' % node['rel'] if node.get('rel', None) else ''))
        self.context.append('</a>')
    else:
        self.context.append('')

def setup(app):
    app.add_node(download_reference, html=(visit_download_reference, HTMLTranslator.depart_download_reference))

完整延期就在这里

这篇关于如何在HTML输出中将rel属性添加到docutils sphinx引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆