[PATCH 1 of 7] minirst: extract function that filters parsed blocks by section name

Yuya Nishihara yuya at tcha.org
Wed Aug 22 12:19:49 UTC 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1533436736 -32400
#      Sun Aug 05 11:38:56 2018 +0900
# Node ID 16d3fd4426d606ea6f154358003f48522a835ec8
# Parent  a2388d4e91380a2aec2027be5cda918f1c2a9d9e
minirst: extract function that filters parsed blocks by section name

I'll move some bits from minirst.format() to caller to make the function
interface simpler.

diff --git a/mercurial/minirst.py b/mercurial/minirst.py
--- a/mercurial/minirst.py
+++ b/mercurial/minirst.py
@@ -666,8 +666,21 @@ def formatblocks(blocks, width):
 def format(text, width=80, indent=0, keep=None, style='plain', section=None):
     """Parse and format the text according to width."""
     blocks, pruned = parse(text, indent, keep or [])
+    if section:
+        blocks = filtersections(blocks, section)
+    if style == 'html':
+        text = formathtml(blocks)
+    else:
+        text = ''.join(formatblock(b, width) for b in blocks)
+    if keep is None:
+        return text
+    else:
+        return text, pruned
+
+def filtersections(blocks, section):
+    """Select parsed blocks under the specified section"""
     parents = []
-    if section:
+    if True:
         sections = getsections(blocks)
         blocks = []
         i = 0
@@ -714,14 +727,7 @@ def format(text, width=80, indent=0, kee
                                '.'.join(path + [realline[0]]).replace('"', ''))
                 del blocks[s[0]:real]
 
-    if style == 'html':
-        text = formathtml(blocks)
-    else:
-        text = ''.join(formatblock(b, width) for b in blocks)
-    if keep is None:
-        return text
-    else:
-        return text, pruned
+    return blocks
 
 def getsections(blocks):
     '''return a list of (section name, nesting level, blocks) tuples'''


More information about the Mercurial-devel mailing list