[PATCH 1 of 2 pager-dispatch-tweaks] help: move rst formatting of help documents into help.py
Augie Fackler
raf at durin42.com
Tue Feb 21 21:14:52 UTC 2017
# HG changeset patch
# User Augie Fackler <augie at google.com>
# Date 1487704651 18000
# Tue Feb 21 14:17:31 2017 -0500
# Node ID f489d1ec2070f25a09f571177b97d8284b3b1932
# Parent 59e69ed81776f0186cb6e6c69f8d7659afab26b7
help: move rst formatting of help documents into help.py
This slight refactor will help me improve the 'unknown command'
experience in our new glorious pager future.
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -39,7 +39,6 @@ from . import (
hg,
lock as lockmod,
merge as mergemod,
- minirst,
obsolete,
patch,
phases,
@@ -2721,10 +2720,6 @@ def help_(ui, name=None, **opts):
Returns 0 if successful.
"""
- textwidth = ui.configint('ui', 'textwidth', 78)
- termwidth = ui.termwidth() - 2
- if textwidth <= 0 or termwidth < textwidth:
- textwidth = termwidth
keep = opts.get('system') or []
if len(keep) == 0:
@@ -2740,37 +2735,7 @@ def help_(ui, name=None, **opts):
if ui.verbose:
keep.append('verbose')
- 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 help.subtopics:
- subtopic = remaining
- else:
- section = remaining
-
- text = help.help_(ui, name, subtopic=subtopic, **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)
+ formatted = help.formattedhelp(ui, name, keep=keep, **opts)
ui.pager('help')
ui.write(formatted)
diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -605,3 +605,47 @@ def help_(ui, name, unknowncmd=False, fu
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
More information about the Mercurial-devel
mailing list