Skip to main content

HTML transcription functions.

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 tediuous and error prone entity escaping.

  • 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

  • BR: a convenience token for <br/>, which I use a lot

A “token” in this scheme is:

  • a string: transcribed safely to HTML

  • an int or float: transcribed safely to HTML

  • a preformed token object with .tag (a string) and .attrs (a mapping) attributes

  • 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

    • because a string like “&foo” gets its “&” transcribed into the entity “&amp;”, a single element list whose tag begins with “&” encodes an entity, example: [”&lt;”]

Example:

from cs.html import puttoken, BR
[...]
table = ['TABLE', {'width': '80%'},
         ['TR',
          ['TD', 'a truism'],
          ['TD', '1 < 2']
         ]
        ]
[...]
puttoken(sys.stdout,
          'Here is a line with a line break.', BR,
          'Here is a trite table.',
          table)

Release files for cs.html 20150721

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cs.html 20150721
File Size Uploaded
cs.html-20150721.tar.gz 4.3 kB Details

Release files / cs.html-20150721.tar.gz

Download URL cs.html-20150721.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0ed36eda20b526a03c9860d31646740ae0daf8953c9332ea5cde899b7debaf97
BLAKE2b-256 checksum
How to use checksums
864aff73b9e99b242aa1377ce6bbbacf6ad11ab1bb368b7ea1e7211b954341f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

20150721 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page