doc, minirst: support hg interpreted text role
authorMartin Geisler <mg@aragost.com>
Thu, 22 Apr 2010 10:04:53 +0200
changeset 10972 0a2c6948f5f4
parent 10971 cbe400a8e217
child 10973 49a07f441496
doc, minirst: support hg interpreted text role
doc/runrst
mercurial/minirst.py
tests/test-minirst.py
tests/test-minirst.py.out
--- a/doc/runrst	Thu Apr 22 09:57:04 2010 +0200
+++ b/doc/runrst	Thu Apr 22 10:04:53 2010 +0200
@@ -13,7 +13,16 @@
 """
 
 import sys
+from docutils.parsers.rst import roles
 from docutils.core import publish_cmdline
+from docutils import nodes, utils
+
+def role_hg(name, rawtext, text, lineno, inliner,
+            options={}, content=[]):
+    node = nodes.literal(rawtext, "hg " + utils.unescape(text, True))
+    return [node], []
+
+roles.register_local_role("hg", role_hg)
 
 if __name__ == "__main__":
     if len(sys.argv) < 2:
--- a/mercurial/minirst.py	Thu Apr 22 09:57:04 2010 +0200
+++ b/mercurial/minirst.py	Thu Apr 22 10:04:53 2010 +0200
@@ -252,6 +252,15 @@
     return blocks
 
 
+_hgrolere = re.compile(r':hg:`([^`]+)`')
+
+def hgrole(blocks):
+    for b in blocks:
+        if b['type'] == 'paragraph':
+            b['lines'] = [_hgrolere.sub(r'"hg \1"', l) for l in b['lines']]
+    return blocks
+
+
 def addmargins(blocks):
     """Adds empty blocks for vertical spacing.
 
@@ -333,6 +342,7 @@
     blocks = findliteralblocks(blocks)
     blocks, pruned = prunecontainers(blocks, keep or [])
     blocks = inlineliterals(blocks)
+    blocks = hgrole(blocks)
     blocks = splitparagraphs(blocks)
     blocks = updatefieldlists(blocks)
     blocks = findsections(blocks)
--- a/tests/test-minirst.py	Thu Apr 22 09:57:04 2010 +0200
+++ b/tests/test-minirst.py	Thu Apr 22 10:04:53 2010 +0200
@@ -178,3 +178,6 @@
 debugformat('containers (debug)', containers, 60, keep=['debug'])
 debugformat('containers (verbose debug)', containers, 60,
             keep=['verbose', 'debug'])
+
+roles = """Please see :hg:`add`."""
+debugformat('roles', roles, 60)
--- a/tests/test-minirst.py.out	Thu Apr 22 09:57:04 2010 +0200
+++ b/tests/test-minirst.py.out	Thu Apr 22 10:04:53 2010 +0200
@@ -298,3 +298,8 @@
 []
 ----------------------------------------------------------------------
 
+roles formatted to fit within 60 characters:
+----------------------------------------------------------------------
+Please see "hg add".
+----------------------------------------------------------------------
+