Sphinx: code-block directive should have a tab-width option

Created on 17 Sep 2018  路  6Comments  路  Source: sphinx-doc/sphinx

Subject: code-block doesn't allow tab-expansion size specification

Problem

  • The includeliteral directive provides the tab-width option, which code-block should ideally support as well.

Procedure to reproduce the problem

N/A

Error logs / results

Every tab is expanded to an un-configurable 8 spaces

Expected results

Ideally, I should be able to specify a tab expansion (or possibly no expansion?) length. My personal preference being '4' in most cases.

Reproducible project / your project

.. code-block:: json

    {"I got indented too far"}

Environment info

  • OS: Linux Mint 18
  • Python version: 3.6.5
  • Sphinx version: 1.7.9
  • build target: HTML
markup question

All 6 comments

I think the expanding tabs inside directive is strongly related with parsing reST.
So I have to say it is hard to implement.
How about using tab_width in docutils.conf?
http://docutils.sourceforge.net/docs/user/config.html#tab-width

That looks like it would cover the situation that led me to make this issue, but I do maintain that the use case for setting tab-width on a per-code-block basis exists. For example, if formatting some code-blocks that contain HTML and some code-blocks that contain Javascript, it may be desirable to use 2-space indentation for HTML and 4-space indentation for Javascript (which, incidentally, is actually how my text editor is set up to expand tabs for those syntaxes). Since the tab-width option exists on includeliteral blocks, I would hazard a guess that the issue of when in the parsing process to introduce expansion of tabs in literal code has been addressed, at least somewhat.

Also, note that although sphinx documentation says it will use a docutils.conf in the configuration directory and Docutils reports that it supports the tab_width option in a parser section, placing the following file in the same directory as my conf.py (and naming it docutils.conf) doesn't appear to affect tab width:

[restructuredtext parser]
tab_width: 4

Sorry for late. I try to reproduce the problem. But, on my local, docutils.conf which you wrote above works fine to me with docutils-0.14 and Sphinx-2.1.0.

Since the tab-width option exists on includeliteral blocks, I would hazard a guess that the issue of when in the parsing process to introduce expansion of tabs in literal code has been addressed, at least somewhat.

They are different program. The .rst file is parsed by docutils, and each directives are processed by each directive implementation. Espacially includeliteral directive is implemented by Sphinx team.
So please use includeliteral for each program.

I'm closing this because not reproduced.
Please feel free to reopen if you're still in trouble.
It would be very helpful if you make a reproducible example.

Thanks,

So I think the problem is just with my outdated Sphinx version (v 1.8.5 b/c we build our project on ReadtheDocs). When I build locally with 2.1.2 it seems to be working.

Was this page helpful?
0 / 5 - 0 ratings