r""" htmlize(): generic function example # BEGIN HTMLIZE_DEMO >>> htmlize({1, 2, 3}) # <1> '
{1, 2, 3}
' >>> htmlize(abs) '
<built-in function abs>
' >>> htmlize('Heimlich & Co.\n- a game') # <2> '

Heimlich & Co.
\n- a game

' >>> htmlize(42) # <3> '
42 (0x2a)
' >>> print(htmlize(['alpha', 66, {3, 2, 1}])) # <4> # END HTMLIZE_DEMO """ # BEGIN HTMLIZE from functools import singledispatch from collections import abc import numbers import html @singledispatch # <1> def htmlize(obj): content = html.escape(repr(obj)) return '
{}
'.format(content) @htmlize.register(str) # <2> def _(text): # <3> content = html.escape(text).replace('\n', '
\n') return '

{0}

'.format(content) @htmlize.register(numbers.Integral) # <4> def _(n): return '
{0} (0x{0:x})
'.format(n) @htmlize.register(tuple) # <5> @htmlize.register(abc.MutableSequence) def _(seq): inner = '\n
  • '.join(htmlize(item) for item in seq) return '' # END HTMLIZE