Sphinx (documentation tool): set tab width in output
-
18-09-2019 - |
Question
How do you set tab width in HTML output of Sphinx code snippets highlighted by Pygments? By default it is the annoying 8, but I want 4. Did not find a word about this setting in Sphinx conf.py.
Solution
You will need to write a Sphinx Extension. Add your custom Lexer, and apply it with VisibleWhitespaceFilter.
OTHER TIPS
Add something like this to your conf.py:
import re
def process_docstring(app, what, name, obj, options, lines):
spaces_pat = re.compile(r"( {8})")
ll = []
for l in lines:
ll.append(spaces_pat.sub(" ",l))
lines[:] = ll
def setup(app):
app.connect('autodoc-process-docstring', process_docstring)
See also Sphinx Docstring preprocessing documentation.
I asked the same question on sphinx-dev group and it turns out it's a problem with Docutils which is used by Sphinx. Docutils replace all tabs with 8 spaces and currently there is no way to change that value from Sphinx.
http://groups.google.com/group/sphinx-dev/browse_thread/thread/35b8071ffe9a8feb
The only feasible solution seems to be to follow the advice from S.Lott and John Paulett in comments to my question -- use spaces instead of tabs.