818
Du
dustymarkdown / files
dustymarkdown / node_modules / dust / lib / dust.js
10.5 KB
1var dust = {}; 2 3(function(dust) { 4 5dust.cache = {}; 6 7dust.register = function(name, tmpl) { 8 if (!name) return; 9 dust.cache[name] = tmpl; 10}; 11 12dust.render = function(name, context, callback) { 13 var chunk = new Stub(callback).head; 14 dust.load(name, chunk, Context.wrap(context)).end(); 15}; 16 17dust.stream = function(name, context) { 18 var stream = new Stream(); 19 dust.nextTick(function() { 20 dust.load(name, stream.head, Context.wrap(context)).end(); 21 }); 22 return stream; 23}; 24 25dust.renderSource = function(source, context, callback) { 26 return dust.compileFn(source)(context, callback); 27}; 28 29dust.compileFn = function(source, name) { 30 var tmpl = dust.loadSource(dust.compile(source, name)); 31 return function(context, callback) { 32 var master = callback ? new Stub(callback) : new Stream(); 33 dust.nextTick(function() { 34 tmpl(master.head, Context.wrap(context)).end(); 35 }); 36 return master; 37 } 38}; 39 40dust.load = function(name, chunk, context) { 41 var tmpl = dust.cache[name]; 42 if (tmpl) { 43 return tmpl(chunk, context); 44 } else { 45 if (dust.onLoad) { 46 return chunk.map(function(chunk) { 47 dust.onLoad(name, function(err, src) { 48 if (err) return chunk.setError(err); 49 if (!dust.cache[name]) dust.loadSource(dust.compile(src, name)); 50 dust.cache[name](chunk, context).end(); 51 }); 52 }); 53 } 54 return chunk.setError(new Error("Template Not Found: " + name)); 55 } 56}; 57 58dust.loadSource = function(source, path) { 59 return eval(source); 60}; 61 62if (Array.isArray) { 63 dust.isArray = Array.isArray; 64} else { 65 dust.isArray = function(arr) { 66 return Object.prototype.toString.call(arr) == "[object Array]"; 67 }; 68} 69 70dust.nextTick = function(callback) { 71 setTimeout(callback, 0); 72} 73 74dust.isEmpty = function(value) { 75 if (dust.isArray(value) && !value.length) return true; 76 if (value === 0) return false; 77 return (!value); 78}; 79 80dust.filter = function(string, auto, filters) { 81 if (filters) { 82 for (var i=0, len=filters.length; i<len; i++) { 83 var name = filters[i]; 84 if (name === "s") { 85 auto = null; 86 } else { 87 string = dust.filters[name](string); 88 } 89 } 90 } 91 if (auto) { 92 string = dust.filters[auto](string); 93 } 94 return string; 95}; 96 97dust.filters = { 98 h: function(value) { return dust.escapeHtml(value); }, 99 j: function(value) { return dust.escapeJs(value); }, 100 u: encodeURI, 101 uc: encodeURIComponent 102} 103 104function Context(stack, global, blocks) { 105 this.stack = stack; 106 this.global = global; 107 this.blocks = blocks; 108} 109 110dust.makeBase = function(global) { 111 return new Context(new Stack(), global); 112} 113 114Context.wrap = function(context) { 115 if (context instanceof Context) { 116 return context; 117 } 118 return new Context(new Stack(context)); 119} 120 121Context.prototype.get = function(key) { 122 var ctx = this.stack, value; 123 124 while(ctx) { 125 if (ctx.isObject) { 126 value = ctx.head[key]; 127 if (!(value === undefined)) { 128 return value; 129 } 130 } 131 ctx = ctx.tail; 132 } 133 return this.global ? this.global[key] : undefined; 134}; 135 136Context.prototype.getPath = function(cur, down) { 137 var ctx = this.stack, 138 len = down.length; 139 140 if (cur && len === 0) return ctx.head; 141 if (!ctx.isObject) return undefined; 142 ctx = ctx.head; 143 var i = 0; 144 while(ctx && i < len) { 145 ctx = ctx[down[i]]; 146 i++; 147 } 148 return ctx; 149}; 150 151Context.prototype.push = function(head, idx, len) { 152 return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks); 153}; 154 155Context.prototype.rebase = function(head) { 156 return new Context(new Stack(head), this.global, this.blocks); 157}; 158 159Context.prototype.current = function() { 160 return this.stack.head; 161}; 162 163Context.prototype.getBlock = function(key) { 164 var blocks = this.blocks; 165 166 if (!blocks) return; 167 var len = blocks.length, fn; 168 while (len--) { 169 fn = blocks[len][key]; 170 if (fn) return fn; 171 } 172} 173 174Context.prototype.shiftBlocks = function(locals) { 175 var blocks = this.blocks; 176 177 if (locals) { 178 if (!blocks) { 179 newBlocks = [locals]; 180 } else { 181 newBlocks = blocks.concat([locals]); 182 } 183 return new Context(this.stack, this.global, newBlocks); 184 } 185 return this; 186} 187 188function Stack(head, tail, idx, len) { 189 this.tail = tail; 190 this.isObject = !dust.isArray(head) && head && typeof head === "object"; 191 this.head = head; 192 this.index = idx; 193 this.of = len; 194} 195 196function Stub(callback) { 197 this.head = new Chunk(this); 198 this.callback = callback; 199 this.out = ''; 200} 201 202Stub.prototype.flush = function() { 203 var chunk = this.head; 204 205 while (chunk) { 206 if (chunk.flushable) { 207 this.out += chunk.data; 208 } else if (chunk.error) { 209 this.callback(chunk.error); 210 this.flush = function() {}; 211 return; 212 } else { 213 return; 214 } 215 chunk = chunk.next; 216 this.head = chunk; 217 } 218 this.callback(null, this.out); 219} 220 221function Stream() { 222 this.head = new Chunk(this); 223} 224 225Stream.prototype.flush = function() { 226 var chunk = this.head; 227 228 while(chunk) { 229 if (chunk.flushable) { 230 this.emit('data', chunk.data); 231 } else if (chunk.error) { 232 this.emit('error', chunk.error); 233 this.flush = function() {}; 234 return; 235 } else { 236 return; 237 } 238 chunk = chunk.next; 239 this.head = chunk; 240 } 241 this.emit('end'); 242} 243 244Stream.prototype.emit = function(type, data) { 245 var events = this.events; 246 247 if (events && events[type]) { 248 events[type](data); 249 } 250} 251 252Stream.prototype.on = function(type, callback) { 253 if (!this.events) { 254 this.events = {}; 255 } 256 this.events[type] = callback; 257 return this; 258} 259 260function Chunk(root, next, taps) { 261 this.root = root; 262 this.next = next; 263 this.data = ''; 264 this.flushable = false; 265 this.taps = taps; 266} 267 268Chunk.prototype.write = function(data) { 269 var taps = this.taps; 270 271 if (taps) { 272 data = taps.go(data); 273 } 274 this.data += data; 275 return this; 276} 277 278Chunk.prototype.end = function(data) { 279 if (data) { 280 this.write(data); 281 } 282 this.flushable = true; 283 this.root.flush(); 284 return this; 285} 286 287Chunk.prototype.map = function(callback) { 288 var cursor = new Chunk(this.root, this.next, this.taps), 289 branch = new Chunk(this.root, cursor, this.taps); 290 291 this.next = branch; 292 this.flushable = true; 293 callback(branch); 294 return cursor; 295} 296 297Chunk.prototype.tap = function(tap) { 298 var taps = this.taps; 299 300 if (taps) { 301 this.taps = taps.push(tap); 302 } else { 303 this.taps = new Tap(tap); 304 } 305 return this; 306} 307 308Chunk.prototype.untap = function() { 309 this.taps = this.taps.tail; 310 return this; 311} 312 313Chunk.prototype.render = function(body, context) { 314 return body(this, context); 315} 316 317Chunk.prototype.reference = function(elem, context, auto, filters) { 318 if (typeof elem === "function") { 319 elem = elem(this, context, null, {auto: auto, filters: filters}); 320 if (elem instanceof Chunk) { 321 return elem; 322 } 323 } 324 if (!dust.isEmpty(elem)) { 325 return this.write(dust.filter(elem, auto, filters)); 326 } else { 327 return this; 328 } 329}; 330 331Chunk.prototype.section = function(elem, context, bodies, params) { 332 if (typeof elem === "function") { 333 elem = elem(this, context, bodies, params); 334 if (elem instanceof Chunk) { 335 return elem; 336 } 337 } 338 339 var body = bodies.block, 340 skip = bodies['else']; 341 342 if (params) { 343 context = context.push(params); 344 } 345 346 if (dust.isArray(elem)) { 347 if (body) { 348 var len = elem.length, chunk = this; 349 for (var i=0; i<len; i++) { 350 chunk = body(chunk, context.push(elem[i], i, len)); 351 } 352 return chunk; 353 } 354 } else if (elem === true) { 355 if (body) return body(this, context); 356 } else if (elem || elem === 0) { 357 if (body) return body(this, context.push(elem)); 358 } else if (skip) { 359 return skip(this, context); 360 } 361 return this; 362}; 363 364Chunk.prototype.exists = function(elem, context, bodies) { 365 var body = bodies.block, 366 skip = bodies['else']; 367 368 if (!dust.isEmpty(elem)) { 369 if (body) return body(this, context); 370 } else if (skip) { 371 return skip(this, context); 372 } 373 return this; 374} 375 376Chunk.prototype.notexists = function(elem, context, bodies) { 377 var body = bodies.block, 378 skip = bodies['else']; 379 380 if (dust.isEmpty(elem)) { 381 if (body) return body(this, context); 382 } else if (skip) { 383 return skip(this, context); 384 } 385 return this; 386} 387 388Chunk.prototype.block = function(elem, context, bodies) { 389 var body = bodies.block; 390 391 if (elem) { 392 body = elem; 393 } 394 395 if (body) { 396 return body(this, context); 397 } 398 return this; 399}; 400 401Chunk.prototype.partial = function(elem, context) { 402 if (typeof elem === "function") { 403 return this.capture(elem, context, function(name, chunk) { 404 dust.load(name, chunk, context).end(); 405 }); 406 } 407 return dust.load(elem, this, context); 408}; 409 410Chunk.prototype.helper = function(name, context, bodies, params) { 411 return dust.helpers[name](this, context, bodies, params); 412}; 413 414Chunk.prototype.capture = function(body, context, callback) { 415 return this.map(function(chunk) { 416 var stub = new Stub(function(err, out) { 417 if (err) { 418 chunk.setError(err); 419 } else { 420 callback(out, chunk); 421 } 422 }); 423 body(stub.head, context).end(); 424 }); 425}; 426 427Chunk.prototype.setError = function(err) { 428 this.error = err; 429 this.root.flush(); 430 return this; 431}; 432 433dust.helpers = { 434 sep: function(chunk, context, bodies) { 435 if (context.stack.index === context.stack.of - 1) { 436 return chunk; 437 } 438 return bodies.block(chunk, context); 439 }, 440 441 idx: function(chunk, context, bodies) { 442 return bodies.block(chunk, context.push(context.stack.index)); 443 } 444} 445 446function Tap(head, tail) { 447 this.head = head; 448 this.tail = tail; 449} 450 451Tap.prototype.push = function(tap) { 452 return new Tap(tap, this); 453}; 454 455Tap.prototype.go = function(value) { 456 var tap = this; 457 458 while(tap) { 459 value = tap.head(value); 460 tap = tap.tail; 461 } 462 return value; 463}; 464 465var HCHARS = new RegExp(/[&<>\"]/), 466 AMP = /&/g, 467 LT = /</g, 468 GT = />/g, 469 QUOT = /\"/g; 470 471dust.escapeHtml = function(s) { 472 if (typeof s === "string") { 473 if (!HCHARS.test(s)) { 474 return s; 475 } 476 return s.replace(AMP,'&').replace(LT,'<').replace(GT,'>').replace(QUOT,'"'); 477 } 478 return s; 479}; 480 481var BS = /\\/g, 482 CR = /\r/g, 483 LS = /\u2028/g, 484 PS = /\u2029/g, 485 NL = /\n/g, 486 LF = /\f/g, 487 SQ = /'/g, 488 DQ = /"/g, 489 TB = /\t/g; 490 491dust.escapeJs = function(s) { 492 if (typeof s === "string") { 493 return s 494 .replace(BS, '\\\\') 495 .replace(DQ, '\\"') 496 .replace(SQ, "\\'") 497 .replace(CR, '\\r') 498 .replace(LS, '\\u2028') 499 .replace(PS, '\\u2029') 500 .replace(NL, '\\n') 501 .replace(LF, '\\f') 502 .replace(TB, "\\t"); 503 } 504 return s; 505}; 506 507})(dust); 508 509if (typeof exports !== "undefined") { 510 if (typeof process !== "undefined") { 511 require('./server')(dust); 512 } 513 module.exports = dust; 514}