diff extensions/kwarchive.py @ 133:8d16138e00af

Implemented substitution type keyword expansion
author Franz Glasner <hg@dom66.de>
date Sat, 18 Aug 2018 23:48:57 +0200
parents 91077014c7b7
children aa553e987939
line wrap: on
line diff
--- a/extensions/kwarchive.py	Sat Aug 18 22:48:50 2018 +0200
+++ b/extensions/kwarchive.py	Sat Aug 18 23:48:57 2018 +0200
@@ -56,6 +56,11 @@
     # pre-defined `JustDate'
     #
     MyKeyword = JustDate
+    #
+    # `MyCustomSubstKeyword' is a substitution keyword. It's value is the
+    # plain string after "replace:: ".
+    #
+    MyCustomSubstKeyword = replace:: This is my replacement content
 
 A non-existing ``.hgkwarchive`` file deactivates keyword expansion as does
 an empty ``[patterns]`` section.
@@ -411,8 +416,20 @@
         # aliases. An empty right-side keyword maps the "alias" to itself.
         #
         keyword_whitelist = {}
-        for alias, keyword in cfg.items("keywords"):
-            keyword_whitelist.setdefault(keyword or alias, []).append(alias)
+        #
+        # This are the substitution keywords:
+        # this keywords are substituted with just another plain text
+        # (no real templates!)
+        #
+        keyword_substitutions = {}
+
+        for alias, value in cfg.items("keywords"):
+            if value.startswith("replace:: "):
+                # allow is to be replaced: put it into the global white list
+                keyword_whitelist[alias] = [alias]
+                keyword_substitutions[alias] = value[len("replace:: "):]
+            else:
+                keyword_whitelist.setdefault(value or alias, []).append(alias)
 
         #
         # Get the manifest to be able to determine a file's NodeId
@@ -436,8 +453,11 @@
             # This prevents unwanted keyword expansion here
             _MARKER_RCS = '$'
             _MARKER_RST = '|'
-            for kw, value in itertools.chain(keywords.items(), file_keywords.items()):
-                # an empty database means: all keywords allowed, no aliases
+            for kw, value in itertools.chain(keywords.items(), file_keywords.items(), keyword_substitutions.items()):
+                #
+                # an empty database means: all keywords allowed, no aliases,
+                # no substitutions
+                #
                 if keyword_whitelist:
                     kwds = keyword_whitelist.get(kw, [])
                 else: