Add line anchors to annotate, changeset, diff, file views for hgweb
authorEdward Lee <edward.lee@engineering.uiuc.edu>
Tue, 04 Sep 2007 22:25:37 -0500
changeset 6122 800e2756c9ab
parent 5472 23889160905a
child 6123 f7f25f58693a
Add line anchors to annotate, changeset, diff, file views for hgweb
mercurial/hgweb/common.py
mercurial/hgweb/hgweb_mod.py
templates/gitweb/map
templates/map
--- a/mercurial/hgweb/common.py	Fri Sep 07 16:48:42 2007 +0200
+++ b/mercurial/hgweb/common.py	Tue Sep 04 22:25:37 2007 -0500
@@ -76,3 +76,9 @@
             parity = 1 - parity
             count = 0
 
+def countgen(start=0, step=1):
+    """count forever -- useful for line numbers"""
+    while True:
+        yield start
+        start += step
+
--- a/mercurial/hgweb/hgweb_mod.py	Fri Sep 07 16:48:42 2007 +0200
+++ b/mercurial/hgweb/hgweb_mod.py	Tue Sep 04 22:25:37 2007 -0500
@@ -12,7 +12,7 @@
 from mercurial.i18n import gettext as _
 from mercurial import mdiff, ui, hg, util, archival, streamclone, patch
 from mercurial import revlog, templater
-from common import get_mtime, staticfile, style_map, paritygen
+from common import get_mtime, staticfile, style_map, paritygen, countgen
 
 def _up(p):
     if p[0] != "/":
@@ -170,16 +170,25 @@
                          file=f,
                          filenode=hex(fn or nullid))
 
+        blockcount = countgen()
         def prettyprintlines(diff):
-            for l in diff.splitlines(1):
+            blockno = blockcount.next()
+            for lineno, l in enumerate(diff.splitlines(1)):
+                if blockno == 0:
+                    lineno = lineno + 1
+                else:
+                    lineno = "%d.%d" % (blockno, lineno + 1)
+                type = "diffline"
                 if l.startswith('+'):
-                    yield self.t("difflineplus", line=l)
+                    type = "difflineplus"
                 elif l.startswith('-'):
-                    yield self.t("difflineminus", line=l)
+                    type = "difflineminus"
                 elif l.startswith('@'):
-                    yield self.t("difflineat", line=l)
-                else:
-                    yield self.t("diffline", line=l)
+                    type = "difflineat"
+                yield self.t(type,
+                             line=l,
+                             lineid="l%s" % lineno,
+                             linenumber="% 8s" % lineno)
 
         r = self.repo
         c1 = r.changectx(node1)
@@ -398,9 +407,10 @@
         mt = mt or 'text/plain'
 
         def lines():
-            for l, t in enumerate(text.splitlines(1)):
+            for lineno, t in enumerate(text.splitlines(1)):
                 yield {"line": t,
-                       "linenumber": "% 6d" % (l + 1),
+                       "lineid": "l%d" % (lineno + 1),
+                       "linenumber": "% 6d" % (lineno + 1),
                        "parity": parity.next()}
 
         yield self.t("filerevision",
@@ -427,7 +437,7 @@
 
         def annotate(**map):
             last = None
-            for f, l in fctx.annotate(follow=True):
+            for lineno, (f, l) in enumerate(fctx.annotate(follow=True)):
                 fnode = f.filenode()
                 name = self.repo.ui.shortuser(f.user())
 
@@ -439,7 +449,9 @@
                        "rev": f.rev(),
                        "author": name,
                        "file": f.path(),
-                       "line": l}
+                       "line": l,
+                       "lineid": "l%d" % (lineno + 1),
+                       "linenumber": "% 6d" % (lineno + 1)}
 
         yield self.t("fileannotate",
                      file=f,
--- a/templates/gitweb/map	Fri Sep 07 16:48:42 2007 +0200
+++ b/templates/gitweb/map	Tue Sep 04 22:25:37 2007 -0500
@@ -22,12 +22,12 @@
 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>'
-difflineplus = '<div style="color:#008800;">#line|escape#</div>'
-difflineminus = '<div style="color:#cc0000;">#line|escape#</div>'
-difflineat = '<div style="color:#990099;">#line|escape#</div>'
-diffline = '<div>#line|escape#</div>'
+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;"><a class="linenr" href="##lineid#" id="#lineid#">#linenumber#</a> #line|escape#</div>'
+difflineminus = '<div style="color:#cc0000;"><a class="linenr" href="##lineid#" id="#lineid#">#linenumber#</a> #line|escape#</div>'
+difflineat = '<div style="color:#990099;"><a class="linenr" href="##lineid#" id="#lineid#">#linenumber#</a> #line|escape#</div>'
+diffline = '<div><a class="linenr" href="##lineid#" id="#lineid#">#linenumber#</a> #line|escape#</div>'
 changelogparent = '<tr><th class="parent">parent #rev#:</th><td class="parent"><a href="#url#rev/#node|short#{sessionvars%urlparameter}">#node|short#</a></td></tr>'
 changesetparent = '<tr><td>parent {rev}</td><td style="font-family:monospace"><a class="list" href="{url}rev/{node|short}{sessionvars%urlparameter}">{node|short}</a></td></tr>'
 filerevparent = '<tr><td>parent {rev}</td><td style="font-family:monospace"><a class="list" href="{url}file/{node|short}/{file|urlescape}{sessionvars%urlparameter}">{rename%filerename}{node|short}</a></td></tr>'
--- a/templates/map	Fri Sep 07 16:48:42 2007 +0200
+++ b/templates/map	Tue Sep 04 22:25:37 2007 -0500
@@ -21,13 +21,13 @@
 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>'
-difflineplus = '<span class="plusline">#line|escape#</span>'
-difflineminus = '<span class="minusline">#line|escape#</span>'
-difflineat = '<span class="atline">#line|escape#</span>'
-diffline = '#line|escape#'
+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"><a class="lineno" href="##lineid#" id="#lineid#">#linenumber#</a>#line|escape#</span>'
+difflineminus = '<span class="minusline"><a class="lineno" href="##lineid#" id="#lineid#">#linenumber#</a>#line|escape#</span>'
+difflineat = '<span class="atline"><a class="lineno" href="##lineid#" id="#lineid#">#linenumber#</a>#line|escape#</span>'
+diffline = '<a class="lineno" href="##lineid#" id="#lineid#">#linenumber#</a>#line|escape#'
 changelogparent = '<tr><th class="parent">parent #rev#:</th><td class="parent"><a href="#url#rev/#node|short#{sessionvars%urlparameter}">#node|short#</a></td></tr>'
 changesetparent = '<tr><th class="parent">parent #rev#:</th><td class="parent"><a href="#url#rev/#node|short#{sessionvars%urlparameter}">#node|short#</a></td></tr>'
 filerevparent = '<tr><td class="metatag">parent:</td><td><a href="{url}file/{node|short}/{file|urlescape}{sessionvars%urlparameter}">{rename%filerename}{node|short}</a></td></tr>'