From: Michael Tremer Date: Sun, 23 Oct 2022 12:05:47 +0000 (+0000) Subject: web: Refactor Text UI module X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b384ba487661f8995b85e35e775130376d7b687c;p=pbs.git web: Refactor Text UI module Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index c0806e1d..084303c1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/configure.ac b/configure.ac index a8e3e871..a03cdda6 100644 --- a/configure.ac +++ b/configure.ac @@ -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 index 0d1dcb07..00000000 --- a/src/templates/modules/text.html +++ /dev/null @@ -1,7 +0,0 @@ -{% for paragraph in paragraphs %} -

- {% apply linkify %} - {{ paragraph }} - {% end %} -

-{% end %} \ No newline at end of file diff --git a/src/web/ui_modules.py b/src/web/ui_modules.py index 49ebf48f..1743f66e 100644 --- a/src/web/ui_modules.py +++ b/src/web/ui_modules.py @@ -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 = """%s""" - + """ + 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):