mercurial/help.py
changeset 31079 2a0c8e3636b0
parent 30812 98bfce9bd5e5
child 31081 900996da577a
--- a/mercurial/help.py	Tue Feb 21 11:06:02 2017 -0500
+++ b/mercurial/help.py	Tue Feb 21 14:17:31 2017 -0500
@@ -605,3 +605,47 @@
         rst.extend(helplist(None, **opts))
 
     return ''.join(rst)
+
+def formattedhelp(ui, name, keep=None, unknowncmd=False, full=True, **opts):
+    """get help for a given topic (as a dotted name) as rendered rst
+
+    Either returns the rendered help text or raises an exception.
+    """
+    if keep is None:
+        keep = []
+    fullname = name
+    section = None
+    subtopic = None
+    if name and '.' in name:
+        name, remaining = name.split('.', 1)
+        remaining = encoding.lower(remaining)
+        if '.' in remaining:
+            subtopic, section = remaining.split('.', 1)
+        else:
+            if name in subtopics:
+                subtopic = remaining
+            else:
+                section = remaining
+    textwidth = ui.configint('ui', 'textwidth', 78)
+    termwidth = ui.termwidth() - 2
+    if textwidth <= 0 or termwidth < textwidth:
+        textwidth = termwidth
+    text = help_(ui, name,
+                 subtopic=subtopic, unknowncmd=unknowncmd, full=full, **opts)
+
+    formatted, pruned = minirst.format(text, textwidth, keep=keep,
+                                       section=section)
+
+    # We could have been given a weird ".foo" section without a name
+    # to look for, or we could have simply failed to found "foo.bar"
+    # because bar isn't a section of foo
+    if section and not (formatted and name):
+        raise error.Abort(_("help section not found: %s") % fullname)
+
+    if 'verbose' in pruned:
+        keep.append('omitted')
+    else:
+        keep.append('notomitted')
+    formatted, pruned = minirst.format(text, textwidth, keep=keep,
+                                       section=section)
+    return formatted