import datetime
import feedparser
import markdown
+import markdown.extensions
+import markdown.preprocessors
import re
import textile
import tornado.gen
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)
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",
# 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)