Package python-module-logilab-mtconverter-0 :: Package 8 :: Package 4 :: Package transforms :: Module python
[frames] | no frames]

Source Code for Module python-module-logilab-mtconverter-0.8.4.transforms.python

  1  # copyright 2006-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  2  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  3  # 
  4  # This file is part of logilab-mtconverter. 
  5  # 
  6  # logilab-mtconverter is free software: you can redistribute it and/or modify it 
  7  # under the terms of the GNU Lesser General Public License as published by the 
  8  # Free Software Foundation, either version 2.1 of the License, or (at your 
  9  # option) any later version. 
 10  # 
 11  # logilab-mtconverter is distributed in the hope that it will be useful, but 
 12  # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 13  # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
 14  # for more details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public License along 
 17  # with logilab-mtconverter. If not, see <http://www.gnu.org/licenses/>. 
 18  """ 
 19  Original code from active state recipe 
 20          'Colorize Python source using the built-in tokenizer' 
 21   
 22  ------------------------------------ 
 23       MoinMoin - Python Source Parser 
 24  ------------------------------------ 
 25   
 26   This code is part of MoinMoin (http://moin.sourceforge.net/) and converts 
 27   Python source code to HTML markup, rendering comments, keywords, operators, 
 28   numeric and string literals in different colors. 
 29   
 30   It shows how to use the built-in keyword, token and tokenize modules 
 31   to scan Python source code and re-emit it with no changes to its 
 32   original formatting (which is the hard part). 
 33  """ 
 34   
 35  import keyword, token, tokenize 
 36  from cStringIO import StringIO 
 37   
 38  _KEYWORD = token.NT_OFFSET + 1 
 39  _TEXT    = token.NT_OFFSET + 2 
 40   
41 -class Parser:
42 """Python Source Parser used to get HTML colored python source 43 """
44 - def __init__(self, raw, tags, out):
45 """Store the source text""" 46 self.raw = raw.expandtabs().strip() 47 self.out = out 48 self.tags = tags
49
50 - def format(self):
51 """Parse and send the colored source""" 52 # store line offsets in self.lines 53 self.lines = [0, 0] 54 pos = 0 55 while 1: 56 pos = self.raw.find('\n', pos) + 1 57 if not pos: break 58 self.lines.append(pos) 59 self.lines.append(len(self.raw)) 60 # parse the source and write it 61 self.pos = 0 62 text = StringIO(self.raw) 63 self.out.write('<pre class="python">\n') 64 try: 65 tokenize.tokenize(text.readline, self) 66 except tokenize.TokenError, ex: 67 msg = ex[0] 68 line = ex[1][0] 69 self.out.write("<h5 class='error>'ERROR: %s%s</h5>" % ( 70 msg, self.raw[self.lines[line]:])) 71 self.out.write('\n</pre>\n')
72
73 - def __call__(self, toktype, toktext, (srow,scol), (erow,ecol), line):
74 """Token handler""" 75 # calculate new positions 76 oldpos = self.pos 77 newpos = self.lines[srow] + scol 78 self.pos = newpos + len(toktext) 79 # handle newlines 80 if toktype in [token.NEWLINE, tokenize.NL]: 81 self.out.write('\n') 82 return 83 # send the original whitespace, if needed 84 if newpos > oldpos: 85 self.out.write(self.raw[oldpos:newpos]) 86 # skip indenting tokens 87 if toktype in [token.INDENT, token.DEDENT]: 88 self.pos = newpos 89 return 90 # map token type to a group 91 if token.LPAR <= toktype and toktype <= token.OP: 92 toktype = 'OP' 93 elif toktype == token.NAME and keyword.iskeyword(toktext): 94 toktype = 'KEYWORD' 95 else: 96 toktype = tokenize.tok_name[toktype] 97 98 open_tag = self.tags.get('OPEN_'+toktype, self.tags['OPEN_TEXT']) 99 close_tag = self.tags.get('CLOSE_'+toktype, self.tags['CLOSE_TEXT']) 100 # send text 101 self.out.write(open_tag) 102 self.out.write(html_escape(toktext)) 103 self.out.write(close_tag)
104 105 106 from logilab.mtconverter.transform import Transform 107 from logilab.mtconverter import html_escape 108
109 -class python_to_html(Transform):
110 """Colorize Python source files""" 111 112 name = "python_to_html" 113 inputs = ("text/x-python",) 114 output = "text/html" 115 116 config = { 117 'OPEN_NUMBER': '<span style="color: #0080C0;">', 118 'CLOSE_NUMBER': '</span>', 119 'OPEN_OP': '<span style="color: #0000C0;">', 120 'CLOSE_OP': '</span>', 121 'OPEN_STRING': '<span style="color: #004080;">', 122 'CLOSE_STRING': '</span>', 123 'OPEN_COMMENT': '<span style="color: #008000;">', 124 'CLOSE_COMMENT': '</span>', 125 'OPEN_NAME': '<span style="color: #000000;">', 126 'CLOSE_NAME': '</span>', 127 'OPEN_ERRORTOKEN': '<span style="color: #FF8080;">', 128 'CLOSE_ERRORTOKEN': '</span>', 129 'OPEN_KEYWORD': '<span style="color: #C00000;">', 130 'CLOSE_KEYWORD': '</span>', 131 'OPEN_TEXT': '', 132 'CLOSE_TEXT': '', 133 } 134
135 - def _convert(self, trdata):
136 dest = StringIO() 137 Parser(trdata.data, self.config, dest).format() 138 return dest.getvalue()
139