Merge pull request #1666 from EricFromCanada/highlight_options

Add highlight_options configuration value
This commit is contained in:
Georg Brandl 2015-01-06 17:09:29 +01:00
commit 81313e903b
7 changed files with 38 additions and 8 deletions

View File

@ -9,6 +9,8 @@ Incompatible changes
Features added Features added
-------------- --------------
* Added ``highlight_options`` configuration value.
Bugs fixed Bugs fixed
---------- ----------

View File

@ -303,10 +303,19 @@ Project information
.. versionadded:: 0.5 .. versionadded:: 0.5
.. confval:: highlight_options
A dictionary of options that modify how the lexer specified by
:confval:`highlight_language` generates highlighted source code. These are
lexer-specific; for the options understood by each, see the
`Pygments documentation <http://pygments.org/docs/lexers/>`_.
.. versionadded:: 1.3
.. confval:: pygments_style .. 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. If not set,
style is selected by the theme for HTML output, and ``'sphinx'`` otherwise. either the theme's default style or ``'sphinx'`` is selected for HTML output.
.. versionchanged:: 0.3 .. versionchanged:: 0.3
If the value is a fully-qualified name of a custom Pygments style class, If the value is a fully-qualified name of a custom Pygments style class,

View File

@ -61,6 +61,7 @@ class Config(object):
show_authors = (False, 'env'), show_authors = (False, 'env'),
pygments_style = (None, 'html'), pygments_style = (None, 'html'),
highlight_language = ('python', 'env'), highlight_language = ('python', 'env'),
highlight_options = ({}, 'env'),
templates_path = ([], 'html'), templates_path = ([], 'html'),
template_bridge = (None, 'html'), template_bridge = (None, 'html'),
keep_warnings = (False, 'env'), keep_warnings = (False, 'env'),

View File

@ -134,7 +134,7 @@ class PygmentsBridge(object):
else: else:
return True 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): if not isinstance(source, text_type):
source = source.decode() source = source.decode()
@ -164,7 +164,7 @@ class PygmentsBridge(object):
lexer = lexers[lang] lexer = lexers[lang]
else: else:
try: try:
lexer = lexers[lang] = get_lexer_by_name(lang) lexer = lexers[lang] = get_lexer_by_name(lang, **opts)
except ClassNotFound: except ClassNotFound:
if warn: if warn:
warn('Pygments lexer name %r is not known' % lang) warn('Pygments lexer name %r is not known' % lang)

View File

@ -70,7 +70,9 @@ class HTMLTranslator(BaseTranslator):
self.highlighter = builder.highlighter self.highlighter = builder.highlighter
self.no_smarty = 0 self.no_smarty = 0
self.builder = builder 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.highlightlinenothreshold = sys.maxsize
self.protect_literal_text = 0 self.protect_literal_text = 0
self.permalink_text = builder.config.html_add_permalinks self.permalink_text = builder.config.html_add_permalinks
@ -301,10 +303,15 @@ class HTMLTranslator(BaseTranslator):
highlight_args['force'] = True highlight_args['force'] = True
if 'linenos' in node: if 'linenos' in node:
linenos = node['linenos'] linenos = node['linenos']
if lang is self.highlightlang_base:
# only pass highlighter options for original language
opts = self.highlightopts
else:
opts = {}
def warner(msg): def warner(msg):
self.builder.warn(msg, (self.builder.current_docname, node.line)) self.builder.warn(msg, (self.builder.current_docname, node.line))
highlighted = self.highlighter.highlight_block( highlighted = self.highlighter.highlight_block(
node.rawsource, lang, warn=warner, linenos=linenos, node.rawsource, lang, opts=opts, warn=warner, linenos=linenos,
**highlight_args) **highlight_args)
starttag = self.starttag(node, 'div', suffix='', starttag = self.starttag(node, 'div', suffix='',
CLASS='highlight-%s' % lang) CLASS='highlight-%s' % lang)

View File

@ -1411,10 +1411,15 @@ class LaTeXTranslator(nodes.NodeVisitor):
highlight_args['force'] = True highlight_args['force'] = True
if 'linenos' in node: if 'linenos' in node:
linenos = node['linenos'] 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): def warner(msg):
self.builder.warn(msg, (self.curfilestack[-1], node.line)) self.builder.warn(msg, (self.curfilestack[-1], node.line))
hlcode = self.highlighter.highlight_block(code, lang, warn=warner, hlcode = self.highlighter.highlight_block(code, lang, opts=opts,
linenos=linenos, **highlight_args) warn=warner, linenos=linenos, **highlight_args)
# workaround for Unicode issue # workaround for Unicode issue
hlcode = hlcode.replace(u'', u'@texteuro[]') hlcode = hlcode.replace(u'', u'@texteuro[]')
# must use original Verbatim environment and "tabular" environment # must use original Verbatim environment and "tabular" environment

View File

@ -62,6 +62,12 @@ def test_detect_interactive():
assert ret.startswith("<div class=\"highlight\">") assert ret.startswith("<div class=\"highlight\">")
def test_lexer_options():
bridge = PygmentsBridge('html')
ret = bridge.highlight_block('//comment', 'php', opts={'startinline' : True})
assert '<span class="c1">//comment</span>' in ret
def test_set_formatter(): def test_set_formatter():
PygmentsBridge.html_formatter = MyFormatter PygmentsBridge.html_formatter = MyFormatter
try: try: