hgkw/keyword.py
branchkwmap-templates
changeset 136 5e9d6c57d679
parent 135 a4c748fc7e00
child 137 bfea48fbcb88
equal deleted inserted replaced
135:a4c748fc7e00 136:5e9d6c57d679
   103     provides keyword expansion function.
   103     provides keyword expansion function.
   104     '''
   104     '''
   105     def __init__(self, ui, repo):
   105     def __init__(self, ui, repo):
   106         self.ui = ui
   106         self.ui = ui
   107         self.repo = repo
   107         self.repo = repo
   108         self.templates = dict(ui.configitems('keywordmaps')) or deftemplates
   108         templates = dict(ui.configitems('keywordmaps'))
       
   109         if templates:
       
   110             # parse templates here for less overhead in kwsub matchfunc
       
   111             for k in templates.keys():
       
   112                 templates[k] = templater.parsestring(templates[k],
       
   113                         quoted=False)
       
   114         self.templates = templates or deftemplates
   109         self.re_kw = re.compile(r'\$(%s)[^$]*?\$' %
   115         self.re_kw = re.compile(r'\$(%s)[^$]*?\$' %
   110                 '|'.join(re.escape(k) for k in self.templates.keys()))
   116                 '|'.join(re.escape(k) for k in self.templates.keys()))
   111         templater.common_filters['utcdate'] = utcdate
   117         templater.common_filters['utcdate'] = utcdate
   112         try:
   118         try:
   113             self.t = cmdutil.changeset_templater(ui, repo,
   119             self.t = cmdutil.changeset_templater(ui, repo,
   118                     False, None, '', False)
   124                     False, None, '', False)
   119 
   125 
   120     def kwsub(self, mobj, path, node):
   126     def kwsub(self, mobj, path, node):
   121         '''Substitutes keyword using corresponding template.'''
   127         '''Substitutes keyword using corresponding template.'''
   122         kw = mobj.group(1)
   128         kw = mobj.group(1)
   123         template = templater.parsestring(self.templates[kw], quoted=False)
   129         self.t.use_template(self.templates[kw])
   124         self.t.use_template(template)
       
   125         self.ui.pushbuffer()
   130         self.ui.pushbuffer()
   126         self.t.show(changenode=node, root=self.repo.root, file=path)
   131         self.t.show(changenode=node, root=self.repo.root, file=path)
   127         keywordsub = templater.firstline(self.ui.popbuffer())
   132         keywordsub = templater.firstline(self.ui.popbuffer())
   128         return '$%s: %s $' % (kw, keywordsub)
   133         return '$%s: %s $' % (kw, keywordsub)
   129 
   134