Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the word "module" from Sphinx documentation

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.

like image 279
jessexknight Avatar asked May 16 '18 01:05

jessexknight


2 Answers

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.

like image 92
Roger Dahl Avatar answered Oct 15 '22 11:10

Roger Dahl


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!

like image 31
jessexknight Avatar answered Oct 15 '22 09:10

jessexknight