【问题标题】:Using jinja inside sphinx raw html在 sphinx raw html 中使用 jinja
【发布时间】:2013-07-27 11:14:44
【问题描述】:

所以我有一个狮身人面像生成的网站。它的一部分在原始 html 中,由 sphinx + jinja 解析。 现在我想在原始 html 中使用指向 toc-tree 某些部分的链接。 有没有办法做到这一点? 目前我正在退出原始 html 并使用 rst。 这看起来像

  .. raw:: html

      <small class="float-right example-links">

  :ref:`Examples<general_examples>`                                                         

  .. raw:: html

      </small>      

不是这么丑,它还会把生成的 html 弄乱。 有没有更好的方法来做到这一点?

谢谢:)

【问题讨论】:

    标签: jinja2 python-sphinx restructuredtext


    【解决方案1】:

    如果您知道ids 部分是如何生成的,您可以简单地使用 HTML 链接元素 &lt;a href="..."...&lt;/a&gt;。目录中项目的ids 只是小写的部分标题,其中空格替换为连字符-。所以这个 reStructuredText

    Title
    =====
    
    .. contents:: Table of Contents
    
    Section 1
    ---------
    
    Some filler text...
    
    Section 2
    ---------
    
    Some filler text...
    

    导致以下 HTML sn-p(&lt;head&gt; 等已删除)

    <body>
    <div class="document" id="title">
    <h1 class="title">Title</h1>
    
    <div class="contents topic" id="table-of-contents">
    <p class="topic-title first">Table of Contents</p>
    <ul class="simple">
    <li><a class="reference internal" href="#section-1" id="id1">Section 1</a></li>
    <li><a class="reference internal" href="#section-2" id="id2">Section 2</a></li>
    </ul>
    </div>
    <div class="section" id="section-1">
    <h1><a class="toc-backref" href="#id1">Section 1</a></h1>
    <p>Some filler text...</p>
    </div>
    <div class="section" id="section-2">
    <h1><a class="toc-backref" href="#id2">Section 2</a></h1>
    <p>Some filler text...</p>
    </div>
    </div>
    </body>
    

    因此,为了链接到目录项,您可以使用以下 reStructuredText

    .. raw:: html
    
       <small class="..."><a href="#section-1">Section 1</a></small>
    

    如果您想链接到该部分本身,请将href 值替换为#id1 或相关部分的id

    【讨论】:

    • 这似乎只有在链接指向同一个文件时才有效,对吧?但对我来说,目标在不同的页面上。
    • 你是对的。我是从纯粹的 reStructuredText 角度来看的。希望其他人能想出一个 Sphinx 解决方案。
    猜你喜欢
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2023-01-31
    • 2019-11-12
    相关资源
    最近更新 更多