diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-25 14:29:51 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-25 14:29:51 +0200 |
commit | ac2ad8d8b5b539cc86c13229d230caa2dd461afa (patch) | |
tree | 78f2703d3d53922495815e7f6e60ce02cd8fc492 /lib/templating | |
parent | 14ce495e9e6f162a74ef1e202fef62855da2a5fc (diff) | |
download | gitbook-ac2ad8d8b5b539cc86c13229d230caa2dd461afa.zip gitbook-ac2ad8d8b5b539cc86c13229d230caa2dd461afa.tar.gz gitbook-ac2ad8d8b5b539cc86c13229d230caa2dd461afa.tar.bz2 |
Add conrefs loader for templating
Diffstat (limited to 'lib/templating')
-rw-r--r-- | lib/templating/conrefsLoader.js | 72 | ||||
-rw-r--r-- | lib/templating/index.js | 5 | ||||
-rw-r--r-- | lib/templating/renderFile.js | 27 |
3 files changed, 103 insertions, 1 deletions
diff --git a/lib/templating/conrefsLoader.js b/lib/templating/conrefsLoader.js new file mode 100644 index 0000000..e46b4d0 --- /dev/null +++ b/lib/templating/conrefsLoader.js @@ -0,0 +1,72 @@ +var path = require('path'); +var nunjucks = require('nunjucks'); + +var fs = require('../utils/fs'); +var Git = require('../utils/git'); +var LocationUtils = require('../utils/location'); +var PathUtils = require('../utils/path'); + + +/** + Template loader resolving both: + - relative url ("./test.md") + - absolute url ("/test.md") + - git url ("") +*/ +var ConrefsLoader = nunjucks.Loader.extend({ + async: true, + + init: function(rootFolder, logger) { + this.rootFolder = rootFolder; + this.logger = logger; + this.git = new Git(); + }, + + getSource: function(sourceURL, callback) { + var that = this; + + this.git.resolve(sourceURL) + .then(function(filepath) { + // Is local file + if (!filepath) { + filepath = path.resolve(sourceURL); + } else { + if (that.logger) that.logger.debug.ln('resolve from git', sourceURL, 'to', filepath); + } + + // Read file from absolute path + return fs.readFile(filepath) + .then(function(source) { + return { + src: source.toString('utf8'), + path: filepath + }; + }); + }) + .nodeify(callback); + }, + + resolve: function(from, to) { + // If origin is in the book, we enforce result file to be in the book + if (PathUtils.isInRoot(this.root, from)) { + var href = LocationUtils.toAbsolute(to, path.dirname(from), ''); + return PathUtils.resolveInRoot(this.root, href); + } + + // If origin is in a git repository, we resolve file in the git repository + var gitRoot = this.git.resolveRoot(from); + if (gitRoot) { + return PathUtils.resolveInRoot(gitRoot, to); + } + + // If origin is not in the book (include from a git content ref) + return path.resolve(path.dirname(from), to); + }, + + // Handle all files as relative, so that nunjucks pass responsability to 'resolve' + isRelative: function(filename) { + return LocationUtils.isRelative(filename); + } +}); + +module.exports = ConrefsLoader; diff --git a/lib/templating/index.js b/lib/templating/index.js index 3a8f989..4cdf6ef 100644 --- a/lib/templating/index.js +++ b/lib/templating/index.js @@ -1,5 +1,8 @@ module.exports = { render: require('./render'), - postRender: require('./postRender') + renderFile: require('./renderFile'), + postRender: require('./postRender'), + + ConrefsLoader: require('./conrefsLoader') }; diff --git a/lib/templating/renderFile.js b/lib/templating/renderFile.js new file mode 100644 index 0000000..13a9c5b --- /dev/null +++ b/lib/templating/renderFile.js @@ -0,0 +1,27 @@ +var Promise = require('../utils/promise'); + +var replaceShortcuts = require('./replaceShortcuts'); + +/** + Render a template + + @param {TemplateEngine} engine + @param {String} filePath + @param {String} content + @param {Object} ctx + @return {Promise<String>} +*/ +function renderTemplateFile(engine, filePath, content, context) { + context = context || {}; + var env = engine.toNunjucks(); + + content = replaceShortcuts(engine, filePath, content); + + return Promise.nfcall( + env.render.bind(env), + content, + context + ); +} + +module.exports = renderTemplateFile; |