-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
43 lines (40 loc) · 1.36 KB
/
__init__.py
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
34
35
36
37
38
39
40
41
42
43
import re
import filetype
from datasette import hookimpl
import jinja2
sequence_re = re.compile(r"((?:\\x[0-9a-f]{2})+)")
octet_re = re.compile(r"(\\x[0-9a-f]{2})")
@hookimpl(trylast=True)
def render_cell(value):
if not isinstance(value, bytes):
return None
# Attempt to guess filetype
suggestion = None
match = filetype.guess(value[:1000])
if match:
suggestion = "{} ({})".format(match.extension, match.mime)
encoded = repr(value)
# Ditch the b' and trailing '
if encoded.startswith("b'") and encoded.endswith("'"):
encoded = encoded[2:-1]
# Split it into sequences of octets and characters
chunks = sequence_re.split(encoded)
html = []
if suggestion:
html.append(
'<p style="margin-top: 0; font-family: monospace; font-size: 0.8em;">Suggestion: {}</p>'.format(
jinja2.escape(suggestion)
)
)
for chunk in chunks:
if sequence_re.match(chunk):
octets = octet_re.findall(chunk)
octets = [o[2:] for o in octets]
html.append(
'<code style="color: #999; font-family: monospace">{}</code>'.format(
" ".join(octets).upper()
)
)
else:
html.append(jinja2.escape(chunk.replace("\\\\", "\\")))
return jinja2.Markup(" ".join(html).strip())