[py-svn] r64028 - in py/trunk: doc py/test/plugin

hpk at codespeak.net hpk at codespeak.net
Mon Apr 13 14:54:59 CEST 2009


Author: hpk
Date: Mon Apr 13 14:54:58 2009
New Revision: 64028

Modified:
   py/trunk/doc/path.txt
   py/trunk/py/test/plugin/pytest_restdoc.py
Log:
use pygments for syntax-coloring python code and console 


Modified: py/trunk/doc/path.txt
==============================================================================
--- py/trunk/doc/path.txt	(original)
+++ py/trunk/doc/path.txt	Mon Apr 13 14:54:58 2009
@@ -20,7 +20,9 @@
 number of modules.
 
 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
   >>> temppath = py.test.ensuretemp('py.path_documentation')
@@ -46,7 +48,9 @@
 versioning, and both in a way more user-friendly manner than existing other
 solutions.
 
-Some example usage of :api:`py.path.svnurl`::
+Some example usage of :api:`py.path.svnurl`:
+
+.. sourcecode:: pycon
 
   .. >>> import py
   .. >>> if not py.test.config.option.urlcheck: raise ValueError('skipchunk')
@@ -59,7 +63,9 @@
   >>> time.strftime('%Y-%m-%d', time.gmtime(firstentry.date))
   '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')
   >>> temp = py.test.ensuretemp('py.path_documentation')
@@ -98,7 +104,7 @@
 Search for a particular string inside all files with a .txt extension in a
 specific directory.
 
-::
+.. sourcecode:: pycon
 
   >>> dirpath = temppath.ensure('testdir', dir=True)
   >>> dirpath.join('textfile1.txt').write('foo bar baz')
@@ -120,7 +126,9 @@
 This example shows the :api:`py.path` features to deal with
 filesystem paths Note that the filesystem is never touched,
 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')
   >>> p2 = p1.join('baz/qux')
@@ -153,7 +161,9 @@
 .......................
 
 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.check() # does it exist?
@@ -177,7 +187,9 @@
 .......................
 
 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')
   >>> wc.propget('foo')
@@ -195,7 +207,9 @@
 .......................
 
 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')
   >>> auth = py.path.SvnAuth('anonymous', 'user', cache_auth=False,

Modified: py/trunk/py/test/plugin/pytest_restdoc.py
==============================================================================
--- py/trunk/py/test/plugin/pytest_restdoc.py	(original)
+++ py/trunk/py/test/plugin/pytest_restdoc.py	Mon Apr 13 14:54:58 2009
@@ -96,6 +96,47 @@
         toctree_directive.options = {'maxdepth': int, 'glob': directives.flag,
                              'hidden': directives.flag}
         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):
         apigen_relpath = self.project.apigen_relpath



More information about the pytest-commit mailing list