How do I include the homepage in the Sphinx TOC? - python-sphinx

Let’s say I’ve got a Sphinx project with the following sources:
index.rst
installation.rst
templating/
index.rst
module.rst
fieldtype.rst
index.rst (the homepage) has the following TOC tree:
.. toctree::
:titlesonly:
installation
templating/index
I want my template to include a sidebar that lists all 3 top-level pages (homepage, installation, templating/index).
I've tried adding a second, hidden TOC tree in the homepage:
.. toctree::
:hidden:
index
.. toctree::
:titlesonly:
installation
templating/index
That actually gives me the desired result, except that it makes the next variable set to the current page. So this code in my template:
Next up: {{ next.title }}
…always outputs the homepage link from the homepage. No good.
I’ve always tried to hardcode the actual homepage link right into the sidebar of the template:
{% set homeClass = 'current' if pagename == 'index' else '' %}
<ul class="{{ homeClass }}">
<li class="toctree-l1 {{ homeClass }}"><a class="{{ homeClass }} reference internal" href="/index.html">Home</a></li>
</ul>
{{ toctree() }}
That also works, except that I don’t want to force the docs to be accessed on the webroot of a web server – I want them to work from the file system too.
I can’t simply set the URL to “index.html” because that won’t work when you’re in a file within templating/.
Am I missing something obvious? There must be a way to get the homepage into the TOC, without breaking next links and with a dynamic path that works on a local file system, even from within subfolders.

Turns out the answer was hiding in plain sight on Sphinx’s TOC tree page:
The special entry name self stands for the document containing the toctree directive. This is useful if you want to generate a “sitemap” from the toctree.
Adding self to the TOC tree did the trick perfectly! And if you place it in a separate, hidden toctree directive, it won’t show up on the homepage’s table of contents either:
.. toctree::
:hidden:
self
.. toctree::
:titlesonly:
installation
templating/index

Is it possible for you to rename your Sphinx project's root toctree page, or, alternatively, the templating/index page? The master_doc variable lets you name the file in your project that contains the root toctree directive, and it doesn't have to be called index.rst... in our documentation project to get around a problem very like this, we have a template/index.html file and our root toctree page is actually called reference.rst.

Related

Generated links broken in Hugo/Blogdown when publishing to domain with subfolder

I am trying to use Hugo Xmin to create a static site. My site will be hosted in the gh-pages of my repository, therefore the final URL will look like:
https://myuser.github.io/myrepo/
Note At my current stage, I haven't modified any file in the example site that comes along with the theme, so basically the site I am deploying is the same exact in the original repository.
Problem I
In my config.toml I have set:
baseurl = "https://myuser.github.io/myrepo/"
Because with baseurl = "/" my links to the static CSS files in the <head> were not working and the site looked messy. By specifying the base url, CSS is loaded fine, but then top bar links are broken because this is what is generated:
<li>
About
</li>
Why is the generated URL looking so odd? The repository name is duplicated.
Problem II
The theme example site, in /content/_index.Rmarkdown has a few links at the end:
You can also visit the list page of a single section, e.g., [posts](/post/), or [notes](/note/). See the [About](/about/) page for more info.
Those do not work in my case because the URL that is generated is:
posts
Which will redirect the user to: https://myuser.github.io/post/ which is wrong. Basically this theme does not work when the hosting domain has a subfolder. Or am I missing anything here?
Troubleshooting
By investigating a bit I can see the theme defines those links like the following:
<ul class="menu">
{{ range .Site.Menus.main }}
<li>{{ .Name }}</li>
{{ end }}
</ul>
Function relURL is taking what comes after the hostname ans spitting it twice in the generated URL :(
The behavior of relURL seems to have changed since a certain version of Hugo (I don't know which). Anyway, you can remove the leading slashes in those menu items, e.g., change
[[menu.main]]
name = "About"
url = "/about/"
to
[[menu.main]]
name = "About"
url = "about/"

Disabling the top navigation bar (breadcrumbs) in Sphinx ReadTheDocs theme

I would like to disable the top navigation bar of the ReadTheDocs theme. For other theme, for instance classic it is just an option
html_theme = "classic"
html_theme_options = {
"showrelbartop": False
}
How I can modify the read the docs theme to disable this top navigation bar?
Edit:
After processing the source files with make html, I have to remove these lines in html files
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li>Docs »</li>
<li></li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
to obtain the expected result. Is it possible to obtain this result before compiling the sources with make html?
I assume you refer to the breadcrumbs (breadcrumbs.html). You can simply open the main template file - layout.html - and delete or comment out the following line:
{% include "breadcrumbs.html" %}
An old question, but I needed this as well. A simpler answer than the currently accepted one, which works for Sphinx==3.5.3 and sphinx-rtd-theme==0.5.2 is:
in docs/_templates/, create a file called breadcrumbs.html
in that file, write an html comment like <!-- Empty override file take the place of env/lib/python3.8/site-packages/sphinx_rtd_theme/breadcrumbs.html, which generates the top nav breadcrumbs. -->.
Rebuild, and you're done.

Different toctree title and document title, specified in document source only

When inserting a document in a toctree, the link displayed is the main title of the document. So when I do:
.. toctree::
materials/diffuse
materials/glossy
materials/specular
I get:
Materials
Diffuse material
Glossy material
Specular material
The word "material" is clearly redundant in the toctree, but is important in the document titles for good understanding.
RST allows me to write this:
.. toctree::
Diffuse<materials/diffuse>
Glossy<materials/glossy>
Specular<materials/specular>
But I do not like this, as renaming a document requires updating the index toctree, and link updating is why I went from MediaWiki to Sphinx. Also, this disables the use of :glob: and wildcards in the toctree
Question: Is there any way to specify a toctree title in the leaf document itself, for instance in "diffuse.rst", as a meta-property?
Thanks!
In concept, the css content property would be applicable here. Assuming your title is rendered with <h1> element, you'd want something like this:
.. raw:: html
<style>
h1:after {
content: " Material";
}
</style>
The <style>...</style> block is doing the work. Your docs keep the one word titles (Diffuse, Glossy, Specular) and the css adds " Material" on render. So your pages have the title you want, and your toctree doesn't have the redundant "Material" for each item. However, you'd want that block to appear only on the pages in your "Materials" section -- you can't just add it to the main css file, or it will affect all <h1> elements. Unfortunately, Sphinx doesn't have a mechanism for properly creating a <style> block in a page's <head> section, as far as I know. Therefore to use this css technique you have to accept the complete hack of dropping it in a .. raw:: html directive as shown, somewhere on your page. It won't really matter where you put it in your doc -- any location you choose will produce non-valid html, but will render what you want: your toctree labels will not contain "material"; your page titles will.

first time jekyll user - confused about 'page links'

So I finally got a blog going on gh-pages branch in a github repository using jekyll, and the theme lanyon. I love it. But something still bothers me.
On pages, the 'link' at the top of the article seems to default to a url I haven't really specified. This is what the top of my [YYYY-MM-DD-NAME].md files looks like...
---
layout: post
title: Page Name Here
---
Now, it renders okay, but Page Name Here shows up at the top, and is clickable, but I cannot figure out where to set the base url that it goes to. As it renders now, it does ...
[site root]/[page full filename]
but it should be ...
[site root]/[repo name]/[page full filename]
And I'm not clear on which variable in _config.yml I need to set to make this work right. Any suggestions?
As you're using Jekyll Bootstrap a post url tag is like this :
{{ post.title }}
You could also work with creating a url variable in _config.yml
url: www.website.com
and then creating a layout for posts with.
{{ post.title }}

Sphinx toctree either displays a TOC in sidebar with bulleted list in body, or nothing at all

I'm working with Sphinx (sphinx-1.2b1-py2.7). I want a TOC to appear in a sidebar. It seems binary: I can only get both a TOC in the sidebar and a bulleted list in the body of the text, or I get nothing (no TOC in the sidebar and no bulleted list).
When I use the toctree directive like this:
.. toctree::
:hidden:
Topic1
Topic2
Result: no TOC in the sidebar, no bulleted list of topics in body.
When I use the toctree directive like this:
.. toctree::
Topic1
Topic2
Result: TOC in the sidebar AND a bulleted list of topics in the body.
I just want the TOC in the sidebar. Other commands (maxdepth, includehidden) don't work. I've seen it done, but cannot get it to work. The conf.py looks fine, but no luck after several days of searching for an answer.
Thanks.
I had trouble with this too; I found the answer here.
The TOC is shown via a call to toctree() inside, e.g., a file called layout.html. In particular, it is shown in the sidebar via a snippet of code similar to the following, which resides in <div class="sidebar">:
{% block sidebartoc %}
<h3>{{ _('Table Of Contents') }}</h3>
{{ toctree() }}
{% endblock %}
Since I am using a theme, layout.html is within the theme directory inside the directory _themes; otherwise layout.html might be inside the directory _templates.
In newer versions of Sphinx, what is needed to display the TOC when :hidden: is used as in
.. toctree::
:hidden:
is to add the argument includehidden=True to the call to toctree(), as in
{% block sidebartoc %}
<h3>{{ _('Table Of Contents') }}</h3>
{{ toctree(includehidden=True) }}
{% endblock %}

Resources