sphinx / tests /

# -*- coding: utf-8 -*-

    Test the Pygments highlighting bridge.

    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.

from util import *

    import pygments
except ImportError:
    from nose.plugins.skip import SkipTest
    raise SkipTest('pygments not available')

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):

class ComplainOnUnhighlighted(PygmentsBridge):
    def unhighlighted(self, source):
        raise AssertionError("should highlight %r" % source)

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_detect_interactive():
    bridge = ComplainOnUnhighlighted('html')
    blocks = [
        >>> testing()
    for block in blocks:
        ret = bridge.highlight_block(block.lstrip(), 'python')
        assert ret.startswith("<div class=\"highlight\">")

def test_set_formatter():
    PygmentsBridge.html_formatter = MyFormatter
        bridge = PygmentsBridge('html')
        ret = bridge.highlight_block('foo', 'python')
        assert ret == 'test'
        PygmentsBridge.html_formatter = HtmlFormatter
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.