summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES2
-rw-r--r--doc/markup/code.rst5
-rw-r--r--sphinx/directives/code.py3
-rw-r--r--tests/root/includes.txt10
-rw-r--r--tests/root/tabs.inc5
-rw-r--r--tests/test_build_html.py16
6 files changed, 34 insertions, 7 deletions
diff --git a/CHANGES b/CHANGES
index c69942243..aaac057c3 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,8 @@
Release 1.0 (in development)
============================
+* Added ``tab-width`` option to ``literalinclude`` directive.
+
* The ``html_sidebars`` config value can now contain patterns as
keys, and the values can be lists that explicitly select which
sidebar templates should be rendered. That means that the builtin
diff --git a/doc/markup/code.rst b/doc/markup/code.rst
index 8c223297e..968711158 100644
--- a/doc/markup/code.rst
+++ b/doc/markup/code.rst
@@ -103,6 +103,9 @@ Includes
is absolute (starting with ``/``), it is relative to the top source
directory.
+ Tabs in the input are expanded if you give a ``tab-width`` option with the
+ desired tab width.
+
The directive also supports the ``linenos`` flag option to switch on line
numbers, and a ``language`` option to select a language different from the
current file's standard language. Example with options::
@@ -153,7 +156,7 @@ Includes
The ``pyobject``, ``lines``, ``start-after`` and ``end-before`` options,
as well as support for absolute filenames.
.. versionadded:: 1.0
- The ``prepend`` and ``append`` options.
+ The ``prepend`` and ``append`` options, as well as ``tab-width``.
.. rubric:: Footnotes
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py
index 1ae8b3a84..6535bdf56 100644
--- a/sphinx/directives/code.py
+++ b/sphinx/directives/code.py
@@ -81,6 +81,7 @@ class LiteralInclude(Directive):
final_argument_whitespace = False
option_spec = {
'linenos': directives.flag,
+ 'tab-width': int,
'language': directives.unchanged_required,
'encoding': directives.encoding,
'pyobject': directives.unchanged_required,
@@ -174,6 +175,8 @@ class LiteralInclude(Directive):
lines.append(append + '\n')
text = ''.join(lines)
+ if self.options.get('tab-width'):
+ text = text.expandtabs(self.options['tab-width'])
retnode = nodes.literal_block(text, text, source=fn)
retnode.line = 1
if self.options.get('language', ''):
diff --git a/tests/root/includes.txt b/tests/root/includes.txt
index d6da137f0..cb1708403 100644
--- a/tests/root/includes.txt
+++ b/tests/root/includes.txt
@@ -49,6 +49,16 @@ Literalinclude options
:prepend: START CODE
:append: END CODE
+.. cssclass:: inc-tab3
+.. literalinclude:: tabs.inc
+ :tab-width: 3
+ :language: text
+
+.. cssclass:: inc-tab8
+.. literalinclude:: tabs.inc
+ :tab-width: 8
+ :language: python
+
Test if dedenting before parsing works.
.. highlight:: python
diff --git a/tests/root/tabs.inc b/tests/root/tabs.inc
new file mode 100644
index 000000000..20b51820d
--- /dev/null
+++ b/tests/root/tabs.inc
@@ -0,0 +1,5 @@
+Tabs include file test
+----------------------
+
+The next line has a tab:
+-| |-
diff --git a/tests/test_build_html.py b/tests/test_build_html.py
index e8189e67b..25f733a48 100644
--- a/tests/test_build_html.py
+++ b/tests/test_build_html.py
@@ -119,18 +119,22 @@ HTML_XPATH = {
if pygments:
HTML_XPATH['includes.html'].update({
".//pre/span[@class='s']": u'üöä',
- ".//div[@class='inc-pyobj1 highlight-text']/div/pre":
+ ".//div[@class='inc-pyobj1 highlight-text']//pre":
r'^class Foo:\n pass\n\s*$',
- ".//div[@class='inc-pyobj2 highlight-text']/div/pre":
+ ".//div[@class='inc-pyobj2 highlight-text']//pre":
r'^ def baz\(\):\n pass\n\s*$',
- ".//div[@class='inc-lines highlight-text']/div/pre":
+ ".//div[@class='inc-lines highlight-text']//pre":
r'^class Foo:\n pass\nclass Bar:\n$',
- ".//div[@class='inc-startend highlight-text']/div/pre":
+ ".//div[@class='inc-startend highlight-text']//pre":
ur'^foo = u"Including Unicode characters: üöä"\n$',
- ".//div[@class='inc-preappend highlight-text']/div/pre":
+ ".//div[@class='inc-preappend highlight-text']//pre":
r'(?m)^START CODE$',
- ".//div[@class='inc-pyobj-dedent highlight-python']/div/pre/span":
+ ".//div[@class='inc-pyobj-dedent highlight-python']//span":
r'def',
+ ".//div[@class='inc-tab3 highlight-text']//pre":
+ r'-| |-',
+ ".//div[@class='inc-tab8 highlight-python']//pre":
+ r'-| |-',
})
HTML_XPATH['subdir/includes.html'].update({
".//pre/span": 'line 1',