[PATCH 1 of 2] help: add topic rewriting hooks

Patrick Mezard pmezard at gmail.com
Fri Oct 22 17:08:59 CDT 2010


# HG changeset patch
# User Patrick Mezard <pmezard at gmail.com>
# Date 1287784814 -7200
# Branch stable
# Node ID 35659a6222674083ac87f6e9f6f1a912962601b9
# Parent  0e284735e65b15f4e01e5d8943de8addef9c2d04
help: add topic rewriting hooks

They are useful when updating help topics dynamically from extensions.

diff -r 0e284735e65b -r 35659a622267 mercurial/help.py
--- a/mercurial/help.py	Fri Oct 22 22:58:17 2010 +0200
+++ b/mercurial/help.py	Sat Oct 23 00:00:14 2010 +0200
@@ -79,7 +79,11 @@
                 break
 
         path = os.path.join(docdir, topic + ".txt")
-        return gettext(open(path).read())
+        doc = gettext(open(path).read())
+        for rewriter in helphooks.get(topic, []):
+            doc = rewriter(doc)
+        return doc
+
     return loader
 
 helptable = [
@@ -102,3 +106,8 @@
     (["hgweb"], _("Configuring hgweb"), loaddoc('hgweb')),
     (["glossary"], _("Glossary"), loaddoc('glossary')),
 ]
+
+# Map topics to lists of callable taking the current topic help and
+# returning the updated version
+helphooks = {
+}
diff -r 0e284735e65b -r 35659a622267 tests/test-help.t
--- a/tests/test-help.t	Fri Oct 22 22:58:17 2010 +0200
+++ b/tests/test-help.t	Sat Oct 23 00:00:14 2010 +0200
@@ -756,3 +756,30 @@
       The reserved name "." indicates the working directory parent. If no
       working directory is checked out, it is equivalent to null. If an
       uncommitted merge is in progress, "." is the revision of the first parent.
+
+Test help hooks
+
+  $ cat > helphook1.py <<EOF
+  > from mercurial import help
+  > 
+  > def rewrite(doc):
+  >     return doc + '\nhelphook1\n'
+  > 
+  > def extsetup(ui):
+  >     help.helphooks.setdefault('revsets', []).append(rewrite)
+  > EOF
+  $ cat > helphook2.py <<EOF
+  > from mercurial import help
+  > 
+  > def rewrite(doc):
+  >     return doc + '\nhelphook2\n'
+  > 
+  > def extsetup(ui):
+  >     help.helphooks.setdefault('revsets', []).append(rewrite)
+  > EOF
+  $ echo '[extensions]' >> $HGRCPATH
+  $ echo "helphook1 = `pwd`/helphook1.py" >> $HGRCPATH
+  $ echo "helphook2 = `pwd`/helphook2.py" >> $HGRCPATH
+  $ hg help revsets | grep helphook
+      helphook1
+      helphook2


More information about the Mercurial-devel mailing list