UNPKG

2.55 kBJavaScriptView Raw
1/**
2 * Module dependencies
3 */
4
5var JLexer = require('jade').Lexer;
6var inherits = require('util').inherits;
7
8module.exports = Lexer;
9
10function Lexer() {
11 JLexer.apply(this, arguments);
12}
13inherits(Lexer, JLexer);
14
15Lexer.prototype.conditional = function() {
16 var captures;
17 if (captures = /^(if|unless|else if|else)\b([^\n]*)/.exec(this.input)) {
18 this.consume(captures[0].length);
19 var type = captures[1]
20 var js = captures[2];
21 var isIf = false;
22 var isElse = false;
23
24 switch (type) {
25 case 'if':
26 js = 'IFF' + js;
27 isIf = true;
28 break;
29 case 'unless':
30 js = 'NIF' + js;
31 isIf = true;
32 break;
33 case 'else if':
34 js = 'ELF' + js;
35 isIf = true;
36 isElse = true;
37 break;
38 case 'else':
39 if (js && js.trim()) {
40 throw new Error('`else` cannot have a condition, perhaps you meant `else if`');
41 }
42 js = 'ELS';
43 isElse = true;
44 break;
45 }
46 var tok = this.tok('code', js);
47 tok.isElse = isElse;
48 tok.isIf = isIf;
49 tok.requiresBlock = true;
50 return tok;
51 }
52};
53
54Lexer.prototype.each = function() {
55 var captures;
56 if (captures = /^(?:- *)?(?:each|for|repeat) +([^\n]+)/.exec(this.input)) {
57 this.consume(captures[0].length);
58 var tok = this.tok('each', captures[1]);
59 tok.key = captures[2] || '$index';
60 tok.code = captures[3];
61 return tok;
62 }
63};
64
65Lexer.prototype.code = function() {
66 var captures;
67 if (captures = /^(!?=|-)[ \t]*([^\n]+)/.exec(this.input)) {
68 this.consume(captures[0].length);
69 var flags = captures[1];
70 captures[1] = captures[2];
71 var tok = this.tok('code', 'EXP' + captures[1]);
72 tok.escape = flags.charAt(0) === '=';
73 tok.buffer = flags.charAt(0) === '=' || flags.charAt(1) === '=';
74 return tok;
75 }
76};
77
78Lexer.prototype['yield'] = function() {
79 var captures = /^yield\b *([^\n]+)?/.exec(this.input);
80 if (captures) {
81 this.consume(captures[0].length);
82 var name = captures[1];
83
84 var parts = (name || '').split(/ *\( */);
85
86 var tok = this.tok('yield', parts[0]);
87
88 if (parts[1]) tok.args = '(' + parts[1];
89
90 return tok;
91 }
92};
93
94Lexer.prototype.block = function() {
95 var captures = /^block\b *(?:(prepend|append) +)?([^\n]+)/.exec(this.input);
96 if (captures) {
97 this.consume(captures[0].length);
98 var mode = captures[1] || 'replace';
99 var name = captures[2];
100
101 var parts = (name || '').split(/ *\( */);
102
103 var tok = this.tok('block', parts[0]);
104
105 if (parts[1]) tok.args = '(' + parts[1];
106
107 tok.mode = mode;
108 return tok;
109 }
110};