diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-02-11 18:18:33 +0100 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-02-11 18:18:33 +0100 |
commit | e7eed2abbe91fa44bd071819123bd9ea04d1702a (patch) | |
tree | db6290eec86c94cb31ab8d2eeb26fa7145c6c09e /lib/template | |
parent | 98a13cf487e3b2823cfde38ab1d1b8f97f614517 (diff) | |
download | gitbook-e7eed2abbe91fa44bd071819123bd9ea04d1702a.zip gitbook-e7eed2abbe91fa44bd071819123bd9ea04d1702a.tar.gz gitbook-e7eed2abbe91fa44bd071819123bd9ea04d1702a.tar.bz2 |
Complete conrefs in parsing
Diffstat (limited to 'lib/template')
-rw-r--r-- | lib/template/index.js | 24 | ||||
-rw-r--r-- | lib/template/loader.js | 8 |
2 files changed, 27 insertions, 5 deletions
diff --git a/lib/template/index.js b/lib/template/index.js index a3021e6..b59dd04 100644 --- a/lib/template/index.js +++ b/lib/template/index.js @@ -2,6 +2,7 @@ var _ = require('lodash'); var path = require('path'); var nunjucks = require('nunjucks'); var parsers = require('gitbook-parsers'); +var escapeStringRegexp = require('escape-string-regexp'); var Promise = require('../utils/promise'); var error = require('../utils/error'); @@ -333,7 +334,7 @@ TemplateEngine.prototype.renderString = function(content, context, options) { } // Replace shortcuts - //content = this.applyShortcuts(options.type, content); + content = this.applyShortcuts(options.type, content); return Promise.nfcall(this.env.renderString.bind(this.env), content, context, options) .fail(function(err) { @@ -343,5 +344,26 @@ TemplateEngine.prototype.renderString = function(content, context, options) { }); }; +// Apply a shortcut to a string +TemplateEngine.prototype.applyShortcut = function(content, shortcut) { + var regex = new RegExp( + escapeStringRegexp(shortcut.start) + '([\\s\\S]*?[^\\$])' + escapeStringRegexp(shortcut.end), + 'g' + ); + return content.replace(regex, function(all, match) { + return '{% '+shortcut.tag.start+' %}'+ match + '{% '+shortcut.tag.end+' %}'; + }); +}; + +// Apply all shortcuts to a template +TemplateEngine.prototype.applyShortcuts = function(type, content) { + return _.chain(this.shortcuts) + .filter(function(shortcut) { + return _.contains(shortcut.parsers, type); + }) + .reduce(this.applyShortcut, content) + .value(); +}; + module.exports = TemplateEngine; diff --git a/lib/template/loader.js b/lib/template/loader.js index 23ffb77..5d30fb2 100644 --- a/lib/template/loader.js +++ b/lib/template/loader.js @@ -16,16 +16,16 @@ var Loader = nunjucks.Loader.extend({ this.git = new Git(this.fs.tmpdir()); }, - getSource: function(soureURL, callback) { + getSource: function(sourceURL, callback) { var that = this; - this.git.resolveFile(soureURL) + this.git.resolve(sourceURL) .then(function(filepath) { // Is local file if (!filepath) { - filepath = that.book.resolve(soureURL); + filepath = that.book.resolve(sourceURL); } else { - that.book.log.debug.ln('resolve from git', soureURL, 'to', filepath); + that.book.log.debug.ln('resolve from git', sourceURL, 'to', filepath); } // Read file from absolute path |