2008-11-30 12:58:29 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
test_highlighting
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test the Pygments highlighting bridge.
|
|
|
|
|
2014-03-01 01:18:16 -06:00
|
|
|
:copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
|
2008-12-27 05:19:17 -06:00
|
|
|
:license: BSD, see LICENSE for details.
|
2008-11-30 12:58:29 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
from pygments.lexer import RegexLexer
|
|
|
|
from pygments.token import Text, Name
|
2008-11-30 13:29:34 -06:00
|
|
|
from pygments.formatters.html import HtmlFormatter
|
2008-11-30 12:58:29 -06:00
|
|
|
|
|
|
|
from sphinx.highlighting import PygmentsBridge
|
|
|
|
|
2013-04-01 04:39:32 -05:00
|
|
|
from util import with_app, SkipTest
|
|
|
|
|
|
|
|
try:
|
|
|
|
import pygments
|
|
|
|
except ImportError:
|
|
|
|
raise SkipTest('pygments not available')
|
|
|
|
|
2008-11-30 12:58:29 -06:00
|
|
|
|
|
|
|
class MyLexer(RegexLexer):
|
|
|
|
name = 'testlexer'
|
|
|
|
|
|
|
|
tokens = {
|
|
|
|
'root': [
|
|
|
|
('a', Name),
|
|
|
|
('b', Text),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2009-01-01 10:33:41 -06:00
|
|
|
|
2008-11-30 13:29:34 -06:00
|
|
|
class MyFormatter(HtmlFormatter):
|
|
|
|
def format(self, tokensource, outfile):
|
2009-06-16 14:53:53 -05:00
|
|
|
for tok in tokensource:
|
|
|
|
outfile.write(tok[1])
|
2008-11-30 13:29:34 -06:00
|
|
|
|
2008-11-30 12:58:29 -06:00
|
|
|
|
2009-02-06 15:18:16 -06:00
|
|
|
class ComplainOnUnhighlighted(PygmentsBridge):
|
|
|
|
def unhighlighted(self, source):
|
|
|
|
raise AssertionError("should highlight %r" % source)
|
|
|
|
|
|
|
|
|
2008-11-30 12:58:29 -06:00
|
|
|
@with_app()
|
|
|
|
def test_add_lexer(app):
|
|
|
|
app.add_lexer('test', MyLexer())
|
|
|
|
|
|
|
|
bridge = PygmentsBridge('html')
|
|
|
|
ret = bridge.highlight_block('ab', 'test')
|
|
|
|
assert '<span class="n">a</span>b' in ret
|
2008-11-30 13:29:34 -06:00
|
|
|
|
2009-01-01 10:33:41 -06:00
|
|
|
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\">")
|
|
|
|
|
2008-11-30 13:29:34 -06:00
|
|
|
def test_set_formatter():
|
|
|
|
PygmentsBridge.html_formatter = MyFormatter
|
|
|
|
try:
|
|
|
|
bridge = PygmentsBridge('html')
|
2009-06-16 14:53:53 -05:00
|
|
|
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'
|
2008-11-30 13:29:34 -06:00
|
|
|
finally:
|
|
|
|
PygmentsBridge.html_formatter = HtmlFormatter
|