Webware / WebKit / Examples /

import os
import sys

from MiscUtils import StringIO
from WebKit.Page import Page

class Colorize(Page):
    """Syntax highlights Python source files.

    Set a variable 'filename' in the request so I know which file to work on.
    This also demonstrates forwarding. The View servlet actually forwards
    its request here.


    def respond(self, transaction):
        """Write out a syntax hilighted version of the file.

        The filename is an attribute of the request object.

        res = transaction._response
        req = self.request()
        if not req.hasField('filename'):
            res.write('<h3 style="color:red">Error</h3><p>'
                'No filename given to syntax color!</p>')
        filename = req.field('filename')
        filename = self.request().serverSidePath(os.path.basename(filename))
        if not os.path.exists(filename):
            res.write('<h3 style="color:red">Error</h3><p>'
                'The requested file %r does not exist'
                ' in the proper directory.</p>' % os.path.basename(filename))
        from DocSupport import py2html
        myout = StringIO()
        stdout = sys.stdout
        sys.stdout = myout
        py2html.main((None, '-stdout', '-files', filename))
        results = myout.getvalue()
        results = results.replace('\t', '    ') # 4 spaces per tab
        sys.stdout = stdout
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.