Files: 59c0f9d88cdd78f718fca44638dffb31ada0c2a1 / generator / common.py
5970 bytesRaw
1 | import datetime |
2 | import bs4 |
3 | import html as htmllib |
4 | import marko |
5 | import re |
6 | import urllib.parse |
7 | |
8 | import parse_content |
9 | |
10 | days = parse_content.days |
11 | entries = parse_content.entries |
12 | months = parse_content.months |
13 | series = parse_content.series |
14 | slugify = parse_content.slugify |
15 | tags = parse_content.tags |
16 | updated = parse_content.updated |
17 | years = parse_content.years |
18 | |
19 | |
20 | def atom_entry(id): |
21 | return ( |
22 | "<entry><title>" |
23 | + entries[id]["title"] |
24 | + "</title><id>" |
25 | + base_url |
26 | + url_of_entry(id) |
27 | + "</id>" |
28 | f"""<link href="{base_url}{url_of_entry(id)}" rel="alternate" type="text/html"/>""" |
29 | "<published>" + entries[id]["date"].isoformat() + "</published>" |
30 | "<updated>" |
31 | + ( |
32 | entries[id]["modified"].isoformat() |
33 | if "modified" in entries[id] |
34 | else entries[id]["date"].isoformat() |
35 | ) |
36 | + "</updated>" |
37 | + ( |
38 | ( |
39 | ( |
40 | "<summary>" |
41 | + print_html_as_text(entries[id]["description"]) |
42 | + "</summary>" |
43 | ) |
44 | if "description" in entries[id] |
45 | else "" |
46 | ) |
47 | ) |
48 | + '<content type="html">' |
49 | + "\n" |
50 | + print_html_escaped(convert_to_html(entries[id].content)) |
51 | + "</content>" |
52 | + "</entry>" |
53 | ) |
54 | |
55 | |
56 | base_url = "https://gkn.me.uk" |
57 | |
58 | colour_class = f"month-{list(reversed(months))[0].month:02d}" |
59 | |
60 | |
61 | def convert_to_html(hypertext): |
62 | return marko.convert(hypertext) |
63 | |
64 | |
65 | def entries_in_series(series): |
66 | return dict( |
67 | [ |
68 | (id, entries[id]) |
69 | for id in entries |
70 | if "series" in entries[id] |
71 | and slugify(series) == slugify(entries[id]["series"]) |
72 | ] |
73 | ) |
74 | |
75 | |
76 | def entries_with_tag(tag): |
77 | return dict( |
78 | [ |
79 | (id, entries[id]) |
80 | for id in entries |
81 | if "tags" in entries[id] |
82 | and slugify(tag) in map(slugify, entries[id]["tags"]) |
83 | ] |
84 | ) |
85 | |
86 | |
87 | head = """ |
88 | <meta charset="utf-8"> |
89 | <link href="/style/tarazed.css" rel="stylesheet"> |
90 | <link href="/style/icon.svg" rel="icon"> |
91 | <link href="/feed" rel="alternate" type="application/atom+xml" title="Grey Nicholson"> |
92 | """ |
93 | |
94 | hues = [15, 30, 52, 82, 149, 187, 210, 246, 269, 291, 321, 352] |
95 | |
96 | link_home = '<a href="/"><strong>Home</strong></a>' |
97 | |
98 | link_to_all_entries = '<a href="/entries"><strong>All entries</strong></a>' |
99 | |
100 | link_to_feed = ( |
101 | '<a href="/feed" rel="alternate" type="application/atom+xml">' |
102 | "<strong>Subscribe</strong>" |
103 | "</a>" |
104 | ) |
105 | |
106 | footer = ( |
107 | "<footer>" |
108 | + "<hr/>" |
109 | + "<nav><ul>" |
110 | + "<li>" |
111 | + link_to_feed |
112 | + "</li>" |
113 | + "<li>" |
114 | + link_home |
115 | + "</li>" |
116 | + "</ul></nav>" |
117 | + "</footer>" |
118 | ) |
119 | |
120 | |
121 | def link_to_entry(id, **kwargs): |
122 | return ( |
123 | f'<a href="{url_of_entry(id)}"' |
124 | + (f''' rel="{kwargs["rel"]}"''' if "rel" in kwargs else "") |
125 | + ">" |
126 | + "<span>" |
127 | + ("<strong>" + entries[id]["title"] + "</strong>") |
128 | + "<span>" |
129 | + ('<span class="hidden"> ·</span>' + " ") |
130 | + ( |
131 | "from " + "<cite>" + entries[id]["series"] + "</cite>" + " · " |
132 | if "series" in entries[id] and "omit_series" not in kwargs |
133 | else "" |
134 | ) |
135 | + ( |
136 | f'<time datetime="{entries[id]["date"]:%Y-%m-%dT%H:%MZ}">' |
137 | + entries[id]["date"].date().isoformat() |
138 | + "</time>" |
139 | ) |
140 | + ( |
141 | " / " |
142 | + f'<time datetime="{entries[id]["modified"]:%Y-%m-%dT%H:%MZ}">' |
143 | + entries[id]["modified"].date().isoformat() |
144 | + "</time>" |
145 | if "modified" in entries[id] |
146 | else "" |
147 | ) |
148 | + ( |
149 | " · " + ", ".join("<b>" + label + "</b>" for label in kwargs["labels"]) |
150 | if "labels" in kwargs |
151 | else "" |
152 | ) |
153 | + "</span>" |
154 | + ( |
155 | ( |
156 | '<span class="hidden"> ·</span>' |
157 | + " " |
158 | + '<i class="description">' |
159 | + print_html_as_text(entries[id]["description"]) |
160 | + "</i>" |
161 | ) |
162 | if "description" in entries[id] |
163 | else "" |
164 | ) |
165 | + "</span>" |
166 | + "</a>" |
167 | ) |
168 | |
169 | |
170 | def link_with_details(url, title, description=None, rel=None, type=None): |
171 | return ( |
172 | f'<a href="{url}"' |
173 | + (f' rel="{rel}"' if rel else "") |
174 | + ">" |
175 | + "<span>" |
176 | + ("<strong>" + title + "</strong>") |
177 | + ( |
178 | ( |
179 | '<span class="hidden"> ·</span>' |
180 | + " " |
181 | + '<i class="description">' |
182 | + description |
183 | + "</i>" |
184 | ) |
185 | if description |
186 | else "" |
187 | ) |
188 | + "</span>" |
189 | + "</a>" |
190 | ) |
191 | |
192 | |
193 | def offset_id(seq, id, offset): |
194 | part_seq = list(seq)[list(seq).index(id) :: offset] |
195 | if len(part_seq) > 1: |
196 | return part_seq[1] |
197 | |
198 | |
199 | def print_html(html): |
200 | return str(bs4.BeautifulSoup(html, "html.parser")) |
201 | |
202 | |
203 | def print_html_as_text(html): |
204 | return str(bs4.BeautifulSoup(html, "html.parser").get_text()) |
205 | |
206 | |
207 | def print_html_escaped(html): |
208 | return htmllib.escape(str(bs4.BeautifulSoup(html, "html.parser"))) |
209 | |
210 | |
211 | repeated_tags = [tag for tag in tags if len(entries_with_tag(tag)) >= 4] |
212 | |
213 | |
214 | def stardate(date): |
215 | return format(int(date.strftime("%s")) / 100000, ".1f") |
216 | |
217 | |
218 | def title(string): |
219 | return string + " · Grey Nicholson" if string else "Grey Nicholson" |
220 | |
221 | |
222 | def url_of_day(date): |
223 | return f"/{date.year}/{date.month:02d}/{date.day:02d}" |
224 | |
225 | |
226 | def url_of_entry(id): |
227 | return f"/{id}" |
228 | |
229 | |
230 | def url_of_month(date): |
231 | return f"/{date.year}/{date.month:02d}" |
232 | |
233 | |
234 | def url_of_tag(tag): |
235 | return f"/entries/{slugify(tag)}" |
236 | |
237 | |
238 | def url_of_tag_feed(tag): |
239 | return f"/entries/{slugify(tag)}/feed" |
240 | |
241 | |
242 | def url_of_series(series): |
243 | return f"/{slugify(series)}" |
244 | |
245 | |
246 | def url_of_series_feed(series): |
247 | return f"/{slugify(series)}/feed" |
248 | |
249 | |
250 | def url_of_year(date): |
251 | return f"/{date.year}" |
252 |
Built with git-ssb-web