-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
33 lines (23 loc) · 716 Bytes
/
main.py
File metadata and controls
33 lines (23 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from functools import singledispatch
from collections import abc
import html
import numbers
@singledispatch
def htmlize(obj: object) -> str:
return f"<pre>{html.escape(repr(obj))}</pre>"
@htmlize.register
def _(text: str) -> str:
content = html.escape(text).replace("\n", "<br />\n")
return f"<p>{content}</p>"
@htmlize.register
def _(seq: abc.Sequence):
inner = "</li>\n\t<li>".join(htmlize(item) for item in seq)
return "<ul>\n\t<li>" + inner + "</li>\n</ul>"
@htmlize.register
def _(n: numbers.Integral):
return f"<pre>{n} (0x{n:x})</pre>"
if __name__ == "__main__":
print(htmlize("Hello\nElahe"))
print(htmlize([1, 2, 3]))
print(htmlize(12))
print(htmlize(max))