[PATCH] Allow C&P from file revision in hgweb without also copying line numbers

Jordi Gutiérrez Hermoso jordigh at octave.org
Tue Oct 11 11:15:00 CDT 2011


# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh at octave.org>
# Date 1318312976 18000
# Node ID df28d9372ba72bc84a5f78c4e993127a5234284b
# Parent  c133296b0a3a7d6372457fdd37c6b60c2b7c3bca
Allow C&P from file revision in hgweb without also copying line numbers

It's useful from the web interface to be able to highlight code in
order to copy and paste it elsewhere. Unfortunately with the current
web interface, this has the annoying result of also including the line
numbers, so the pasted code will have them too and requires further
surgery to work.

This patch puts the line numbers for file revisions in a separate <td>
inside a table, and the actual code in another <td>, so there are only
two <td>s in the table. This is also done by passing the line numbers
to the template in a separate generator object than the actual line
contents themselves.

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -67,22 +67,30 @@
     f = fctx.path()
     text = fctx.data()
     parity = paritygen(web.stripecount)
+    parity_nums = paritygen(web.stripecount)
 
     if binary(text):
         mt = mimetypes.guess_type(f)[0] or 'application/octet-stream'
         text = '(binary:%s)' % mt
 
+    all_lines = text.splitlines(True)
+
     def lines():
-        for lineno, t in enumerate(text.splitlines(True)):
+        for t in all_lines:
             yield {"line": t,
-                   "lineid": "l%d" % (lineno + 1),
+                   "parity": parity.next()}
+
+    def linenums():
+        for lineno in range(0, len(all_lines)):
+            yield {"lineid": "l%d" % (lineno + 1),
                    "linenumber": "% 6d" % (lineno + 1),
-                   "parity": parity.next()}
+                   "parity": parity_nums.next()}
 
     return tmpl("filerevision",
                 file=f,
                 path=webutil.up(f),
                 text=lines(),
+                textnums=linenums(),
                 rev=fctx.rev(),
                 node=fctx.hex(),
                 author=fctx.user(),
diff --git a/mercurial/templates/coal/map b/mercurial/templates/coal/map
--- a/mercurial/templates/coal/map
+++ b/mercurial/templates/coal/map
@@ -65,7 +65,9 @@
 filediff = ../paper/filediff.tmpl
 filelog = ../paper/filelog.tmpl
 fileline = '
-  <div class="parity{parity} source"><a href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</div>'
+         <div class="parity{parity} source"> {line|escape}</div>'
+filelinenums ='
+             <div class="parity{parity} source"> <a href="#{lineid}" id="{lineid}">{linenumber}</a></div>'
 filelogentry = ../paper/filelogentry.tmpl
 
 annotateline = '
diff --git a/mercurial/templates/gitweb/filerevision.tmpl b/mercurial/templates/gitweb/filerevision.tmpl
--- a/mercurial/templates/gitweb/filerevision.tmpl
+++ b/mercurial/templates/gitweb/filerevision.tmpl
@@ -58,7 +58,16 @@
 </div>
 
 <div class="page_body">
-{text%fileline}
+  <table cellspacing="0" cellpadding="0">
+    <tr>
+      <td>
+        {textnums%filelinenums}
+      </td>
+      <td>
+        {text%fileline}
+      </td>
+    </tr>
+  </table>
 </div>
 
 {footer}
diff --git a/mercurial/templates/gitweb/map b/mercurial/templates/gitweb/map
--- a/mercurial/templates/gitweb/map
+++ b/mercurial/templates/gitweb/map
@@ -83,9 +83,9 @@
 filediff = filediff.tmpl
 filelog = filelog.tmpl
 fileline = '
-  <div style="font-family:monospace" class="parity{parity}">
-    <pre><a class="linenr" href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</pre>
-  </div>'
+         <div style="font-family:monospace" class="parity{parity}"><pre>{line|escape} </pre></div>'
+filelinenums ='
+         <div style="font-family:monospace" class="parity{parity}"><pre><a class="linenr" href="#{lineid}" id="{lineid}">{linenumber}</a></pre></div>'
 annotateline = '
   <tr style="font-family:monospace" class="parity{parity}">
     <td class="linenr" style="text-align: right;">
diff --git a/mercurial/templates/monoblue/filerevision.tmpl b/mercurial/templates/monoblue/filerevision.tmpl
--- a/mercurial/templates/monoblue/filerevision.tmpl
+++ b/mercurial/templates/monoblue/filerevision.tmpl
@@ -59,7 +59,16 @@
     <p class="description">{desc|strip|escape|addbreaks|nonempty}</p>
 
     <div class="source">
-    {text%fileline}
+      <table cellspacing="0" cellpadding="0">
+        <tr>
+          <td>
+            {textnums%filelinenums}
+          </td>
+          <td>
+            {text%fileline}
+          </td>
+        </tr>
+      </table>
     </div>
 
 {footer}
diff --git a/mercurial/templates/monoblue/map b/mercurial/templates/monoblue/map
--- a/mercurial/templates/monoblue/map
+++ b/mercurial/templates/monoblue/map
@@ -77,7 +77,11 @@
 filelog = filelog.tmpl
 fileline = '
   <div style="font-family:monospace" class="parity{parity}">
-    <pre><a class="linenr" href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</pre>
+    <pre>{line|escape} </pre>
+  </div>'
+filelinenums = '
+  <div style="font-family:monospace" class="parity{parity}">
+    <pre><a class="linenr" href="#{lineid}" id="{lineid}">{linenumber}</a></pre>
   </div>'
 annotateline = '
   <tr class="parity{parity}">
diff --git a/mercurial/templates/paper/filerevision.tmpl b/mercurial/templates/paper/filerevision.tmpl
--- a/mercurial/templates/paper/filerevision.tmpl
+++ b/mercurial/templates/paper/filerevision.tmpl
@@ -67,7 +67,16 @@
 
 <div class="overflow">
 <div class="sourcefirst"> line source</div>
+<table cellspacing="0" cellpadding="0">
+<tr>
+<td>
+{textnums%filelinenums}
+</td>
+<td>
 {text%fileline}
+</td>
+</tr>
+</table>
 <div class="sourcelast"></div>
 </div>
 </div>
diff --git a/mercurial/templates/paper/map b/mercurial/templates/paper/map
--- a/mercurial/templates/paper/map
+++ b/mercurial/templates/paper/map
@@ -64,7 +64,9 @@
 filediff = filediff.tmpl
 filelog = filelog.tmpl
 fileline = '
-  <div class="parity{parity} source"><a href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</div>'
+         <div class="parity{parity} source"> {line|escape}</div>'
+filelinenums ='
+             <div class="parity{parity} source"> <a href="#{lineid}" id="{lineid}">{linenumber}</a></div>'
 filelogentry = filelogentry.tmpl
 
 annotateline = '
diff --git a/mercurial/templates/spartan/filerevision.tmpl b/mercurial/templates/spartan/filerevision.tmpl
--- a/mercurial/templates/spartan/filerevision.tmpl
+++ b/mercurial/templates/spartan/filerevision.tmpl
@@ -40,8 +40,19 @@
 </tr>
 </table>
 
-<pre>
-{text%fileline}
-</pre>
+<table cellspacing="0" cellpadding="0">
+  <tr>
+    <td>
+      <pre style="line-height: 16px">
+        {textnums%filelinenums}
+      </pre>
+    </td>
+    <td>
+      <pre style="line-height: 16px">
+        {text%fileline}
+      </pre>
+    </td>
+  </tr>
+</table>
 
 {footer}
diff --git a/mercurial/templates/spartan/map b/mercurial/templates/spartan/map
--- a/mercurial/templates/spartan/map
+++ b/mercurial/templates/spartan/map
@@ -47,7 +47,8 @@
 fileannotate = fileannotate.tmpl
 filediff = filediff.tmpl
 filelog = filelog.tmpl
-fileline = '<div class="parity{parity}"><a class="lineno" href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</div>'
+fileline = '<div class="parity{parity}"> {line|escape}</div>'
+filelinenums = '<div class="parity{parity}"><a class="lineno" href="#{lineid}" id="{lineid}">{linenumber}</a></div>'
 filelogentry = filelogentry.tmpl
 
 # The   ensures that all table cells have content (even if there


More information about the Mercurial-devel mailing list