hgkw/keyword.py
branchkwmap-templates
changeset 134 f869c65156f7
parent 133 cb60196a500b
child 135 a4c748fc7e00
--- a/hgkw/keyword.py	Fri Feb 09 13:47:11 2007 +0100
+++ b/hgkw/keyword.py	Fri Feb 09 14:43:01 2007 +0100
@@ -105,18 +105,32 @@
         templater.common_filters['utcdate'] = utcdate
         self.t = cmdutil.changeset_templater(ui, repo, False, '', False)
 
-    def expand(self, mobj, path, node):
-        '''Expands keyword using corresponding template.'''
+    def kwsub(self, mobj, path, node):
+        '''Substitutes keyword using corresponding template.'''
         kw = mobj.group(1)
         template = templater.parsestring(self.templates[kw], quoted=False)
         self.t.use_template(template)
         self.ui.pushbuffer()
         self.t.show(changenode=node, root=self.repo.root, file=path)
-        kwsub = templater.firstline(self.ui.popbuffer())
-        return '$%s: %s $' % (kw, kwsub)
+        keywordsub = templater.firstline(self.ui.popbuffer())
+        return '$%s: %s $' % (kw, keywordsub)
+
+    def expand(self, path, node, data):
+        '''Returns data with expanded keywords.'''
+        if util.binary(data):
+            return data
+        return self.re_kw.sub(lambda m: self.kwsub(m, path, node), data)
+
+    def expandn(self, path, node, data):
+        '''Returns data with expanded keywords and number of expansions.'''
+        if util.binary(data):
+            return data, None
+        return self.re_kw.subn(lambda m: self.kwsub(m, path, node), data)
 
     def shrink(self, text):
         '''Returns text with all keyword substitutions removed.'''
+        if util.binary(text):
+            return text
         return self.re_kw.sub(r'$\1$', text)
 
 
@@ -167,14 +181,13 @@
             overwrite = []
             for f in candidates:
                 data = self.wfile(f).read()
-                if not util.binary(data):
-                    data, kwct = kwt.re_kw.subn(lambda m:
-                            kwt.expand(m, f, node), data)
-                    if kwct:
-                        ui.debug(_('overwriting %s expanding keywords\n' % f))
-                        self.wfile(f, 'w').write(data)
-                        overwrite.append(f)
-            self.dirstate.update(overwrite, 'n')
+                data, kwct = kwt.expandn(f, node, data)
+                if kwct:
+                    ui.debug(_('overwriting %s expanding keywords\n' % f))
+                    self.wfile(f, 'w').write(data)
+                    overwrite.append(f)
+            if overwrite:
+                self.dirstate.update(overwrite, 'n')
             return node
 
     class kwfilelog(filelog.filelog):
@@ -193,30 +206,25 @@
             else:
                 self.kwt = None
 
-        def iskwcandidate(self, data):
-            '''Decides whether to act on keywords.'''
-            return self.kwt is not None and not util.binary(data)
-
         def read(self, node):
             '''Substitutes keywords when reading filelog.'''
             data = super(kwfilelog, self).read(node)
-            if self.iskwcandidate(data):
+            if self.kwt:
                 c = context.filectx(self._repo, self._path,
                                     fileid=node, filelog=self)
-                return self.kwt.re_kw.sub(lambda m:
-                        self.kwt.expand(m, self._path, c.node()), data)
+                data = self.kwt.expand(self._path, c.node(), data)
             return data
 
         def add(self, text, meta, tr, link, p1=None, p2=None):
             '''Removes keyword substitutions when adding to filelog.'''
-            if self.iskwcandidate(text):
+            if self.kwt:
                 text = self.kwt.shrink(text)
             return super(kwfilelog, self).add(text,
                             meta, tr, link, p1=p1, p2=p2)
 
         def cmp(self, node, text):
             '''Removes keyword substitutions for comparison.'''
-            if self.iskwcandidate(text):
+            if self.kwt:
                 text = self.kwt.shrink(text)
             return super(kwfilelog, self).cmp(node, text)