]> git.ipfire.org Git - pbs.git/commitdiff
web: Refactor Text UI module
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 23 Oct 2022 12:05:47 +0000 (12:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 23 Oct 2022 12:05:47 +0000 (12:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
configure.ac
src/templates/modules/text.html [deleted file]
src/web/ui_modules.py

index c0806e1d68e5722aa5e4f23b686cd78416dd14f1..084303c12a3774b86027f77cc8401dfcd0168316 100644 (file)
@@ -255,8 +255,7 @@ dist_templates_modules_DATA = \
        src/templates/modules/commit-message.html \
        src/templates/modules/link-to-user.html \
        src/templates/modules/packages-files-table.html \
-       src/templates/modules/source-table.html \
-       src/templates/modules/text.html
+       src/templates/modules/source-table.html
 
 templates_modulesdir = $(templatesdir)/modules
 
index a8e3e871f32dc33b7ba77bbadbe600bcc1ae4fb4..a03cdda6a2ff1e75ad8ed99fc9301859ad60dc1c 100644 (file)
@@ -86,6 +86,7 @@ m4_pattern_forbid([^_?PKG_[A-Z_]+$],[*** pkg.m4 missing, please install pkg-conf
 AX_PYTHON_MODULE([boto3], [fatal])
 AX_PYTHON_MODULE([kerberos], [fatal])
 AX_PYTHON_MODULE([location], [fatal])
+AX_PYTHON_MODULE([markdown], [fatal])
 AX_PYTHON_MODULE([pygments], [fatal])
 AX_PYTHON_MODULE([systemd.journal], [fatal])
 
diff --git a/src/templates/modules/text.html b/src/templates/modules/text.html
deleted file mode 100644 (file)
index 0d1dcb0..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-{% for paragraph in paragraphs %}
-    <p>
-            {% apply linkify %}
-                {{ paragraph }}
-            {% end %}
-    </p>
-{% end %}
\ No newline at end of file
index 49ebf48f914d625407986afe439a6f727ceb392c..1743f66ed8337fea80b86c0b71db7588e6ef93b2 100644 (file)
@@ -1,6 +1,7 @@
 #!/usr/bin/python
 
 import datetime
+import markdown
 import pygments
 import pygments.formatters
 import pygments.lexers
@@ -17,34 +18,41 @@ class UIModule(tornado.web.UIModule):
 
 
 class TextModule(UIModule):
-       BUGZILLA_PATTERN = re.compile(r"(?:bug\s?|#)(\d+)")
-       CVE_PATTERN      = re.compile(r"(?:CVE)[\s\-](\d{4}\-\d{4})")
-
-       LINK = """<a href="%s" target="_blank" rel="noopener">%s</a>"""
-
+       """
+               Renders the text through the Markdown processor
+       """
        def render(self, text):
-               # Handle empty messages
-               if not text:
-                       return ""
+               return markdown.markdown(text,
+                       extensions=[
+                               PrettyLinksExtension(),
+                               "codehilite",
+                               "fenced_code",
+                               "sane_lists",
+                       ])
+
 
-               # Search for bug ids that need to be linked to bugzilla
-               text = re.sub(self.BUGZILLA_PATTERN, self._bugzilla_repl, text, re.I|re.U)
+class PrettyLinksExtension(markdown.extensions.Extension):
+       def extendMarkdown(self, md):
+               md.preprocessors.register(BugzillaLinksPreprocessor(md), "bugzilla", 10)
+               md.preprocessors.register(CVELinksPreprocessor(md), "cve", 10)
 
-               # Search for CVE numbers and create hyperlinks.
-               text = re.sub(self.CVE_PATTERN, self._cve_repl, text, re.I|re.U)
 
-               return self.render_string("modules/text.html", paragraphs=text.split("\n\n"))
+class BugzillaLinksPreprocessor(markdown.preprocessors.Preprocessor):
+       regex = re.compile(r"(?:#(\d{5,}))", re.I)
 
-       def _bugzilla_repl(self, m):
-               bug_id = m.group(1)
+       def run(self, lines):
+               for line in lines:
+                       yield self.regex.sub(
+                               r"[#\1](https://bugzilla.ipfire.org/show_bug.cgi?id=\1)", line)
 
-               # Get the URL
-               bug_url = self.backend.bugzilla.bug_url(bug_id)
 
-               return self.LINK % (bug_url, m.group(0))
+class CVELinksPreprocessor(markdown.preprocessors.Preprocessor):
+       regex = re.compile(r"(?:CVE)[\s\-](\d{4}\-\d+)")
 
-       def _cve_repl(self, m):
-               return self.LINK % ("http://cve.mitre.org/cgi-bin/cvename.cgi?name=%s" % m.group(1), m.group(0))
+       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 HighlightModule(UIModule):