Package epydoc :: Package docwriter :: Module html_colorize
[hide private]
[frames] | no frames]

Module html_colorize
source code

Functions to produce colorized HTML code for various objects. Currently, colorize defines functions to colorize regular expressions and doctest blocks.

Classes [hide private]
PythonSourceColorizer A class that renders a python module's source code into HTML pages.

Functions [hide private]
string colorize_re(regexp)
string _colorize_re(tree, noparen=0)
Recursively descend the given regexp parse tree to produce the HTML code for a colorized version of the regexp.
string colorize_doctestblock(str)
string _doctest_sub(match)
This helper function is used by colorize_doctestblock to add colorization to matching expressions.

Variables [hide private]
_PROMPT_RE The regular expression used to find Python prompts (">>>" and "...") in doctest blocks.
_DOCTEST_RE The regular expression used by _doctest_sub to colorize doctest blocks.
PYSRC_JAVASCRIPTS Javascript code for the PythonSourceColorizer
PYSRC_EXPANDTO_JAVASCRIPT  
HDR  
FOOT  
s  
f  
    Regular Expression Tags
RE_TAG The CSS class for colorizing regular expressions.
ANY_TAG The CSS class for colorizing "." in regexps.
ESCAPE_TAG The CSS class for colorizing escaped characters (such as r"\(") in regexps.
CATEGORY_TAG The CSS class for colorizing character categories (such as r"\d")) in regexps.
AT_TAG The CSS class for colorizing character locations (such as "^") in regexps.
BRANCH_TAG The CSS class for colorizing "|" in regexps.
STAR_TAG The CSS class for colorizing "*" and "*?" in regexps.
PLUS_TAG The CSS class for colorizing "+" and "+?" in regexps.
QMRK_TAG The CSS class for colorizing "?" and "??" in regexps.
RNG_TAG The CSS class for colorizing repeat ranges (such as "a{3,8}") in regexps.
PAREN_TAG The CSS class for colorizing parenthases in regexps.
CHOICE_TAG The CSS class for colorizing character choice expressions (such as "[abc]") in regexps.
ASSERT_TAG The CSS class for colorizing assertions (such as "(?=abc)") in regexps.
REF_TAG The CSS class for colorizing references (such as r"\1") in regexps.

Function Details [hide private]

colorize_re(regexp)

source code 
call graph 
Parameters:
  • regexp (SRE_Pattern or string) - The regular expression to colorize.
Returns: string
The HTML code for a colorized version of the pattern for the given SRE regular expression. If colorize_re can't figure out how to colorize the regexp, then it will simply return the (uncolorized) pattern, with '&', '<', and '>' escaped as HTML entities. The colorized expression includes spans with the following css classes:
  • re: The entire regular expression.
  • re-char: Special characters (such as '.', '\('), character categories (such as '\w'), and locations (such as '\b').
  • re-op: Operators (such as '*' and '|').
  • re-group: Grouping constructs (such as '(...)').
  • re-ref References (such as '\1')
Raises:
  • sre_constants.error - If regexp is not a valid regular expression.

_colorize_re(tree, noparen=0)

source code 
call graph 
Recursively descend the given regexp parse tree to produce the HTML code for a colorized version of the regexp.
Parameters:
  • tree (sre_parse.SubPattern) - The regexp parse tree for the regexp that should be colorized.
  • noparen (boolean) - If true, then don't include parenthases around the expression in tree, even if it contains multiple elements.
Returns: string
The HTML code for a colorized version of tree

colorize_doctestblock(str)

source code 
call graph 
Parameters:
  • str (string) - The contents of the doctest block to be colorized.
Returns: string
The HTML code for a colorized version of a given doctest block. In particular, this identifies spans with the following css classes:
  • py-src: The Python source code.
  • py-prompt: The ">>>" and "..." prompts.
  • py-string: Strings in the Python source code.
  • py-comment: Comments in the Python source code.
  • py-keyword: Keywords in the Python source code.
  • py-output: Python's output (lines without a prompt).
The string that is passed to colorize_doctest should already have HTML characters escaped (e.g., ">" should be encoded as "&gt;").

_doctest_sub(match)

source code 
call graph 
This helper function is used by colorize_doctestblock to add colorization to matching expressions. It is called by _DOCTEST_RE.sub with an expression that matches _DOCTEST_RE.
Returns: string
The HTML code for the colorized expression.

See Also: _DOCTEST_RE


Variables Details [hide private]

RE_TAG

The CSS class for colorizing regular expressions.
Value:
're'                                                                   
      

ANY_TAG

The CSS class for colorizing "." in regexps.
Value:
're-char'                                                              
      

ESCAPE_TAG

The CSS class for colorizing escaped characters (such as r"\(") in regexps.
Value:
're-char'                                                              
      

CATEGORY_TAG

The CSS class for colorizing character categories (such as r"\d")) in regexps.
Value:
're-char'                                                              
      

AT_TAG

The CSS class for colorizing character locations (such as "^") in regexps.
Value:
're-char'                                                              
      

BRANCH_TAG

The CSS class for colorizing "|" in regexps.
Value:
're-op'                                                                
      

STAR_TAG

The CSS class for colorizing "*" and "*?" in regexps.
Value:
're-op'                                                                
      

PLUS_TAG

The CSS class for colorizing "+" and "+?" in regexps.
Value:
're-op'                                                                
      

QMRK_TAG

The CSS class for colorizing "?" and "??" in regexps.
Value:
're-op'                                                                
      

RNG_TAG

The CSS class for colorizing repeat ranges (such as "a{3,8}") in regexps.
Value:
're-op'                                                                
      

PAREN_TAG

The CSS class for colorizing parenthases in regexps.
Value:
're-group'                                                             
      

CHOICE_TAG

The CSS class for colorizing character choice expressions (such as "[abc]") in regexps.
Value:
're-group'                                                             
      

ASSERT_TAG

The CSS class for colorizing assertions (such as "(?=abc)") in regexps.
Value:
're-group'                                                             
      

REF_TAG

The CSS class for colorizing references (such as r"\1") in regexps.
Value:
're-ref'                                                               
      

_PROMPT_RE

The regular expression used to find Python prompts (">>>" and "...") in doctest blocks.
Value:
(^\s*(&gt;&gt;&gt;|\.\.\.)(\s|$))                                      
      

_DOCTEST_RE

The regular expression used by _doctest_sub to colorize doctest blocks.
Value:
(&quot;&quot;&quot;(&quot;&quot;&quot;|.*?((?!&quot;).)&quot;&quot;&qu\
ot;))|(&quot;(&quot;|.*?((?!&quot;).)&quot;))|('''('''|.*?[^\\']'''))|\
('('|.*?[^\\']'))|(#.*?$)|(\bdel\b)|(\bfrom\b)|(\blambda\b)|(\breturn\\
b)|(\band\b)|(\bor\b)|(\bis\b)|(\bglobal\b)|(\bnot\b)|(\btry\b)|(\bbre\
ak\b)|(\belse\b)|(\bif\b)|(\belif\b)|(\bwhile\b)|(\bclass\b)|(\bexcept\
\b)|(\bimport\b)|(\bpass\b)|(\braise\b)|(\bcontinue\b)|(\bfinally\b)|(\
\bin\b)|(\bprint\b)|(\bdef\b)|(\bfor\b)                                
      

PYSRC_JAVASCRIPTS

Javascript code for the PythonSourceColorizer
Value:
'''function expand(id) {
  var elt = document.getElementById(id+"-expanded");
  if (elt) elt.style.display = "block";
  var elt = document.getElementById(id+"-expanded-linenums");
  if (elt) elt.style.display = "block";
  var elt = document.getElementById(id+"-collapsed");
  if (elt) { elt.innerHTML = ""; elt.style.display = "none"; }
  var elt = document.getElementById(id+"-collapsed-linenums");
...                                                                    
      

PYSRC_EXPANDTO_JAVASCRIPT

Value:
'''<script type="text/javascript">
<!--
expandto(location.href);
// -->
</script>
'''                                                                    
      

HDR

Value:
'''<?xml version="1.0" encoding="ascii"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang=\
"en">
        <head>
          <title>$title$</title>
          <link rel="stylesheet" href="epydoc.css" type="text/css" />
...                                                                    
      

FOOT

Value:
'</body></html>'                                                       
      

s

Value:
PythonSourceColorizer('/tmp/fo.py','epydoc.apidoc').colorize()         
      

f

Value:
codecs.open('/home/edloper/public_html/color3.html','w','ascii','xmlch\
arrefreplace')