diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-10-15 21:20:10 +0200 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-10-15 21:20:10 +0200 |
commit | 52e83618467a238f6b20f5fb77f6f6a2e1450c8c (patch) | |
tree | 96b43d8396fedea81409677f1e10698a0b36265c /lib/parse | |
parent | 49e3bbc3d6e9e128df721494e9a10eaf15b7ddc8 (diff) | |
download | gitbook-52e83618467a238f6b20f5fb77f6f6a2e1450c8c.zip gitbook-52e83618467a238f6b20f5fb77f6f6a2e1450c8c.tar.gz gitbook-52e83618467a238f6b20f5fb77f6f6a2e1450c8c.tar.bz2 |
Add include support, with variables and the whole shebang
Fixes #462
Diffstat (limited to 'lib/parse')
-rw-r--r-- | lib/parse/code_include.js | 20 | ||||
-rw-r--r-- | lib/parse/include.js | 48 | ||||
-rw-r--r-- | lib/parse/page.js | 15 | ||||
-rw-r--r-- | lib/parse/renderer.js | 5 |
4 files changed, 54 insertions, 34 deletions
diff --git a/lib/parse/code_include.js b/lib/parse/code_include.js deleted file mode 100644 index 977dc3d..0000000 --- a/lib/parse/code_include.js +++ /dev/null @@ -1,20 +0,0 @@ -var fs = require('graceful-fs'); -var path = require('path'); - -module.exports = function(code, folder) { - folder = folder || ''; - - return code.replace(/{{([\s\S]+?)}}/g, function(match, filename) { - // Normalize filename - var fname = path.join(folder, filename.trim()); - - // Try including snippet from FS - try { - // Trim trailing newlines/space of imported snippets - return fs.readFileSync(fname, 'utf8').trimRight(); - } catch(err) {} - - // If fails leave content as is - return match; - }); -}; diff --git a/lib/parse/include.js b/lib/parse/include.js new file mode 100644 index 0000000..5fba2be --- /dev/null +++ b/lib/parse/include.js @@ -0,0 +1,48 @@ +var _ = require('lodash'); +var fs = require('graceful-fs'); +var path = require('path'); + + +// Include helper +function importInclude(name, paths) { + return paths + .map(function(folder) { + // Try including snippet from FS + try { + var fname = path.join(folder, name); + // Trim trailing newlines/space of imported snippets + return fs.readFileSync(fname, 'utf8').trimRight(); + } catch(err) {} + }) + .filter(Boolean)[0]; +} + +function includer(ctx, folders) { + return function(key) { + key = key.trim(); + return ctx[key] || importInclude(key, folders); + }; +} + +module.exports = function(markdown, folder, ctx) { + // List of folders to search for includes + var folders = []; + + // Handle folder arg (string or array) + if(_.isString(folder)) { + folders = [folder]; + } else if(_.isArray(folder)) { + folders = folder; + } + + // variable context + ctx = ctx || {}; + + // Memoized include function (to cache lookups) + var _include = _.memoize(includer(ctx, folders)); + + return markdown.replace(/{{([\s\S]+?)}}/g, function(match, key) { + // If fails leave content as is + return _include(key) || match; + }); +}; diff --git a/lib/parse/page.js b/lib/parse/page.js index ae71666..846d339 100644 --- a/lib/parse/page.js +++ b/lib/parse/page.js @@ -5,7 +5,7 @@ var hljs = require('highlight.js'); var lex = require('./lex'); var renderer = require('./renderer'); -var codeInclude = require('./code_include'); +var include = require('./include'); var lnormalize = require('../utils/lang').normalize; @@ -51,7 +51,7 @@ function parsePage(src, options) { options = options || {}; // Lex if not already lexed - return (_.isArray(src) ? src : lex(src)) + return (_.isArray(src) ? src : lex(include(src, options.dir, options.variables))) .map(function(section) { // Transform given type if(section.type === 'exercise') { @@ -74,20 +74,15 @@ function parsePage(src, options) { // Main language var lang = validLangs ? langs[0] : null; - // codeInclude shortcut - var ci = function(code) { - return codeInclude(code, options.dir); - }; - return { id: section.id, type: section.type, content: render(nonCodeNodes, options), lang: lang, code: { - base: ci(codeNodes[0].text), - solution: ci(codeNodes[1].text), - validation: ci(codeNodes[2].text), + base: codeNodes[0].text, + solution: codeNodes[1].text, + validation: codeNodes[2].text, // Context is optional context: codeNodes[3] ? codeNodes[3].text : null, } diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js index faa416c..61b3d9b 100644 --- a/lib/parse/renderer.js +++ b/lib/parse/renderer.js @@ -1,8 +1,6 @@ var url = require('url'); var inherits = require('util').inherits; var links = require('../utils').links; -var codeInclude = require('./code_include'); - var path = require('path'); @@ -130,8 +128,7 @@ GitBookRenderer.prototype.listitem = function(text) { GitBookRenderer.prototype.code = function(code, lang, escaped) { return GitBookRenderer.super_.prototype.code.call( this, - // Import code snippets - codeInclude(code, this._extra_options.dir), + code, lang, escaped ); |