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.
A:
You will need to write a Sphinx Extension. Add your custom Lexer, and apply it with VisibleWhitespaceFilter.
iElectric
2009-11-08 15:49:49
I updated my Sphinx to 0.6.3 and Pygments to 1.1.1 (pygmentize -V).Touched all my files, then added this to conf.py:def setup(app): from sphinx.highlighting import lexers from pygments.lexers.compiled import JavaLexer from pygments.filters import VisibleWhitespaceFilter myLexer = JavaLexer() myLexer.add_filter(VisibleWhitespaceFilter(spaces='!')); app.add_lexer('java', myLexer);This replaced all tabs in my Java code with eight "!" and all spaceswith "!" as expected.When I tried option tabs='!', nothing changed.When I tried option tabsize=4, nothing changed.
zilupe
2009-11-09 15:13:01
It seems like tabs have been replaced with spaces somewhere earlier inthe chain.So, I tried to add this filter to other lexers (rest, none) [withlexers['rest'].add_filter(...)], but none of that had any effect ontabs or tabsize.
zilupe
2009-11-09 15:13:39
A:
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%5Fthread/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.
zilupe
2009-11-30 09:51:16