diff --git a/CHANGES b/CHANGES index d84f2ff94..1909ffddb 100644 --- a/CHANGES +++ b/CHANGES @@ -9,6 +9,8 @@ Incompatible changes Features added -------------- +* Added ``highlight_options`` configuration value. + Bugs fixed ---------- diff --git a/doc/config.rst b/doc/config.rst index 5b72f2722..94e33f456 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -303,9 +303,17 @@ Project information .. versionadded:: 0.5 +.. confval:: highlight_options + + A dictionary of options that modify the behavior of the lexer for the + selected language. These are lexer-specific. For the options understood by + each, see the `Pygments documentation `_. + + .. versionadded:: 1.3 + .. confval:: pygments_style - The style name to use for Pygments highlighting of source code. The default + The style name to use for Pygments highlighting of source code. The given style is selected by the theme for HTML output, and ``'sphinx'`` otherwise. .. versionchanged:: 0.3 diff --git a/sphinx/config.py b/sphinx/config.py index be9b66f72..145e60675 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -61,6 +61,7 @@ class Config(object): show_authors = (False, 'env'), pygments_style = (None, 'html'), highlight_language = ('python', 'env'), + highlight_options = ({}, 'env'), templates_path = ([], 'html'), template_bridge = (None, 'html'), keep_warnings = (False, 'env'), diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index b6062e83e..63a46fbd7 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -134,7 +134,7 @@ class PygmentsBridge(object): else: return True - def highlight_block(self, source, lang, warn=None, force=False, **kwargs): + def highlight_block(self, source, lang, opts=None, warn=None, force=False, **kwargs): if not isinstance(source, text_type): source = source.decode() @@ -164,7 +164,7 @@ class PygmentsBridge(object): lexer = lexers[lang] else: try: - lexer = lexers[lang] = get_lexer_by_name(lang) + lexer = lexers[lang] = get_lexer_by_name(lang, **opts) except ClassNotFound: if warn: warn('Pygments lexer name %r is not known' % lang) diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index ba3129f79..c63b27698 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -70,7 +70,9 @@ class HTMLTranslator(BaseTranslator): self.highlighter = builder.highlighter self.no_smarty = 0 self.builder = builder - self.highlightlang = builder.config.highlight_language + self.highlightlang = self.highlightlang_base = \ + builder.config.highlight_language + self.highlightopts = builder.config.highlight_options self.highlightlinenothreshold = sys.maxsize self.protect_literal_text = 0 self.permalink_text = builder.config.html_add_permalinks @@ -301,10 +303,15 @@ class HTMLTranslator(BaseTranslator): highlight_args['force'] = True if 'linenos' in node: linenos = node['linenos'] + if lang is self.highlightlang_base: + # only pass highlighter options for original language + opts = self.highlightopts + else: + opts = {} def warner(msg): self.builder.warn(msg, (self.builder.current_docname, node.line)) highlighted = self.highlighter.highlight_block( - node.rawsource, lang, warn=warner, linenos=linenos, + node.rawsource, lang, opts=opts, warn=warner, linenos=linenos, **highlight_args) starttag = self.starttag(node, 'div', suffix='', CLASS='highlight-%s' % lang) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 27e6d5f2c..c96b4df2f 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1378,10 +1378,15 @@ class LaTeXTranslator(nodes.NodeVisitor): highlight_args['force'] = True if 'linenos' in node: linenos = node['linenos'] + if lang is self.hlsettingstack[0][0]: + # only pass highlighter options for original language + opts = self.builder.config.highlight_options + else: + opts = {} def warner(msg): self.builder.warn(msg, (self.curfilestack[-1], node.line)) - hlcode = self.highlighter.highlight_block(code, lang, warn=warner, - linenos=linenos, **highlight_args) + hlcode = self.highlighter.highlight_block(code, lang, opts=opts, + warn=warner, linenos=linenos, **highlight_args) # workaround for Unicode issue hlcode = hlcode.replace(u'€', u'@texteuro[]') # must use original Verbatim environment and "tabular" environment diff --git a/tests/test_highlighting.py b/tests/test_highlighting.py index c3ea03873..02b928916 100644 --- a/tests/test_highlighting.py +++ b/tests/test_highlighting.py @@ -62,6 +62,12 @@ def test_detect_interactive(): assert ret.startswith("
") +def test_lexer_options(): + bridge = PygmentsBridge('html') + ret = bridge.highlight_block('//comment', 'php', opts={'startinline' : True}) + assert '//comment' in ret + + def test_set_formatter(): PygmentsBridge.html_formatter = MyFormatter try: