node-pandoc
このモジュールを使用して、Markdown から PDF を生成したいと考えています。しかし、これらの Markdown をその場で作成する必要があります。プレーンテキスト/マークダウンを生成できる node.js 用のテンプレート エンジンはありますか?
2159 次
2 に答える
0
最近、 rhoで記述されたプレーン テキスト ファイル(Markdown などのプレーン テキストから HTML へのツールでもある) でアンダースコアを使用して、動的データを含むプレーン テキスト ドキュメントを生成しました。template
これが私のモジュールのコードです(必要ない場合はキャッシュを省略してください):
// compiler.js
'use strict';
var fs = require('fs')
, path = require('path')
, _ = require('underscore');
var cache = {};
exports.getTemplate = function(templateId, cb) {
// Use your extension here
var file = path.join(__dirname, templateId + ".rho");
fs.stat(file, function(err, stat) {
if (err) return cb(err);
// Try to get it from cache
var cached = cache[templateId];
if (cached && cached.mtime >= stat.mtime)
return cb(null, cached.template);
// Read it from file
fs.readFile(file, { encoding: 'utf-8' }, function(err, data) {
if (err) return cb(err);
// Compile it
var template = _.template(data);
// Cache it
cache[templateId] = {
mtime: stat.mtime,
template: template
};
// Return it
return cb(null, template);
});
});
};
exports.compile = function(templateId, data, cb) {
exports.getTemplate(templateId, function(err, template) {
if (err) return cb(err);
try {
return cb(null, template(data));
} catch (e) {
return cb(e);
}
});
}
では使い方です。hello.rho
次の内容があるとします。
# Hello, <%= name %>!
We are happy to have you here, <%= name %>!
次のようにコンパイルできます。
require('./compiler').compile('hello', { name: 'World' }, function(err, text) {
if (err) // Handle the error somehow
return console.log(err);
console.log(text);
// You'll get "# Hello, World!\n\nWe're happy to have you here, World!"
// Now chain the compilation to rho, markdown, pandoc or whatever else.
});
アンダースコアが気に入らない場合は、ejsでも問題なく機能すると思います。
于 2013-11-10T06:34:59.853 に答える
0
テンプレートからREADMEドキュメントを生成することに重点を置いていますが、テンプレートからマークダウン ドキュメントを生成する方法の良い例です。
于 2013-11-17T03:07:16.483 に答える