Back to index

plone3  3.1.7
Public Member Functions | Public Attributes
PortalTransforms.transforms.python.Parser Class Reference

List of all members.

Public Member Functions

def __init__
def format
def __call__

Public Attributes

 raw
 out
 tags
 lines
 pos
 calculate new positions

Detailed Description

Send colored python source.

Definition at line 30 of file python.py.


Constructor & Destructor Documentation

def PortalTransforms.transforms.python.Parser.__init__ (   self,
  raw,
  tags,
  out 
)
Store the source text.

Definition at line 34 of file python.py.

00034 
00035     def __init__(self, raw, tags, out):
00036         """ Store the source text.
00037         """
00038         self.raw = string.strip(string.expandtabs(raw))
00039         self.out = out
00040         self.tags = tags


Member Function Documentation

def PortalTransforms.transforms.python.Parser.__call__ (   self,
  toktype,
  toktext,
  srow,
  scol,
  erow,
  ecol,
  line 
)
Token handler.

Definition at line 66 of file python.py.

00066 
00067     def __call__(self, toktype, toktext, (srow,scol), (erow,ecol), line):
00068         """ Token handler.
00069         """
00070         #print "type", toktype, token.tok_name[toktype], "text", toktext,
00071         #print "start", srow,scol, "end", erow,ecol, "<br>"
00072 
00073         ## calculate new positions
00074         oldpos = self.pos
00075         newpos = self.lines[srow] + scol
00076         self.pos = newpos + len(toktext)
00077 
00078         ## handle newlines
00079         if toktype in [token.NEWLINE, tokenize.NL]:
00080             self.out.write('\n')
00081             return
00082 
00083         ## send the original whitespace, if needed
00084         if newpos > oldpos:
00085             self.out.write(self.raw[oldpos:newpos])
00086 
00087         ## skip indenting tokens
00088         if toktype in [token.INDENT, token.DEDENT]:
00089             self.pos = newpos
00090             return
00091 
00092         ## map token type to a group
00093         if token.LPAR <= toktype and toktype <= token.OP:
00094             toktype = 'OP'
00095         elif toktype == token.NAME and keyword.iskeyword(toktext):
00096             toktype = 'KEYWORD'
00097         else:
00098             toktype = tokenize.tok_name[toktype]
00099 
00100         open_tag = self.tags.get('OPEN_'+toktype, self.tags['OPEN_TEXT'])
00101         close_tag = self.tags.get('CLOSE_'+toktype, self.tags['CLOSE_TEXT'])
00102 
00103         ## send text
00104         self.out.write(open_tag)
00105         self.out.write(html_quote(toktext))
00106         self.out.write(close_tag)
00107 
00108 

Parse and send the colored source.

Definition at line 41 of file python.py.

00041 
00042     def format(self):
00043         """ Parse and send the colored source.
00044         """
00045         # store line offsets in self.lines
00046         self.lines = [0, 0]
00047         pos = 0
00048         while 1:
00049             pos = string.find(self.raw, '\n', pos) + 1
00050             if not pos: break
00051             self.lines.append(pos)
00052         self.lines.append(len(self.raw))
00053 
00054         # parse the source and write it
00055         self.pos = 0
00056         text = StringIO(self.raw)
00057         self.out.write('<pre class="python">\n')
00058         try:
00059             tokenize.tokenize(text.readline, self)
00060         except tokenize.TokenError, ex:
00061             msg = ex[0]
00062             line = ex[1][0]
00063             self.out.write("<h5 class='error>'ERROR: %s%s</h5>" % (
00064                 msg, self.raw[self.lines[line]:]))
00065         self.out.write('\n</pre>\n')

Here is the caller graph for this function:


Member Data Documentation

Definition at line 45 of file python.py.

Definition at line 38 of file python.py.

calculate new positions

handle newlines

send the original whitespace, if needed skip indenting tokens

Definition at line 54 of file python.py.

Definition at line 37 of file python.py.

Definition at line 39 of file python.py.


The documentation for this class was generated from the following file: