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 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
Obsolete:
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
Example:
from cs.html import transcribe, transcribe_s, xtranscribe, puttok
[...]
table = ['TABLE', {'width': '80%'},
['TR',
['TD', 'a truism'],
['TD', '1 < 2']
]
['TR',
['TD', 'a couple'],
['TD', 'A & B']
]
]
prose_with_table = [
'Here is a line with a line break.', ['BR'],
'Here is a trite table:',
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):
fp.write(s)
[...]
# write XHTML tokens to a file
for s in xtranscribe(*prose_with_table):
fp.write(s)
Metadata
Release files for cs.html 20160828
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cs.html-20160828.tar.gz | 5.3 kB | Without provenance | Details for cs.html-20160828.tar.gz |
Release files / cs.html-20160828.tar.gz
| Download URL | cs.html-20160828.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
922bc765d288675576f193aa4c5018ecf0902dd4a9e573fbce2cb98e788065f4
|
|
BLAKE2b-256 checksum How to use checksums |
606b8fe5444a5da94edceb55c700b2aede2e903a3636668ac96233c7064c16c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for cs.html 20160828
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.