⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pygments.py

📁 一款基于web的项目管理、bug跟踪系统。提供了与svn集成的操作界面、问题跟踪
💻 PY
字号:
# -*- coding: utf-8 -*-## Copyright (C) 2006-2008 Edgewall Software# All rights reserved.## This software is licensed as described in the file COPYING, which# you should have received as part of this distribution. The terms# are also available at http://trac.edgewall.org/wiki/TracLicense.## This software consists of voluntary contributions made by many# individuals. For the exact contribution history, see the revision# history and logs, available at http://trac.edgewall.org/log/.import osimport reimport unittestfrom genshi.core import Stream, TEXTfrom genshi.input import HTMLParser, XMLtry:    pygments = __import__('pygments', {}, {}, [])    have_pygments = Trueexcept ImportError:    have_pygments = Falsefrom trac.mimeview.api import Mimeview, Contextif have_pygments:    from trac.mimeview.pygments import PygmentsRendererfrom trac.test import EnvironmentStub, Mockfrom trac.web.chrome import Chromefrom trac.web.href import Hrefclass PygmentsRendererTestCase(unittest.TestCase):    def setUp(self):        self.env = EnvironmentStub(enable=[Chrome, PygmentsRenderer])        self.pygments = Mimeview(self.env).renderers[0]        self.req = Mock(base_path='',chrome={}, args={},                        abs_href=Href('/'), href=Href('/'),                        session={}, perm=None, authname=None, tz=None)        self.context = Context.from_request(self.req)        pygments_html = open(os.path.join(os.path.split(__file__)[0],                                       'pygments.html'))        self.pygments_html = Stream(list(HTMLParser(pygments_html)))    def _expected(self, expected_id):        return self.pygments_html.select(            '//div[@id="%s"]/*|//div[@id="%s"]/text())' %             (expected_id, expected_id))    def _test(self, expected_id, result):        expected = str(self._expected(expected_id))        result = str(result)        #print "\nE: " + repr(expected)        #print "\nR: " + repr(result)        expected, result = expected.splitlines(), result.splitlines()        for exp, res in zip(expected, result):            self.assertEquals(exp, res)        self.assertEquals(len(expected), len(result))    def test_python_hello(self):        """        Simple Python highlighting with Pygments (direct)        """        result = self.pygments.render(self.context, 'text/x-python', """def hello():        return "Hello World!"""")        self.assertTrue(result)        self._test('python_hello', result)    def test_python_hello_mimeview(self):        """        Simple Python highlighting with Pygments (through Mimeview.render)        """        result = mimeview = Mimeview(self.env).render(self.context,                                                      'text/x-python', """def hello():        return "Hello World!"""")        self.assertTrue(result)        self._test('python_hello_mimeview', result)    def test_empty_content(self):        """        Simple test for direct rendering of empty content.        """        result = self.pygments.render(self.context, 'text/x-python', '')        self.assertTrue(result)        self._test('empty_content', result)    def test_newline_content(self):        """        stripnl defaults to True in Pygments!        Even without it set, files still end up one line shorter.        """        result = self.pygments.render(self.context, 'text/x-python', '\n\n\n\n')        self.assertTrue(result)        t = "".join([r[1] for r in result if r[0] is TEXT])        self.assertEqual("\n\n\n", t)    def test_empty_content(self):        """        A '\n' token is generated for an empty file, so we have to bypass        pygments when rendering empty files.        """        result = self.pygments.render(self.context, 'text/x-python', '')        self.assertEqual(None, result)def suite():    suite = unittest.TestSuite()    if have_pygments:        suite.addTest(unittest.makeSuite(PygmentsRendererTestCase, 'test'))    else:        print 'SKIP: mimeview/tests/pygments (no pygments installed)'    return suiteif __name__ == '__main__':    unittest.main(defaultTest='suite')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -