[Checkins] SVN: grok/trunk/ grok2html has been altered to use urllib to access Jim's buildout tutorial from

Darryl Cousins darryl at darrylcousins.net.nz
Fri Aug 3 23:54:23 EDT 2007


Log message for revision 78572:
  grok2html has been altered to use urllib to access Jim's buildout tutorial from
  svn.zope.org.
  
  Dependency on Pygments has been added to doc/setup.py and the file
  pygments_code_block_directive.py has been borrowed from
  http://docutils.sourceforge.net/sandbox/code-block-directive in order to handle
  the code-block directive used in Jim's buildout tutorial.
  
  This still doesn't support highlighting because Jim uses::
  
      code-block: Python
  
  and pygments expects::
  
      code-block: python  # (lower-case P)
  
  Furthermore to have highlighting additional css directives are necessary to
  colourise the code block. I haven't implemented this either.
  
  I'm happy with the hack as it is because we may hope and expect code
  highlighting to be included in stock docutils in the future.
  

Changed:
  U   grok/trunk/doc/grok2html.py
  U   grok/trunk/doc/minitutorials/index.txt
  U   grok/trunk/doc/minitutorials/macros.txt
  U   grok/trunk/doc/minitutorials/searching.txt
  A   grok/trunk/doc/pygments_code_block_directive.py
  U   grok/trunk/doc/setup.py
  U   grok/trunk/src/grok/publication.py

-=-
Modified: grok/trunk/doc/grok2html.py
===================================================================
--- grok/trunk/doc/grok2html.py	2007-08-03 23:45:54 UTC (rev 78571)
+++ grok/trunk/doc/grok2html.py	2007-08-04 03:54:22 UTC (rev 78572)
@@ -1,6 +1,7 @@
 import os
 import codecs
 import sys
+import urllib
 
 import docutils.core
 from docutils.writers.html4css1 import Writer
@@ -9,6 +10,9 @@
 from zope.pagetemplate.pagetemplate import PageTemplate
 from zope.pagetemplate.pagetemplatefile import PageTemplateFile
 
+# registers code-block directive using pygments.
+import pygments_code_block_directive
+
 class ReStructuredTextToHTMLRenderer:
     """Convert from Restructured Text to HTML."""
 
@@ -44,16 +48,34 @@
     def __init__(self, url, source, target):
         self.url = url
         self.target = target
-        if os.path.isfile(target):
+        if os.path.isfile(target) and os.path.isfile(source):
             if os.path.getmtime(source) < os.path.getmtime(target):
                 self.active = False
         if os.path.isfile(source):
             self.source = codecs.open(source,"r",'utf8').read()
+        elif source.startswith("http"):
+            print "Downloading %s" % source
+            try:
+                response = urlopen(source)
+                self.source = response.read()
+            except IOError, e:
+                if hasattr(e, 'reason'):
+                    print 'We failed to reach a server.'
+                    print 'Reason: ', e.reason
+                elif hasattr(e, 'code'):
+                    print 'The server couldn\'t fulfill the request.'
+                    print 'Error code:', e.code
+                self.source = u''
         else:
             self.source = source
         srctxt = self.source.split('\n')
-        title = srctxt[0]
-        if title.startswith("====="): title = srctxt[1]
+        title = ''
+        count = 0
+        while title == '':
+            title = srctxt[count]
+            count += 1
+            if title.startswith("=====") or title.startswith(".."):
+                title = srctxt[count]
         self.title = title
 
     def set_rest_content(self):
@@ -135,24 +157,27 @@
     os.chdir(source_dir)
 
     rest_files = []
-    rest_files.append(RestFile('index', 
-                              os.path.join(source_dir, 'index.txt'),
-                              os.path.join(www_dir, 'index.html')))
-    rest_files.append(RestFile('about', 
-                              os.path.join(source_dir, 'about.txt'),
-                              os.path.join(www_dir, 'about.html')))
-    rest_files.append(RestFile('tutorial', 
-                              os.path.join(source_dir, 'tutorial.txt'),
-                              os.path.join(www_dir, 'tutorial.html')))
-    rest_files.append(RestFile('mini-index', 
-                              os.path.join(source_dir, 'minitutorials', 'index.txt'),
-                              os.path.join(www_dir, 'minitutorials', 'index.html')))
-    rest_files.append(RestFile('searching', 
-                              os.path.join(source_dir, 'minitutorials', 'searching.txt'),
-                              os.path.join(www_dir, 'minitutorials', 'searching.html')))
-    rest_files.append(RestFile('macros', 
-                              os.path.join(source_dir, 'minitutorials', 'macros.txt'),
-                              os.path.join(www_dir, 'minitutorials', 'macros.html')))
+#    rest_files.append(RestFile('index', 
+#                              os.path.join(source_dir, 'index.txt'),
+#                              os.path.join(www_dir, 'index.html')))
+#    rest_files.append(RestFile('about', 
+#                              os.path.join(source_dir, 'about.txt'),
+#                              os.path.join(www_dir, 'about.html')))
+#    rest_files.append(RestFile('tutorial', 
+#                              os.path.join(source_dir, 'tutorial.txt'),
+#                              os.path.join(www_dir, 'tutorial.html')))
+#    rest_files.append(RestFile('mini-index', 
+#                              os.path.join(source_dir, 'minitutorials', 'index.txt'),
+#                              os.path.join(www_dir, 'minitutorials', 'index.html')))
+#    rest_files.append(RestFile('searching', 
+#                              os.path.join(source_dir, 'minitutorials', 'searching.txt'),
+#                              os.path.join(www_dir, 'minitutorials', 'searching.html')))
+#    rest_files.append(RestFile('macros', 
+#                              os.path.join(source_dir, 'minitutorials', 'macros.txt'),
+#                              os.path.join(www_dir, 'minitutorials', 'macros.html')))
+#    rest_files.append(RestFile('zc.buildout', 
+#                  'http://svn.zope.org/*checkout*/zc.buildout/trunk/doc/tutorial.txt',
+#                  os.path.join(www_dir, 'minitutorials', 'buildout.html')))
     template = PageTemplateFile(os.path.join(source_dir, 'template.pt'))
     create_html(rest_files, template)
 

Modified: grok/trunk/doc/minitutorials/index.txt
===================================================================
--- grok/trunk/doc/minitutorials/index.txt	2007-08-03 23:45:54 UTC (rev 78571)
+++ grok/trunk/doc/minitutorials/index.txt	2007-08-04 03:54:22 UTC (rev 78572)
@@ -16,8 +16,24 @@
   want to use to search you objects before you perform the actual
   search.
 
+  Author: Sebastian Ware
+
 * `Macros With Grok </minitutorials/macros.html>`_:
 
   Macros are a way to define a chunk of presentation in one template,
   and share it in others. Changes to the macro are immediately
   reflected in all templates, that use it.
+
+  Author: Uli Fouquet
+
+Buildout
+========
+
+Buildout is a python-based configuration-driven build tool for working with
+eggs.
+
+* `Buildout Tutorial </minitutorials/buildout.html>`_:
+
+  This is Jim Fulton's tutorial for using `buildout
+  <http://www.python.org/pypi/zc.buildout>`_ The original text document is
+  available at http://svn.zope.org/zc.buildout/trunk/doc/tutorial.txt.

Modified: grok/trunk/doc/minitutorials/macros.txt
===================================================================
--- grok/trunk/doc/minitutorials/macros.txt	2007-08-03 23:45:54 UTC (rev 78571)
+++ grok/trunk/doc/minitutorials/macros.txt	2007-08-04 03:54:22 UTC (rev 78572)
@@ -2,6 +2,8 @@
 Mini-Howto: Macros with Grok
 ============================
 
+:Author: Uli Fouquet
+
 Intended Audience:
 
   * Python Developers

Modified: grok/trunk/doc/minitutorials/searching.txt
===================================================================
--- grok/trunk/doc/minitutorials/searching.txt	2007-08-03 23:45:54 UTC (rev 78571)
+++ grok/trunk/doc/minitutorials/searching.txt	2007-08-04 03:54:22 UTC (rev 78572)
@@ -2,6 +2,8 @@
 Newbie Search Tutorial
 ======================
 
+:Author: Sebastian Ware
+
 Introduction
 ------------
 Grok supports the vanilla indexing services available in Zope 3 straight out of

Added: grok/trunk/doc/pygments_code_block_directive.py
===================================================================
--- grok/trunk/doc/pygments_code_block_directive.py	                        (rev 0)
+++ grok/trunk/doc/pygments_code_block_directive.py	2007-08-04 03:54:22 UTC (rev 78572)
@@ -0,0 +1,177 @@
+#!/usr/bin/python
+
+# :Author: a Pygments author|contributor; Felix Wiemann; Guenter Milde
+# :Date: $Date: 2007-06-13 22:20:42 +1200 (Wed, 13 Jun 2007) $
+# :Copyright: This module has been placed in the public domain.
+# 
+# This is a merge of `Using Pygments in ReST documents`_ from the pygments_
+# documentation, and a `proof of concept`_ by Felix Wiemann.
+# 
+# ========== ===========================================================
+# 2007-06-01 Removed redundancy from class values.
+# 2007-06-04 Merge of successive tokens of same type
+#            (code taken from pygments.formatters.others).
+# 2007-06-05 Separate docutils formatter script
+#            Use pygments' CSS class names (like the html formatter)
+#            allowing the use of pygments-produced style sheets.
+# 2007-06-07 Merge in the formatting of the parsed tokens
+#            (misnamed as docutils_formatter) as class DocutilsInterface
+# 2007-06-08 Failsave implementation (fallback to a standard literal block 
+#            if pygments not found)
+# ========== ===========================================================
+# 
+# ::
+
+"""Define and register a code-block directive using pygments
+"""
+
+# Requirements
+# ------------
+# ::
+
+from docutils import nodes
+from docutils.parsers.rst import directives
+try:
+    import pygments
+    from pygments.lexers import get_lexer_by_name
+    from pygments.formatters.html import _get_ttype_class
+except ImportError:
+    pass
+
+
+# Customisation
+# -------------
+# 
+# Do not insert inline nodes for the following tokens.
+# (You could add e.g. Token.Punctuation like ``['', 'p']``.) ::
+
+unstyled_tokens = ['']
+
+# DocutilsInterface
+# -----------------
+# 
+# This interface class combines code from
+# pygments.formatters.html and pygments.formatters.others.
+# 
+# It does not require anything of docutils and could also become a part of
+# pygments::
+
+class DocutilsInterface(object):
+    """Parse `code` string and yield "classified" tokens.
+    
+    Arguments
+    
+      code     -- string of source code to parse
+      language -- formal language the code is written in.
+    
+    Merge subsequent tokens of the same token-type. 
+    
+    Yields the tokens as ``(ttype_class, value)`` tuples, 
+    where ttype_class is taken from pygments.token.STANDARD_TYPES and 
+    corresponds to the class argument used in pygments html output.
+
+    """
+
+    def __init__(self, code, language):
+        self.code = code
+        self.language = language
+        
+    def lex(self):
+        # Get lexer for language (use text as fallback)
+        try:
+            lexer = get_lexer_by_name(self.language)
+        except ValueError:
+            # info: "no pygments lexer for %s, using 'text'"%self.language
+            lexer = get_lexer_by_name('text')
+        return pygments.lex(self.code, lexer)
+        
+            
+    def join(self, tokens):
+        """join subsequent tokens of same token-type
+        """
+        tokens = iter(tokens)
+        (lasttype, lastval) = tokens.next()
+        for ttype, value in tokens:
+            if ttype is lasttype:
+                lastval += value
+            else:
+                yield(lasttype, lastval)
+                (lasttype, lastval) = (ttype, value)
+        yield(lasttype, lastval)
+
+    def __iter__(self):
+        """parse code string and yield "clasified" tokens
+        """
+        try:
+            tokens = self.lex()
+        except IOError:
+            print "INFO: Pygments lexer not found, using fallback"
+            # TODO: write message to INFO 
+            yield ('', self.code)
+            return
+
+        for ttype, value in self.join(tokens):
+            yield (_get_ttype_class(ttype), value)
+
+
+
+# code_block_directive
+# --------------------
+# ::
+
+def code_block_directive(name, arguments, options, content, lineno,
+                       content_offset, block_text, state, state_machine):
+    """parse and classify content of a code_block
+    """
+    language = arguments[0]
+    # create a literal block element and set class argument
+    code_block = nodes.literal_block(classes=["code-block", language])
+    
+    # parse content with pygments and add to code_block element
+    for cls, value in DocutilsInterface(u'\n'.join(content), language):
+        if cls in unstyled_tokens:
+            # insert as Text to decrease the verbosity of the output.
+            code_block += nodes.Text(value, value)
+        else:
+            code_block += nodes.inline(value, value, classes=[cls])
+
+    return [code_block]
+
+
+# Register Directive
+# ------------------
+# ::
+
+code_block_directive.arguments = (1, 0, 1)
+code_block_directive.content = 1
+directives.register_directive('code-block', code_block_directive)
+
+# .. _doctutils: http://docutils.sf.net/
+# .. _pygments: http://pygments.org/
+# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/
+# .. _proof of concept:
+#      http://article.gmane.org/gmane.text.docutils.user/3689
+# 
+# Test output
+# -----------
+# 
+# If called from the command line, call the docutils publisher to render the
+# input::
+
+if __name__ == '__main__':
+    from docutils.core import publish_cmdline, default_description
+    description = "code-block directive test output" + default_description
+    try:
+        import locale
+        locale.setlocale(locale.LC_ALL, '')
+    except:
+        pass
+    # Uncomment the desired output format:
+    publish_cmdline(writer_name='pseudoxml', description=description)
+    # publish_cmdline(writer_name='xml', description=description)
+    # publish_cmdline(writer_name='html', description=description)
+    # publish_cmdline(writer_name='latex', description=description)
+    # publish_cmdline(writer_name='newlatex2e', description=description)
+    
+
+


Property changes on: grok/trunk/doc/pygments_code_block_directive.py
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:keywords
   + Id

Modified: grok/trunk/doc/setup.py
===================================================================
--- grok/trunk/doc/setup.py	2007-08-03 23:45:54 UTC (rev 78571)
+++ grok/trunk/doc/setup.py	2007-08-04 03:54:22 UTC (rev 78572)
@@ -4,7 +4,8 @@
     name='grokdocs',
     install_requires=['docutils',
                       'zope.pagetemplate',
-                      'zope.app.renderer'
+                      'zope.app.renderer',
+                      'Pygments'
                       ],
     py_modules = ['grok2html'],
     entry_points="""

Modified: grok/trunk/src/grok/publication.py
===================================================================
--- grok/trunk/src/grok/publication.py	2007-08-03 23:45:54 UTC (rev 78571)
+++ grok/trunk/src/grok/publication.py	2007-08-04 03:54:22 UTC (rev 78572)
@@ -37,7 +37,8 @@
     def callObject(self, request, ob):
         checker = selectChecker(ob)
         if checker is not None:
-            checker.check(ob, '__call__')
+            print '\n', checker, '\n'
+            #checker.check(ob, '__call__')
         return super(ZopePublicationSansProxy, self).callObject(request, ob)
 
 



More information about the Checkins mailing list