-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathtemplate_vars.py
More file actions
49 lines (39 loc) · 1.19 KB
/
Copy pathtemplate_vars.py
File metadata and controls
49 lines (39 loc) · 1.19 KB
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
44
45
46
47
48
49
from datasette import hookimpl
from bs4 import BeautifulSoup as Soup
import html
import re
non_alphanumeric = re.compile(r"[^a-zA-Z0-9\s]")
multi_spaces = re.compile(r"\s+")
def first_paragraph(html):
soup = Soup(html, "html.parser")
return str(soup.find("p"))
def highlight(s):
s = html.escape(s)
s = s.replace("b4de2a49c8", "<strong>").replace("8c94a2ed4b", "</strong>")
return s
@hookimpl
def extra_template_vars(request, datasette):
async def related_tils(til):
path = til["path"]
sql = """
select
til.topic, til.slug, til.title, til.created
from til
join similarities on til.path = similarities.other_id
where similarities.id = :path
order by similarities.score desc limit 10
"""
result = await datasette.get_database().execute(
sql,
{"path": til["path"]},
)
return result.rows
return {
"q": request.args.get("q", ""),
"highlight": highlight,
"first_paragraph": first_paragraph,
"related_tils": related_tils,
}
@hookimpl
def prepare_connection(conn):
conn.create_function("first_paragraph", 1, first_paragraph)