#!/usr/bin/python3
import difflib
+import hashlib
import logging
+import markdown
+import markdown.extensions
+import markdown.preprocessors
import os.path
import re
import urllib.parse
page = self._get_page("INSERT INTO wiki(page, author_uid, markdown, changes, address) \
VALUES(%s, %s, %s, %s, %s) RETURNING *", page, author.uid, content or None, changes, address)
+ # Store any linked files
+ page._store_linked_files()
+
# Send email to all watchers
page._send_watcher_emails(excludes=[author])
filename, author.uid, address, mimetype, blob.id, len(data))
def render(self, path, text):
- r = WikiRenderer(self.backend, path)
-
- return r.render(text)
+ return WikiRenderer(self.backend, path, text)
class Page(misc.Object):
lines.append(line)
- return self.backend.wiki.render(self.page, "\n".join(lines))
+ renderer = self.backend.wiki.render(self.page, "\n".join(lines))
+
+ return renderer.html
+
+ # Linked Files
+
+ @property
+ def files(self):
+ renderer = self.backend.wiki.render(self.page, self.markdown)
+
+ return renderer.files
+
+ def _store_linked_files(self):
+ self.db.executemany("INSERT INTO wiki_linked_files(page_id, path) \
+ VALUES(%s, %s)", ((self.id, file) for file in self.files))
@property
def timestamp(self):
if isinstance(other, self.__class__):
return self.id == other.id
+ return NotImplemented
+
@property
def url(self):
- return os.path.join(self.path, self.filename)
+ return "/docs%s" % os.path.join(self.path, self.filename)
@property
def path(self):
return self.data.created_at
def delete(self, author=None):
+ if not self.can_be_deleted():
+ raise RuntimeError("Cannot delete %s" % self)
+
self.db.execute("UPDATE wiki_files SET deleted_at = NOW(), deleted_by = %s \
WHERE id = %s", author.uid if author else None, self.id)
+ def can_be_deleted(self):
+ # Cannot be deleted if still in use
+ if self.pages:
+ return False
+
+ # Can be deleted
+ return True
+
@property
def deleted_at(self):
return self.data.deleted_at
return thumbnail
+ @property
+ def pages(self):
+ """
+ Returns a list of all pages this file is linked by
+ """
+ pages = self.backend.wiki._get_pages("""
+ SELECT
+ wiki.*
+ FROM
+ wiki_linked_files
+ JOIN
+ wiki_current ON wiki_linked_files.page_id = wiki_current.id
+ LEFT JOIN
+ wiki ON wiki_linked_files.page_id = wiki.id
+ WHERE
+ wiki_linked_files.path = %s
+ ORDER BY
+ wiki.page
+ """, os.path.join(self.path, self.filename),
+ )
+
+ return list(pages)
+
class WikiRenderer(misc.Object):
schemas = (
)
# Links
- links = re.compile(r"<a href=\"(.*?)\">(.*?)</a>")
+ _links = re.compile(r"<a href=\"(.*?)\">(.*?)</a>")
# Images
- images = re.compile(r"<img alt(?:=\"(.*?)\")? src=\"(.*?)\" (?:title=\"(.*?)\" )?/>")
+ _images = re.compile(r"<img alt(?:=\"(.*?)\")? src=\"(.*?)\" (?:title=\"(.*?)\" )?/>")
- def init(self, path):
+ def init(self, path, text):
self.path = path
+ self.text = text
+
+ # Markdown Renderer
+ self.renderer = markdown.Markdown(
+ extensions=[
+ LinkedFilesExtractorExtension(),
+ PrettyLinksExtension(),
+ "codehilite",
+ "fenced_code",
+ "footnotes",
+ "nl2br",
+ "sane_lists",
+ "tables",
+ "toc",
+ ],
+ )
+
+ # Render!
+ self.html = self._render()
def _render_link(self, m):
url, text = m.groups()
def _render_image(self, m):
alt_text, url, caption = m.groups()
+ # Compute a hash over the URL
+ h = hashlib.new("md5")
+ h.update(url.encode())
+ id = h.hexdigest()
+
html = """
<div class="columns is-centered">
<div class="column is-8">
- <figure class="image">
- <img src="/docs%s" alt="%s">
- <figcaption class="figure-caption">%s</figcaption>
+ <figure class="image modal-trigger" data-target="%(id)s">
+ <img src="/docs%(url)s" alt="%(caption)s">
+
+ <figcaption class="figure-caption">%(caption)s</figcaption>
</figure>
+
+ <div class="modal is-large" id="%(id)s">
+ <div class="modal-background"></div>
+
+ <div class="modal-content">
+ <p class="image">
+ <img src="/docs%(plain_url)s?s=1920" alt="%(caption)s"
+ loading="lazy">
+ </p>
+ </div>
+
+ <button class="modal-close is-large" aria-label="close"></button>
+ </div>
</div>
</div>
"""
# Skip any absolute and external URLs
if url.startswith("/") or url.startswith("https://") or url.startswith("http://"):
- return html % (url, alt_text, caption or "")
+ return html % {
+ "caption" : caption or "",
+ "id" : id,
+ "plain_url" : url,
+ "url" : url,
+ }
# Try to split query string
url, delimiter, qs = url.partition("?")
args = urllib.parse.parse_qs(qs)
# Build absolute path
- url = self.backend.wiki.make_path(self.path, url)
+ plain_url = url = self.backend.wiki.make_path(self.path, url)
# Find image
file = self.backend.wiki.get_file_by_path(url)
if args:
url = "%s?%s" % (url, urllib.parse.urlencode(args))
- return html % (url, caption, caption or "")
+ return html % {
+ "caption" : caption or "",
+ "id" : id,
+ "plain_url" : plain_url,
+ "url" : url,
+ }
- def render(self, text):
+ def _render(self):
logging.debug("Rendering %s" % self.path)
- # Borrow this from the blog
- text = self.backend.blog._render_text(text, lang="markdown")
+ # Render...
+ text = self.renderer.convert(self.text)
# Postprocess links
- text = self.links.sub(self._render_link, text)
+ text = self._links.sub(self._render_link, text)
# Postprocess images to <figure>
- text = self.images.sub(self._render_image, text)
+ text = self._images.sub(self._render_image, text)
return text
+
+ @lazy_property
+ def files(self):
+ """
+ A list of all linked files that have been part of the rendered markup
+ """
+ files = []
+
+ for url in self.renderer.files:
+ # Skip external images
+ if url.startswith("https://") or url.startswith("http://"):
+ continue
+
+ # Make the URL absolute
+ url = self.backend.wiki.make_path(self.path, url)
+
+ # Check if this is a file (it could also just be a page)
+ file = self.backend.wiki.get_file_by_path(url)
+ if file:
+ files.append(url)
+
+ return files
+
+
+class PrettyLinksExtension(markdown.extensions.Extension):
+ def extendMarkdown(self, md):
+ # Create links to Bugzilla
+ md.preprocessors.register(BugzillaLinksPreprocessor(md), "bugzilla", 10)
+
+ # Create links to CVE
+ md.preprocessors.register(CVELinksPreprocessor(md), "cve", 10)
+
+
+class BugzillaLinksPreprocessor(markdown.preprocessors.Preprocessor):
+ regex = re.compile(r"(?:#(\d{5,}))", re.I)
+
+ def run(self, lines):
+ for line in lines:
+ yield self.regex.sub(r"[#\1](https://bugzilla.ipfire.org/show_bug.cgi?id=\1)", line)
+
+
+class CVELinksPreprocessor(markdown.preprocessors.Preprocessor):
+ regex = re.compile(r"(?:CVE)[\s\-](\d{4}\-\d+)")
+
+ def run(self, lines):
+ for line in lines:
+ yield self.regex.sub(r"[CVE-\1](https://cve.mitre.org/cgi-bin/cvename.cgi?name=\1)", line)
+
+
+class LinkedFilesExtractor(markdown.treeprocessors.Treeprocessor):
+ """
+ Finds all Linked Files
+ """
+ def run(self, root):
+ self.md.files = []
+
+ # Find all images and store the URLs
+ for image in root.findall(".//img"):
+ src = image.get("src")
+
+ self.md.files.append(src)
+
+ # Find all links
+ for link in root.findall(".//a"):
+ href = link.get("href")
+
+ self.md.files.append(href)
+
+
+class LinkedFilesExtractorExtension(markdown.extensions.Extension):
+ def extendMarkdown(self, md):
+ md.treeprocessors.register(LinkedFilesExtractor(md), "linked-files-extractor", 10)