# -*- coding: utf-8 -*- """ Colorize - Python source formatter that outputs Python code in XHTML. This script is based on MoinMoin - The Python Source Parser. Usage: colorize.py [source file name] [optional author name] """ # Imports import cgi import string import sys import cStringIO import keyword import token import tokenize import re import os #Filepath of source file from command line parameter. sourcefile = sys.argv[1] #Get file name of source file. filename = os.path.split(sourcefile)[1] #Get optional author name parameter (it is added to the DC metadata) if len(sys.argv)> 2: authorname = sys.argv[2] else: authorname = "Unknown" #Set up basic values. _KEYWORD = token.NT_OFFSET + 1 _TEXT = token.NT_OFFSET + 2 _classes = { token.NUMBER: 'token_number', token.OP: 'token_op', token.STRING: 'token_string', tokenize.COMMENT: 'token_comment', token.NAME: 'token_name', token.ERRORTOKEN: 'token_error', _KEYWORD: 'keyword', _TEXT: 'text', } _DEFAULTENCODING = "utf-8" #Define start of XHtml document. docstart = """
""" #Add css from file to output document. docstart += """\n\t\n""" #Close Xhtml document. docend = "\n\t\n" #Set default encoding for output document. docencoding = _DEFAULTENCODING def getEncodingOfFile(sourcefile): """Get encoding of source file. If no encoding found, returns _DEFAULTENCODING.""" myfile = file(sourcefile) line = myfile.readline() #try line 1 encoding = parseEncoding(line) #encoding found? if encoding != "": return encoding #if not - try line two line = myfile.readline() encoding = parseEncoding(line) #encoding found? if encoding != "": return encoding #if not - try BOM if myfile.encoding != None: return myfile.encoding #if not - return default encoding return _DEFAULTENCODING def parseEncoding(textline): """Parse encoding from textline.""" #Check line for encoding match regex = re.compile("coding[=:]\s*([-\w.]+)") match = regex.search(textline, 1) if match != None: #Return found encoding return match.group(1) else: #return default return "" class Parser: """ Send colored python source. """ def __init__(self, raw, out = sys.stdout): """ Store the source text. """ self.raw = string.strip(string.expandtabs(raw)) self.out = out def format(self, formatter, form): """ Parse and send the colored source. """ # store line offsets in self.lines self.lines = [0, 0] pos = 0 while 1: pos = string.find(self.raw, '\n', pos) + 1 if not pos: break self.lines.append(pos) self.lines.append(len(self.raw)) # parse the source and write it self.out.write(docstart) # write metadata self.out.write("\n')
try:
tokenize.tokenize(text.readline, self)
except tokenize.TokenError, ex:
msg = ex[0]
line = ex[1][0]
self.out.write("ERROR: %s
%s\n" % (
msg, self.raw[self.lines[line]:]))
self.out.write('
\n')
self.out.write(docend)
def __call__(self, toktype, toktext, (srow,scol), (erow,ecol), line):
""" Token handler.
"""
if 0:
print "type", toktype, token.tok_name[toktype], "text", toktext,
print "start", srow, scol, "end", erow, ecol, "