Пример #1
0
  parse: function (html, isBuild) {
    html = html.replace(/\n\r*/img,'').
      replace(this.RE_TAG_OPEN, '\n\r$&').
      replace(this.RE_TAG_END, '$&\n\r').
      split('\n\r');
    var _vars = [];
    html = mace.map(html, function (piece) {
      piece = piece.trim();
      var matched;
      if (mace.some(this.RE_MAP, function (reg) {
        return matched = piece.match(reg);
      })) {
        matched = mace.makeArray(matched,1);
        var tag = matched.shift();
        var handle = this[tag];
        if (handle) {
          _vars = _vars.concat(matched);
          return handle.apply(this, matched);
        }
        mace.error('Failed build this with tag: %s expression: %s', tag, piece)
      }
      if (!piece) {
        return piece;
      }
      return '_.ret += "'+ piece.replace(/\"|\'/mg,'\\$&') + '";\r\n';
    }, this);

    var vars = {};
    mace.each(_vars, function (varstr) {
      var str = varstr.match(/\s*([a-z$_]+[a-z+$_0-9])/i);
      if (str && str[1]) {
        vars[str[1]] = 1;
      }
    });
    _vars = [];
    // 完成去重得到所有的变量,开始注册
    mace.each(vars, function (v,name) {
      if (JS_TOKENS.indexOf(name) === -1) {
        _vars.push('var '+name+'=$.'+name+';');
      }
    });
    _vars.push('_.ret = "";');
    _vars.push('_.each = _.each || '+ mace.forEach.toString() + ';');
    _vars.push('_.escapeHTML = _.escapeHTML || ' + mace.escapeHTML.toString() + ';');
    _vars.push('_.echo = _.echo || function (val) {this.ret += this.escapeHTML(val)};');
    _vars.push('_.unSafeEcho = _.unSafeEcho || function (val) {this.ret += val || ""};');
    html.unshift(_vars.join('\n\r'));
    html.push("return _.ret;");
    require('fs').writeFileSync(__dirname+'/test.js', html.join(''));
    return new Function('$, _', html.join('\n\r'));
  },
Пример #2
0
mace.each(require('os').networkInterfaces(), function (networkInterface) {
  mace.each(networkInterface, function (item) {
    if (!IPList.localhost[item.address]) {
      IPList.local.push(item.address);
    }
    IPList.push(item.address);
  });
});
Пример #3
0
function PACHandle (iplist, pathlist) {
  var iplist = iplist || [];
  var pathlist = pathlist || [];
  var IP_LIST = '';
  mace.each(iplist.concat(exports.IPList), function (ip) {
    IP_LIST+=',"'+ ip + '"';
  });
  IP_LIST = '[' + IP_LIST.substr(1) + ']';
  var PATH_LIST = '';
  mace.each(pathlist.concat([
    /^https/i,
    /^ws/i,
    /^wss/i
  ]), function (regexp) {
    if (typeof regexp === 'string') {
      PATH_LIST += ',' + String(new RegExp(regexp,'i'));
    } else {
      PATH_LIST += ',' +String(regexp);
    }
  });
  PATH_LIST = '['+PATH_LIST.substr(1)+']';
  var PAC_TPL = fs.readFileSync(path.join(__dirname, '/resource/proxy.pac.js'));

  return function (req, res, next) {
    if (url.parse(req.url,true).pathname.match(/.*\.pac$/i)) {
      var local = (req.connection.localAddress || '').split(':').pop() || exports.IPList.local[0];
      var pacfile = [
        'var iplist = ' + IP_LIST + ';',
        'var pathlist = ' + PATH_LIST + ';'
      ];
      pacfile.push('var local="' + local + '";');
      pacfile.push(PAC_TPL);
      pacfile = pacfile.join('\n\r');
      res.writeHeader(200, {
        'Content-Type': 'application/octet-stream'
      });
      debug('PAC file \n\r ', pacfile);
      res.end(pacfile);
      return true;
    }
    next && next();
    return false;
  };
};
Пример #4
0
 set: function (name, value) {
   var res = this.response;
   if (mace.isObject(name)) {
     mace.each(name, function (value, name) {
       res.setHeader(name, value);
     });
     return this;
   }
   res.setHeader(name, value);
   return this;
 },
Пример #5
0
function JsEngine (options) {
  var tags = JsEngine.TAGS;
  
  this.TAG_OPEN = JsEngine.TAG_OPEN;
  this.TAG_END = JsEngine.TAG_END;

  var TAG_OPEN = this.makeRegExpSafe(this.TAG_OPEN);
  var TAG_END = this.makeRegExpSafe(this.TAG_END);

  this.RE_TAG_OPEN = new RegExp(TAG_OPEN, 'ig');
  this.RE_TAG_END = new RegExp(TAG_END, 'ig');

  this.RE_MAP = [];
  mace.each(tags, function (handle, name) {
    this.RE_MAP.push(new RegExp(TAG_OPEN +'\\s*('+ name + ')\\s*([\\w\\W]*?)\\s*' + TAG_END));
    this[name] = handle;
  }, this);
  return this;
}