From b9ee3412828f321fd81d99e7dc8d9f81b360e100 Mon Sep 17 00:00:00 2001 From: Alan Gutierrez Date: Tue, 25 Jun 2013 11:02:55 -0400 Subject: [PATCH] Rename `$` to `json`. File Raw Min Zip Min/Zip index.js 30301/29.59 9884/09.65 7580/07.40 3821/03.73 Closes #109. Closes #107. --- README.md | 61 +++++++++++++++++++++++++++++ diary.md | 12 ++++++ index.js | 28 +++++++------ json/file.js | 15 ++++--- t/context/fixtures/resolver.stencil | 2 +- t/context/resolver.t.js | 2 +- t/directives/proof.js | 5 +-- t/tags/proof.js | 5 +-- 8 files changed, 103 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 0342df6..284e0d2 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,67 @@ serialize to older HTML flavors for older browsers. On the browser, when we generate Stencil XML, we simply import it into the existing DOM using `Document.adoptNode`. +### The Context Object + +From within your templates you can reference the context object itself either by +referencing `this` or the special variable dollar sign `$` with is an alias for +for `this`. + +```xml + + +
+ +
+
+
+
+ + +``` + +You can also use the dollar sign `$` alias, if you find that more aesthetically +pleasing. + +```xml + + +
+ +
+
+
+
+ + +``` + +But, again, why? Only because I'm doing this stupid thing with prototypes that +I'm going to try to document right here... + +### The `stencil` Variable + +The `stencil` variable is a special object added to the context by the Stencil +that has properties of the current template. It is not visible in the ordinary +scope of template, (why? why not? I mean really, why was this important? now you +have to add a paragraph explaining special properties? Are you just being a +bonehead? Didn't we talk about that? No really, I can't wait to see this...) + +You can distinguish the context variables set by the Stencil engine from the +context variables set by your application using `hasOwnProperty`. I'm not sure +if this is useful, but it's there. + +(Oh, you're a real prize. Can we please [kill this +poodle](http://www.lileks.com/bleats/archive/03/0103/010301.html#010303)? You +know how else you could distinguish between a property set by the Stencil engine +and a property provided by your application? `key == "stencil"`. Yes. You are a +bonehead. TODO: Don't be such a bonehead. Then you don't have to document this. +You might not be able to accept this right now, but there are people in this +world how are smart enough to figure that out for themselves, I know, hard to +accept that world won't embrace the genius of your using object prototypes to do +something, that you know what you're doing, but there are object prototypes +involved and that's really special, so, I can't even, I can't.) + ## Tag Libraries Using the same language as used in templates, Stencil supports the creation of diff --git a/diary.md b/diary.md index a9a5012..053fedc 100644 --- a/diary.md +++ b/diary.md @@ -55,6 +55,18 @@ uses JSON resources, it loads JavaScript modules, it loads XML, but it does not load JSON resources. That should be set in the context by the application or the application framework, then pushed through Stencil to the scaffolds that use it. +**Umdate**: Um, but, how are you going to market Stencil? As a command line +application or as a the cure for a noder's Rails envy? Oh, hai, yes, you just +bolt on the only way your program will ever work if you need it, but it wasn't +necessary because in theory, Stencil could be used in burgeoning command line +generated HTML5 field of applications. + +Have you stopped to think about how the one thing everyone has to swallow is +that stencil generates **hideous** HTML? Stencil is a DOM templating language, +not a string templating language, they'll hear you say, and then your going to +waggle your ***hideous*** HTML in their face by spewing it all over their +console? People are not going to like you as a person. + ## Module Loading I'm not loving having all that cladding wrapped around a Stencil scaffold, when diff --git a/index.js b/index.js index a5f69dc..de0974d 100644 --- a/index.js +++ b/index.js @@ -32,11 +32,14 @@ } if (!json) { - json = function (url, callback) { - get(url, 'responseText', function (error, body) { - if (error) callback(error); - else callback(null, JSON.parse(body)); - }); + json = function (url) { + return function (callback) { + url = this.stencil.absolutize(this.stencil.url + '/..', url); + get(url, 'responseText', function (error, body) { + if (error) callback(error); + else callback(null, JSON.parse(body)); + }); + } } } @@ -460,13 +463,7 @@ function rewrite (parent, frames, page, template, includes, named, directives, path, context, generating, callback) { var okay = validator(callback), prefix = '$'; - context = extend(Object.create({ $: function (url) { - return function (callback) { - json(absolutize(template.url + '/..', url), function (error, result) { - callback(error, result); - }); - } - }}), context); + context = extend(Object.create(template.context), context); if (frames[0].attributes) { frames[0].attributes.forEach(function (attributes) { context[prefix + 'attributes'] = attributes; @@ -641,7 +638,12 @@ // Create our template. templates[url] = { - url: url, page: page, directives: directives, tags: tags + url: url, page: page, directives: directives, tags: tags, + context: { + stencil: { + url: url, absolutize: absolutize, normalize: normalize, json: json + } + } }; // Send the template to our caller. diff --git a/json/file.js b/json/file.js index 9286edd..65670da 100644 --- a/json/file.js +++ b/json/file.js @@ -1,10 +1,13 @@ var fs = require("fs"), path = require("path"); -exports.create = function create (base) { - return function json (file, callback) { - try { - callback(null, JSON.parse(fs.readFileSync(path.join(base, file), "utf8"))); - } catch (error) { - callback(error); +exports.create = function (base) { + return function (file) { + return function (callback) { + try { + file = this.stencil.absolutize(this.stencil.url + '/..', file); + callback(null, JSON.parse(fs.readFileSync(path.join(base, file), "utf8"))); + } catch (error) { + callback(error); + } } } } diff --git a/t/context/fixtures/resolver.stencil b/t/context/fixtures/resolver.stencil index 9fa515b..c20c83a 100644 --- a/t/context/fixtures/resolver.stencil +++ b/t/context/fixtures/resolver.stencil @@ -1,7 +1,7 @@ diff --git a/t/context/resolver.t.js b/t/context/resolver.t.js index 93af0a3..cd238ae 100755 --- a/t/context/resolver.t.js +++ b/t/context/resolver.t.js @@ -1,7 +1,7 @@ #!/usr/bin/env node require('./proof')(2, function (step, context, ok, compare, fixture) { - var fs = require('fs'); + var fs = require('fs'), json = require('../../json/file').create(__dirname); step(function (stencil, resolver) { context.generate('fixtures/resolver.stencil', {}, step()); diff --git a/t/directives/proof.js b/t/directives/proof.js index 125538f..95ca667 100644 --- a/t/directives/proof.js +++ b/t/directives/proof.js @@ -1,10 +1,9 @@ var fs = require('fs'), path = require('path'); module.exports = require('proof')(function () { var javascript = require('../../javascript/common').create(__dirname), - xml = require('../../xml/file').create(__dirname), - json = require('../../json/file').create(__dirname); + xml = require('../../xml/file').create(__dirname); var context = - { context: require('../..').create(javascript, xml, json) + { context: require('../..').create(javascript, xml) , compare: require('../compare') , fixture: function (file, callback) { fs.readFile(path.resolve(__dirname, file), 'utf8', callback) } }; diff --git a/t/tags/proof.js b/t/tags/proof.js index 125538f..95ca667 100644 --- a/t/tags/proof.js +++ b/t/tags/proof.js @@ -1,10 +1,9 @@ var fs = require('fs'), path = require('path'); module.exports = require('proof')(function () { var javascript = require('../../javascript/common').create(__dirname), - xml = require('../../xml/file').create(__dirname), - json = require('../../json/file').create(__dirname); + xml = require('../../xml/file').create(__dirname); var context = - { context: require('../..').create(javascript, xml, json) + { context: require('../..').create(javascript, xml) , compare: require('../compare') , fixture: function (file, callback) { fs.readFile(path.resolve(__dirname, file), 'utf8', callback) } };