]> git.ipfire.org Git - ipfire.org.git/commitdiff
docs: Implement our own Markdown renderer based on the blog
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 20 Dec 2023 12:12:36 +0000 (12:12 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 20 Dec 2023 12:12:36 +0000 (12:12 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/blog.py
src/backend/wiki.py

index b21c9aa9056bda1b26045fa2dfbfcb11f968d093..aed8311b1c40edfc87108180759e5d8733c998a0 100644 (file)
@@ -4,13 +4,12 @@ import datetime
 import feedparser
 import html2text
 import markdown
-import markdown.extensions
-import markdown.preprocessors
 import re
 import textile
 import unicodedata
 
 from . import misc
+from . import wiki
 from .decorators import *
 
 class Blog(misc.Object):
@@ -119,7 +118,7 @@ class Blog(misc.Object):
                if lang == "markdown":
                        return markdown.markdown(text,
                                extensions=[
-                                       PrettyLinksExtension(),
+                                       wiki.PrettyLinksExtension(),
                                        "codehilite",
                                        "fenced_code",
                                        "footnotes",
@@ -132,7 +131,8 @@ class Blog(misc.Object):
                elif lang == "textile":
                        return textile.textile(text)
 
-               return text
+               else:
+                       return text
 
        def refresh(self):
                """
@@ -403,25 +403,3 @@ class Post(misc.Object):
                        # Mark this post as announced
                        self.db.execute("UPDATE blog SET announced_at = CURRENT_TIMESTAMP \
                                WHERE id = %s", self.id)
-
-
-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{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)
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)