Advertisement
stuppid_bot

Untitled

Sep 11th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. isDom = typeof window != 'undefined';
  2.  
  3. (function (root) {  
  4.     var self = {};
  5.  
  6.     self.addSlashes = function (str) {
  7.         return str.replace(/\\/g, '\\\\')
  8.             .replace(/"/g, '\\"')
  9.             .replace(/\r/g, '\\r')
  10.             .replace(/\n/g, '\\n');
  11.     };
  12.  
  13.     self.escape = function (data) {
  14.         if (typeof data != 'string') {
  15.             data += '';
  16.         }
  17.  
  18.         return data.replace(/&|<|>|'|"/g, function (unit) {
  19.             return {
  20.                 '&': '&amp;',
  21.                 '<': '&lt;',
  22.                 '>': '&gt;',            
  23.                 '"': '&quot;',
  24.                 "'": '&#39;'
  25.             } [unit];
  26.         });
  27.     };
  28.  
  29.     self.compile = function (str) {    
  30.         var re = /\{\*(?:[\s\S]*?)\*\}|\{literal\}([\s\S]*?)\{\/literal\}|<\?([\s\S]*?)\?>|^@(.*)|(#|\$)\{(.*?)\}|\{>(.*?)\}/img;
  31.         var matches;                
  32.         var start = 0;
  33.         var end;
  34.         var body = 'var out="",escape=' + self.escape + ';with(context){';
  35.  
  36.         for (;;) {
  37.             matches = re.exec(str);    
  38.             end = matches ? re.lastIndex - matches[0].length : str.length;
  39.  
  40.             if (end > start) {
  41.                 body += 'out+="' + self.addSlashes(str.slice(start, end)) + '";';
  42.             }
  43.  
  44.             if (!matches) {
  45.                 break;
  46.             }
  47.  
  48.             if (matches[1]) {
  49.                 body += 'out+="' + self.addSlashes(matches[1]) + '";'
  50.             }
  51.             else if (matches[2]) {
  52.                 body += matches[2];
  53.             }
  54.             else if (matches[3]) {
  55.                 body += matches[3];
  56.             }
  57.             else if (matches[4]) {
  58.                 body += 'out+='
  59.                     + (matches[4] == '$'
  60.                         ? 'escape(' + matches[5] + ')'
  61.                         : matches[5])
  62.                     + ';'
  63.             }
  64.             else if (matches[6]) {
  65.                 body += 'out+=' + matches[6] + '(context);';
  66.             }
  67.  
  68.             start = re.lastIndex;          
  69.         }
  70.  
  71.         body += '}return out';
  72.  
  73.         try {
  74.             var fn = new Function('context', body);
  75.             // console.log(fn + '');
  76.  
  77.             return function (context) {
  78.                 context = context || {};
  79.  
  80.                 try {
  81.                     return fn(context);
  82.                 }
  83.                 catch (err) {
  84.                     console.log(err);
  85.                     return '';
  86.                 }
  87.             };
  88.         }
  89.         catch (err) {
  90.             console.log(err);
  91.             return '';
  92.         }
  93.     };
  94.  
  95.     self.render = isDom
  96.         ? function (id) {
  97.             var elem = root.document.getElementById(id);
  98.             return self.compile(elem.innerHTML);
  99.         }
  100.  
  101.         : function (filename) {
  102.             var fs = require('fs');
  103.             var data = fs.readFileSync(filename, 'utf8');
  104.             var fn = self.compile(data);
  105.  
  106.             fs.watchFile(filename, function (curr, prev) {
  107.                 fs.readFile(filename, 'utf8', function (err, data) {
  108.                     if (err) {
  109.                         throw err;
  110.                     }
  111.  
  112.                     fn = self.compile(data);
  113.                 });
  114.             });
  115.  
  116.             return function (data) {
  117.                 return fn(data);
  118.             };
  119.         };
  120.  
  121.     root[isDom ? 'Template' : 'exports'] = self;
  122. })(isDom ? window : module);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement