Patchwork [3,of,4] minirst: support passing admonitions into findadmonitions() and parse()

login
register
mail settings
Submitter Gregory Szorc
Date Feb. 16, 2017, 1:59 a.m.
Message ID <9ab3ad4934aedc649d28.1487210340@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/18532/
State Accepted
Headers show

Comments

Gregory Szorc - Feb. 16, 2017, 1:59 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1487188152 28800
#      Wed Feb 15 11:49:12 2017 -0800
# Node ID 9ab3ad4934aedc649d28f74dd70207c6c6e88596
# Parent  197ba3e5885366038d453b9b22fa2910a0792988
minirst: support passing admonitions into findadmonitions() and parse()

This will allow consumers to declare a custom list of admonitions
to parse. Without this patch, custom admonitions would get removed
when prunecomments() is run. We could add an argument controlling
whether prunecomments() is run. However, it is better to convert
the "paragraph" block to an "admonition" block so consumers don't
have to parse for custom admonitions.

Patch

diff --git a/mercurial/minirst.py b/mercurial/minirst.py
--- a/mercurial/minirst.py
+++ b/mercurial/minirst.py
@@ -425,12 +425,14 @@  def prunecomments(blocks):
     'warning',
 ])
 
-def findadmonitions(blocks):
+def findadmonitions(blocks, admonitions=None):
     """
     Makes the type of the block an admonition block if
     the first line is an admonition directive
     """
-    admonitionre = re.compile(r'\.\. (%s)::' % '|'.join(sorted(_admonitions)),
+    admonitions = admonitions or _admonitions
+
+    admonitionre = re.compile(r'\.\. (%s)::' % '|'.join(sorted(admonitions)),
                               flags=re.IGNORECASE)
 
     i = 0
@@ -642,7 +644,7 @@  def formathtml(blocks):
 
     return ''.join(out)
 
-def parse(text, indent=0, keep=None):
+def parse(text, indent=0, keep=None, admonitions=None):
     """Parse text into a list of blocks"""
     pruned = []
     blocks = findblocks(text)
@@ -657,7 +659,7 @@  def parse(text, indent=0, keep=None):
     blocks = splitparagraphs(blocks)
     blocks = updatefieldlists(blocks)
     blocks = updateoptionlists(blocks)
-    blocks = findadmonitions(blocks)
+    blocks = findadmonitions(blocks, admonitions=admonitions)
     blocks = addmargins(blocks)
     blocks = prunecomments(blocks)
     return blocks, pruned