summaryrefslogtreecommitdiffstats
path: root/lib/parse
diff options
context:
space:
mode:
authorcodepiano <codepiano.li@gmail.com>2014-11-02 01:34:28 +0800
committercodepiano <codepiano.li@gmail.com>2014-11-02 01:34:28 +0800
commit1502d65286eb36a76b8d0016f7bad64b9bf9f39f (patch)
treef954a62a351413faef03818b49d92a6312d5e4df /lib/parse
parent9da10e2b25cb8ad919bb8563cfdc81d9e0612341 (diff)
parent0feb672d708e2d73ee159d5a94b614f90c85e9e2 (diff)
downloadgitbook-1502d65286eb36a76b8d0016f7bad64b9bf9f39f.zip
gitbook-1502d65286eb36a76b8d0016f7bad64b9bf9f39f.tar.gz
gitbook-1502d65286eb36a76b8d0016f7bad64b9bf9f39f.tar.bz2
Merge branch 'master' of github.com:GitbookIO/gitbook into fork
Conflicts: lib/parse/page.js
Diffstat (limited to 'lib/parse')
-rw-r--r--lib/parse/include.js42
-rw-r--r--lib/parse/includer.js15
-rw-r--r--lib/parse/index.js3
-rw-r--r--lib/parse/page.js2
-rw-r--r--lib/parse/renderer.js8
5 files changed, 24 insertions, 46 deletions
diff --git a/lib/parse/include.js b/lib/parse/include.js
index 5fba2be..483b184 100644
--- a/lib/parse/include.js
+++ b/lib/parse/include.js
@@ -1,48 +1,12 @@
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 || {};
+module.exports = function(markdown, includer) {
// Memoized include function (to cache lookups)
- var _include = _.memoize(includer(ctx, folders));
+ var _include = _.memoize(includer);
return markdown.replace(/{{([\s\S]+?)}}/g, function(match, key) {
// If fails leave content as is
+ key = key.trim();
return _include(key) || match;
});
};
diff --git a/lib/parse/includer.js b/lib/parse/includer.js
new file mode 100644
index 0000000..f7f20e0
--- /dev/null
+++ b/lib/parse/includer.js
@@ -0,0 +1,15 @@
+// Return a fs inclduer
+module.exports = function(ctx, folders, resolveFile, readFile) {
+ return function(name) {
+ return ctx[name] ||
+ folders.map(function(folder) {
+ // Try including snippet from FS
+ try {
+ var fname = resolveFile(folder, name);
+ // Trim trailing newlines/space of imported snippets
+ return readFile(fname, 'utf8').trimRight();
+ } catch(err) {}
+ })
+ .filter(Boolean)[0];
+ }
+};
diff --git a/lib/parse/index.js b/lib/parse/index.js
index c8c15e6..23471af 100644
--- a/lib/parse/index.js
+++ b/lib/parse/index.js
@@ -6,5 +6,6 @@ module.exports = {
lex: require('./lex'),
progress: require('./progress'),
navigation: require('./navigation'),
- readme: require('./readme')
+ readme: require('./readme'),
+ includer: require('./includer')
};
diff --git a/lib/parse/page.js b/lib/parse/page.js
index e4d9c46..2cbbbf4 100644
--- a/lib/parse/page.js
+++ b/lib/parse/page.js
@@ -51,7 +51,7 @@ function parsePage(page, options) {
options = options || {};
// Lex if not already lexed
- page.lexed = (_.isArray(page.content) ? page.content : lex(include(page.content, [options.dir, options.includes_dir], options.variables)))
+ page.lexed = (_.isArray(page.content) ? page.content : lex(include(page.content, options.includer || function() { return undefined; })))
return page.lexed
.map(function(section) {
// Transform given type
diff --git a/lib/parse/renderer.js b/lib/parse/renderer.js
index 61b3d9b..0f6640b 100644
--- a/lib/parse/renderer.js
+++ b/lib/parse/renderer.js
@@ -1,9 +1,7 @@
var url = require('url');
+var _ = require('lodash');
var inherits = require('util').inherits;
var links = require('../utils').links;
-
-var path = require('path');
-
var kramed = require('kramed');
var rendererId = 0;
@@ -50,11 +48,11 @@ GitBookRenderer.prototype.link = function(href, title, text) {
// Parsed version of the url
var parsed = url.parse(href);
-
var o = this._extra_options;
+ var extname = _.last(parsed.path.split("."));
// Relative link, rewrite it to point to github repo
- if(links.isRelative(_href) && path.extname(parsed.path) == ".md") {
+ if(links.isRelative(_href) && extname == "md") {
_href = links.toAbsolute(_href, o.dir || "./", o.outdir || "./");
_href = _href.replace(".md", ".html");
}