use pygments for sourcecode highlightning

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-04-13 14:36:16 +02:00
parent 5c854bea30
commit dfc6ac3b5a
2 changed files with 63 additions and 8 deletions

View File

@ -20,7 +20,9 @@ of course all the functionality is bundled together rather than spread over a
number of modules. number of modules.
Example usage, here we use the :api:`py.test.ensuretemp()` function to create Example usage, here we use the :api:`py.test.ensuretemp()` function to create
a :api:`py.path.local` object for us (which wraps a directory):: a :api:`py.path.local` object for us (which wraps a directory):
.. sourcecode:: pycon
>>> import py >>> import py
>>> temppath = py.test.ensuretemp('py.path_documentation') >>> temppath = py.test.ensuretemp('py.path_documentation')
@ -46,7 +48,9 @@ Both allow you to access relatively advanced features such as metadata and
versioning, and both in a way more user-friendly manner than existing other versioning, and both in a way more user-friendly manner than existing other
solutions. solutions.
Some example usage of :api:`py.path.svnurl`:: Some example usage of :api:`py.path.svnurl`:
.. sourcecode:: pycon
.. >>> import py .. >>> import py
.. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk') .. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk')
@ -59,7 +63,9 @@ Some example usage of :api:`py.path.svnurl`::
>>> time.strftime('%Y-%m-%d', time.gmtime(firstentry.date)) >>> time.strftime('%Y-%m-%d', time.gmtime(firstentry.date))
'2004-10-02' '2004-10-02'
Example usage of :api:`py.path.svnwc`:: Example usage of :api:`py.path.svnwc`:
.. sourcecode:: pycon
.. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk') .. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk')
>>> temp = py.test.ensuretemp('py.path_documentation') >>> temp = py.test.ensuretemp('py.path_documentation')
@ -98,7 +104,7 @@ Searching `.txt` files
Search for a particular string inside all files with a .txt extension in a Search for a particular string inside all files with a .txt extension in a
specific directory. specific directory.
:: .. sourcecode:: pycon
>>> dirpath = temppath.ensure('testdir', dir=True) >>> dirpath = temppath.ensure('testdir', dir=True)
>>> dirpath.join('textfile1.txt').write('foo bar baz') >>> dirpath.join('textfile1.txt').write('foo bar baz')
@ -120,7 +126,9 @@ Working with Paths
This example shows the :api:`py.path` features to deal with This example shows the :api:`py.path` features to deal with
filesystem paths Note that the filesystem is never touched, filesystem paths Note that the filesystem is never touched,
all operations are performed on a string level (so the paths all operations are performed on a string level (so the paths
don't have to exist, either):: don't have to exist, either):
.. sourcecode:: pycon
>>> p1 = py.path.local('/foo/bar') >>> p1 = py.path.local('/foo/bar')
>>> p2 = p1.join('baz/qux') >>> p2 = p1.join('baz/qux')
@ -153,7 +161,9 @@ Checking path types
....................... .......................
Now we will show a bit about the powerful 'check()' method on paths, which Now we will show a bit about the powerful 'check()' method on paths, which
allows you to check whether a file exists, what type it is, etc.:: allows you to check whether a file exists, what type it is, etc.:
.. sourcecode:: pycon
>>> file1 = temppath.join('file1') >>> file1 = temppath.join('file1')
>>> file1.check() # does it exist? >>> file1.check() # does it exist?
@ -177,7 +187,9 @@ Setting svn-properties
....................... .......................
As an example of 'uncommon' methods, we'll show how to read and write As an example of 'uncommon' methods, we'll show how to read and write
properties in an :api:`py.path.svnwc` instance:: properties in an :api:`py.path.svnwc` instance:
.. sourcecode:: pycon
.. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk') .. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk')
>>> wc.propget('foo') >>> wc.propget('foo')
@ -195,7 +207,9 @@ SVN authentication
....................... .......................
Some uncommon functionality can also be provided as extensions, such as SVN Some uncommon functionality can also be provided as extensions, such as SVN
authentication:: authentication:
.. sourcecode:: pycon
.. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk') .. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk')
>>> auth = py.path.SvnAuth('anonymous', 'user', cache_auth=False, >>> auth = py.path.SvnAuth('anonymous', 'user', cache_auth=False,

View File

@ -96,6 +96,47 @@ class ReSTSyntaxTest(py.test.collect.Item):
toctree_directive.options = {'maxdepth': int, 'glob': directives.flag, toctree_directive.options = {'maxdepth': int, 'glob': directives.flag,
'hidden': directives.flag} 'hidden': directives.flag}
directives.register_directive('toctree', toctree_directive) directives.register_directive('toctree', toctree_directive)
self.register_pygments()
def register_pygments(self):
# taken from pygments-main/external/rst-directive.py
try:
from pygments.formatters import HtmlFormatter
except ImportError:
def pygments_directive(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
return []
else:
# The default formatter
DEFAULT = HtmlFormatter(noclasses=True)
# Add name -> formatter pairs for every variant you want to use
VARIANTS = {
# 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
}
from docutils import nodes
from docutils.parsers.rst import directives
from pygments import highlight
from pygments.lexers import get_lexer_by_name, TextLexer
def pygments_directive(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
try:
lexer = get_lexer_by_name(arguments[0])
except ValueError:
# no lexer found - use the text one instead of an exception
lexer = TextLexer()
# take an arbitrary option if more than one is given
formatter = options and VARIANTS[options.keys()[0]] or DEFAULT
parsed = highlight(u'\n'.join(content), lexer, formatter)
return [nodes.raw('', parsed, format='html')]
pygments_directive.arguments = (1, 0, 1)
pygments_directive.content = 1
pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS])
directives.register_directive('sourcecode', pygments_directive)
def resolve_linkrole(self, name, text, check=True): def resolve_linkrole(self, name, text, check=True):
apigen_relpath = self.project.apigen_relpath apigen_relpath = self.project.apigen_relpath