PySourceColor (version 2.0, 15 February 2005)
index
PySourceColor.py
PySourceColor.py.html

 PySourceColor.py
----------------------------------------------------------------------------
 A python source to colorized html/css/xhtml converter.
 Hacked by M.E.Farmer Jr. 2004, 2005
 Python license
----------------------------------------------------------------------------
 - HTML markup does not create w3c valid html, but it works on every
   browser i've tried so far.(I.E.,Mozilla/Firefox,Opera,Konqueror,wxHTML).
 - CSS markup is w3c validated html 4.01 strict,
   but will not render correctly on all browsers.
 - XHTML markup is w3c validated xhtml 1.0 strict,
   like html 4.01, will not render correctly on all browsers.
----------------------------------------------------------------------------
 Features:
 -Three types of markup:
    html (default) 
    css/html 4.01 strict
    xhtml 1.0 strict
 
 -Can tokenize and colorize:
    12 types of strings
    2 comment types
    numbers
    operators
    brackets
    math operators
    class / name
    def / name
    decorator / name
    keywords
    arguments class/def/decorator
    linenumbers
    names
    text
 
 -Eight colorschemes built-in:
    null
    mono
    lite (default)
    dark 
    dark2
    idle
    viewcvs
    pythonwin
 
 -Header and footer
    set to '' for builtin header / footer.
    give path to a file containing the html
        you want added as header or footer.
 
 -Arbitrary text and html
    html markup converts all to raw (TEXT token)
    #@# for raw -> send raw text.
    #$# for span -> inline html and text.
    #%# for div -> block level html and text.
 
 -Linenumbers
    Supports all styles. New token is called LINE.
    Defaults to NAME if not defined.
 
 Style options
 -ALL markups support these text styles:
         b = bold
         i = italic
         u = underline
 -CSS and XHTML has limited support  for borders:
     HTML markup functions will ignore these.
     Optional: Border color in RGB hex
     Defaults to the text forecolor.
         #rrggbb = border color
     Border size:
         l = thick
         m = medium
         t = thin
     Border type:
         - = dashed
         . = dotted
         s = solid
         d = double
         g = groove
         r = ridge
         n = inset
         o = outset
     You can specify multiple sides,
     they will all use the same style.
     Optional: Default is full border.
         v = bottom
         < = left
         > = right
         ^ = top
     NOTE: Specify the styles you want.
           The markups will ignore unsupported styles
           Also note not all browsers can show these options
 
 -All tokens default to NAME if not defined
     so the only absolutely critical ones to define are:
     NAME, ERRORTOKEN, PAGEBACKGROUND
 
----------------------------------------------------------------------------
 Example usage:
----------------------------------------------------------------------------
 # import
 import PySourceColor as psc
 psc.convert('c:/Python22/PySourceColor.py', colors=psc.idle, show=1)
----------------------------------------------------------------------------
 # from module import *
 from PySourceColor import *
 convert('c:/Python22/Lib', colors=lite, markup="css",
          header='#$#<b>This is a simpe heading</b><hr/>')
----------------------------------------------------------------------------
 # How to use a custom colorscheme, and most of the 'features'
 from PySourceColor import *
 new = {
   ERRORTOKEN:             ('bui','#FF8080',''),
   DECORATOR_NAME:         ('s','#AACBBC',''),
   DECORATOR:              ('n','#333333',''),
   NAME:                   ('t.<v','#1133AA','#DDFF22'),
   NUMBER:                 ('','#236676','#FF5555'),
   OPERATOR:               ('b','#454567','#BBBB11'),
   MATH_OPERATOR:          ('','#935623','#423afb'),
   BRACKETS:               ('b','#ac34bf','#6457a5'),
   COMMENT:                ('t-#0022FF','#545366','#AABBFF'),
   DOUBLECOMMENT:          ('<l#553455','#553455','#FF00FF'),
   CLASS_NAME:             ('m^v-','#000000','#FFFFFF'),
   DEF_NAME:               ('l=<v','#897845','#000022'),
   KEYWORD:                ('.b','#345345','#FFFF22'),
   SINGLEQUOTE:            ('mn','#223344','#AADDCC'),
   SINGLEQUOTE_R:          ('','#344522',''),
   SINGLEQUOTE_U:          ('','#234234',''),
   DOUBLEQUOTE:            ('m#0022FF','#334421',''),
   DOUBLEQUOTE_R:          ('','#345345',''),
   DOUBLEQUOTE_U:          ('','#678673',''),
   TRIPLESINGLEQUOTE:      ('tv','#FFFFFF','#000000'),
   TRIPLESINGLEQUOTE_R:    ('tbu','#443256','#DDFFDA'),
   TRIPLESINGLEQUOTE_U:    ('','#423454','#DDFFDA'),
   TRIPLEDOUBLEQUOTE:      ('li#236fd3b<>','#000000','#FFFFFF'),
   TRIPLEDOUBLEQUOTE_R:    ('tub','#000000','#FFFFFF'),
   TRIPLEDOUBLEQUOTE_U:    ('-', '#CCAABB','#FFFAFF'),
   LINE:                   ('ib-','#ff66aa','#7733FF'),]
   TEXT:                   ('','#546634',''), 
   PAGEBACKGROUND:         '#FFFAAA',
     }
 if __name__ == '__main__':
     import sys
     convert(sys.argv[1], './xhtml.html', colors=new, markup='xhtml', show=1,
             linenumbers=1)
     convert(sys.argv[1], './html.html', colors=new, markup='html', show=1,
             linenumbers=1)
----------------------------------------------------------------------------

 
Modules
            
cStringIO
getopt
glob
keyword
os
sys
time
token
tokenize
traceback
webbrowser
 
Classes
            
exceptions.Exception
PySourceColorError
InputError
PathError
__builtin__.object
Parser
 
class InputError(PySourceColorError)
       
  
Method resolution order:
InputError
PySourceColorError
exceptions.Exception

Methods defined here:
__init__(self, msg)

Data and non-method functions defined here:
__doc__ = None
__module__ = 'PySourceColor'

Methods inherited from PySourceColorError:
__repr__(self)
__str__ = __repr__(self)

Methods inherited from exceptions.Exception:
__getitem__(...)
 
class Parser(__builtin__.object)
      MoinMoin python parser heavily chopped :)
 
   Methods defined here:
__call__(self, toktype, toktext, (srow, scol), (erow, ecol), line)
Token handler. Order is important do not rearrange.
__init__(self, raw, colors=None, title='', out=<open file '<stdout>', mode 'w' at 0x00762EF8>, markup='html', header=None, footer=None, linenumbers=0, numberlinks=0)
Store the source text & set some flags
_doCSSEnd(self)
_doCSSFooter(self)
_doCSSHeader(self)
_doCSSStart(self)
_doCSSStyleSheet(self)
_doHTMLEnd(self)
_doHTMLFooter(self)
_doHTMLHeader(self)
_doHTMLStart(self)
_doPageEnd(self)
_doPageFooter(self)
_doPageHeader(self)
_doPageStart(self)
_doSnippetEnd(self)
_doSnippetStart(self)
_doXHTMLEnd(self)
_doXHTMLFooter(self)
_doXHTMLHeader(self)
_doXHTMLStart(self)
_doXHTMLStyleSheet(self)
_getBackColor(self, key)
_getCSSStyle(self, key)
_getDocumentCreatedBy(self)
_getFile(self, filepath)
_getForeColor(self, key)
_getHTMLStyles(self, toktype, toktext)
_getLineNumber(self)
_getMarkupClass(self, key)
_getPageColor(self)
_getStyle(self, key)
_getTags(self, key)
_sendCSSStyle(self, external=0)
 create external and internal style sheets
_sendCSSText(self, toktype, toktext)
_sendHTMLText(self, toktype, toktext)
_sendXHTMLText(self, toktype, toktext)
format(self, form=None)
Parse and send the colorized source

Data and non-method functions defined here:
__dict__ = <dict-proxy object at 0x008DE820>
__doc__ = 'MoinMoin python parser heavily chopped :)'
__module__ = 'PySourceColor'
__weakref__ = <member '__weakref__' of 'Parser' objects>

Methods inherited from __builtin__.object:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__hash__(...)
x.__hash__() <==> hash(x)
__reduce__(...)
helper for pickle
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__str__(...)
x.__str__() <==> str(x)

Data and non-method functions inherited from __builtin__.object:
__class__ = <type 'type'>
__new__ = <built-in method __new__ of type object at 0x1E0CD418>
T.__new__(S, ...) -> a new object with type S, a subtype of T
 
class PathError(PySourceColorError)
       
  
Method resolution order:
PathError
PySourceColorError
exceptions.Exception

Methods defined here:
__init__(self, msg)

Data and non-method functions defined here:
__doc__ = None
__module__ = 'PySourceColor'

Methods inherited from PySourceColorError:
__repr__(self)
__str__ = __repr__(self)

Methods inherited from exceptions.Exception:
__getitem__(...)
 
class PySourceColorError(exceptions.Exception)
       
   Methods defined here:
__init__(self, msg='')
# Base for custom errors
__repr__(self)
__str__ = __repr__(self)

Data and non-method functions defined here:
__doc__ = None
__module__ = 'PySourceColor'

Methods inherited from exceptions.Exception:
__getitem__(...)
 
Functions
            
Usage()
-----------------------------------------------------------------------------
 PySourceColor.py ver: %s
-----------------------------------------------------------------------------
 Module summary:
    This module is designed to colorize python source code.
        Input--->python source
        Output-->colorized (html, html4.01/css, xhtml1.0)
    Standalone:
        This module will work from the command line with options.
        This module will work with redirected stdio.
    Imported:
        This module can be imported and used directly in your code.
-----------------------------------------------------------------------------
 Command line options:
    -h, --help
        Optional-> Display this help message.
    -t, --test
        Optional-> Will ignore all others flags but  --profile
            test all schemes and markup combinations
    -p, --profile
        Optional-> Works only with --test or -t
            runs profile.py and makes the test work in quiet mode.
    -i, --in, --input
        Optional-> If you give input on stdin.
        Use any of these for the current dir (.,cwd)
        Input can be file or dir.
        Input from stdin use one of the following (-,stdin)
        If stdin is used as input stdout is output unless specified.
    -o, --out, --output
        Optional-> output dir for the colorized source.
            default: output dir is the input dir.
        To output html to stdout use one of the following (-,stdout)
        Stdout can be used without stdin if you give a file as input.
    -c, --color
        Optional-> null, mono, dark, dark2, lite, idle, pythonwin, viewcvs
            default: dark 
    -s, --show
        Optional-> Show page after creation.
            default: no show
    -m, --markup
        Optional-> html, css, xhtml
            css, xhtml also support external stylesheets (-e,--external)
            default: HTML
    -e, --external
        Optional-> use with css, xhtml
            Writes an style sheet instead of embedding it in the page
            saves it as pystyle.css in the same directory.
            html markup will silently ignore this flag.
    -H, --header
        Opional-> add a page header to the top of the output
        -H
            Builtin header (name,date,hrule)
        --header
            You must specify a filename.
            The header file must be valid html
            and must handle its own font colors.
            ex. --header c:/tmp/header.txt
    -F, --footer
        Opional-> add a page footer to the bottom of the output
        -F 
            Builtin footer (hrule,name,date)
        --footer
            You must specify a filename.
            The footer file must be valid html
            and must handle its own font colors.
            ex. --footer c:/tmp/footer.txt  
    -l, --linenumbers
        Optional-> default is no linenumbers
            Adds line numbers to the start of each line in the code.
   --convertpage
        Given a webpage that has code embedded in tags it will
            convert embedded code to colorized html. 
            (see pageconvert for details)
-----------------------------------------------------------------------------
 Option usage:
  # Test and show pages
     python PySourceColor.py -t -s
  # Test and only show profile results
     python PySourceColor.py -t -p
  # Colorize all .py,.pyw files in cwdir you can also use: (.,cwd)
     python PySourceColor.py -i .
  # Using long options w/ =
     python PySourceColor.py --in=c:/myDir/my.py --color=lite --show
  # Using short options w/out =
     python PySourceColor.py -i c:/myDir/  -c idle -m css -e
  # Using any mix
     python PySourceColor.py --in . -o=c:/myDir --show
  # Place a custom header on your files
     python PySourceColor.py -i . -o c:/tmp -m xhtml --header c:/header.txt
-----------------------------------------------------------------------------
 Stdio usage:
  # Stdio using no options
     python PySourceColor.py < c:/MyFile.py >> c:/tmp/MyFile.html
  # Using stdin alone automatically uses stdout for output: (stdin,-)
     python PySourceColor.py -i- < c:/MyFile.py >> c:/tmp/myfile.html
  # Stdout can also be written to directly from a file instead of stdin
     python PySourceColor.py -i c:/MyFile.py -m css -o- >> c:/tmp/myfile.html
  # Stdin can be used as input , but output can still be specified
     python PySourceColor.py -i- -o c:/pydoc.py.html -s < c:/Python22/my.py
_____________________________________________________________________________
_printinfo(message, quiet)
Helper to print messages
_test(show=0, quiet=0)
Test the parser and most of the functions.
 
There are 19 test total(eight colorschemes in three diffrent markups,
and a str2file test. Most functions are tested by this.
cli()
Handle command line args and redirections
convert(source, outdir=None, colors=None, show=0, markup='html', quiet=0, header=None, footer=None, linenumbers=0, form=None)
Takes a file or dir as input and places the html in the outdir.
 
If outdir is none it defaults to the input dir
escape(text)
escape text for html. similar to cgi.escape
pageconvert(path, out=None, colors={1: ('', '#333333', ''), 2: ('b', '#DD2200', ''), 50: ('b', '#000000', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('', '#007F00', ''), 257: ('b', '#000000', ''), 258: ('', '#608060', ''), 259: ('b', '#0000FF', ''), 260: ('b', '#9C7A00', ''), 261: ('b', '#0000AF', ''), ...}, markup='xhtml', linenumbers=0, dosheet=1, tagstart='<py>', tagend='</py>', stylesheet='pystyle', show=1, returnstr=0)
This function can colorize Python source
 
that is written in a webpage enclosed in tags.
path2file(sourcePath, out=None, colors=None, show=0, markup='html', quiet=0, form=None, header=None, footer=None, linenumbers=0, count=1)
 Converts python source to html file
path2html(sourcepath, colors=None, markup='html', header=None, footer=None, linenumbers=0, form=None)
Converts code(file) to colorized HTML. Returns an HTML string.
 
form='code',or'snip' (for "<pre>yourcode</pre>" only)
colors=null,mono,lite,dark,dark2,idle,or pythonwin
path2stdout(sourcepath, title='', colors=None, markup='html', header=None, footer=None, linenumbers=0, form=None)
Converts code(file) to colorized HTML. Writes to stdout.
 
form='code',or'snip' (for "<pre>yourcode</pre>" only)
colors=null,mono,lite,dark,dark2,idle,or pythonwin
showpage(path)
Helper function to open webpages
str2css(sourcestring, colors=None, title='', markup='css', header=None, footer=None, linenumbers=0, form=None)
Converts a code string to colorized CSS/HTML. Returns CSS/HTML string
 
If form != None then this will return (stylesheet_str, code_str)
colors=null,mono,lite,dark,dark2,idle,or pythonwin
str2file(sourcestring, outfile, colors=None, title='', markup='html', header=None, footer=None, linenumbers=0, show=0, dosheet=1, form=None)
Converts a code string to a file.
 
makes no attempt at correcting bad pathnames
str2html(sourcestring, colors=None, title='', markup='html', header=None, footer=None, linenumbers=0, form=None)
Converts a code(string) to colorized HTML. Returns an HTML string.
 
form='code',or'snip' (for "<pre>yourcode</pre>" only)
colors=null,mono,lite,dark,dark2,idle,or pythonwin
str2markup(sourcestring, colors=None, title='', markup='xhtml', header=None, footer=None, linenumbers=0, form=None)
 Convert code strings into ([stylesheet or None], colorized string)
str2stdout(sourcestring, colors=None, title='', markup='html', header=None, footer=None, linenumbers=0, form=None)
Converts a code(string) to colorized HTML. Writes to stdout.
 
form='code',or'snip' (for "<pre>yourcode</pre>" only)
colors=null,mono,lite,dark,dark2,idle,or pythonwin
tagreplace(sourcestr, colors={1: ('', '#333333', ''), 2: ('b', '#DD2200', ''), 50: ('b', '#000000', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('', '#007F00', ''), 257: ('b', '#000000', ''), 258: ('', '#608060', ''), 259: ('b', '#0000FF', ''), 260: ('b', '#9C7A00', ''), 261: ('b', '#0000AF', ''), ...}, markup='xhtml', linenumbers=0, dosheet=1, tagstart='<py>', tagend='</py>', stylesheet='pystyle')
This is a helper function for pageconvert. Returns css, page.
unescape(text)
unsecape escaped text
walkdir(dir)
Return a list of .py and .pyw files from a given directory.
 
This function can be written as a generator Python 2.3, or a genexp
in Python 2.4. But 2.2 and 2.1 would be left out....
 
Data
             ARGS = 257
BRACKETS = 277
CLASS_NAME = 259
COMMENT = 52
DECORATOR = 275
DECORATOR_NAME = 276
DEF_NAME = 260
DOUBLECOMMENT = 258
DOUBLEQUOTE = 265
DOUBLEQUOTE_R = 266
DOUBLEQUOTE_U = 267
ERRORTOKEN = 51
KEYWORD = 261
LINE = 279
MARKUPDICT = {1: 'py_name', 2: 'py_num', 50: 'py_op', 51: 'py_err', 52: 'py_com', 257: 'py_args', 258: 'py_dcom', 259: 'py_clsn', 260: 'py_defn', 261: 'py_key', ...}
MATH_OPERATOR = 278
NAME = 1
NUMBER = 2
OPERATOR = 50
PAGEBACKGROUND = 274
SINGLEQUOTE = 262
SINGLEQUOTE_R = 263
SINGLEQUOTE_U = 264
TEXT = 280
TOKEN_NAMES = {'ARGS': 257, 'BRACKETS': 277, 'CLASS_NAME': 259, 'COMMENT': 52, 'DECORATOR': 275, 'DECORATOR_NAME': 276, 'DEF_NAME': 260, 'DOUBLECOMMENT': 258, 'DOUBLEQUOTE': 265, 'DOUBLEQUOTE_R': 266, ...}
TRIPLEDOUBLEQUOTE = 271
TRIPLEDOUBLEQUOTE_R = 272
TRIPLEDOUBLEQUOTE_U = 273
TRIPLESINGLEQUOTE = 268
TRIPLESINGLEQUOTE_R = 269
TRIPLESINGLEQUOTE_U = 270
__all__ = ['ERRORTOKEN', 'DECORATOR_NAME', 'DECORATOR', 'ARGS', 'TOKEN_NAMES', 'NAME', 'NUMBER', 'OPERATOR', 'COMMENT', 'MATH_OPERATOR', 'DOUBLECOMMENT', 'CLASS_NAME', 'DEF_NAME', 'KEYWORD', 'BRACKETS', 'SINGLEQUOTE', 'SINGLEQUOTE_R', 'SINGLEQUOTE_U', 'DOUBLEQUOTE', 'DOUBLEQUOTE_R', ...]
__author__ = 'M.E.Farmer Jr.'
__credits__ = 'This was originally based on a python recipe\nsub...in my head.\nM.E.Farmer 2004, 2005\nPython license\n'
__date__ = '15 February 2005'
__file__ = r'.\PySourceColor.pyc'
__name__ = 'PySourceColor'
__title__ = 'PySourceColor'
__version__ = '2.0'
dark = {1: ('', '#DDDDDD', ''), 2: ('', '#FF0000', ''), 50: ('b', '#FAF785', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('', '#45FCA0', ''), 257: ('b', '#CCCCEE', ''), 258: ('i', '#A7C7A9', ''), 259: ('b', '#B666FD', ''), 260: ('b', '#EBAE5C', ''), 261: ('b', '#8680FF', ''), ...}
dark2 = {1: ('', '#C0C0C0', ''), 2: ('b', '#00FF00', ''), 50: ('b', '#FF090F', ''), 51: ('', '#FF0000', ''), 52: ('i', '#D0D000', '#522000'), 257: ('b', '#EEEEEE', ''), 258: ('i', '#D0D000', '#522000'), 259: ('b', '#EE4080', ''), 260: ('b', '#FF8040', ''), 261: ('b', '#4726E1', ''), ...}
defaultColors = {1: ('', '#333333', ''), 2: ('b', '#DD2200', ''), 50: ('b', '#000000', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('', '#007F00', ''), 257: ('b', '#000000', ''), 258: ('', '#608060', ''), 259: ('b', '#0000FF', ''), 260: ('b', '#9C7A00', ''), 261: ('b', '#0000AF', ''), ...}
idle = {1: ('', '#000000', ''), 2: ('', '#000000', ''), 50: ('', '#000000', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('', '#DD0000', ''), 258: ('', '#DD0000', ''), 259: ('', '#0000FF', ''), 260: ('', '#0000FF', ''), 261: ('', '#FF7700', ''), 262: ('', '#00AA00', ''), ...}
lite = {1: ('', '#333333', ''), 2: ('b', '#DD2200', ''), 50: ('b', '#000000', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('', '#007F00', ''), 257: ('b', '#000000', ''), 258: ('', '#608060', ''), 259: ('b', '#0000FF', ''), 260: ('b', '#9C7A00', ''), 261: ('b', '#0000AF', ''), ...}
mono = {1: ('', '#000000', ''), 2: ('b', '#000000', ''), 50: ('b', '#000000', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('i', '#000000', ''), 257: ('b', '#000000', ''), 258: ('b', '#000000', ''), 259: ('bu', '#000000', ''), 260: ('b', '#000000', ''), 261: ('b', '#000000', ''), ...}
null = {1: ('', '#000000', ''), 2: ('', '#000000', ''), 50: ('', '#000000', ''), 51: ('', '#000000', ''), 52: ('', '#000000', ''), 257: ('', '#000000', ''), 258: ('', '#000000', ''), 259: ('', '#000000', ''), 260: ('', '#000000', ''), 261: ('', '#000000', ''), ...}
pythonwin = {1: ('', '#303030', ''), 2: ('', '#008080', ''), 50: ('', '#000000', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('', '#007F00', ''), 257: ('', '#000000', ''), 258: ('', '#7F7F7F', ''), 259: ('b', '#0000FF', ''), 260: ('b', '#007F7F', ''), 261: ('b', '#000080', ''), ...}
viewcvs = {1: ('', '#000000', ''), 2: ('', '#000000', ''), 50: ('', '#000000', ''), 51: ('s#FF0000', '#FF8080', ''), 52: ('i', '#b22222', ''), 257: ('', '#000000', ''), 258: ('i', '#b22222', ''), 259: ('', '#000000', ''), 260: ('b', '#0000ff', ''), 261: ('b', '#a020f0', ''), ...}
 
Author
             M.E.Farmer Jr.
 
Credits
             This was originally based on a python recipe
submitted by Jürgen Hermann to ASPN. Now based on the voices in my head.
M.E.Farmer 2004, 2005
Python license