summaryrefslogtreecommitdiffstats
path: root/aurweb/scripts
diff options
context:
space:
mode:
authorLukas Fleischer <lfleischer@archlinux.org>2017-04-23 14:56:07 +0200
committerLukas Fleischer <lfleischer@archlinux.org>2017-04-24 17:41:35 +0200
commitbb623fc545f0e0187cc9b32386c383b732bcc9ae (patch)
treea07d2fe5ff5fcddf32cc1c85de4d04bc6807b6f9 /aurweb/scripts
parenta9ac385cb90d0251253a2a3925f72a71af52f97b (diff)
downloadaur-bb623fc545f0e0187cc9b32386c383b732bcc9ae.tar.gz
aur-bb623fc545f0e0187cc9b32386c383b732bcc9ae.tar.xz
Make references to Git commits clickable
Automatically detect Git commit identifiers, shorten them, and make them link to the cgit interface. Implements FS#43290. Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
Diffstat (limited to 'aurweb/scripts')
-rwxr-xr-xaurweb/scripts/rendercomment.py59
1 files changed, 54 insertions, 5 deletions
diff --git a/aurweb/scripts/rendercomment.py b/aurweb/scripts/rendercomment.py
index c8921f8f..659e18ad 100755
--- a/aurweb/scripts/rendercomment.py
+++ b/aurweb/scripts/rendercomment.py
@@ -1,12 +1,17 @@
#!/usr/bin/python3
import re
+import pygit2
import sys
import bleach
import markdown
+import aurweb.config
import aurweb.db
+repo_path = aurweb.config.get('serve', 'repo-path')
+commit_uri = aurweb.config.get('options', 'commit_uri')
+
class LinkifyPreprocessor(markdown.preprocessors.Preprocessor):
_urlre = re.compile(r'(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?'
@@ -21,10 +26,53 @@ class LinkifyExtension(markdown.extensions.Extension):
md.preprocessors.add('linkify', LinkifyPreprocessor(md), '_end')
+class GitCommitsPreprocessor(markdown.preprocessors.Preprocessor):
+ _oidre = re.compile(r'(\b)([0-9a-f]{7,40})(\b)')
+ _repo = pygit2.Repository(repo_path)
+ _head = None
+
+ def __init__(self, md, head):
+ self._head = head
+ super(markdown.preprocessors.Preprocessor, self).__init__(md)
+
+ def handleMatch(self, m):
+ oid = m.group(2)
+ if oid not in self._repo:
+ return oid
+
+ prefixlen = 12
+ while prefixlen < 40:
+ if oid[:prefixlen] in self._repo:
+ break
+ prefixlen += 1
+
+ html = '[`' + oid[:prefixlen] + '`]'
+ html += '(' + commit_uri % (self._head, oid[:prefixlen]) + ')'
+
+ return html
+
+ def run(self, lines):
+ return [self._oidre.sub(self.handleMatch, line) for line in lines]
+
+
+class GitCommitsExtension(markdown.extensions.Extension):
+ _head = None
+
+ def __init__(self, head):
+ self._head = head
+ super(markdown.extensions.Extension, self).__init__()
+
+ def extendMarkdown(self, md, md_globals):
+ preprocessor = GitCommitsPreprocessor(md, self._head)
+ md.preprocessors.add('git-commits', preprocessor, '_end')
+
+
def get_comment(conn, commentid):
- cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?',
- [commentid])
- return cur.fetchone()[0]
+ cur = conn.execute('SELECT PackageComments.Comments, PackageBases.Name '
+ 'FROM PackageComments INNER JOIN PackageBases '
+ 'ON PackageBases.ID = PackageComments.PackageBaseID '
+ 'WHERE PackageComments.ID = ?', [commentid])
+ return cur.fetchone()
def save_rendered_comment(conn, commentid, html):
@@ -37,8 +85,9 @@ def main():
conn = aurweb.db.Connection()
- text = get_comment(conn, commentid)
- html = markdown.markdown(text, extensions=['nl2br', LinkifyExtension()])
+ text, pkgbase = get_comment(conn, commentid)
+ html = markdown.markdown(text, extensions=['nl2br', LinkifyExtension(),
+ GitCommitsExtension(pkgbase)])
allowed_tags = bleach.sanitizer.ALLOWED_TAGS + ['p', 'br']
html = bleach.clean(html, tags=allowed_tags)
save_rendered_comment(conn, commentid, html)