[extensions-web] js: Use flat storage for the mustache templates



commit 2a8be8727d51e3caac873521a924b97803ee0fad
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Sep 25 23:30:55 2012 -0300

    js: Use flat storage for the mustache templates
    
    This reduces the amount of effort that clients have to do by a tiny
    bit.

 sweettooth/static/js/templates.js                 |   22 ++----------------
 sweettooth/static/js/templates/build_templates.py |   21 ++++++++++-------
 sweettooth/static/js/templates/templatedata.js    |   24 ++++++++------------
 3 files changed, 25 insertions(+), 42 deletions(-)
---
diff --git a/sweettooth/static/js/templates.js b/sweettooth/static/js/templates.js
index d31284e..126e69a 100644
--- a/sweettooth/static/js/templates.js
+++ b/sweettooth/static/js/templates.js
@@ -6,26 +6,10 @@ define(['templates/templatedata', 'mustache'], function(templatedata, mustache)
     var exports = {};
     var cache = {};
 
-    function _processPartials(prefix, data) {
-        for (var prop in data) {
-            var value = data[prop];
-            var name;
-
-            if (prefix)
-                name = prefix + "/" + prop;
-            else
-                name = prop;
-
-            if (typeof(value) === typeof({})) {
-                // Subdirectory. Recurse.
-                _processPartials(name, value);
-            } else {
-                // Template. Mustache will cache all partials for us.
-                cache[name] = mustache.compilePartial(name, value);
-            }
-        }
+    for (var prop in templatedata) {
+        var value = templatedata[prop];
+        cache[prop] = mustache.compilePartial(prop, value);
     }
-    _processPartials("", templatedata);
 
     exports.get = function get(name) {
         return cache[name];
diff --git a/sweettooth/static/js/templates/build_templates.py b/sweettooth/static/js/templates/build_templates.py
index b02b4a8..94165e2 100644
--- a/sweettooth/static/js/templates/build_templates.py
+++ b/sweettooth/static/js/templates/build_templates.py
@@ -6,15 +6,18 @@ import os.path
 
 def _build_templates(directory):
     templates = {}
-    for filename in os.listdir(directory):
-        joined = os.path.join(directory, filename)
-        name, ext = os.path.splitext(filename)
-        if os.path.isdir(joined):
-            templates[name] = _build_templates(joined)
-        elif ext == ".mustache":
-            f = open(joined, 'r')
-            templates[name] = f.read().strip()
-            f.close()
+    for path, dirs, files in os.walk(directory):
+        relpath = os.path.relpath(path, directory)
+        path_parts = relpath.split(os.path.sep)
+        for filename in files:
+            name, ext = os.path.splitext(filename)
+            if ext != ".mustache":
+                continue
+
+            with open(os.path.join(path, filename)) as f:
+                template = f.read().strip()
+            full_name = '/'.join(path_parts + [name])
+            templates[full_name] = template
     return templates
 
 def build_templates(directory):
diff --git a/sweettooth/static/js/templates/templatedata.js b/sweettooth/static/js/templates/templatedata.js
index ea08ac3..a668fb8 100644
--- a/sweettooth/static/js/templates/templatedata.js
+++ b/sweettooth/static/js/templates/templatedata.js
@@ -2,18 +2,14 @@
 "use strict";
 
 define({
-  "extensions": {
-    "comment": "<div class=\"comment\">\n  {{#is_extension_creator}}\n  <div class=\"extension-creator-badge\">Author</div>\n  {{/is_extension_creator}}\n  <img src=\"{{gravatar}}\" class=\"gravatar\">\n  <div class=\"rating-author\">\n    {{#has_rating}}\n      <div class=\"rating\" data-rating-value=\"{{rating}}\"></div> by\n    {{/has_rating}}\n    <a class=\"comment-author\" href=\"{{author.url}}\">{{author.username}}</a>\n    <p>{{comment}}</p>\n    <time datetime=\"{{date.timestamp}}Z\">{{date.standard}}</time>\n  </div>\n</div>", 
-    "comments_list": "{{#.}}\n  {{>extensions/comment}}\n  <hr>\n{{/.}}\n{{^.}}\n  <p>There are no comments. Be the first!</p>\n{{/.}}", 
-    "error_report_template": "What's wrong?\n\n\n\nWhat have you tried?\n\n\n\nAutomatically detected errors:\n\n{{#errors}}\n  {{.}}\n\n================\n{{/errors}}\n{{^errors}}\nGNOME Shell Extensions did not detect any errors with this extension.\n{{/errors}}\n\nVersion information:\n\n    Shell version: {{sv}}\n    Extension version: {{#ev}}{{ev}}{{/ev}}{{^ev}}Unknown{{/ev}}", 
-    "info": "<div class=\"extension\" data-uuid=\"{{uuid}}\">\n  {{>extensions/info_contents}}\n</div>", 
-    "info_contents": "<div class=\"switch\"></div>\n<div class=\"extra-buttons\">\n  <div class=\"upgrade-button\" title=\"Upgrade this extension\"></div>\n  <div class=\"configure-button\" title=\"Configure this extension\"></div>\n</div>\n<h3 class=\"extension-name\">\n  {{#link}}\n    <a href=\"{{link}}\" class=\"title-link\"> <img src=\"{{icon}}\" class=\"icon\"> {{name}} </a>\n  {{/link}}\n  {{^link}}\n  {{name}}\n  {{/link}}\n</h3>\n{{#creator}}\n  <span class=\"author\">by <a href=\"{{creator_url}}\"> {{creator}} </a></span>\n{{/creator}}\n<p class=\"description\">{{first_line_of_description}}</p>\n{{#want_uninstall}}\n  <button class=\"uninstall\" title=\"Uninstall\">Uninstall</button>\n{{/want_uninstall}}", 
-    "info_list": "<ul class=\"extensions\">\n{{#extensions}}\n  <li class=\"extension\" data-svm=\"{{shell_version_map}}\">\n    {{>extensions/info_contents}}\n  </li>\n{{/extensions}}\n</ul>", 
-    "uninstall": "You uninstalled <b>{{name}}</b>."
-  }, 
-  "messages": {
-    "cannot_list_errors": "GNOME Shell Extensions cannot automatically detect any errors.", 
-    "cannot_list_local": "GNOME Shell Extensions cannot list your installed extensions.", 
-    "dummy_proxy": "You do not appear to have an up to date version of GNOME3. You won't be able to install extensions from here. See the <a href=\"about/#old-version\">about page</a> for more information."
-  }
+  "extensions/comment": "<div class=\"comment\">\n  {{#is_extension_creator}}\n  <div class=\"extension-creator-badge\">Author</div>\n  {{/is_extension_creator}}\n  <img src=\"{{gravatar}}\" class=\"gravatar\">\n  <div class=\"rating-author\">\n    {{#has_rating}}\n      <div class=\"rating\" data-rating-value=\"{{rating}}\"></div> by\n    {{/has_rating}}\n    <a class=\"comment-author\" href=\"{{author.url}}\">{{author.username}}</a>\n    <p>{{comment}}</p>\n    <time datetime=\"{{date.timestamp}}Z\">{{date.standard}}</time>\n  </div>\n</div>", 
+  "extensions/comments_list": "{{#.}}\n  {{>extensions/comment}}\n  <hr>\n{{/.}}\n{{^.}}\n  <p>There are no comments. Be the first!</p>\n{{/.}}", 
+  "extensions/error_report_template": "What's wrong?\n\n\n\nWhat have you tried?\n\n\n\nAutomatically detected errors:\n\n{{#errors}}\n  {{.}}\n\n================\n{{/errors}}\n{{^errors}}\nGNOME Shell Extensions did not detect any errors with this extension.\n{{/errors}}\n\nVersion information:\n\n    Shell version: {{sv}}\n    Extension version: {{#ev}}{{ev}}{{/ev}}{{^ev}}Unknown{{/ev}}", 
+  "extensions/info": "<div class=\"extension\" data-uuid=\"{{uuid}}\">\n  {{>extensions/info_contents}}\n</div>", 
+  "extensions/info_contents": "<div class=\"switch\"></div>\n<div class=\"extra-buttons\">\n  <div class=\"upgrade-button\" title=\"Upgrade this extension\"></div>\n  <div class=\"configure-button\" title=\"Configure this extension\"></div>\n</div>\n<h3 class=\"extension-name\">\n  {{#link}}\n    <a href=\"{{link}}\" class=\"title-link\"> <img src=\"{{icon}}\" class=\"icon\"> {{name}} </a>\n  {{/link}}\n  {{^link}}\n  {{name}}\n  {{/link}}\n</h3>\n{{#creator}}\n  <span class=\"author\">by <a href=\"{{creator_url}}\"> {{creator}} </a></span>\n{{/creator}}\n<p class=\"description\">{{first_line_of_description}}</p>\n{{#want_uninstall}}\n  <button class=\"uninstall\" title=\"Uninstall\">Uninstall</button>\n{{/want_uninstall}}", 
+  "extensions/info_list": "<ul class=\"extensions\">\n{{#extensions}}\n  <li class=\"extension\" data-svm=\"{{shell_version_map}}\">\n    {{>extensions/info_contents}}\n  </li>\n{{/extensions}}\n</ul>", 
+  "extensions/uninstall": "You uninstalled <b>{{name}}</b>.", 
+  "messages/cannot_list_errors": "GNOME Shell Extensions cannot automatically detect any errors.", 
+  "messages/cannot_list_local": "GNOME Shell Extensions cannot list your installed extensions.", 
+  "messages/dummy_proxy": "You do not appear to have an up to date version of GNOME3. You won't be able to install extensions from here. See the <a href=\"about/#old-version\">about page</a> for more information."
 });



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]