UNPKG

3.21 kBJavaScriptView Raw
1var debug = require('debug')
2var lg = debug('vash:main');
3
4var Lexer = require('./lib/lexer');
5var Parser = require('./lib/parser');
6var codegen = require('./lib/codegen');
7var runtime = require('./runtime');
8var helperbatch = require('./lib/helperbatch');
9var copyrtl = require('./lib/util/copyrtl');
10
11// Attach all runtime exports to enable backwards compatible behavior,
12// like `vash.install` to still be accessible in a full build.
13require('./lib/helpers');
14copyrtl(exports, runtime);
15
16exports.config = {
17
18 // Parser Options
19 favorText: false,
20 // TODO: are these even needed with proper codegen?
21 saveAT: false,
22 saveTextTag: false,
23
24 // Compiler Options
25 useWith: false,
26 htmlEscape: true,
27 helpersName: 'html',
28 modelName: 'model',
29 debug: true,
30 source: null,
31 simple: false,
32
33 // Runtime options
34 asHelper: false,
35 args: null // Internal, for compiled helpers
36}
37
38exports.version = require('./package.json').version;
39
40exports.compileStream = function() {
41 // This could eventually handle waiting until a `null`
42 // is pushed into the lexer, etc.
43 throw new Error('NotImplemented');
44}
45
46exports.compile = function(markup, options) {
47
48 if(markup === '' || typeof markup !== 'string') {
49 throw new Error('Empty or non-string cannot be compiled');
50 }
51
52 var opts = copyrtl({}, exports.config, options || {});
53
54 var l = new Lexer();
55
56 l.write(markup);
57 var tokens = l.read();
58
59 var p = new Parser(opts);
60 p.write(tokens);
61 var more = true;
62 while(more !== null) more = p.read();
63
64 p.checkStack();
65
66 // Stash the original input (new lines normalized by the lexer).
67 opts.source = l.originalInput;
68
69 p.lg(p.dumpAST());
70
71 var compiled = codegen(p.stack[0], opts);
72 lg(compiled);
73 var tpl = runtime.link(compiled, opts);
74
75 return tpl;
76}
77
78///////////////////////////////////////////////////////////////////////////
79// VASH.COMPILEHELPER
80//
81// Allow multiple helpers to be compiled as templates, for helpers that
82// do a lot of markup output.
83//
84// Takes a template such as:
85//
86// vash.helpers.p = function(text){
87// <p>@text</p>
88// }
89//
90// And compiles it. The template is then added to `vash.helpers`.
91//
92// Returns the compiled templates as named properties of an object.
93//
94// This is string manipulation at its... something. It grabs the arguments
95// and function name using a regex, not actual parsing. Definitely error-
96// prone, but good enough. This is meant to facilitate helpers with complex
97// markup, but if something more advanced needs to happen, a plain helper
98// can be defined and markup added using the manual Buffer API.
99exports['compileHelper'] = helperbatch.bind(null, 'helper', exports.compile);
100
101///////////////////////////////////////////////////////////////////////////
102// VASH.COMPILEBATCH
103//
104// Allow multiple templates to be contained within the same string.
105// Templates are separated via a sourceURL-esque string:
106//
107// //@batch = tplname/or/path
108//
109// The separator is forgiving in terms of whitespace:
110//
111// // @ batch=tplname/or/path
112//
113// Is just as valid.
114//
115// Returns the compiled templates as named properties of an object.
116exports['compileBatch'] = exports['batch'] = helperbatch.bind(null, 'batch', exports.compile);
\No newline at end of file