""" test_highlighting ~~~~~~~~~~~~~~~~~ Test the Pygments highlighting bridge. :copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. """ from unittest import mock from pygments.formatters.html import HtmlFormatter from pygments.lexer import RegexLexer from pygments.token import Text, Name from sphinx.highlighting import PygmentsBridge 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) def test_add_lexer(app, status, warning): app.add_lexer('test', MyLexer) bridge = PygmentsBridge('html') ret = bridge.highlight_block('ab', 'test') assert 'ab' 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("
print '
'"Hello sphinx world"\n
reST ``like`` text\n
print '
'"Hello sphinx world"\n