13

Using Sphinx for documenting my Python project. I want to remove the word "module" which follows the name of each python file (in the navbar, TOC, the page title, etc).

e.g. Details:

The project is composed of 2 files utils.py and main.py.

In my index.rst file, I use:

.. toctree::
   :maxdepth: 2

   utils
   main

to import both files as "modules". From the docs/ folder, I then call:

sphinx-apidoc -f -o ./source/ .. 
make html

to generate the static site. In the site, the word "module" follows every file name, and I would like to remove it.

mzjn
  • 48,958
  • 13
  • 128
  • 248
jessexknight
  • 756
  • 7
  • 20
  • After `sphinx-apidoc -f -o ./source/ ..`, what is in your `utils.rst` and `main.rst`? I think part of the problem is that [sphinx-apidoc is designed for packages](http://www.sphinx-doc.org/en/stable/man/sphinx-apidoc.html#description), not modules: "sphinx-apidoc is a tool for automatic generation of Sphinx sources that, using the autodoc extension, **document a whole package** in the style of other automatic API documentation tools." – Steve Piercy May 16 '18 at 04:09
  • 1
    Possible duplicate of https://stackoverflow.com/q/29385564/407651 – mzjn May 16 '18 at 04:41

2 Answers2

15

Sphinx 2.2 adds templating for the reST files generated by sphinx-apidoc.

Use the --templatedir option to set the path to a dir containing module.rst_t, package.rst_t and toc.rst_t files. The files can be created from the corresponding files in site-packages/sphinx/templates/apidoc.

Then, in package.rst_treplace

{{- [submodule, "module"] | join(" ") | e | heading(2) }}

with

{{- submodule | e | heading(2) }}

Repeat for module.rst_t.

Roger Dahl
  • 15,132
  • 8
  • 62
  • 82
  • 1
    According to https://www.sphinx-doc.org/en/master/changes.html#id9, the `--templatedir` option was added already in Sphinx 2.2.0. – mzjn Aug 22 '19 at 06:00
  • @mzjn True, but as far as I could tell, that version wasn't released (not available via PyPI / pip). – Roger Dahl Aug 27 '19 at 16:29
  • 1
    Strange that it does not work for you. I was able to install it via pip. And https://pypi.org/project/Sphinx/ says that 2.2.0 is the latest release. – mzjn Aug 27 '19 at 16:49
  • 1
    Ah -- it was released on Aug 18. I posted this answer on the 16th :) – Roger Dahl Aug 27 '19 at 16:57
  • 4
    I can't find the default files in `site-packages/sphinx/templates/apidoc` on my computer, but you can find them [here](https://github.com/sphinx-doc/sphinx/tree/master/sphinx/templates/apidoc) – jessexknight Sep 22 '19 at 14:57
  • I expected the `toc.rst_t` template to be used to generate `modules.rst` (based on the packaged templates) but this does not seem to work. – OrangeDog Nov 28 '22 at 15:44
2

One possible solution uses JS to find & replace the word "module" after the page loads:

Create a file source/_templates/layout.html with the following content:

{% extends "!layout.html" %}
{% block extrahead %}
<script type="text/javascript">
  window.onload = function() {
    document.body.innerHTML = document.body.innerHTML.replace(/ module/g, '');
  }
</script>
{% endblock %}

Make sure that conf.py has templates_path = ['_templates'] set, then Sphinx will append the script to the <head> of all documentation pages, and voila!

jessexknight
  • 756
  • 7
  • 20