Skip to content
Browse files

Expose compile in the API. Bump version to 0.1.4

  • Loading branch information...
1 parent f7b0de1 commit 7adf07038f0041e055076a102af6e56aab1c99d5 @creationix creationix committed
Showing with 28 additions and 18 deletions.
  1. +1 −0 .gitignore
  2. +5 −1 README.markdown
  3. +21 −16 kernel.js
  4. +1 −1 package.json
View
1 .gitignore
@@ -0,0 +1 @@
+node_modules
View
6 README.markdown
@@ -114,7 +114,7 @@ Usage in the browser is almost identical. Simply include the kernel.js script i
</script>
</body>
</html>
-```
+```
## Advanced Usage
@@ -126,6 +126,10 @@ This function is used by `Kernel()` to load a template. By default it's FS.read
If you want to load templates some other way, simply override this function.
+### Kernel.compile(source, filename)
+
+Pass in pre-loaded template source and a filename (used for debugging) and it will tokenize, parse, generate, and eval all in one step. Returns the compiled template function.
+
### Kernel.tokenizer(source)
The tokenizer takes in a string of kernel source and returns an array of tokens.
View
37 kernel.js
@@ -33,6 +33,7 @@ Kernel.cacheLifetime = 1000;
Kernel.tokenizer = tokenizer;
Kernel.parser = parser;
Kernel.generator = generator;
+Kernel.compile = compile;
Kernel.withit = withit;
var variable = /[_$a-z][_$a-z0-8]*(?:\.[_$a-z][_$a-z0-8]*)*(?!\w*:)/gi;
@@ -131,7 +132,7 @@ function Kernel(filename, callback) {
// Start a new batch, call the real function, and report.
var batch = templateBatch[filename] = [callback];
- compile(filename, function (err, template) {
+ compileFile(filename, function (err, template) {
// We don't want to cache in case of errors
if (!err && Kernel.cacheLifetime) {
@@ -154,25 +155,12 @@ function Kernel(filename, callback) {
}
// Load a file from disk and compile into executable template
-function compile(filename, callback) {
+function compileFile(filename, callback) {
Kernel.resourceLoader(filename, function (err, source) {
if (err) { callback(err); return; }
var template;
try {
- var blocks = [];
- generator(parser(tokenizer(source), source, filename), blocks);
- var code = blocks.map(function (block, i) {
- return "function block_" + i + "(callback) {\n" +
- block + "\n}\n";
- }).join("\n");
- if (blocks.needsLib) {
- code += "\n" + lib;
- }
- if (blocks.needsBlock) {
- code += "\n" + libBlock;
- }
- template = Function("locals", "callback", '"use strict"\n\n' +
- "block_0.call(locals, callback);\n\n" + code);
+ template = compile(source, filename);
} catch (err) {
callback(err); return;
}
@@ -180,6 +168,23 @@ function compile(filename, callback) {
});
}
+function compile(source, filename) {
+ var blocks = [];
+ generator(parser(tokenizer(source), source, filename), blocks);
+ var code = blocks.map(function (block, i) {
+ return "function block_" + i + "(callback) {\n" +
+ block + "\n}\n";
+ }).join("\n");
+ if (blocks.needsLib) {
+ code += "\n" + lib;
+ }
+ if (blocks.needsBlock) {
+ code += "\n" + libBlock;
+ }
+ return new Function("locals", "callback", '"use strict"\n\n' +
+ "block_0.call(locals, callback);\n\n" + code);
+}
+
function plain(token) {
if (typeof token === "string") { return JSON.stringify(token); }
return "(" + withit(token.name) + ")";
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Tim Caswell <tim@creationix.com>",
"name": "kernel",
"description": "A simple async template language similair to dustjs and mustache",
- "version": "0.1.3",
+ "version": "0.1.4",
"repository": {
"url": ""
},

0 comments on commit 7adf070

Please sign in to comment.
Something went wrong with that request. Please try again.