diff extensions/kwarchive.py @ 321:103df4b7a0c8

A new more flexible syntax for custom keyword expansion: with Python template format syntax now
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 20 Feb 2019 23:09:10 +0100
parents e9ded6c1db41
children 950a9bd55d7e
line wrap: on
line diff
--- a/extensions/kwarchive.py	Wed Feb 20 22:48:46 2019 +0100
+++ b/extensions/kwarchive.py	Wed Feb 20 23:09:10 2019 +0100
@@ -59,14 +59,13 @@
     Revision =
     #
     # Additionally: `MyKeyword' is expanded with the contents of the
-    # pre-defined `JustDate'
+    # pre-defined `JustDate' (this is in Python's format syntax).
     #
-    MyKeyword = JustDate
+    MyKeyword = {JustDate}
     #
-    # `MyCustomSubstKeyword' is a substitution keyword. It's value is the
-    # plain string after "replace:: ".
+    # `MyCustomSubstKeyword' is a substitution keyword.
     #
-    MyCustomSubstKeyword = replace:: This is my replacement content
+    MyCustomSubstKeyword = This is my replacement content
 
 A non-existing ``.hgkwarchive`` file deactivates keyword expansion as does
 an empty ``[patterns]`` section.
@@ -506,7 +505,7 @@
         if filterdata is None:
             return data
         matcher, matcher_rcs, matcher_rst, manifest, \
-            keyword_whitelist, keyword_substitutions, keywords  = filterdata
+            keyword_substitutions, keywords  = filterdata
 
         if not matcher(rel_name_in_subrepo):
             #ui.write("NOT MATCHER for " + real_name + " (name: " + name + ") \n")
@@ -518,17 +517,28 @@
         # file specific keywords
         file_keywords = make_file_keywords(
             keywords, rel_name_in_subrepo, nodeid)
+        _predef_keywords = keywords.copy()
+        _predef_keywords.update(file_keywords)
         # This prevents unwanted keyword expansion here
         _MARKER_RCS = '$'
         _MARKER_RST = '|'
         for kw, value in itertools.chain(keyword_substitutions.items(), keywords.items(), file_keywords.items()):
             #
-            # an empty database means: all keywords allowed, no aliases,
-            # no substitutions
+            # Non-empty keyword_substitutions are an implicit whitelist and
+            # the value are Python format templates when expanding.
             #
-            if keyword_whitelist:
-                kwds = keyword_whitelist.get(kw, [])
+            if keyword_substitutions:
+                if kw in keyword_substitutions:
+                    kwds = [kw]
+                    if value:
+                        value = value.format(**_predef_keywords)
+                    else:
+                        value = _predef_keywords[kw]
+                else:
+                    # not whitelisted -> ignore
+                    kwds = []
             else:
+                # Empty keyword_substitutions mean: expand built-in keywords 
                 kwds = [kw]
             for kw in kwds:
                 if matcher_rcs(rel_name_in_subrepo):
@@ -621,25 +631,16 @@
                                       include=[], exclude=[])
 
         #
-        # This is the mapping from all the (white-listed) pre-defined
-        # keywords to the keywords to be expanded in files (which can be
-        # aliases. An empty right-side keyword maps the "alias" to itself.
-        #
-        keyword_whitelist = {}
-        #
-        # This are the substitution keywords:
-        # this keywords are substituted with just another plain text
-        # (no real templates!)
+        # This are the settings of the [keywords] section in .hgkwarchive.
+        # An empty section means: all default keywords are enabled.
+        # Otherwise only the given keywords are enabled with their expanded
+        # values on the right side. An empty right side means: use the default
+        # expansion.
         #
         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)
+            keyword_substitutions[alias] = value
 
         #
         # Get the manifest to be able to determine a file's NodeId
@@ -648,7 +649,7 @@
 
         return (matcher, matcher_rcs, matcher_rst,
                 manifest,
-                keyword_whitelist, keyword_substitutions, keywords,)
+                keyword_substitutions, keywords,)
 
 
 def _amend_filterdata_by_subrepos(filterdata_by_subrepos,