From 516b151a6f5679eda635c183121d814fcb81dd7c Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sun, 4 Sep 2016 14:46:37 -0400 Subject: [PATCH] rest2html: Add support for highlight directive This adds support for the `highlight` directive, a Sphinx feature which allows one to set the default syntax highlighting language used for code blocks. --- lib/github/commands/rest2html | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/github/commands/rest2html b/lib/github/commands/rest2html index 7ecfe272..00479482 100755 --- a/lib/github/commands/rest2html +++ b/lib/github/commands/rest2html @@ -46,7 +46,7 @@ import codecs from docutils import nodes from docutils.parsers.rst import directives, roles -from docutils.parsers.rst.directives.body import CodeBlock +from docutils.parsers.rst.directives.body import CodeBlock, Directive from docutils.core import publish_parts from docutils.writers.html4css1 import Writer, HTMLTranslator @@ -64,6 +64,18 @@ SETTINGS = { 'field_name_limit': 50, } +default_highlight_language = None + +class HighlightDirective(Directive): + required_arguments = 1 + optional_arguments = 1 + option_spec = {} + def run(self): + """Track the default syntax highlighting language + """ + global default_highlight_language + default_highlight_language = self.arguments[0] + return [] class DoctestDirective(CodeBlock): """Render Sphinx 'doctest:: [group]' blocks as 'code:: python' @@ -102,6 +114,8 @@ class GitHubHTMLTranslator(HTMLTranslator): language = classes[1] del classes[:] self.body.append(self.starttag(node, 'pre', lang=language)) + elif default_highlight_language is not None: + self.body.append(self.starttag(node, 'pre', lang=default_highlight_language)) else: self.body.append(self.starttag(node, 'pre')) @@ -172,6 +186,9 @@ def main(): # Render source code in Sphinx doctest blocks directives.register_directive('doctest', DoctestDirective) + # Set default highlight language + directives.register_directive('highlight', HighlightDirective) + parts = publish_parts(text, writer=writer, settings_overrides=SETTINGS) if 'html_body' in parts: html = parts['html_body']