]> git.ipfire.org Git - ipfire.org.git/commitdiff
wiki: Implement automatic links for CVEs and Bugzilla
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 17 Jul 2019 14:30:40 +0000 (15:30 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 17 Jul 2019 14:30:40 +0000 (15:30 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/blog.py

index 7536407e5fc8c08d221c64f38257ee88e0cdf3d1..f87c71c8d02177667c9a0095f7df2b4fa6b8bb55 100644 (file)
@@ -3,6 +3,8 @@
 import datetime
 import feedparser
 import markdown
+import markdown.extensions
+import markdown.preprocessors
 import re
 import textile
 import tornado.gen
@@ -12,15 +14,6 @@ from . import misc
 from . import util
 from .decorators import *
 
-# Used to automatically link some things
-link_patterns = (
-       # Find bug reports
-       (re.compile(r"(?:#(\d+))", re.I), r"https://bugzilla.ipfire.org/show_bug.cgi?id=\1"),
-
-       # CVE Numbers
-       (re.compile(r"(?:CVE)[\s\-](\d{4}\-\d+)"), r"https://cve.mitre.org/cgi-bin/cvename.cgi?name=\1"),
-)
-
 class Blog(misc.Object):
        def _get_post(self, query, *args):
                res = self.db.get(query, *args)
@@ -141,8 +134,9 @@ class Blog(misc.Object):
 
        def _render_text(self, text, lang="markdown"):
                if lang == "markdown":
-                       return markdown.markdown(text, link_patterns=link_patterns,
+                       return markdown.markdown(text,
                                extensions=[
+                                       PrettyLinksExtension(),
                                        "codehilite",
                                        "fenced_code",
                                        "footnotes",
@@ -363,3 +357,25 @@ class Post(misc.Object):
 
                # Update search indices
                self.backend.blog.refresh()
+
+
+class PrettyLinksExtension(markdown.extensions.Extension):
+       def extendMarkdown(self, md):
+               md.preprocessors.register(BugzillaLinksPreprocessor(md), "bugzilla", 10)
+               md.preprocessors.register(CVELinksPreprocessor(md), "cve", 10)
+
+
+class BugzillaLinksPreprocessor(markdown.preprocessors.Preprocessor):
+       regex = re.compile(r"(?:#(\d+))", 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)