-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderhtml.py
45 lines (43 loc) · 1.32 KB
/
renderhtml.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
from html import escape
import re
def render_html(results, search, search_words):
bold_regex = re.compile(fr"\b({'|'.join(re.escape(escape(word)) for word in search_words)})\b", re.IGNORECASE)
def bold_search_terms(string):
return bold_regex.sub(r"<mark>\1</mark>", string)
return f"""<!doctype html>
<title>CLocKPJWaRP: {escape(search)}</title>
<style>
h1 {{
margin: 0;
font-family: monaco, lucida console, monospace;
}}
h2 {{
font-size: 1.25em;
margin-bottom: 0;
}}
p {{
margin-top: 0;
}}
input{{
border-radius: 5px;
border: solid thin black;
}}
#button{{
background-color: blue;
border-color: blue;
color: white;
}}
a.home {{
color: black;
text-decoration: none;
}}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="search" type="application/opensearchdescription+xml" title="CLocKPJWaRP" href="opensearch.xml">
<h1><a href="https://clockpjwarp.com" class="home">CLocKPJWaRP</a></h1>
<form action="search" method="get">
<input type="text" name="q" value="{escape(search)}">
<input type="submit" value="Find it" id="button">
</form>
""" + '\n'.join(f'''<h2><a href="{escape(str(url, encoding="UTF-8"))}">{bold_search_terms(escape(str(title, encoding="UTF-8")))}</a></h2>
<p>{bold_search_terms(escape(str(excerpt, encoding="UTF-8")))}</p>''' for (url, title, excerpt) in results)