Patchwork help: include section heading if section depth changes

login
register
mail settings
Submitter timeless@mozdev.org
Date Jan. 1, 2016, 6:57 p.m.
Message ID <b225e760bdb47c0aafdf.1451674627@waste.org>
Download mbox | patch
Permalink /patch/12464/
State Accepted
Headers show

Comments

timeless@mozdev.org - Jan. 1, 2016, 6:57 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1451667553 0
#      Fri Jan 01 16:59:13 2016 +0000
# Node ID b225e760bdb47c0aafdfde7268905fdcefd97779
# Parent  44f717c879033f28d3f7e7dc9f34aa394d2fea3f
help: include section heading if section depth changes

This makes it easier to distinguish between:

"format"
--------
"usestore"
...
    Enabled by default.

and

"progress.format"
    Format of the progress bar.
Yuya Nishihara - Jan. 5, 2016, 1:36 p.m.
On Fri, 01 Jan 2016 12:57:07 -0600, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1451667553 0
> #      Fri Jan 01 16:59:13 2016 +0000
> # Node ID b225e760bdb47c0aafdfde7268905fdcefd97779
> # Parent  44f717c879033f28d3f7e7dc9f34aa394d2fea3f
> help: include section heading if section depth changes

Updated the test output and pushed to the clowncopter, thanks. A couple of
trivial comments follow.

> --- a/mercurial/minirst.py
> +++ b/mercurial/minirst.py
> @@ -661,12 +661,32 @@
>          sections = getsections(blocks)
>          blocks = []
>          i = 0
> +        lastparents = []
> +        synthetic = []
> +        collapse = True
>          while i < len(sections):
>              name, nest, b = sections[i]
>              del parents[nest:]
> -            parents.append(name)
> +            parents.append(i)
>              if name == section:
> -                b[0]['path'] = parents[3:]
> +                if lastparents != parents:
> +                    llen = len(lastparents)
> +                    plen = len(parents)
> +                    if llen and llen != plen:
> +                        collapse = False
> +                    s = []
> +                    for j in xrange(3, plen - 1):
> +                        parent = parents[j]
> +                        if (j >= llen or
> +                            lastparents[j] != parent):
> +                            s.append(len(blocks))
> +                            sec = sections[parent][2]
> +                            blocks.append(sec[0])
> +                            blocks.append(sec[-1])

I would add new {'type': 'margin', ...} object instead of sec[-1].

> +                    if s:
> +                        synthetic.append(s)
> +
> +                lastparents = parents[:]
>                  blocks.extend(b)
>  
>                  ## Also show all subnested sections
> @@ -674,18 +694,19 @@
>                      i += 1
>                      blocks.extend(sections[i][2])
>              i += 1
> +        if collapse:
> +            synthetic.reverse()
> +            for s in synthetic:
> +                path = [blocks[i]['lines'][0] for i in s]
> +                real = s[-1] + 2
> +                realline = blocks[real]['lines']
> +                realline[0] = ('"%s"' %
> +                    '.'.join(path + [realline[0]]).replace('"',''))
> +                del blocks[s[0]:real]

It will be nice if the whole process can be refactored to 2 or 3 phases,
for example,

 1. scan sections to decide "collapse" and the lowest "nest" level
 2.a. if "collapse", build "path" and append matched body to "blocks"
 2.b. else, collect section headers down to matched body and append them

Regards,

Patch

diff --git a/mercurial/minirst.py b/mercurial/minirst.py
--- a/mercurial/minirst.py
+++ b/mercurial/minirst.py
@@ -661,12 +661,32 @@ 
         sections = getsections(blocks)
         blocks = []
         i = 0
+        lastparents = []
+        synthetic = []
+        collapse = True
         while i < len(sections):
             name, nest, b = sections[i]
             del parents[nest:]
-            parents.append(name)
+            parents.append(i)
             if name == section:
-                b[0]['path'] = parents[3:]
+                if lastparents != parents:
+                    llen = len(lastparents)
+                    plen = len(parents)
+                    if llen and llen != plen:
+                        collapse = False
+                    s = []
+                    for j in xrange(3, plen - 1):
+                        parent = parents[j]
+                        if (j >= llen or
+                            lastparents[j] != parent):
+                            s.append(len(blocks))
+                            sec = sections[parent][2]
+                            blocks.append(sec[0])
+                            blocks.append(sec[-1])
+                    if s:
+                        synthetic.append(s)
+
+                lastparents = parents[:]
                 blocks.extend(b)
 
                 ## Also show all subnested sections
@@ -674,18 +694,19 @@ 
                     i += 1
                     blocks.extend(sections[i][2])
             i += 1
+        if collapse:
+            synthetic.reverse()
+            for s in synthetic:
+                path = [blocks[i]['lines'][0] for i in s]
+                real = s[-1] + 2
+                realline = blocks[real]['lines']
+                realline[0] = ('"%s"' %
+                    '.'.join(path + [realline[0]]).replace('"',''))
+                del blocks[s[0]:real]
 
     if style == 'html':
         text = formathtml(blocks)
     else:
-        if len([b for b in blocks if b['type'] == 'definition']) > 1:
-            i = 0
-            while i < len(blocks):
-                if blocks[i]['type'] == 'definition':
-                    if 'path' in blocks[i]:
-                        blocks[i]['lines'][0] = '"%s"' % '.'.join(
-                            blocks[i]['path'])
-                i += 1
         text = ''.join(formatblock(b, width) for b in blocks)
     if keep is None:
         return text
diff --git a/tests/test-help.t b/tests/test-help.t
--- a/tests/test-help.t
+++ b/tests/test-help.t
@@ -1162,6 +1162,32 @@ 
   $ hg help config.type | egrep '^$'|wc -l
   \s*3 (re)
 
+Separate sections from subsections
+
+  $ hg help config.format | egrep '^    ("|-)|^\s*$' | uniq
+  
+      "format"
+      --------
+  
+      "usegeneraldelta"
+  
+      "usestore"
+  
+      "usefncache"
+  
+      "dotencode"
+  
+      "profiling"
+      -----------
+  
+      "format"
+  
+      "progress"
+      ----------
+  
+      "format"
+  
+
 Last item in help config.*:
 
   $ hg help config.`hg help config|grep '^    "'| \