Files: 274a734cb1eb39f332d1ab99e2d14881d3c3d924 / generator / common.py
5898 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(marko.convert(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 entries_in_series(series): |
62 | return dict( |
63 | [ |
64 | (id, entries[id]) |
65 | for id in entries |
66 | if "series" in entries[id] |
67 | and slugify(series) == slugify(entries[id]["series"]) |
68 | ] |
69 | ) |
70 | |
71 | |
72 | def entries_with_tag(tag): |
73 | return dict( |
74 | [ |
75 | (id, entries[id]) |
76 | for id in entries |
77 | if "tags" in entries[id] |
78 | and slugify(tag) in map(slugify, entries[id]["tags"]) |
79 | ] |
80 | ) |
81 | |
82 | |
83 | head = """ |
84 | <meta charset="utf-8"> |
85 | <link href="/style/tarazed.css" rel="stylesheet"> |
86 | <link href="/style/icon.svg" rel="icon"> |
87 | <link href="/feed" rel="alternate" type="application/atom+xml" title="Grey Nicholson"> |
88 | """ |
89 | |
90 | hues = [15, 30, 52, 82, 149, 187, 210, 246, 269, 291, 321, 352] |
91 | |
92 | link_home = '<a href="/"><strong>Home</strong></a>' |
93 | |
94 | link_to_all_entries = '<a href="/entries"><strong>All entries</strong></a>' |
95 | |
96 | link_to_feed = ( |
97 | '<a href="/feed" rel="alternate" type="application/atom+xml">' |
98 | "<strong>Subscribe</strong>" |
99 | "</a>" |
100 | ) |
101 | |
102 | footer = ( |
103 | "<footer>" |
104 | + "<hr/>" |
105 | + "<nav><ul>" |
106 | + "<li>" |
107 | + link_to_feed |
108 | + "</li>" |
109 | + "<li>" |
110 | + link_home |
111 | + "</li>" |
112 | + "</ul></nav>" |
113 | + "</footer>" |
114 | ) |
115 | |
116 | |
117 | def link_to_entry(id, **kwargs): |
118 | return ( |
119 | f'<a href="{url_of_entry(id)}"' |
120 | + (f''' rel="{kwargs["rel"]}"''' if "rel" in kwargs else "") |
121 | + ">" |
122 | + "<span>" |
123 | + ("<strong>" + entries[id]["title"] + "</strong>") |
124 | + "<span>" |
125 | + ('<span class="hidden"> ·</span>' + " ") |
126 | + ( |
127 | "from " + "<cite>" + entries[id]["series"] + "</cite>" + " · " |
128 | if "series" in entries[id] and "omit_series" not in kwargs |
129 | else "" |
130 | ) |
131 | + ( |
132 | f'<time datetime="{entries[id]["date"]:%Y-%m-%dT%H:%MZ}">' |
133 | + entries[id]["date"].date().isoformat() |
134 | + "</time>" |
135 | ) |
136 | + ( |
137 | " / " |
138 | + f'<time datetime="{entries[id]["modified"]:%Y-%m-%dT%H:%MZ}">' |
139 | + entries[id]["modified"].date().isoformat() |
140 | + "</time>" |
141 | if "modified" in entries[id] |
142 | else "" |
143 | ) |
144 | + ( |
145 | " · " + ", ".join("<b>" + label + "</b>" for label in kwargs["labels"]) |
146 | if "labels" in kwargs |
147 | else "" |
148 | ) |
149 | + "</span>" |
150 | + ( |
151 | ( |
152 | '<span class="hidden"> ·</span>' |
153 | + " " |
154 | + '<i class="description">' |
155 | + print_html_as_text(entries[id]["description"]) |
156 | + "</i>" |
157 | ) |
158 | if "description" in entries[id] |
159 | else "" |
160 | ) |
161 | + "</span>" |
162 | + "</a>" |
163 | ) |
164 | |
165 | |
166 | def link_with_details(url, title, description=None, rel=None, type=None): |
167 | return ( |
168 | f'<a href="{url}"' |
169 | + (f' rel="{rel}"' if rel else "") |
170 | + ">" |
171 | + "<span>" |
172 | + ("<strong>" + title + "</strong>") |
173 | + ( |
174 | ( |
175 | '<span class="hidden"> ·</span>' |
176 | + " " |
177 | + '<i class="description">' |
178 | + description |
179 | + "</i>" |
180 | ) |
181 | if description |
182 | else "" |
183 | ) |
184 | + "</span>" |
185 | + "</a>" |
186 | ) |
187 | |
188 | |
189 | def offset_id(seq, id, offset): |
190 | part_seq = list(seq)[list(seq).index(id) :: offset] |
191 | if len(part_seq) > 1: |
192 | return part_seq[1] |
193 | |
194 | |
195 | def print_html(html): |
196 | return str(bs4.BeautifulSoup(html, "html.parser")) |
197 | |
198 | |
199 | def print_html_as_text(html): |
200 | return str(bs4.BeautifulSoup(html, "html.parser").get_text()) |
201 | |
202 | |
203 | def print_html_escaped(html): |
204 | return htmllib.escape(str(bs4.BeautifulSoup(html, "html.parser"))) |
205 | |
206 | |
207 | repeated_tags = [tag for tag in tags if len(entries_with_tag(tag)) >= 4] |
208 | |
209 | |
210 | def stardate(date): |
211 | return format(int(date.strftime("%s")) / 100000, ".1f") |
212 | |
213 | |
214 | def title(string): |
215 | return string + " · Grey Nicholson" if string else "Grey Nicholson" |
216 | |
217 | |
218 | def url_of_day(date): |
219 | return f"/{date.year}/{date.month:02d}/{date.day:02d}" |
220 | |
221 | |
222 | def url_of_entry(id): |
223 | return f"/{id}" |
224 | |
225 | |
226 | def url_of_month(date): |
227 | return f"/{date.year}/{date.month:02d}" |
228 | |
229 | |
230 | def url_of_tag(tag): |
231 | return f"/entries/{slugify(tag)}" |
232 | |
233 | |
234 | def url_of_tag_feed(tag): |
235 | return f"/entries/{slugify(tag)}/feed" |
236 | |
237 | |
238 | def url_of_series(series): |
239 | return f"/{slugify(series)}" |
240 | |
241 | |
242 | def url_of_series_feed(series): |
243 | return f"/{slugify(series)}/feed" |
244 | |
245 | |
246 | def url_of_year(date): |
247 | return f"/{date.year}" |
248 |
Built with git-ssb-web