X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=src%2Fbackend%2Fwiki.py;h=0a2d925a8cff60094b22e7f6bcede7802a34e0dc;hb=245a2e365aacae8dde44fc319e11349d6339151e;hp=cc5964d14a693e3633d3b65ebfbfb731958f0336;hpb=6fdc2a4833f61107954bd43c9832bd712efb1905;p=ipfire.org.git diff --git a/src/backend/wiki.py b/src/backend/wiki.py index cc5964d1..0a2d925a 100644 --- a/src/backend/wiki.py +++ b/src/backend/wiki.py @@ -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"") + # 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)