【问题标题】:How can I configure the separator character used for :menuselection:?如何配置用于 :menuselection: 的分隔符?
【发布时间】:2015-09-25 00:52:16
【问题描述】:

我正在使用 Sphinx 为我的项目生成 HTML 文档。在 Inline Markup 下,Sphinx 文档讨论了 :menuselection: 使用以下标记标记一系列菜单选择:

:menuselection:`Start --> Programs`

这会产生以下 HTML:

<span class="menuselection">Start ‣ Programs</span>

--&gt; 被转换为小三角形,我确定它是 U+2023,TRIANGULAR BULLET。

这一切都很好,但我想使用不同的字符而不是三角形。我已经在 Sphinx 包和主题包 (sphinx-bootstrap-theme) 中彻底搜索了“menuselection”、三角形字符和其他一些东西,但没有找到任何可以将 --&gt; 替换为(对我来说没什么明显的,反正)。但是必须在我的 .rst 源和 html 之间进行转换。

我的问题是:具体是在做什么转换(sphinx core?HTML writer?Theme JS?)?

【问题讨论】:

    标签: python-sphinx restructuredtext


    【解决方案1】:

    转换在sphinx.roles.menusel_role() 函数中完成。您可以使用不同的分隔符创建您自己的此函数版本并将其注册以供使用。

    将以下内容添加到项目的 conf.py 中:

    from docutils import nodes, utils
    from docutils.parsers.rst import roles
    from sphinx.roles import _amp_re
    
    def patched_menusel_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
        text = utils.unescape(text)
        if typ == 'menuselection':
            text = text.replace('-->', u'\N{RIGHTWARDS ARROW}')  # Here is the patch 
        spans = _amp_re.split(text)  
    
        node = nodes.emphasis(rawtext=rawtext)
        for i, span in enumerate(spans):
            span = span.replace('&&', '&')
            if i == 0:
                if len(span) > 0:
                    textnode = nodes.Text(span)
                    node += textnode
                continue
            accel_node = nodes.inline()
            letter_node = nodes.Text(span[0])
            accel_node += letter_node
            accel_node['classes'].append('accelerator')
            node += accel_node
            textnode = nodes.Text(span[1:])
            node += textnode
    
        node['classes'].append(typ)
        return [node], []
    
    # Use 'patched_menusel_role' function for processing the 'menuselection' role
    roles.register_local_role("menuselection", patched_menusel_role)
    

    在构建 html 时,请确保先make clean,以便更新的 conf.py 与补丁一起重新解析。

    【讨论】:

      猜你喜欢
      • 2012-10-27
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多