Package epydoc :: Package markup :: Module doctest :: Class DoctestColorizer
[hide private]
[frames] | no frames]

ClassType DoctestColorizer

source code

Known Subclasses:

An abstract base class for performing syntax highlighting on doctest blocks and other bits of Python code. Subclasses should provide definitions for:

Instance Methods [hide private]
 
colorize_inline(self, s)
Colorize a string containing Python code.
source code
 
colorize_codeblock(self, s)
Colorize a string containing only Python code.
source code
 
colorize_doctest(self, s, strip_directives=False)
Colorize a string containing one or more doctest examples.
source code
call graph 
 
subfunc(self, match) source code
call graph 
 
markup(self, s, tag)
Apply syntax highlighting to a single substring from a doctest block.
source code
Class Variables [hide private]
  PREFIX = None
A string that is added to the beginning of the strings returned by colorize_codeblock and colorize_doctest.
  SUFFIX = None
A string that is added to the end of the strings returned by colorize_codeblock and colorize_doctest.
  NEWLINE = '\n'
The string used to divide lines
  _KEYWORDS = ['and', 'del', 'for', 'is', 'raiseassert', 'elif',...
A list of the names of all Python keywords.
  _BUILTINS = ['clear', 'copy', 'fromkeys', 'get', 'has_key', 'i...
A list of all Python builtins.
  _KEYWORD_GRP = '\\band\\b|\\bdel\\b|\\bfor\\b|\\bis\\b|\\brais...
A regexp group that matches keywords.
  _BUILTIN_GRP = '(?<!\\.)(?:\\bclear\\b|\\bcopy\\b|\\bfromkeys\...
A regexp group that matches Python builtins.
  _STRING_GRP = '("""("""|.*?((?!").)"""))|("("|.*?((?!").)"))|(...
A regexp group that matches Python strings.
  _COMMENT_GRP = '(#.*?$)'
A regexp group that matches Python comments.
  _PROMPT1_GRP = '^[ \\t]*>>>(?:[ \\t]|$)'
A regexp group that matches Python ">>>" prompts.
  _PROMPT2_GRP = '^[ \\t]*\\.\\.\\.(?:[ \\t]|$)'
A regexp group that matches Python "..." prompts.
  _DEFINE_GRP = '\\b(?:def|class)[ \\t]+\\w+'
A regexp group that matches function and class definitions.
  PROMPT_RE = re.compile(r'(?ms)(^[ \t]*>>>(?:[ \t]|$)|[ \t]*\.\...
A regexp that matches Python prompts
  PROMPT2_RE = re.compile(r'(?ms)(^[ \t]*\.\.\.(?:[ \t]|$))')
A regexp that matches Python "..." prompts.
  EXCEPT_RE = re.compile(r'(?ms)^[ \t]*Traceback \(most recent c...
A regexp that matches doctest exception blocks.
  DOCTEST_DIRECTIVE_RE = re.compile(r'#[ \t]*doctest:.*')
A regexp that matches doctest directives.
  DOCTEST_RE = re.compile(r'(?ms)(.*?)((?P<STRING>("""("""|.*?((...
A regexp that matches all of the regions of a doctest block that should be colored.
  DOCTEST_EXAMPLE_RE = re.compile(r'(?mx)(?P<source>(?:^(?P<inde...
This regular expression is used to find doctest examples in a string.
  _BI = 'values'
  _KW = 'as'
Method Details [hide private]

colorize_inline(self, s)

source code 

Colorize a string containing Python code. Do not add the PREFIX and SUFFIX strings to the returned value. This method is intended for generating syntax-highlighted strings that are appropriate for inclusion as inline expressions.

colorize_codeblock(self, s)

source code 

Colorize a string containing only Python code. This method differs from colorize_doctest in that it will not search for doctest prompts when deciding how to colorize the string.

markup(self, s, tag)

source code 

Apply syntax highlighting to a single substring from a doctest block. s is the substring, and tag is the tag that should be applied to the substring. tag will be one of the following strings:

  • prompt -- the Python PS1 prompt (>>>)
  • more -- the Python PS2 prompt (...)
  • keyword -- a Python keyword (for, if, etc.)
  • builtin -- a Python builtin name (abs, dir, etc.)
  • string -- a string literal
  • comment -- a comment
  • except -- an exception traceback (up to the next >>>)
  • output -- the output from a doctest block.
  • defname -- the name of a function or class defined by a def or class statement.
  • other -- anything else (does *not* include output.)

Class Variable Details [hide private]

PREFIX

A string that is added to the beginning of the strings returned by colorize_codeblock and colorize_doctest. Typically, this string begins a preformatted area.

Value:
None

SUFFIX

A string that is added to the end of the strings returned by colorize_codeblock and colorize_doctest. Typically, this string ends a preformatted area.

Value:
None

_KEYWORDS

A list of the names of all Python keywords. ('as' is included even though it is technically not a keyword.)

Value:
['and',
 'del',
 'for',
 'is',
 'raiseassert',
 'elif',
 'from',
 'lambda',
...

_BUILTINS

A list of all Python builtins.

Value:
['clear',
 'copy',
 'fromkeys',
 'get',
 'has_key',
 'items',
 'iteritems',
 'iterkeys',
...

_KEYWORD_GRP

A regexp group that matches keywords.

Value:
'\\band\\b|\\bdel\\b|\\bfor\\b|\\bis\\b|\\braiseassert\\b|\\belif\\b|\\
\bfrom\\b|\\blambda\\b|\\breturnbreak\\b|\\belse\\b|\\bglobal\\b|\\bno\
t\\b|\\btryclass\\b|\\bexcept\\b|\\bif\\b|\\bor\\b|\\bwhilecontinue\\b\
|\\bexec\\b|\\bimport\\b|\\bpass\\b|\\byielddef\\b|\\bfinally\\b|\\bin\
\\b|\\bprint\\b|\\bas\\b'

_BUILTIN_GRP

A regexp group that matches Python builtins.

Value:
'(?<!\\.)(?:\\bclear\\b|\\bcopy\\b|\\bfromkeys\\b|\\bget\\b|\\bhas_key\
\\b|\\bitems\\b|\\biteritems\\b|\\biterkeys\\b|\\bitervalues\\b|\\bkey\
s\\b|\\bpop\\b|\\bpopitem\\b|\\bsetdefault\\b|\\bupdate\\b|\\bvalues\\\
b)'

_STRING_GRP

A regexp group that matches Python strings.

Value:
'("""("""|.*?((?!").)"""))|("("|.*?((?!").)"))|(\'\'\'(\'\'\'|.*?[^\\\\
\\']\'\'\'))|(\'(\'|.*?[^\\\\\']\'))'

PROMPT_RE

A regexp that matches Python prompts

Value:
re.compile(r'(?ms)(^[ \t]*>>>(?:[ \t]|$)|[ \t]*\.\.\.(?:[ \t]|$))')

EXCEPT_RE

A regexp that matches doctest exception blocks.

Value:
re.compile(r'(?ms)^[ \t]*Traceback \(most recent call last\):.*')

DOCTEST_RE

A regexp that matches all of the regions of a doctest block that should be colored.

Value:
re.compile(r'(?ms)(.*?)((?P<STRING>("""("""|.*?((?!").)"""))|("("|.*?(\
(?!").)"))|(\'\'\'(\'\'\'|.*?[^\\\']\'\'\'))|(\'(\'|.*?[^\\\']\')))|(?\
P<COMMENT>(#.*?$))|(?P<DEFINE>\b(?:def|class)[ \t]+\w+)|(?P<KEYWORD>\b\
and\b|del\b|for\b|is\b|raiseassert\b|elif\b|from\b|lambda\b|returnbrea\
k\b|else\b|global\b|not\b|tryclass\b|except\b|if\b|or\b|whilecontinue\\
b|exec\b|import\b|pass\b|yielddef\b|finally\b|in\b|print\b|as\b)|(?P<B\
UILTIN>(?<!\.)(?:\bclear\b|copy\b|fromkeys\b|get\b|has_key\b|items\b|i\
teritems\b|iterkeys\b|itervalues\b|keys\b|pop\b|popitem\b|setdefault\b\
...

DOCTEST_EXAMPLE_RE

This regular expression is used to find doctest examples in a string. This is copied from the standard Python doctest.py module (after the refactoring in Python 2.4+).

Value:
re.compile(r'(?mx)(?P<source>(?:^(?P<indent> *)>>>.*)(?:\n *\.\.\..*)*\
\n?)(?P<want>(?:(?! *$)(?! *>>>).*$\n?)*)')