Files
sphinx/tests/test_highlighting.py
2008-11-30 20:29:34 +01:00

52 lines
1.1 KiB
Python

# -*- coding: utf-8 -*-
"""
test_highlighting
~~~~~~~~~~~~~~~~~
Test the Pygments highlighting bridge.
:copyright: 2008 by Georg Brandl.
:license: BSD.
"""
from util import *
from pygments.lexer import RegexLexer
from pygments.token import Text, Name
from pygments.formatters.html import HtmlFormatter
from sphinx.highlighting import PygmentsBridge
class MyLexer(RegexLexer):
name = 'testlexer'
tokens = {
'root': [
('a', Name),
('b', Text),
],
}
class MyFormatter(HtmlFormatter):
def format(self, tokensource, outfile):
outfile.write('test')
@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
def test_set_formatter():
PygmentsBridge.html_formatter = MyFormatter
try:
bridge = PygmentsBridge('html')
ret = bridge.highlight_block('foo', 'python')
assert ret == 'test'
finally:
PygmentsBridge.html_formatter = HtmlFormatter