Example #1
0
      .pipe(through.obj(function(file, enc, cb) {

        // only if this file has the placeholder
        if(file.contents.toString().match(placeHolder)) {

          var tmpl = tinyliquid.compile("{% include toc.html %}");
          var locals = { toc: toc };
          var includes = _.get(file, "pageLocals.page.includes") || config.liquid.includes;

          helpers.renderLiquidTemplate(tmpl, locals, includes, function(err, data) {

            // now replace the placeholder with the rendered liquid
            // in the file.
            var content = file.contents.toString();
            file.contents = new Buffer(content.replace(placeHolder, data.toString()));
            file.$el = undefined;

            cb(err, file);
          });

        } else {
          cb(null, file);
        }

      }));
Example #2
0
 function (err, text) {
   if (err) return callback(err);
   try {
     tplRenders[tpl] = tinyliquid.compile(text);
   } catch (err) {
     return callback(err);
   }
   startRender();
 });
Example #3
0
}, function (err) {
  if (err) throw err;


  // 生成首页
  var renderIndex = tinyliquid.compile(fs.readFileSync('_home.html', 'utf8'));
  var c = tinyliquid.newContext();
  c.setLocals('articles', list);
  renderIndex(c, function (err, html) {
    if (err) throw err;

    fs.writeFileSync('index.html', html);
  });

});
Example #4
0
      through.obj(function(file, enc, cb) {
        file.$el = file.$el || cheerio.load(file.contents.toString());

        var footnotes = [];

        // find all footnotes in document
        file.$el("span[data-type=footnote]").each(function(i) {
          var jel = file.$el(this);
          // create object to be used to inject footnote in liquid tag
          var fn = {
            id: "fn" + (i + 1),
            label: jel.html()
          };

          // replace text of footnote with link
          jel.html('<a href="#' + fn.id + '">' + (i + 1) + "</a>");

          footnotes.push(fn);
        });

        // if we have any footnotes
        var placeholder = file.$el("div[data-placeholder-footnotes]");
        if (placeholder.length > 0) {
          // render the footnotes
          var tmpl = tinyliquid.compile("{% include footnotes.html %}");
          var locals = { footnotes: footnotes };
          var includes =
            _.get(file, "pageLocals.page.includes") || config.liquid.includes;

          helpers.renderLiquidTemplate(tmpl, locals, includes, function(
            err,
            data
          ) {
            placeholder.replaceWith(data.toString());
            file.contents = new Buffer(file.$el("body").html());
            cb(err, file);
          });
        } else {
          cb(null, file);
        }
      })
Example #5
0
function liquify(contents, locals, includeBase){
  var template;
  var context = tinyliquid.newContext({
      locals: locals
    });

  if(!contents) return;

  if(typeof contents != "string"){
    template = contents;
  } else {
    template = tinyliquid.compile(contents);
  }

  return new Promise(function (resolve, reject) {

    context.onInclude(function (name, callback) {
      fs.readFile((includeBase || "./") + name, 'utf8', function (err, text) {

        if (err) {
          reject(err);
          return callback(err);
        }

        var ast = tinyliquid.parse(text);
        callback(null, ast);
      });
    });

    template(context, function (err) {
      if (err) return reject(err);
      resolve(context.getBuffer());
    });
  });

};
Example #6
0
var liquify = function(contents, locals, includeBase, prefix, filters){
  var template;
  var context = tinyliquid.newContext({
      locals: locals,
      filters: filters
    });

  if(!contents) {
    contents = '';
  };

  if(typeof contents != "string"){
    template = contents;
  } else {
    template = tinyliquid.compile(contents);
  }

  return new Promise(function (resolve, reject) {

    context.onInclude(function (name, callback) {

      var absolute = isAbsolute(name);
      var ext = path.extname(name);
      var filePath;

      if(!absolute) {

        if(prefix) {
          name = prefix + name;
        }

        if(!ext) {
          name += ".liquid";
        }

        filePath = path.join(includeBase, name);

      } else {
        filePath = name;
      }


      fs.readFile(filePath, 'utf8', function (err, text) {

        if (err) {
          reject(err);
          return callback(err);
        }

        var ast = tinyliquid.parse(text);
        callback(null, ast);
      });
    });

    template(context, function (err) {
      if (err) return reject(err);
      resolve(context.getBuffer(), template);
    });
  });

};
Example #7
0
 .then(function(contents) {
   return tinyliquid.compile(contents.toString());
 });
Example #8
0
    if (a.month === b.month) {
      return b.day - a.day;
    }
    return b.month - a.month;
  }
  return b.year - a.year;
});

// 生成文章
var ARTICLE_PATH = './articles';

fs.readdirSync(ARTICLE_PATH).forEach(function (f) {
  fs.unlinkSync(path.resolve(ARTICLE_PATH, f));
});

var renderArticle = tinyliquid.compile(fs.readFileSync('_article.html', 'utf8'));
async.eachSeries(list, function (item, next) {

  var c = tinyliquid.newContext({locals: item});
  renderArticle(c, function (err, html) {
    if (err) return next(err);

    fs.writeFileSync(path.resolve(ARTICLE_PATH, item.name + '.html'), html);
    next();
  });
}, function (err) {
  if (err) throw err;


  // 生成首页
  var renderIndex = tinyliquid.compile(fs.readFileSync('_home.html', 'utf8'));