]>
Commit | Line | Data |
---|---|---|
181d08f3 MT |
1 | #!/usr/bin/python3 |
2 | ||
4ed1dadb | 3 | import difflib |
181d08f3 | 4 | import logging |
6ac7e934 | 5 | import os.path |
181d08f3 | 6 | import re |
9e90e800 | 7 | import urllib.parse |
181d08f3 MT |
8 | |
9 | from . import misc | |
9523790a | 10 | from . import util |
181d08f3 MT |
11 | from .decorators import * |
12 | ||
181d08f3 MT |
13 | class Wiki(misc.Object): |
14 | def _get_pages(self, query, *args): | |
15 | res = self.db.query(query, *args) | |
16 | ||
17 | for row in res: | |
18 | yield Page(self.backend, row.id, data=row) | |
19 | ||
d398ca08 MT |
20 | def _get_page(self, query, *args): |
21 | res = self.db.get(query, *args) | |
22 | ||
23 | if res: | |
24 | return Page(self.backend, res.id, data=res) | |
25 | ||
86368c12 MT |
26 | def __iter__(self): |
27 | return self._get_pages( | |
28 | "SELECT wiki.* FROM wiki_current current \ | |
29 | LEFT JOIN wiki ON current.id = wiki.id \ | |
30 | WHERE current.deleted IS FALSE \ | |
31 | ORDER BY page", | |
32 | ) | |
33 | ||
c78ad26e MT |
34 | def make_path(self, page, path): |
35 | # Nothing to do for absolute links | |
36 | if path.startswith("/"): | |
37 | pass | |
38 | ||
39 | # Relative links (one-level down) | |
40 | elif path.startswith("./"): | |
41 | path = os.path.join(page, path) | |
42 | ||
43 | # All other relative links | |
44 | else: | |
45 | p = os.path.dirname(page) | |
46 | path = os.path.join(p, path) | |
47 | ||
48 | # Normalise links | |
49 | return os.path.normpath(path) | |
50 | ||
9ff59d70 MT |
51 | def page_exists(self, path): |
52 | page = self.get_page(path) | |
53 | ||
54 | # Page must have been found and not deleted | |
55 | return page and not page.was_deleted() | |
56 | ||
6ac7e934 | 57 | def get_page_title(self, page, default=None): |
50c8dc11 MT |
58 | # Try to retrieve title from cache |
59 | title = self.memcache.get("wiki:title:%s" % page) | |
60 | if title: | |
61 | return title | |
62 | ||
63 | # If the title has not been in the cache, we will | |
64 | # have to look it up | |
6ac7e934 MT |
65 | doc = self.get_page(page) |
66 | if doc: | |
50c8dc11 MT |
67 | title = doc.title |
68 | else: | |
69 | title = os.path.basename(page) | |
6ac7e934 | 70 | |
50c8dc11 MT |
71 | # Save in cache for forever |
72 | self.memcache.set("wiki:title:%s" % page, title) | |
73 | ||
74 | return title | |
6ac7e934 | 75 | |
181d08f3 MT |
76 | def get_page(self, page, revision=None): |
77 | page = Page.sanitise_page_name(page) | |
78 | assert page | |
79 | ||
80 | if revision: | |
d398ca08 | 81 | return self._get_page("SELECT * FROM wiki WHERE page = %s \ |
181d08f3 MT |
82 | AND timestamp = %s", page, revision) |
83 | else: | |
d398ca08 | 84 | return self._get_page("SELECT * FROM wiki WHERE page = %s \ |
181d08f3 MT |
85 | ORDER BY timestamp DESC LIMIT 1", page) |
86 | ||
11afe905 MT |
87 | def get_recent_changes(self, account, limit=None): |
88 | pages = self._get_pages("SELECT * FROM wiki \ | |
11afe905 MT |
89 | ORDER BY timestamp DESC") |
90 | ||
91 | for page in pages: | |
92 | if not page.check_acl(account): | |
93 | continue | |
94 | ||
95 | yield page | |
96 | ||
97 | limit -= 1 | |
98 | if not limit: | |
99 | break | |
181d08f3 | 100 | |
495e9dc4 | 101 | def create_page(self, page, author, content, changes=None, address=None): |
181d08f3 MT |
102 | page = Page.sanitise_page_name(page) |
103 | ||
aba5e58a MT |
104 | # Write page to the database |
105 | page = self._get_page("INSERT INTO wiki(page, author_uid, markdown, changes, address) \ | |
df01767e | 106 | VALUES(%s, %s, %s, %s, %s) RETURNING *", page, author.uid, content or None, changes, address) |
181d08f3 | 107 | |
50c8dc11 | 108 | # Update cache |
980e486d | 109 | self.memcache.set("wiki:title:%s" % page.page, page.title) |
50c8dc11 | 110 | |
aba5e58a MT |
111 | # Send email to all watchers |
112 | page._send_watcher_emails(excludes=[author]) | |
113 | ||
114 | return page | |
115 | ||
495e9dc4 | 116 | def delete_page(self, page, author, **kwargs): |
181d08f3 MT |
117 | # Do nothing if the page does not exist |
118 | if not self.get_page(page): | |
119 | return | |
120 | ||
121 | # Just creates a blank last version of the page | |
495e9dc4 | 122 | self.create_page(page, author=author, content=None, **kwargs) |
181d08f3 | 123 | |
3168788e MT |
124 | def make_breadcrumbs(self, url): |
125 | # Split and strip all empty elements (double slashes) | |
181d08f3 MT |
126 | parts = list(e for e in url.split("/") if e) |
127 | ||
3168788e | 128 | ret = [] |
b1bf7d48 | 129 | for part in ("/".join(parts[:i]) for i in range(1, len(parts))): |
3168788e | 130 | ret.append(("/%s" % part, self.get_page_title(part, os.path.basename(part)))) |
181d08f3 | 131 | |
3168788e | 132 | return ret |
181d08f3 | 133 | |
11afe905 | 134 | def search(self, query, account=None, limit=None): |
9523790a MT |
135 | res = self._get_pages("SELECT wiki.* FROM wiki_search_index search_index \ |
136 | LEFT JOIN wiki ON search_index.wiki_id = wiki.id \ | |
22e56c4a MT |
137 | WHERE search_index.document @@ websearch_to_tsquery('english', %s) \ |
138 | ORDER BY ts_rank(search_index.document, websearch_to_tsquery('english', %s)) DESC", | |
11afe905 | 139 | query, query) |
9523790a | 140 | |
df80be2c | 141 | pages = [] |
11afe905 MT |
142 | for page in res: |
143 | # Skip any pages the user doesn't have permission for | |
144 | if not page.check_acl(account): | |
145 | continue | |
146 | ||
147 | # Return any other pages | |
df80be2c | 148 | pages.append(page) |
11afe905 | 149 | |
df80be2c MT |
150 | # Break when we have found enough pages |
151 | if limit and len(pages) >= limit: | |
11afe905 | 152 | break |
9523790a | 153 | |
df80be2c MT |
154 | return pages |
155 | ||
9523790a MT |
156 | def refresh(self): |
157 | """ | |
158 | Needs to be called after a page has been changed | |
159 | """ | |
160 | self.db.execute("REFRESH MATERIALIZED VIEW wiki_search_index") | |
161 | ||
2f23c558 MT |
162 | def get_watchlist(self, account): |
163 | pages = self._get_pages( | |
164 | "WITH pages AS (SELECT * FROM wiki_current \ | |
165 | LEFT JOIN wiki ON wiki_current.id = wiki.id) \ | |
166 | SELECT * FROM wiki_watchlist watchlist \ | |
167 | LEFT JOIN pages ON watchlist.page = pages.page \ | |
168 | WHERE watchlist.uid = %s", | |
169 | account.uid, | |
170 | ) | |
171 | ||
172 | return sorted(pages) | |
173 | ||
11afe905 MT |
174 | # ACL |
175 | ||
176 | def check_acl(self, page, account): | |
177 | res = self.db.query("SELECT * FROM wiki_acls \ | |
178 | WHERE %s ILIKE (path || '%%') ORDER BY LENGTH(path) DESC LIMIT 1", page) | |
179 | ||
180 | for row in res: | |
181 | # Access not permitted when user is not logged in | |
182 | if not account: | |
183 | return False | |
184 | ||
185 | # If user is in a matching group, we grant permission | |
186 | for group in row.groups: | |
93402e56 | 187 | if account.is_member_of_group(group): |
11afe905 MT |
188 | return True |
189 | ||
190 | # Otherwise access is not permitted | |
191 | return False | |
192 | ||
193 | # If no ACLs are found, we permit access | |
194 | return True | |
195 | ||
f2cfd873 MT |
196 | # Files |
197 | ||
198 | def _get_files(self, query, *args): | |
199 | res = self.db.query(query, *args) | |
200 | ||
201 | for row in res: | |
202 | yield File(self.backend, row.id, data=row) | |
203 | ||
204 | def _get_file(self, query, *args): | |
205 | res = self.db.get(query, *args) | |
206 | ||
207 | if res: | |
208 | return File(self.backend, res.id, data=res) | |
209 | ||
210 | def get_files(self, path): | |
211 | files = self._get_files("SELECT * FROM wiki_files \ | |
212 | WHERE path = %s AND deleted_at IS NULL ORDER BY filename", path) | |
213 | ||
214 | return list(files) | |
215 | ||
ff14dea3 | 216 | def get_file_by_path(self, path, revision=None): |
f2cfd873 MT |
217 | path, filename = os.path.dirname(path), os.path.basename(path) |
218 | ||
ff14dea3 MT |
219 | if revision: |
220 | # Fetch a specific revision | |
221 | return self._get_file("SELECT * FROM wiki_files \ | |
222 | WHERE path = %s AND filename = %s AND created_at <= %s \ | |
223 | ORDER BY created_at DESC LIMIT 1", path, filename, revision) | |
224 | ||
225 | # Fetch latest version | |
226 | return self._get_file("SELECT * FROM wiki_files \ | |
227 | WHERE path = %s AND filename = %s AND deleted_at IS NULL", | |
228 | path, filename) | |
229 | ||
230 | def get_file_by_path_and_filename(self, path, filename): | |
f2cfd873 | 231 | return self._get_file("SELECT * FROM wiki_files \ |
ff14dea3 MT |
232 | WHERE path = %s AND filename = %s AND deleted_at IS NULL", |
233 | path, filename) | |
f2cfd873 MT |
234 | |
235 | def upload(self, path, filename, data, mimetype, author, address): | |
ff14dea3 MT |
236 | # Replace any existing files |
237 | file = self.get_file_by_path_and_filename(path, filename) | |
238 | if file: | |
239 | file.delete(author) | |
240 | ||
f2cfd873 | 241 | # Upload the blob first |
a3a8a163 MT |
242 | blob = self.db.get("INSERT INTO wiki_blobs(data) VALUES(%s) \ |
243 | ON CONFLICT (digest(data, %s)) DO UPDATE SET data = EXCLUDED.data \ | |
244 | RETURNING id", data, "MD5") | |
f2cfd873 MT |
245 | |
246 | # Create entry for file | |
247 | return self._get_file("INSERT INTO wiki_files(path, filename, author_uid, address, \ | |
248 | mimetype, blob_id, size) VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING *", path, | |
249 | filename, author.uid, address, mimetype, blob.id, len(data)) | |
250 | ||
2901b734 MT |
251 | def render(self, path, text): |
252 | r = WikiRenderer(self.backend, path) | |
181d08f3 | 253 | |
2901b734 | 254 | return r.render(text) |
e2205cff | 255 | |
154f6179 | 256 | |
2901b734 | 257 | class Page(misc.Object): |
181d08f3 MT |
258 | def init(self, id, data=None): |
259 | self.id = id | |
260 | self.data = data | |
261 | ||
dc847af5 MT |
262 | def __repr__(self): |
263 | return "<%s %s %s>" % (self.__class__.__name__, self.page, self.timestamp) | |
264 | ||
c21ffadb MT |
265 | def __eq__(self, other): |
266 | if isinstance(other, self.__class__): | |
267 | return self.id == other.id | |
268 | ||
181d08f3 MT |
269 | def __lt__(self, other): |
270 | if isinstance(other, self.__class__): | |
271 | if self.page == other.page: | |
272 | return self.timestamp < other.timestamp | |
273 | ||
274 | return self.page < other.page | |
275 | ||
276 | @staticmethod | |
277 | def sanitise_page_name(page): | |
278 | if not page: | |
279 | return "/" | |
280 | ||
281 | # Make sure that the page name does NOT end with a / | |
282 | if page.endswith("/"): | |
283 | page = page[:-1] | |
284 | ||
285 | # Make sure the page name starts with a / | |
286 | if not page.startswith("/"): | |
287 | page = "/%s" % page | |
288 | ||
289 | # Remove any double slashes | |
290 | page = page.replace("//", "/") | |
291 | ||
292 | return page | |
293 | ||
294 | @property | |
295 | def url(self): | |
db8448d9 | 296 | return self.page |
181d08f3 | 297 | |
4ed1dadb MT |
298 | @property |
299 | def full_url(self): | |
300 | return "https://wiki.ipfire.org%s" % self.url | |
301 | ||
181d08f3 MT |
302 | @property |
303 | def page(self): | |
304 | return self.data.page | |
305 | ||
306 | @property | |
307 | def title(self): | |
51e7a876 | 308 | return self._title or os.path.basename(self.page[1:]) |
181d08f3 MT |
309 | |
310 | @property | |
311 | def _title(self): | |
312 | if not self.markdown: | |
313 | return | |
314 | ||
315 | # Find first H1 headline in markdown | |
316 | markdown = self.markdown.splitlines() | |
317 | ||
0074e919 | 318 | m = re.match(r"^#\s*(.*)( #)?$", markdown[0]) |
181d08f3 MT |
319 | if m: |
320 | return m.group(1) | |
321 | ||
3b05ef6e MT |
322 | @lazy_property |
323 | def author(self): | |
324 | if self.data.author_uid: | |
325 | return self.backend.accounts.get_by_uid(self.data.author_uid) | |
326 | ||
181d08f3 MT |
327 | @property |
328 | def markdown(self): | |
c21ffadb | 329 | return self.data.markdown or "" |
181d08f3 MT |
330 | |
331 | @property | |
332 | def html(self): | |
2901b734 | 333 | return self.backend.wiki.render(self.page, self.markdown) |
addc18d5 | 334 | |
181d08f3 MT |
335 | @property |
336 | def timestamp(self): | |
337 | return self.data.timestamp | |
338 | ||
339 | def was_deleted(self): | |
4c13230c | 340 | return not self.markdown |
181d08f3 MT |
341 | |
342 | @lazy_property | |
343 | def breadcrumbs(self): | |
344 | return self.backend.wiki.make_breadcrumbs(self.page) | |
345 | ||
d4c68c5c MT |
346 | def is_latest_revision(self): |
347 | return self.get_latest_revision() == self | |
348 | ||
181d08f3 | 349 | def get_latest_revision(self): |
7d699684 MT |
350 | revisions = self.get_revisions() |
351 | ||
352 | # Return first object | |
353 | for rev in revisions: | |
354 | return rev | |
355 | ||
356 | def get_revisions(self): | |
357 | return self.backend.wiki._get_pages("SELECT * FROM wiki \ | |
358 | WHERE page = %s ORDER BY timestamp DESC", self.page) | |
091ac36b | 359 | |
c21ffadb MT |
360 | @lazy_property |
361 | def previous_revision(self): | |
362 | return self.backend.wiki._get_page("SELECT * FROM wiki \ | |
363 | WHERE page = %s AND timestamp < %s ORDER BY timestamp DESC \ | |
364 | LIMIT 1", self.page, self.timestamp) | |
365 | ||
d398ca08 MT |
366 | @property |
367 | def changes(self): | |
368 | return self.data.changes | |
369 | ||
11afe905 MT |
370 | # ACL |
371 | ||
372 | def check_acl(self, account): | |
373 | return self.backend.wiki.check_acl(self.page, account) | |
374 | ||
091ac36b MT |
375 | # Sidebar |
376 | ||
377 | @lazy_property | |
378 | def sidebar(self): | |
379 | parts = self.page.split("/") | |
380 | ||
381 | while parts: | |
3cc5f666 | 382 | sidebar = self.backend.wiki.get_page("%s/sidebar" % os.path.join(*parts)) |
091ac36b MT |
383 | if sidebar: |
384 | return sidebar | |
385 | ||
386 | parts.pop() | |
f2cfd873 | 387 | |
d64a1e35 MT |
388 | # Watchers |
389 | ||
4ed1dadb MT |
390 | @lazy_property |
391 | def diff(self): | |
392 | if self.previous_revision: | |
393 | diff = difflib.unified_diff( | |
394 | self.previous_revision.markdown.splitlines(), | |
395 | self.markdown.splitlines(), | |
396 | ) | |
397 | ||
398 | return "\n".join(diff) | |
399 | ||
aba5e58a MT |
400 | @property |
401 | def watchers(self): | |
402 | res = self.db.query("SELECT uid FROM wiki_watchlist \ | |
403 | WHERE page = %s", self.page) | |
404 | ||
405 | for row in res: | |
406 | # Search for account by UID and skip if none was found | |
407 | account = self.backend.accounts.get_by_uid(row.uid) | |
408 | if not account: | |
409 | continue | |
410 | ||
411 | # Return the account | |
412 | yield account | |
413 | ||
f2e25ded | 414 | def is_watched_by(self, account): |
d64a1e35 MT |
415 | res = self.db.get("SELECT 1 FROM wiki_watchlist \ |
416 | WHERE page = %s AND uid = %s", self.page, account.uid) | |
417 | ||
418 | if res: | |
419 | return True | |
420 | ||
421 | return False | |
422 | ||
423 | def add_watcher(self, account): | |
f2e25ded | 424 | if self.is_watched_by(account): |
d64a1e35 MT |
425 | return |
426 | ||
427 | self.db.execute("INSERT INTO wiki_watchlist(page, uid) \ | |
428 | VALUES(%s, %s)", self.page, account.uid) | |
429 | ||
430 | def remove_watcher(self, account): | |
431 | self.db.execute("DELETE FROM wiki_watchlist \ | |
432 | WHERE page = %s AND uid = %s", self.page, account.uid) | |
433 | ||
aba5e58a MT |
434 | def _send_watcher_emails(self, excludes=[]): |
435 | # Nothing to do if there was no previous revision | |
436 | if not self.previous_revision: | |
437 | return | |
438 | ||
439 | for watcher in self.watchers: | |
440 | # Skip everyone who is excluded | |
441 | if watcher in excludes: | |
442 | logging.debug("Excluding %s" % watcher) | |
443 | continue | |
444 | ||
516da0a9 MT |
445 | # Check permissions |
446 | if not self.backend.wiki.check_acl(self.page, watcher): | |
447 | logging.debug("Watcher %s does not have permissions" % watcher) | |
448 | continue | |
449 | ||
aba5e58a MT |
450 | logging.debug("Sending watcher email to %s" % watcher) |
451 | ||
4ed1dadb MT |
452 | # Compose message |
453 | self.backend.messages.send_template("wiki/messages/page-changed", | |
ba14044c | 454 | account=watcher, page=self, priority=-10) |
aba5e58a | 455 | |
d4c68c5c MT |
456 | def restore(self, author, address): |
457 | changes = "Restore to revision from %s" % self.timestamp.isoformat() | |
458 | ||
459 | return self.backend.wiki.create_page(self.page, | |
460 | author, self.markdown, changes=changes, address=address) | |
461 | ||
f2cfd873 MT |
462 | |
463 | class File(misc.Object): | |
464 | def init(self, id, data): | |
465 | self.id = id | |
466 | self.data = data | |
467 | ||
ff14dea3 MT |
468 | def __eq__(self, other): |
469 | if isinstance(other, self.__class__): | |
470 | return self.id == other.id | |
471 | ||
f2cfd873 MT |
472 | @property |
473 | def url(self): | |
474 | return os.path.join(self.path, self.filename) | |
475 | ||
476 | @property | |
477 | def path(self): | |
478 | return self.data.path | |
479 | ||
480 | @property | |
481 | def filename(self): | |
482 | return self.data.filename | |
483 | ||
484 | @property | |
485 | def mimetype(self): | |
486 | return self.data.mimetype | |
487 | ||
488 | @property | |
489 | def size(self): | |
490 | return self.data.size | |
491 | ||
8cb0bea4 MT |
492 | @lazy_property |
493 | def author(self): | |
494 | if self.data.author_uid: | |
495 | return self.backend.accounts.get_by_uid(self.data.author_uid) | |
496 | ||
497 | @property | |
498 | def created_at(self): | |
499 | return self.data.created_at | |
500 | ||
b26c705a MT |
501 | def delete(self, author=None): |
502 | self.db.execute("UPDATE wiki_files SET deleted_at = NOW(), deleted_by = %s \ | |
503 | WHERE id = %s", author.uid if author else None, self.id) | |
ff14dea3 MT |
504 | |
505 | @property | |
506 | def deleted_at(self): | |
507 | return self.data.deleted_at | |
508 | ||
509 | def get_latest_revision(self): | |
510 | revisions = self.get_revisions() | |
511 | ||
512 | # Return first object | |
513 | for rev in revisions: | |
514 | return rev | |
515 | ||
516 | def get_revisions(self): | |
517 | revisions = self.backend.wiki._get_files("SELECT * FROM wiki_files \ | |
2225edd9 | 518 | WHERE path = %s AND filename = %s ORDER BY created_at DESC", self.path, self.filename) |
ff14dea3 MT |
519 | |
520 | return list(revisions) | |
521 | ||
8cb0bea4 MT |
522 | def is_pdf(self): |
523 | return self.mimetype in ("application/pdf", "application/x-pdf") | |
524 | ||
f2cfd873 MT |
525 | def is_image(self): |
526 | return self.mimetype.startswith("image/") | |
527 | ||
528 | @lazy_property | |
529 | def blob(self): | |
530 | res = self.db.get("SELECT data FROM wiki_blobs \ | |
531 | WHERE id = %s", self.data.blob_id) | |
532 | ||
533 | if res: | |
534 | return bytes(res.data) | |
79dd9a0f MT |
535 | |
536 | def get_thumbnail(self, size): | |
75d9b3da MT |
537 | cache_key = "-".join((self.path, util.normalize(self.filename), self.created_at.isoformat(), "%spx" % size)) |
538 | ||
539 | # Try to fetch the data from the cache | |
540 | thumbnail = self.memcache.get(cache_key) | |
541 | if thumbnail: | |
542 | return thumbnail | |
543 | ||
544 | # Generate the thumbnail | |
5ef115cd | 545 | thumbnail = util.generate_thumbnail(self.blob, size) |
75d9b3da MT |
546 | |
547 | # Put it into the cache for forever | |
548 | self.memcache.set(cache_key, thumbnail) | |
549 | ||
550 | return thumbnail | |
2901b734 MT |
551 | |
552 | ||
553 | class WikiRenderer(misc.Object): | |
4ddad3e5 MT |
554 | schemas = ( |
555 | "ftp://", | |
556 | "git://", | |
557 | "http://", | |
558 | "https://", | |
559 | "rsync://", | |
560 | "sftp://", | |
561 | "ssh://", | |
562 | "webcal://", | |
563 | ) | |
564 | ||
565 | # Links | |
566 | links = re.compile(r"<a href=\"(.*?)\">(.*?)</a>") | |
2901b734 | 567 | |
c78ad26e | 568 | # Images |
e9c6d581 | 569 | images = re.compile(r"<img alt(?:=\"(.*?)\")? src=\"(.*?)\" (?:title=\"(.*?)\" )?/>") |
c78ad26e | 570 | |
2901b734 MT |
571 | def init(self, path): |
572 | self.path = path | |
573 | ||
4ddad3e5 MT |
574 | def _render_link(self, m): |
575 | url, text = m.groups() | |
2901b734 | 576 | |
4ddad3e5 MT |
577 | # Emails |
578 | if "@" in url: | |
579 | # Strip mailto: | |
580 | if url.startswith("mailto:"): | |
581 | url = url[7:] | |
2901b734 | 582 | |
4ddad3e5 MT |
583 | return """<a class="link-external" href="mailto:%s">%s</a>""" % \ |
584 | (url, text or url) | |
2901b734 | 585 | |
4ddad3e5 MT |
586 | # External Links |
587 | for schema in self.schemas: | |
588 | if url.startswith(schema): | |
589 | return """<a class="link-external" href="%s">%s</a>""" % \ | |
590 | (url, text or url) | |
2901b734 | 591 | |
4ddad3e5 MT |
592 | # Everything else must be an internal link |
593 | path = self.backend.wiki.make_path(self.path, url) | |
2901b734 | 594 | |
4ddad3e5 MT |
595 | return """<a href="%s">%s</a>""" % \ |
596 | (path, text or self.backend.wiki.get_page_title(path)) | |
2901b734 | 597 | |
c78ad26e | 598 | def _render_image(self, m): |
e9c6d581 | 599 | alt_text, url, caption = m.groups() |
2901b734 | 600 | |
c78ad26e MT |
601 | # Skip any absolute and external URLs |
602 | if url.startswith("/") or url.startswith("https://") or url.startswith("http://"): | |
fa8c5edd MT |
603 | return """<figure class="figure"><img src="%s" class="figure-img img-fluid rounded" alt="%s"> |
604 | <figcaption class="figure-caption">%s</figcaption></figure> | |
9881e9ef | 605 | """ % (url, alt_text, caption or "") |
2901b734 | 606 | |
c78ad26e MT |
607 | # Try to split query string |
608 | url, delimiter, qs = url.partition("?") | |
2901b734 | 609 | |
c78ad26e MT |
610 | # Parse query arguments |
611 | args = urllib.parse.parse_qs(qs) | |
2901b734 | 612 | |
c78ad26e MT |
613 | # Build absolute path |
614 | url = self.backend.wiki.make_path(self.path, url) | |
2901b734 | 615 | |
c78ad26e MT |
616 | # Find image |
617 | file = self.backend.wiki.get_file_by_path(url) | |
618 | if not file or not file.is_image(): | |
619 | return "<!-- Could not find image %s in %s -->" % (url, self.path) | |
2901b734 | 620 | |
c78ad26e MT |
621 | # Scale down the image if not already done |
622 | if not "s" in args: | |
9ce45afb | 623 | args["s"] = "920" |
2901b734 | 624 | |
fa8c5edd MT |
625 | return """<figure class="figure"><img src="%s?%s" class="figure-img img-fluid rounded" alt="%s"> |
626 | <figcaption class="figure-caption">%s</figcaption></figure> | |
627 | """ % (url, urllib.parse.urlencode(args), caption, caption or "") | |
2901b734 | 628 | |
c78ad26e MT |
629 | def render(self, text): |
630 | logging.debug("Rendering %s" % self.path) | |
2901b734 | 631 | |
9881e9ef MT |
632 | # Borrow this from the blog |
633 | text = self.backend.blog._render_text(text, lang="markdown") | |
634 | ||
4ddad3e5 MT |
635 | # Postprocess links |
636 | text = self.links.sub(self._render_link, text) | |
637 | ||
9881e9ef | 638 | # Postprocess images to <figure> |
c78ad26e MT |
639 | text = self.images.sub(self._render_image, text) |
640 | ||
9881e9ef | 641 | return text |