[PATCH 1 of 4] minirst: create valid output when table data contains a newline

Simon Heimberg simohe at besonet.ch
Thu Feb 20 11:04:41 CST 2014


# HG changeset patch
# User Simon Heimberg <simohe at besonet.ch>
# Date 1392827541 -3600
#      Wed Feb 19 17:32:21 2014 +0100
# Node ID 9a836a1cde06f4b54effd23c3feaf9ca0ff9e269
# Parent  87e52e6425625ea4f7645cfe2fc491a21f9a6b51
minirst: create valid output when table data contains a newline

When table data contained a newline, the result of minirst.maketable
did not look nice plus it was not recognised by minirst.format:
  == === ====
  l1 1   one
  l2 2   2
22
  l3
  == === ====

This problem occurred when the description of options had a very long
translation which was split by newlines. Do not bother a translator with
this detail.

The multiline translations for option descriptions have been fixed in
baf1600adfbe in it.po, de.po and ro.po. I manually did the same as this patch
does, I removed the newlines.

When a newline was in the description, this created unusable help output:
  $ hg help somecommand
  hg somecommand [option]...

  with somecommand, you can...

  options:

  == =================== =======================================================
  =================================== --longdesc VALUE    xxxxxxxxxxxxxxxxxxxxxx
  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -n --norm
  normal desc --newline VALUE line1 line2 == =================== ===============
  ===========================================================================

now this looks much nicer:
  ...
  options:

      --longdesc VALUE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                       xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   -n --norm           normal desc
      --newline VALUE  line1 line2

diff -r 87e52e642562 -r 9a836a1cde06 mercurial/minirst.py
--- a/mercurial/minirst.py	Wed Feb 19 16:46:47 2014 -0600
+++ b/mercurial/minirst.py	Wed Feb 19 17:32:21 2014 +0100
@@ -697,6 +697,10 @@
     for row in data:
         l = []
         for w, v in zip(widths, row):
+            if '\n' in v:
+                # only remove line breaks and indentation, long lines are
+                # handled by the next tool
+                v = ' '.join(e.lstrip() for e in v.split('\n'))
             pad = ' ' * (w - encoding.colwidth(v))
             l.append(v + pad)
         out.append(indent + ' '.join(l) + "\n")
diff -r 87e52e642562 -r 9a836a1cde06 tests/test-help.t
--- a/tests/test-help.t	Wed Feb 19 16:46:47 2014 -0600
+++ b/tests/test-help.t	Wed Feb 19 17:32:21 2014 +0100
@@ -577,7 +577,10 @@
   >     pass
   > 
   > cmdtable = {
-  >     "nohelp": (nohelp, [], "hg nohelp"),
+  >     "nohelp": (nohelp, [('', 'longdesc', 3, 'x'*90),
+  >                         ('n', '', None, 'normal desc'),
+  >                         ('', 'newline', '', 'line1\nline2'),
+  >                        ], "hg nohelp"),
   > }
   > 
   > commands.norepo += ' nohelp'
@@ -592,6 +595,13 @@
   
   (no help text available)
   
+  options:
+  
+      --longdesc VALUE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+                       xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (default: 3)
+   -n --               normal desc
+      --newline VALUE  line1 line2
+  
   use "hg -v help nohelp" to show the global options
 
   $ hg help -k nohelp
diff -r 87e52e642562 -r 9a836a1cde06 tests/test-minirst.py
--- a/tests/test-minirst.py	Wed Feb 19 16:46:47 2014 -0600
+++ b/tests/test-minirst.py	Wed Feb 19 17:32:21 2014 +0100
@@ -244,3 +244,14 @@
 print table
 
 debugformats('table', table)
+
+data = [['s', 'long', 'line\ngoes on here'],
+        ['', 'xy', 'tried to fix here\n        by indenting']]
+
+rst = minirst.maketable(data, 1, False)
+table = ''.join(rst)
+
+print table
+
+debugformats('table+nl', table)
+
diff -r 87e52e642562 -r 9a836a1cde06 tests/test-minirst.py.out
--- a/tests/test-minirst.py.out	Wed Feb 19 16:46:47 2014 -0600
+++ b/tests/test-minirst.py.out	Wed Feb 19 17:32:21 2014 +0100
@@ -773,3 +773,34 @@
 </table>
 ----------------------------------------------------------------------
 
+ = ==== ======================================
+ s long line goes on here                     
+   xy   tried to fix here by indenting        
+ = ==== ======================================
+
+== table+nl ==
+60 column format:
+----------------------------------------------------------------------
+ s long line goes on here
+   xy   tried to fix here by indenting
+----------------------------------------------------------------------
+
+30 column format:
+----------------------------------------------------------------------
+ s long line goes on here
+   xy   tried to fix here by
+        indenting
+----------------------------------------------------------------------
+
+html format:
+----------------------------------------------------------------------
+<table>
+<tr><td>s</td>
+<td>long</td>
+<td>line goes on here</td></tr>
+<tr><td></td>
+<td>xy</td>
+<td>tried to fix here by indenting</td></tr>
+</table>
+----------------------------------------------------------------------
+


More information about the Mercurial-devel mailing list