module homeblog.py2html#

Short summary#

module ensae_teaching_cs.homeblog.py2html

Mark-up Python code file using HTML for syntax highlighting. Syntax highlighting rules are in the spirit of IDLE.

Unless the -r 0 option is used it will also format the code by applying some of the PEP8 spacing guidelines to expressions and assignments.

For those that want a GUI you can try py2htmTk.pyw - (it’s minimal but functional).

::
USAGE in command line mode:

py2html [options] [-i filename]|-I]

OPTIONS:
-h

Print this command line summary

--help

Print more detailed help on styles and revision info.

-o filename Output file (default is « py2html.html ») -i filename Source file. See -I. -p filename HTML page template (must include a %s for inserting

the code). If not specified then a default is used.

-s filename Use a style-file otherwise use built in styles

(see –help for details)

-r 0|1|2 Reformat expressions and definitions.

-r 0 No formatting -r 1 Format as a = 3+4; b = [1, 2, 3] (default) -r 2 Format as a = 3 + 4; b = [1 , 2 , 3]

-R

Replace newlines with <br>, tabs and multi-spaces with &nbsp;

-B

Just make a block (ignores -p)

-O

Print to sys.stdout (ignores -o, no file created)

-I

Use stdin as source file (ignore -i option)

-E 0|1|2|3|4 0 - Don’t do entity substitution.

1 - Substitute < > and & (default) 2 - Substitute < > & and  » 3 - Substitute <> &  » and “ 4 - Substitute all non-ASCIIalphanumeric

source on GitHub

Functions#

function

truncated documentation

apply_style

Supplied with an index this function applies the style using the format rules and returns a formatted verison of …

cmdLine

This is the function that handles command line mode

file2HTML

Reads a file and returns the contents as a string, highlighted with HTML styles. This function uses the …

makeBlock

Applies the block tags to text

readStyleFile

Read a style file and return a style dictionary. The file format is

replaceCodes

Helper function that does the \n and space substition returning the changed text.

substituteEntities

based on level setting do entity substitution and return revised. iso8859-1 ??:

Documentation#

Mark-up Python code file using HTML for syntax highlighting. Syntax highlighting rules are in the spirit of IDLE.

Unless the -r 0 option is used it will also format the code by applying some of the PEP8 spacing guidelines to expressions and assignments.

For those that want a GUI you can try py2htmTk.pyw - (it’s minimal but functional).

::
USAGE in command line mode:

py2html [options] [-i filename]|-I]

OPTIONS:
-h

Print this command line summary

--help

Print more detailed help on styles and revision info.

-o filename Output file (default is « py2html.html ») -i filename Source file. See -I. -p filename HTML page template (must include a %s for inserting

the code). If not specified then a default is used.

-s filename Use a style-file otherwise use built in styles

(see –help for details)

-r 0|1|2 Reformat expressions and definitions.

-r 0 No formatting -r 1 Format as a = 3+4; b = [1, 2, 3] (default) -r 2 Format as a = 3 + 4; b = [1 , 2 , 3]

-R

Replace newlines with <br>, tabs and multi-spaces with &nbsp;

-B

Just make a block (ignores -p)

-O

Print to sys.stdout (ignores -o, no file created)

-I

Use stdin as source file (ignore -i option)

-E 0|1|2|3|4 0 - Don’t do entity substitution.

1 - Substitute < > and & (default) 2 - Substitute < > & and  » 3 - Substitute <> &  » and “ 4 - Substitute all non-ASCIIalphanumeric

source on GitHub

ensae_teaching_cs.homeblog.py2html.apply_style(index, token, start, src, format, style, entity='1')#

Supplied with an index this function applies the style using the format rules and returns a formatted verison of the token. “start” and “src” are (not used yet) for appliance of intelligent line breaks for long lines entity is the replace entities flag

source on GitHub

ensae_teaching_cs.homeblog.py2html.cmdLine()#

This is the function that handles command line mode

source on GitHub

ensae_teaching_cs.homeblog.py2html.file2HTML(file_name, format, style, Replace, entity='1', encoding='utf-8')#

Reads a file and returns the contents as a string, highlighted with HTML styles. This function uses the output of the tokenize module to decide what to colour. It calls apply_style with the token index.

  • If format == “0” then the code will display as the author expected it to.

  • If format == “1” then spaces are added and removed around expressions to standardise the format.

  • If format == “2” as “1” but different rules

  • If style == style dictionary. Replace (boolean)

  • If True then replace \n with <br>, multiple spaces with &nbsp<space>combinations;

source on GitHub

ensae_teaching_cs.homeblog.py2html.makeBlock(data)#

Applies the block tags to text

source on GitHub

ensae_teaching_cs.homeblog.py2html.readStyleFile(filename)#

Read a style file and return a style dictionary. The file format is:

#User editable style substitutions
# This style-file assumes a css is used.
# Format:
# styleName | start | end
# examples:
# block | <pre> | </pre>
# key | <span class="key">'| </span>
# str | <span style = "color: #00AA00"> | </span>

block | <pre class="python_code" id="pycode"> | </pre>
key | <span class="py_key"> | </span>
num | <span class="py_num"> | </span>
str | <span class="py_str"> | </span>
op  | <span class="py_op">  | </span>
com | <span class="py_com"> | </span>
res | <span class="py_res"> | </span>
def | <span class="py_def"> | </span>
brk | <span class="py_brk"> | </span>

source on GitHub

ensae_teaching_cs.homeblog.py2html.replaceCodes(text='')#

Helper function that does the \n and space substition returning the changed text.

source on GitHub

ensae_teaching_cs.homeblog.py2html.substituteEntities(token, level='1', char_set=None)#

based on level setting do entity substitution and return revised. iso8859-1 ??:

  • « 0 » : Don’t do entity substitution.

  • « 1 » : Substitute < > and & (default)

  • « 2 » : Substitute < > & and "

  • « 3 » : Substitute <> & " and '

  • « 4 » : Substitute all non-ASCIIalphanumeric char_set not implemented yet

source on GitHub