diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-02-04 01:50:16 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-02-04 01:50:44 +0900 |
commit | 6ca669295c2692caef2838eb78e15e6f0faf4c34 (patch) | |
tree | 86d8dc375b48de29c8dc612e1f5a4839ebbcc553 /sphinx/directives/code.py | |
parent | 6fd191d52ebe1a8940934875d5e884e6c93c7522 (diff) | |
download | sphinx-git-6ca669295c2692caef2838eb78e15e6f0faf4c34.tar.gz |
Close #1851: Allow to omit an argument for code-block directive
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r-- | sphinx/directives/code.py | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 106d400b9..9ac704c55 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -47,9 +47,11 @@ class Highlight(SphinxDirective): def run(self): # type: () -> List[nodes.Node] + language = self.arguments[0].strip() linenothreshold = self.options.get('linenothreshold', sys.maxsize) - return [addnodes.highlightlang(lang=self.arguments[0].strip(), - linenothreshold=linenothreshold)] + + self.env.temp_data['highlight_language'] = language + return [addnodes.highlightlang(lang=language, linenothreshold=linenothreshold)] class HighlightLang(Highlight): @@ -110,8 +112,8 @@ class CodeBlock(SphinxDirective): """ has_content = True - required_arguments = 1 - optional_arguments = 0 + required_arguments = 0 + optional_arguments = 1 final_argument_whitespace = False option_spec = { 'linenos': directives.flag, @@ -152,10 +154,20 @@ class CodeBlock(SphinxDirective): code = '\n'.join(lines) literal = nodes.literal_block(code, code) # type: nodes.Element - literal['language'] = self.arguments[0] literal['linenos'] = 'linenos' in self.options or \ 'lineno-start' in self.options literal['classes'] += self.options.get('class', []) + if self.arguments: + # highlight language specified + literal['language'] = self.arguments[0] + literal['force_highlighting'] = True + else: + # no highlight language specified. Then this directive refers the current + # highlight setting via ``highlight`` directive or ``highlight_language`` + # configuration. + literal['language'] = self.env.temp_data.get('highlight_language', + self.config.highlight_language) + literal['force_highlighting'] = False extra_args = literal['highlight_args'] = {} if hl_lines is not None: extra_args['hl_lines'] = hl_lines |