From 98568b137743eed35712bae0644b59c1eed84114 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Tue, 23 Nov 2021 21:52:55 +0100 Subject: [PATCH] log: display clickable links Replace explicit urls or email addresses by clickable links. Reuse the existing regular expression in srht/markdown.py. Detect references to valid commit ids and replace them by links to git.sr.ht commits. Signed-off-by: Robin Jarry --- gitsrht/app.py | 3 ++- gitsrht/git.py | 28 +++++++++++++++++++++++++++- gitsrht/templates/utils.html | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/gitsrht/app.py b/gitsrht/app.py index 9efa7c5..4928851 100644 --- a/gitsrht/app.py +++ b/gitsrht/app.py @@ -3,7 +3,7 @@ import os import stat from functools import lru_cache from gitsrht import urls -from gitsrht.git import commit_time, trim_commit, signature_time +from gitsrht.git import commit_time, commit_links, trim_commit, signature_time from gitsrht.repos import GitRepoApi from gitsrht.service import oauth_service, webhooks_notify from gitsrht.types import Access, Redirect, Repository, User @@ -46,6 +46,7 @@ class GitApp(ScmSrhtFlask): self.add_template_filter(urls.log_rss_url) self.add_template_filter(urls.refs_rss_url) self.add_template_filter(url_quote) + self.add_template_filter(commit_links) @self.context_processor def inject(): diff --git a/gitsrht/git.py b/gitsrht/git.py index 2daf611..8bf161b 100644 --- a/gitsrht/git.py +++ b/gitsrht/git.py @@ -4,7 +4,9 @@ from pygit2 import Repository as GitRepository, Tag from jinja2 import Markup, escape from stat import filemode import pygit2 -import json +import json, re +from srht.config import get_origin +from srht.markdown import PlainLink def strip_pgp_signature(text): if not text.strip().endswith("-----END PGP SIGNATURE-----"): @@ -33,6 +35,30 @@ def commit_time(commit): author = commit.author if hasattr(commit, 'author') else commit.get_object().author return signature_time(author) +_gitsrht = get_origin('git.sr.ht') +_commit_id_re = re.compile( r'\b[a-f0-9]{7,40}\b') + +def commit_links(message, repo): + def url_or_mail(match): + mail = match['mail'] + if mail: + return f'{mail}' + url = match['url'] + return f'{url}' + + msg = PlainLink.pattern.sub(url_or_mail, escape(message)) + + repo_url = f'{_gitsrht}/{repo.owner.canonical_name}/{repo.name}' + def commit_link(match): + try: + commit = repo.git_repo.revparse_single(match[0]) + return f'{match[0]}' + except (KeyError, ValueError): + # not a valid commit id in this repository, ignore + return match[0] + + return _commit_id_re.sub(commit_link, msg) + def _get_ref(repo, ref): return repo._get(ref) diff --git a/gitsrht/templates/utils.html b/gitsrht/templates/utils.html index a700b55..8258167 100644 --- a/gitsrht/templates/utils.html +++ b/gitsrht/templates/utils.html @@ -145,7 +145,7 @@ endif %}{% endfor %} {% if not skip_body %} {% if full_body %} -
{{c.message}}
+
{{ c.message | commit_links(repo) | safe }}
 {%- if diff %}
 {{diffstat(diff, anchor=c.oid.hex + "-")}}{% endif -%}
 
-- 2.38.4