diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-03-10 12:23:15 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2019-03-10 12:23:15 +0900 |
commit | a37e508b3fc7b28cf10bbd51b766ce62fc390c4c (patch) | |
tree | a75e561df3cd8b8fb96923f74ddd62e12bbc9f57 /sphinx/directives/patches.py | |
parent | 2606002bb4d0470181460740b608d13d03b84bfe (diff) | |
parent | 63c05f2b6d7c5408978bafe8238913d4dd6637b2 (diff) | |
download | sphinx-git-a37e508b3fc7b28cf10bbd51b766ce62fc390c4c.tar.gz |
Merge branch '2.0'
Diffstat (limited to 'sphinx/directives/patches.py')
-rw-r--r-- | sphinx/directives/patches.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/sphinx/directives/patches.py b/sphinx/directives/patches.py index 40af03e80..ee8014aa6 100644 --- a/sphinx/directives/patches.py +++ b/sphinx/directives/patches.py @@ -14,6 +14,7 @@ from docutils.parsers.rst import directives from docutils.parsers.rst.directives import images, html, tables from sphinx import addnodes +from sphinx.directives import optional_int from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import set_source_info @@ -110,6 +111,52 @@ class ListTable(tables.ListTable): return title, message +class Code(SphinxDirective): + """Parse and mark up content of a code block. + + This is compatible with docutils' :rst:dir:`code` directive. + """ + optional_arguments = 1 + option_spec = { + 'class': directives.class_option, + 'name': directives.unchanged, + 'number-lines': optional_int, + } + has_content = True + + def run(self): + # type: () -> List[nodes.Node] + self.assert_has_content() + + code = '\n'.join(self.content) + node = nodes.literal_block(code, code, + classes=self.options.get('classes', []), + highlight_args={}) + self.add_name(node) + set_source_info(self, node) + + if self.arguments: + # highlight language specified + node['language'] = self.arguments[0] + node['force_highlighting'] = True + else: + # no highlight language specified. Then this directive refers the current + # highlight setting via ``highlight`` directive or ``highlight_language`` + # configuration. + node['language'] = self.env.temp_data.get('highlight_language', + self.config.highlight_language) + node['force_highlighting'] = False + + if 'number-lines' in self.options: + node['linenos'] = True + + # if number given, treat as lineno-start. + if self.options['number-lines']: + node['highlight_args']['linenostart'] = self.options['number-lines'] + + return [node] + + class MathDirective(SphinxDirective): has_content = True required_arguments = 0 @@ -174,6 +221,7 @@ def setup(app): directives.register_directive('table', RSTTable) directives.register_directive('csv-table', CSVTable) directives.register_directive('list-table', ListTable) + directives.register_directive('code', Code) directives.register_directive('math', MathDirective) return { |