sphinx/tests/test_highlighting.py
EricFromCanada a63e4945ea Add highlight_options configuration value
This changeset adds the `highlight_options` configuration value, which
specifies a dict of key-value pairs to pass to the Pygments highlighter
specified with `highlight_language`. For example, specifying
`highlight_options = {'funcnamehighlighting' : False}` for the 'php'
lexer will disable function name highlighting.
Both the HTML and LaTeX writers have been updated to include the
options when calling `highlight_block()`, as long as the currently
selected language matches what was set with `highlight_language`, i.e.
not when modified by `highlight::` or `code-block::` directives.
2015-01-05 12:58:43 -05:00

89 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
"""
test_highlighting
~~~~~~~~~~~~~~~~~
Test the Pygments highlighting bridge.
:copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from pygments.lexer import RegexLexer
from pygments.token import Text, Name
from pygments.formatters.html import HtmlFormatter
from sphinx.highlighting import PygmentsBridge
from util import with_app
class MyLexer(RegexLexer):
name = 'testlexer'
tokens = {
'root': [
('a', Name),
('b', Text),
],
}
class MyFormatter(HtmlFormatter):
def format(self, tokensource, outfile):
for tok in tokensource:
outfile.write(tok[1])
class ComplainOnUnhighlighted(PygmentsBridge):
def unhighlighted(self, source):
raise AssertionError("should highlight %r" % source)
@with_app()
def test_add_lexer(app, status, warning):
app.add_lexer('test', MyLexer())
bridge = PygmentsBridge('html')
ret = bridge.highlight_block('ab', 'test')
assert '<span class="n">a</span>b' in ret
def test_detect_interactive():
bridge = ComplainOnUnhighlighted('html')
blocks = [
"""
>>> testing()
True
""",
]
for block in blocks:
ret = bridge.highlight_block(block.lstrip(), 'python')
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():
PygmentsBridge.html_formatter = MyFormatter
try:
bridge = PygmentsBridge('html')
ret = bridge.highlight_block('foo\n', 'python')
assert ret == 'foo\n'
finally:
PygmentsBridge.html_formatter = HtmlFormatter
def test_trim_doctest_flags():
PygmentsBridge.html_formatter = MyFormatter
try:
bridge = PygmentsBridge('html', trim_doctest_flags=True)
ret = bridge.highlight_block('>>> 1+2 # doctest: SKIP\n3\n', 'pycon')
assert ret == '>>> 1+2 \n3\n'
finally:
PygmentsBridge.html_formatter = HtmlFormatter