1 | var debug = require('debug')
|
2 | var lg = debug('vash:main');
|
3 |
|
4 | var Lexer = require('./lib/lexer');
|
5 | var Parser = require('./lib/parser');
|
6 | var codegen = require('./lib/codegen');
|
7 | var runtime = require('./runtime');
|
8 | var helperbatch = require('./lib/helperbatch');
|
9 | var 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.
|
13 | require('./lib/helpers');
|
14 | copyrtl(exports, runtime);
|
15 |
|
16 | exports.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 |
|
38 | exports.version = require('./package.json').version;
|
39 |
|
40 | exports.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 |
|
46 | exports.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.
|
99 | exports['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.
|
116 | exports['compileBatch'] = exports['batch'] = helperbatch.bind(null, 'batch', exports.compile); |
\ | No newline at end of file |