0

So I have a sphinx-generated website. Parts of it are in raw html, parsed by sphinx + jinja. Now I want to use links to certain parts of the toc-tree inside the raw html. Is there a way to achieve this? Currently I'm exiting the raw html and use rst. This looks something like

  .. raw:: html

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

  :ref:`Examples<general_examples>`                                                         

  .. raw:: html

      </small>      

Not is this ugly, it also messes up the generated html. Is there a better way to do this?

Thanks :)

Andreas Mueller
  • 27,470
  • 8
  • 62
  • 74

1 Answers1

0

You can simply use a HTML link element, <a href="..."...</a> if you know how section ids are generated. The ids for items in the table of contents are simply lower-cased section titles with white space replaced by hyphens, -. So this reStructuredText

Title
=====

.. contents:: Table of Contents

Section 1
---------

Some filler text...

Section 2
---------

Some filler text...

results in the following HTML snippet (<head> etc. removed)

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

Therefore, in order to link to a table of contents item you can use the following reStructuredText

.. raw:: html

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

If you want to link to the section itself then replace the href value with #id1 or the relevant section's id.

Chris
  • 44,602
  • 16
  • 137
  • 156
  • That only seems to work if the link refers to the same file, right? But the targets are on different pages for me. – Andreas Mueller Jul 28 '13 at 09:56
  • You're right. I was coming at this from a purely reStructuredText point of view. Hopefully someone else can come up with a Sphinx solution. – Chris Jul 28 '13 at 11:30