Esempio n. 1
0
exports.iterate = function (indent, parser) {
    var thisArgs = _.clone(this.args),
        operand1 = thisArgs[0],
        operator = thisArgs[1],
        operand2 = parser.parseVariable(thisArgs[2]),
        separator = thisArgs[3],
        operand3 = parser.parseVariable(thisArgs[4]),
        output;

    indent = indent || '';

    if (typeof operator !== 'undefined' && operator !== 'with') {
        throw new Error('Invalid syntax in "iterate" tag');
    }

    if (typeof separator !== 'undefined' && separator !== 'to') {
        throw new Error('Invalid syntax in "iterate" tag');
    }

    if (!helpers.isValidShortName(operand1)) {
        throw new Error('Invalid arguments (' + operand1 + ') passed to "iterate" tag');
    }

    if (!helpers.isValidName(operand2.name)) {
        throw new Error('Invalid arguments (' + operand2.name + ') passed to "iterate" tag');
    }

    if (!helpers.isValidName(operand3.name)) {
        throw new Error('Invalid arguments (' + operand3.name + ') passed to "iterate" tag');
    }

    operand1 = helpers.escapeVarName(operand1);

    output = '(function () {' +
        helpers.setVar('__iterFrom', operand2) +
        helpers.setVar('__iterTo', operand3) +
        '   for (var i = Number(__iterFrom); i <= Number(__iterTo); i++) {\n' +
        '       _context["' + operand1 + '"] = i;\n' +
        parser.compile.call(this, indent + '    ') +
        '   }\n' +
        '})();';

    return output;
};
Esempio n. 2
0
File: for.js Progetto: AliMD/hexo
module.exports = function(indent, parser){
  var args = this.args,
    name = args[0],
    operator = args[1],
    collection = parser.parseVariable(args[2]);

  var loopShared = [
    'loop.index = __loopIndex + 1;',
    'loop.index0 = __loopIndex;',
    'loop.revindex = __loopLength - loop.index0;',
    'loop.revindex0 = loop.revindex - 1;',
    'loop.first = __loopIndex === 0;',
    'loop.last = __loopIndex === __loopLength - 1;',
    parser.compile.apply(this, [indent])
  ].join('\n');

  var out = [
    '(function(){',
      'var loop = {};',
      'var __loopIndex = 0;',
      'var __loopLength = 0;',
      helpers.setVar('__loopObj', collection),
      'else {',
        'return;',
      '}',
      'if (Array.isArray(__loopObj)){',
        '__loopLength = __loopObj.length;',
        'for (; __loopIndex < __loopLength; __loopIndex++){',
          'loop.key = __loopIndex;',
          '_context["' + name + '"] = __loopObj[__loopIndex];',
          loopShared,
        '}',
      '} else if (typeof __loopObj.forEach === "function"){',
        '__loopLength = __loopObj.length;',
        '__loopObj.forEach(function(item, i){',
          'loop.key = __loopIndex = i;',
          '_context["' + name + '"] = item;',
          loopShared,
        '});',
      '} else if (typeof __loopObj === "object"){',
        '__keys = Object.keys(__loopObj);',
        '__loopLength = __keys.length;',
        'for (; __loopIndex < __loopLength; __loopIndex++){',
          'loop.key = __keys[__loopIndex];',
          '_context["' + name + '"] = __loopObj[loop.key];',
          loopShared,
        '}',
      '}',
    '})();'
  ].join('\n');

  return out;
};
Esempio n. 3
0
	var scopedstyle = function(indent, parser) {
		var myArg = parser.parseVariable(this.args[0]),
			output = [];

		output.push(helpers.setVar('__myArg', myArg));
		output.push('_output += "<style scoped>";');
		output.push('_output += "@import url(";');
		output.push('_output += "./js/features/compass/templates/";');
		output.push('_output += __myArg;');
		output.push('_output += ");";');
		output.push('_output += "</style>";');
		return output.join('');
	};