[PATCH 4 of 6 json-style] templater: introduce jsonengine

Gregory Szorc gregory.szorc at gmail.com
Wed Dec 31 16:45:34 CST 2014


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1420056018 28800
#      Wed Dec 31 12:00:18 2014 -0800
# Node ID 850bab749730a01b318ff100858c06fa87c8d78e
# Parent  055802e347f62d47560201096e6f280601b2f60d
templater: introduce jsonengine

This patch introduces a template engine that specializes in rendering
JSON.

If a named template doesn't exist, it defaults to rendering a JSON
object for the passed mapping.

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -625,9 +625,23 @@ class engine(object):
         mapping contains added elements for use during expansion. Is a
         generator.'''
         return _flatten(runtemplate(self, mapping, self._load(t)))
 
-engines = {'default': engine}
+class jsonengine(engine):
+    '''Template engine used for rendering JSON.
+
+    When this engine is asked to process a template, it defaults to converting
+    all passed mappings into an object and returning the JSON representation of
+    that object. However, if a named entry for a template exists, it overrides
+    this default behavior.
+    '''
+    def process(self, t, mapping):
+        try:
+            return super(jsonengine, self).process(t, mapping)
+        except TemplateNotFound:
+            return _flatten(templatefilters.json(mapping))
+
+engines = {'default': engine, 'json': jsonengine}
 
 def stylelist():
     paths = templatepaths()
     if not paths:


More information about the Mercurial-devel mailing list