Skip to main content

easy HTML and XHTML transcription

Project description

Malformed markup is enraging. Therefore, when I must generate HTML I construct a token structure using natural Python objects (strings, lists, dicts) and use this module to transcribe them to syntacticly correct HTML. This also avoids lots of tedious and error prone entity escaping.

A “token” in this scheme is:

  • a string: transcribed safely to HTML, eg “some text here”
  • an int or float: transcribed safely to HTML, eg 1 or 2.5.
  • a sequence: an HTML tag: element 0 is the tag name, element 1 (if a mapping) is the element attributes, any further elements are enclosed tokens, eg: [‘H1’, {‘align’: ‘centre’}, ‘Heading Text’]
    • because a string like “&foo” gets its “&” transcribed into the entity “&”, a single element list whose tag begins with “&” encodes an entity, example: [“<”]
  • a preformed token object with .tag (a string) and .attrs (a mapping) attributes

Core functions:

  • transcribe(*tokens): a generator yielding strings comprising HTML
  • xtranscribe(*tokens): a generator yielding strings comprising XHTML
  • attrquote(s): quote the string s for use as a tag attribute according to HTML 4.01 section 3.2.2
  • nbsp(s): convert s to nonbreaking text: generator yielding tokens with whitespace converted to   entities

Convenience routines:

  • transcribe_s(*tokens): convert tokens into a string containing HTML
  • xtranscribe_s(*tokens): convert tokens into a string containing XHTML


  • tok2s: transcribe tokens to a string; trivial wrapper for puttok
  • puttok: transcribe tokens to a file
  • text2s: transcribe a string to an HTML-safe string; trivial wrapper for puttext
  • puttext: transcribe a string as HTML-safe text to a file


from cs.html import transcribe, transcribe_s, xtranscribe, puttok
table = ['TABLE', {'width': '80%'},
          ['TD', 'a truism'],
          ['TD', '1 < 2']
          ['TD', 'a couple'],
          ['TD', 'A & B']
prose_with_table = [
          'Here is a line with a line break.', ['BR'],
          'Here is a trite table:',
print('Here is the table's HTML:', transcribe_s(table))
# write HTML tokens to a file
for s in transcribe(['H1', {'align': 'left'}, 'Prose'], *prose_with_table):
# write XHTML tokens to a file
for s in xtranscribe(*prose_with_table):

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for cs.html, version 20160828
Filename, size File type Python version Upload date Hashes
Filename, size cs.html-20160828.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page