summaryrefslogtreecommitdiff
path: root/sphinx/directives/code.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2021-01-12 23:34:29 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2021-01-13 01:10:28 +0900
commitfddc42847f9babadb465763391b40ae0bbcfdd9e (patch)
tree3bf5da8fa741aadedc91ecdc4020e3100e39de23 /sphinx/directives/code.py
parente314789f4fc8cf12717c2abfc531b6b195c9c50d (diff)
downloadsphinx-git-fddc42847f9babadb465763391b40ae0bbcfdd9e.tar.gz
Fix #2030: automatic dedent support in code-block directive
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r--sphinx/directives/code.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py
index 10011a324..e01b8f9ec 100644
--- a/sphinx/directives/code.py
+++ b/sphinx/directives/code.py
@@ -7,6 +7,7 @@
"""
import sys
+import textwrap
import warnings
from difflib import unified_diff
from typing import Any, Dict, List, Tuple
@@ -19,6 +20,7 @@ from docutils.statemachine import StringList
from sphinx import addnodes
from sphinx.config import Config
from sphinx.deprecation import RemovedInSphinx40Warning
+from sphinx.directives import optional_int
from sphinx.locale import __
from sphinx.util import logging, parselinenos
from sphinx.util.docutils import SphinxDirective
@@ -68,7 +70,7 @@ class HighlightLang(Highlight):
def dedent_lines(lines: List[str], dedent: int, location: Tuple[str, int] = None) -> List[str]:
if not dedent:
- return lines
+ return textwrap.dedent(''.join(lines)).splitlines(True)
if any(s[:dedent].strip() for s in lines):
logger.warning(__('non-whitespace stripped by dedent'), location=location)
@@ -117,7 +119,7 @@ class CodeBlock(SphinxDirective):
option_spec = {
'force': directives.flag,
'linenos': directives.flag,
- 'dedent': int,
+ 'dedent': optional_int,
'lineno-start': int,
'emphasize-lines': directives.unchanged_required,
'caption': directives.unchanged_required,
@@ -391,7 +393,7 @@ class LiteralInclude(SphinxDirective):
optional_arguments = 0
final_argument_whitespace = True
option_spec = {
- 'dedent': int,
+ 'dedent': optional_int,
'linenos': directives.flag,
'lineno-start': int,
'lineno-match': directives.flag,