##language:zh -- limodou [<>] <> = Python 源代码转成Html = ''现在有许多的Python源码转成Html的程序,这个是从Moin中修改而来的'' == 关于 == 现在有许多的Python源码转成Html的程序,就我所知就有Moin, Twisted, DiveIntoPython带了一个,可能还有别的,我没有再查过。 本来PyTextile是可以将Python转成Html的,但它用的是twisted.python.htmlizer,不过在程序它将导入位置改为了: {{{ import htmlizer }}} 结果要你将htmlizer从twisted中拷贝出来才可以,但现在的htmlizer还要调用别的模块。因些我放弃了。当然,改一下是行的,不过没这么做。 DiveIntoPython的作者Mark Pilgrim写的,也是要用到别的东西。因此还是放弃了。 最后选定了Moin中的python.py。不过,与老版本不同,它使用了wiki的一个应用模块,主要是进行特殊字符的html处理,我改成了cgi模块。但还是发现,对传入的文本是unicode时,处理有问题,没办法改吧。最后形成了这个版本,已经不再是原来的东西了。 == 代码 == {{{#!python # -*- coding: iso-8859-1 -*- """ MoinMoin - Python Source Parser @copyright: 2001 by J黵gen Hermann @license: GNU GPL, see COPYING for details. """ # Imports import cStringIO import keyword, token, tokenize import cgi import types ############################################################################# ### Python Source Parser (does Hilighting) ############################################################################# _KEYWORD = token.NT_OFFSET + 1 _TEXT = token.NT_OFFSET + 2 _colors = { token.NUMBER: '#0080C0', token.OP: '#0000C0', token.STRING: '#804000', tokenize.COMMENT: '#008000', token.NAME: '#000000', token.ERRORTOKEN: '#FF8080', _KEYWORD: '#0000FF', _TEXT: '#000000', } class Parser: """ Send colored python source. """ def __init__(self, raw): """ Store the source text. """ self.raw = raw.expandtabs().rstrip() self.unicode = False self.result = [] def format(self, linenumber=True): """ Parse and send the colored source. """ # store line offsets in self.lines if isinstance(self.raw, types.UnicodeType): self.raw = self.raw.encode('utf-8') self.unicode = True self.lines = [0, 0] pos = 0 while 1: pos = self.raw.find('\n', pos) + 1 if not pos: break self.lines.append(pos) self.lines.append(len(self.raw)) # write line numbers if linenumber: self.result.append('
') self.result.append('
' % _colors[_TEXT])
            for idx in range(1, len(self.lines)-1):
                self.result.append('%3d \n' % idx)
            self.result.append('
') # parse the source and write it self.pos = 0 text = cStringIO.StringIO(self.raw) self.result.append('
')
        try:
            tokenize.tokenize(text.readline, self)
        except tokenize.TokenError, ex:
            msg = ex[0]
            line = ex[1][0]
            self.result.append("

ERROR: %s

%s\n" % ( msg, self.raw[self.lines[line]:])) self.result.append('
') # close table if linenumber: self.result.append('
') text = ''.join(self.result) if self.unicode: text = unicode(text, 'utf-8') return text def __call__(self, toktype, toktext, (srow,scol), (erow,ecol), line): """ Token handler. """ if 0: print "type", toktype, token.tok_name[toktype], "text", toktext, \ "start", srow,scol, "end", erow,ecol, "
" # calculate new positions oldpos = self.pos newpos = self.lines[srow] + scol self.pos = newpos + len(toktext) # handle newlines if toktype in [token.NEWLINE, tokenize.NL]: self.result.append('\n') return # send the original whitespace, if needed if newpos > oldpos: self.result.append(self.raw[oldpos:newpos]) # skip indenting tokens if toktype in [token.INDENT, token.DEDENT]: self.pos = newpos return # map token type to a color group if token.LPAR <= toktype and toktype <= token.OP: toktype = token.OP elif toktype == token.NAME and keyword.iskeyword(toktext): toktype = _KEYWORD color = _colors.get(toktype, _colors[_TEXT]) style = '' if toktype == token.ERRORTOKEN: style = ' style="border: solid 1.5pt #FF0000;"' # send text self.result.append('' % (color, style)) if toktype == _KEYWORD: self.result.append('') self.result.append(cgi.escape(toktext)) if toktype == _KEYWORD: self.result.append('') self.result.append('') if __name__ == "__main__": import os print "Formatting..." # open own source source = open('colourize.py').read() # write colorized version to "python.html" file('colourize.html', 'w').write(Parser(source).format(False)) }}} format中的linenumber可以控制是否要生成行号。 == 讨论 ==