filtering changelog messages - potention extension for hgweb?

Jan Capek jen at jikos.cz
Mon Dec 1 15:14:17 CST 2008


Hi,

I have come across an hgweb usecase where I needed a filtering extension:

Use case
--------
- a mercurial repository is being used with trac for issue tracking. The 
  repository has hooks to check that changelog messages contain valid bug 
  ID's (similar to bugzilla extension)

- the bug ID's that show up in changelog messages should be 
  hyperlinks to the bug tracking system when using the hgweb interface


Implementation
--------------
To implement the above scenario a new filter extension has been created. 
This extension is configurable via hgrc ([urllnk] section).



I am attaching the extension itself and an example patch of the gitweb 
style to show its use.

Would this extension be of any interest to mercurial community, is it 
possible to post it on the selenic.com wiki?


Thanks,

Jan
-------------- next part --------------
Index: git-web-with-logo/summary.tmpl
===================================================================
--- git-web-with-logo.orig/summary.tmpl	2008-11-27 13:20:32.000000000 +0100
+++ git-web-with-logo/summary.tmpl	2008-11-27 16:28:39.000000000 +0100
@@ -29,7 +29,7 @@
 
 <div class="title">&nbsp;</div>
 <table cellspacing="0">
-<tr><td>description</td><td>#desc#</td></tr>
+<tr><td>description</td><td>#desc|urllnk#</td></tr>
 <tr><td>owner</td><td>#owner|obfuscate#</td></tr>
 <tr><td>last change</td><td>#lastchange|rfc822date#</td></tr>
 </table>
Index: git-web-with-logo/changeset.tmpl
===================================================================
--- git-web-with-logo.orig/changeset.tmpl	2008-11-27 13:20:32.000000000 +0100
+++ git-web-with-logo/changeset.tmpl	2008-11-27 16:28:39.000000000 +0100
@@ -29,7 +29,7 @@
 </table></div>
 
 <div class="page_body">
-#desc|strip|escape|addbreaks#
+#desc|strip|escape|addbreaks|urllnk#
 </div>
 <div class="list_head"></div>
 <div class="title_text">
Index: git-web-with-logo/changelogentry.tmpl
===================================================================
--- git-web-with-logo.orig/changelogentry.tmpl	2008-11-27 13:20:32.000000000 +0100
+++ git-web-with-logo/changelogentry.tmpl	2008-11-27 16:28:39.000000000 +0100
@@ -8,7 +8,7 @@
 <i>#author|obfuscate# [#date|rfc822date#] rev #rev#</i><br/>
 </div>
 <div class="log_body">
-#desc|strip|escape|addbreaks#
+#desc|strip|escape|addbreaks|urllnk#
 <br/>
 <br/>
 </div>
Index: git-web-with-logo/fileannotate.tmpl
===================================================================
--- git-web-with-logo.orig/fileannotate.tmpl	2008-11-27 13:20:32.000000000 +0100
+++ git-web-with-logo/fileannotate.tmpl	2008-11-27 16:28:39.000000000 +0100
@@ -48,7 +48,7 @@
 </div>
 
 <div class="page_path">
-{desc|strip|escape|addbreaks}
+{desc|strip|escape|addbreaks|urllnk}
 </div>
 <div class="page_body">
 <table>
-------------- next part --------------
from mercurial import templatefilters
import re

class UrlLnkFilter(object):
    """This Filter allows replacing user tags with URL references

    User may define the 'base URL' and the 'regular expression' to
    match a user tag and the 'replacement expression'. None of these
    need to be defined as there are defaults values
    
    The filter can be used for the hgweb templates to generate links
    into a bug tracking system. The basic idea is to modify the
    templates like this:
    #desc|strip|escape|addbreaks#
    #desc|strip|escape|addbreaks|urllnk#

    By default the filter detects any text referencing tags prefixed
    with #,ticket,issue or bug followed by a number. This is replaced
    by <a href = "base_url/number">original text</a>. Example:

    Input text:
    -----------
    Changelog entry references #3  

    Output text
    -----------
    Changelog entry references <a href="http://some_base_url/ticket/3">#3</a>


    @var base_url - denotes the base URL to be prepended to generated
    link. Defaults to empty string.
    @var utag_regexp - regular expression to match the user tag.
    @var utag_replace - replacement expression 
    """

    def __init__(self, ui, repo):
        """Initializes regular expressions to match user tags

        The defaults can be overiden by the mercurial configuration

        @param self
        @param ui - user interface of the mercurial
        @param repo - current mercurial repository
        """
        conf_section = 'urllnkfilter'

        # base url used for anchor
        self.base_url = ui.config(conf_section, 'base_url', '')

        # for better efficiency, precompile the regular expression
        txt_regexp = ui.config(conf_section, 'ticket_regexp', 
                                     '(\s+)((?:#|(?:ticket|issue|bug)[: ]?)([0-9]+))')
        self.utag_regexp = re.compile(txt_regexp)

        # replacement string
        self.utag_replace = ui.config(conf_section, 'ticket_replace', 
                                 '\g<1><a href="%s/\g<3>">\g<2></a>' % self.base_url)

    def __call__(self, text):
        """The object implements a callable interface to perform filtering.

        @param self
        """
        return re.sub(self.utag_regexp, self.utag_replace, text)


def reposetup(ui, repo):
    templatefilters.filters["urllnk"] = UrlLnkFilter(ui, repo)


More information about the Mercurial mailing list