Add line anchors to file and annotate views for hgweb

Edward Lee edilee at mozilla.com
Sun Sep 2 22:38:15 CDT 2007


This patch makes the line numbers into anchor tags with id "l<num>"
(e.g., l123) that link to itself with href=#l<num>. Updates default
and gitweb templates.

hgweb's ability to show the whole repository for any given changeset
is really useful, but referring to a particular line of a file such as
from a bug report would be nice. The patch adds a couple new
template-vars such as lineid and linenumber. Also, the annotate pages
didn't have line numbers to begin with, so there's a column for that
now.

Ed
-------------- next part --------------
# HG changeset patch
# User Edward Lee <edward.lee at engineering.uiuc.edu>
# Date 1188784859 18000
# Node ID 239641038ccf757c09aa4f220690f957019a9fff
# Parent  a176f9c8b26e0a7cd526b7220bdb76be0b9e8fa9
Add line anchors to file and annotate views for hgweb

diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -396,6 +396,7 @@ class hgweb(object):
         def lines():
             for l, t in enumerate(text.splitlines(1)):
                 yield {"line": t,
+                       "lineid": "l%d" % (l + 1),
                        "linenumber": "% 6d" % (l + 1),
                        "parity": parity.next()}
 
@@ -423,19 +424,23 @@ class hgweb(object):
 
         def annotate(**map):
             last = None
+            lineno = 0
             for f, l in fctx.annotate(follow=True):
                 fnode = f.filenode()
                 name = self.repo.ui.shortuser(f.user())
 
                 if last != fnode:
                     last = fnode
+                lineno = lineno + 1
 
                 yield {"parity": parity.next(),
                        "node": hex(f.node()),
                        "rev": f.rev(),
                        "author": name,
                        "file": f.path(),
-                       "line": l}
+                       "line": l,
+                       "lineid": "l%d" % lineno,
+                       "linenumber": "% 6d" % lineno}
 
         yield self.t("fileannotate",
                      file=f,
diff --git a/templates/gitweb/map b/templates/gitweb/map
--- a/templates/gitweb/map
+++ b/templates/gitweb/map
@@ -22,8 +22,8 @@ fileannotate = fileannotate.tmpl
 fileannotate = fileannotate.tmpl
 filediff = filediff.tmpl
 filelog = filelog.tmpl
-fileline = '<div style="font-family:monospace" class="parity#parity#"><pre><span class="linenr">   #linenumber#</span> #line|escape#</pre></div>'
-annotateline = '<tr style="font-family:monospace" class="parity#parity#"><td class="linenr" style="text-align: right;"><a href="#url#annotate/#node|short#/#file|urlescape#{sessionvars%urlparameter}">#author|obfuscate#@#rev#</a></td><td><pre>#line|escape#</pre></td></tr>'
+fileline = '<div style="font-family:monospace" class="parity#parity#"><pre><a class="linenr" href="##lineid#" id="#lineid#">#linenumber#</a> #line|escape#</pre></div>'
+annotateline = '<tr style="font-family:monospace" class="parity#parity#"><td class="linenr" style="text-align: right;"><a href="#url#annotate/#node|short#/#file|urlescape#{sessionvars%urlparameter}">#author|obfuscate#@#rev#</a></td><td><pre><a class="linenr" href="##lineid#" id="#lineid#">#linenumber#</a></pre></td><td><pre>#line|escape#</pre></td></tr>'
 difflineplus = '<div style="color:#008800;">#line|escape#</div>'
 difflineminus = '<div style="color:#cc0000;">#line|escape#</div>'
 difflineat = '<div style="color:#990099;">#line|escape#</div>'
diff --git a/templates/map b/templates/map
--- a/templates/map
+++ b/templates/map
@@ -21,9 +21,9 @@ fileannotate = fileannotate.tmpl
 fileannotate = fileannotate.tmpl
 filediff = filediff.tmpl
 filelog = filelog.tmpl
-fileline = '<div class="parity#parity#"><span class="lineno">#linenumber#</span>#line|escape#</div>'
+fileline = '<div class="parity#parity#"><a class="lineno" href="##lineid#" id="#lineid#">#linenumber#</a>#line|escape#</div>'
 filelogentry = filelogentry.tmpl
-annotateline = '<tr class="parity#parity#"><td class="annotate"><a href="#url#annotate/#node|short#/#file|urlescape#{sessionvars%urlparameter}">#author|obfuscate#@#rev#</a></td><td><pre>#line|escape#</pre></td></tr>'
+annotateline = '<tr class="parity#parity#"><td class="annotate"><a href="#url#annotate/#node|short#/#file|urlescape#{sessionvars%urlparameter}">#author|obfuscate#@#rev#</a></td><td><a class="lineno" href="##lineid#" id="#lineid#">#linenumber#</a></td><td><pre>#line|escape#</pre></td></tr>'
 difflineplus = '<span class="plusline">#line|escape#</span>'
 difflineminus = '<span class="minusline">#line|escape#</span>'
 difflineat = '<span class="atline">#line|escape#</span>'


More information about the Mercurial mailing list