-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathtemplate_vars.py
56 lines (48 loc) · 1.41 KB
/
template_vars.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
44
45
46
47
48
49
50
51
52
53
54
55
56
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):
text = til["title"] + " " + til["body"]
text = non_alphanumeric.sub(" ", text)
text = multi_spaces.sub(" ", text)
words = list(set(text.lower().strip().split()))
sql = """
select
til.topic, til.slug, til.title, til.created
from
til
join til_fts on til.rowid = til_fts.rowid
where
til_fts match :words
and not (
til.slug = :slug
and til.topic = :topic
)
order by
til_fts.rank
limit
5
"""
result = await datasette.get_database().execute(
sql,
{"words": " OR ".join(words), "slug": til["slug"], "topic": til["topic"]},
)
return result.rows
return {
"q": request.args.get("q", ""),
"highlight": highlight,
"first_paragraph": first_paragraph,
"related_tils": related_tils,
}