-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensions.py
142 lines (105 loc) · 3.6 KB
/
extensions.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import atexit
import os
import re
import warnings
from collections import defaultdict
from copy import deepcopy
from plim import preprocessor_factory
from plim.extensions import md_to_html
warnings.filterwarnings("ignore", category=DeprecationWarning)
# icon#id.cls-[1rem]: filename
PARSE_SVG_PATH_RE = re.compile(
r"icon(?:#(?P<id>[A-Za-z\-0-9]+))?(?P<cls>(?:\.[!A-Za-z\-/0-9\[\]:${}]+)+)? : (?P<path>[A-Za-z\-0-9/_\.]+)"
)
PARSE_MDFILE_PATH_RE = re.compile(
r"md(?:#(?P<id>[A-Za-z\-0-9]+))?(?P<cls>(?:\.[A-Za-z\-/0-9\[\]:]+)+)? : (?P<path>[A-Za-z\-0-9/_\.]+)"
)
PARSE_INDEX_RE = re.compile(
r"index#(?P<id>[A-Za-z\-0-9\._]+?)\.(?P<index>[A-Za-z\-0-9]+)\s+:\s+(?P<title>[^\n]+)"
)
INDEX_DATA = defaultdict(list)
def send_indexes():
global INDEX_DATA
for index, db in INDEX_DATA.items():
send_index(index, db)
atexit.register(send_indexes)
def send_index(index, db):
try:
API_KEY = os.getenv("MEILI_API_KEY")
SEARCH_URL = os.getenv("MEILI_SEARCH_URL")
if not API_KEY or not SEARCH_URL:
return
import requests
requests.post(
f"{SEARCH_URL}/indexes/{index}/documents",
headers={"Authorization": f"Bearer {API_KEY}"},
json=db,
)
except:
pass
INVALID_DOCUMENT_ID_RE = re.compile(r"[^A-Za-z0-9\-_]")
def parse_index(indent_level, current_line, matched, source, syntax):
global INDEX_DATA
API_KEY = os.getenv("MEILI_API_KEY")
SEARCH_URL = os.getenv("MEILI_SEARCH_URL")
title = matched.group("title")
_id = matched.group("id")
index = matched.group("index")
md_lines = []
level = indent_level + 4
for _, line in deepcopy(source):
if line != "\n" and (len(line) < level or line[level - 1] != " "):
break
source.__next__()
md_lines.append(line if len(line) < level else line[level:])
md_source = "".join(md_lines)
html = md_to_html(md_source)
if API_KEY and SEARCH_URL:
# from lxml.html import fromstring
# dom = fromstring(html)
# html_text = dom.text_content()
INDEX_DATA[index].append(
{
"id": INVALID_DOCUMENT_ID_RE.sub("_", _id),
"title": title,
"content": md_source,
}
)
return (html, indent_level, "", source)
def parse_md_path(indent_level, current_line, matched, source, syntax):
path = matched.group("path")
md_id = matched.group("id") or ""
md_class = (matched.group("cls") or "").replace(".", " ")
md_file = f"public/static/markdown/{path}.md"
if not os.path.exists(md_file):
return "", indent_level, "", source
with open(md_file) as f:
rt = f.read()
parsed = md_to_html(rt)
return (
f'<article id="{md_id}" class="{md_class}">{parsed}</article>',
indent_level,
"",
source,
)
def parse_svg_path(indent_level, current_line, matched, source, syntax):
path = matched.group("path")
svg_id = matched.group("id") or ""
svg_class = (matched.group("cls") or "").replace(".", " ")
svg_file = f"public/static/svg/{path}.svg"
if not os.path.exists(svg_file):
return "", indent_level, "", source
with open(svg_file) as f:
rt = f.read()
return (
rt.replace("<svg ", f'<svg id="{svg_id}" class="{svg_class}"'),
indent_level,
"",
source,
)
CUSTOM_PARSERS = [
(PARSE_SVG_PATH_RE, parse_svg_path),
(PARSE_MDFILE_PATH_RE, parse_md_path),
(PARSE_INDEX_RE, parse_index),
]
preprocessor = preprocessor_factory(custom_parsers=CUSTOM_PARSERS, syntax="mako")