Re: [PATCH 2 of 4] minirst: dynamically compile admonitions regexp

2017-03-02 Thread Yuya Nishihara
On Wed, 15 Feb 2017 17:58:59 -0800, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc 
> # Date 1487188034 28800
> #  Wed Feb 15 11:47:14 2017 -0800
> # Node ID 197ba3e5885366038d453b9b22fa2910a0792988
> # Parent  dd90d5f7dc1908d9b69e6a4b8165a73757d1c84b
> minirst: dynamically compile admonitions regexp

> +_admonitions = set([
> +'admonition',
> +'attention',
> +'caution',
> +'danger',
> +'error',
> +'hint',
> +'important',
> +'note',
> +'tip',
> +'warning',
> +])

Maybe this could be combined with _admonitiontitles.
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


[PATCH 2 of 4] minirst: dynamically compile admonitions regexp

2017-02-15 Thread Gregory Szorc
# HG changeset patch
# User Gregory Szorc 
# Date 1487188034 28800
#  Wed Feb 15 11:47:14 2017 -0800
# Node ID 197ba3e5885366038d453b9b22fa2910a0792988
# Parent  dd90d5f7dc1908d9b69e6a4b8165a73757d1c84b
minirst: dynamically compile admonitions regexp

Currently, parsing admonitions uses a static regular expression created
from a pre-defined list of admonitions. A future patch will introduce a
feature that needs to parse custom admonitions. Prepare for this by
compiling the admonitions regular expression during each function
invocation.

Strictly speaking, there is a slight performance loss here. But we only
run this code as part of displaying help text. I don't think the loss
will be noticeable and I don't think we care if it were.

diff --git a/mercurial/minirst.py b/mercurial/minirst.py
--- a/mercurial/minirst.py
+++ b/mercurial/minirst.py
@@ -411,18 +411,31 @@ def prunecomments(blocks):
 i += 1
 return blocks
 
-_admonitionre = re.compile(r"\.\. (admonition|attention|caution|danger|"
-   r"error|hint|important|note|tip|warning)::",
-   flags=re.IGNORECASE)
+
+_admonitions = set([
+'admonition',
+'attention',
+'caution',
+'danger',
+'error',
+'hint',
+'important',
+'note',
+'tip',
+'warning',
+])
 
 def findadmonitions(blocks):
 """
 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)),
+  flags=re.IGNORECASE)
+
 i = 0
 while i < len(blocks):
-m = _admonitionre.match(blocks[i]['lines'][0])
+m = admonitionre.match(blocks[i]['lines'][0])
 if m:
 blocks[i]['type'] = 'admonition'
 admonitiontitle = blocks[i]['lines'][0][3:m.end() - 2].lower()
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel