]> git.ipfire.org Git - ipfire.org.git/blobdiff - src/backend/wiki.py
docs: Implement our own Markdown renderer based on the blog
[ipfire.org.git] / src / backend / wiki.py
index cc5964d14a693e3633d3b65ebfbfb731958f0336..0a2d925a8cff60094b22e7f6bcede7802a34e0dc 100644 (file)
@@ -3,6 +3,9 @@
 import difflib
 import hashlib
 import logging
+import markdown
+import markdown.extensions
+import markdown.preprocessors
 import os.path
 import re
 import urllib.parse
@@ -573,6 +576,31 @@ class File(misc.Object):
                return thumbnail
 
 
+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 WikiRenderer(misc.Object):
        schemas = (
                "ftp://",
@@ -591,6 +619,20 @@ class WikiRenderer(misc.Object):
        # Images
        images = re.compile(r"<img alt(?:=\"(.*?)\")? src=\"(.*?)\" (?:title=\"(.*?)\" )?/>")
 
+       # Markdown Renderer
+       renderer = markdown.Markdown(
+               extensions=[
+                       PrettyLinksExtension(),
+                       "codehilite",
+                       "fenced_code",
+                       "footnotes",
+                       "nl2br",
+                       "sane_lists",
+                       "tables",
+                       "toc",
+               ],
+       )
+
        def init(self, path):
                self.path = path
 
@@ -692,8 +734,8 @@ class WikiRenderer(misc.Object):
        def render(self, text):
                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(text)
 
                # Postprocess links
                text = self.links.sub(self._render_link, text)