Skip to content

Commit

Permalink
rest2html: Add support for highlight directive
Browse files Browse the repository at this point in the history
This adds support for the `highlight` directive, a Sphinx feature which
allows one to set the default syntax highlighting language used for code
blocks.
  • Loading branch information
bgamari committed Sep 4, 2016
1 parent cf74e84 commit c36fc72
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/github/commands/rest2html
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
Expand Down Expand Up @@ -102,9 +114,15 @@ 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'))

def visit_highlight(self, node):
print '========================================'
print node

# always wrap two-backtick rst inline literals in <code>, not <tt>
# this also avoids the generation of superfluous <span> tags
def visit_literal(self, node):
Expand Down Expand Up @@ -172,6 +190,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']
Expand Down

0 comments on commit c36fc72

Please sign in to comment.