Add support for regexp-based replacing.
Mon Dec 25 22:18:20 UTC 2006 Alberto Bertogli <albertito@gmail.com>
* Add support for regexp-based replacing.
Add an option to replace text in the summary, using regexps. This allows you
to configure darcsweb to automatically detect and put links to other commits
or bug numbers.
This patch is heavily based on the one written by Kirill Smelkov
(kirr@mns.spb.ru).
diff -rN -u old-darcsweb/config.py.sample new-darcsweb/config.py.sample
--- old-darcsweb/config.py.sample 2013-07-18 20:17:22.000000000 +0000
+++ new-darcsweb/config.py.sample 2013-07-18 20:17:22.000000000 +0000
@@ -55,6 +55,26 @@
# darcsweb.
#logtimes = "/tmp/darcsweb_times"
+ # If you want darcsweb to automatically detect embedded URLs,
+ # define them here, using python-style regexps like the examples
+ # below. They will be replaced in summaries, logs, and commits.
+ # The following variables are replaced:
+ # myreponame: repository link (darcsweb.cgi?r=repo)
+ # reponame: repository name (repo)
+ #
+ #url_links = (
+ # Format is: (regexp, replacement)
+ # Some examples:
+ #
+ # Detect '#NNN' as a reference to bug database
+ #(r'#([0-9]+)',
+ # r'<a href="/bugs/show_bug.cgi?id=\1">#\1</a>'),
+ #
+ # Replace hashes with commit-links.
+ #(r'(\d{14}-[0-9a-f]{5}-[0-9a-f]{40}\.gz)',
+ # r'<a href="%(myreponame)s;a=commit;h=\1">\1</a>'),
+ #)
+
#
# From now on, every class is a repo configuration, with the same format
diff -rN -u old-darcsweb/darcsweb.cgi new-darcsweb/darcsweb.cgi
--- old-darcsweb/darcsweb.cgi 2013-07-18 20:17:22.000000000 +0000
+++ new-darcsweb/darcsweb.cgi 2013-07-18 20:17:22.000000000 +0000
@@ -161,6 +161,22 @@
pos = s.find("\t")
return s
+def replace_links(s):
+ """Replace user defined strings with links, as specified in the
+ configuration file."""
+ import re
+
+ vardict = {
+ "myreponame": config.myreponame,
+ "reponame": config.reponame,
+ }
+
+ for link_pat, link_dst in config.url_links:
+ s = re.sub(link_pat, link_dst % vardict, s)
+
+ return s
+
+
def highlight(s, l):
"Highlights appearences of s in l"
import re
@@ -1146,7 +1162,7 @@
for p in ps:
if p.comment:
- comment = escape(p.comment)
+ comment = replace_links(escape(p.comment))
fmt_comment = comment.replace('\n', '<br/>') + '\n'
fmt_comment += '<br/><br/>'
else:
@@ -1589,10 +1605,10 @@
'name': escape(p.name),
}
if p.comment:
- comment = escape(p.comment)
+ comment = replace_links(escape(p.comment))
c = comment.replace('\n', '<br/>\n')
print '<div class="page_body">'
- print escape(p.name), '<br/><br/>'
+ print replace_links(escape(p.name)), '<br/><br/>'
print c
print '</div>'
@@ -2321,6 +2337,11 @@
else:
config.logtimes = None
+ if "url_links" in dir(base):
+ config.url_links = base.url_links
+ else:
+ config.url_links = ()
+
if name and "footer" in dir(c):
config.footer = c.footer
elif "footer" in dir(base):