summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/generate/generator.js32
-rw-r--r--lib/generate/generator_json.js47
-rw-r--r--lib/generate/generator_site.js91
-rw-r--r--lib/generate/index.js98
-rw-r--r--lib/generate/template.js66
-rw-r--r--lib/parse/navigation.js10
-rw-r--r--lib/parse/summary.js5
7 files changed, 216 insertions, 133 deletions
diff --git a/lib/generate/generator.js b/lib/generate/generator.js
new file mode 100644
index 0000000..3fe080e
--- /dev/null
+++ b/lib/generate/generator.js
@@ -0,0 +1,32 @@
+var path = require("path");
+var Q = require("q");
+var fs = require("./fs");
+
+var BaseGenerator = function(options) {
+ this.options = options;
+};
+
+BaseGenerator.prototype.convertFile = function(content, input) {
+ return Q.reject(new Error("Could not convert "+input));
+};
+
+BaseGenerator.prototype.transferFile = function(input) {
+ return fs.copy(
+ path.join(this.options.input, input),
+ path.join(this.options.output, input)
+ );
+};
+
+BaseGenerator.prototype.transferFolder = function(input) {
+ return fs.mkdirp(
+ path.join(this.options.output, input)
+ );
+};
+
+BaseGenerator.prototype.finish = function() {
+ return Q.reject(new Error("Could not finish generation"));
+};
+
+
+
+module.exports = BaseGenerator; \ No newline at end of file
diff --git a/lib/generate/generator_json.js b/lib/generate/generator_json.js
new file mode 100644
index 0000000..b331bfd
--- /dev/null
+++ b/lib/generate/generator_json.js
@@ -0,0 +1,47 @@
+var util = require("util");
+var path = require("path");
+var Q = require("q");
+
+var fs = require("./fs");
+var parse = require("../parse");
+var BaseGenerator = require("./generator");
+
+
+var Generator = function() {
+ BaseGenerator.apply(this, arguments);
+};
+util.inherits(Generator, BaseGenerator);
+
+Generator.prototype.transferFile = function(input) {
+ // ignore
+};
+
+Generator.prototype.convertFile = function(content, input) {
+ var that = this;
+ var json = {
+ progress: parse.progress(this.options.navigation, input)
+ };
+
+ return Q()
+ .then(function() {
+ return parse.page(content, {
+ repo: that.options.githubId,
+ dir: path.dirname(input) || '/'
+ });
+ })
+ .then(function(sections) {
+ json.sections = sections;
+ })
+ .then(function() {
+ return fs.writeFile(
+ path.join(that.options.output, input.replace(".md", ".json")),
+ JSON.stringify(json, null, 4)
+ );
+ });
+};
+
+Generator.prototype.finish = function() {
+ // ignore
+};
+
+module.exports = Generator; \ No newline at end of file
diff --git a/lib/generate/generator_site.js b/lib/generate/generator_site.js
new file mode 100644
index 0000000..7d40a1a
--- /dev/null
+++ b/lib/generate/generator_site.js
@@ -0,0 +1,91 @@
+var util = require("util");
+var path = require("path");
+var Q = require("q");
+var swig = require('swig');
+
+var fs = require("./fs");
+var parse = require("../parse");
+var BaseGenerator = require("./generator");
+
+// Swig filter for returning the count of lines in a code section
+swig.setFilter('lines', function(content) {
+ return content.split('\n').length;
+});
+
+// Swig filter for returning a link to the associated html file of a markdown file
+swig.setFilter('mdLink', function(link) {
+ return link.replace(".md", ".html");
+});
+
+var Generator = function() {
+ BaseGenerator.apply(this, arguments);
+
+ // Load base template
+ this.template = swig.compileFile(path.resolve(__dirname, '../../templates/page.html'));
+};
+util.inherits(Generator, BaseGenerator);
+
+
+// Convert a markdown file to html
+Generator.prototype.convertFile = function(content, _input) {
+ var that = this;
+ var progress = parse.progress(this.options.navigation, _input);
+
+ _output = _input.replace(".md", ".html");
+
+ var input = path.join(this.options.input, _input);
+ var output = path.join(this.options.output, _output);
+ var basePath = path.relative(path.dirname(output), this.options.output) || ".";
+
+ return Q()
+ .then(function() {
+ return parse.page(content, {
+ repo: that.options.githubId,
+ dir: path.dirname(input) || '/'
+ });
+ })
+ .then(function(sections) {
+ return that.template({
+ title: that.options.title,
+ description: that.options.description,
+
+ githubAuthor: that.options.github.split("/")[0],
+ githubId: that.options.github,
+ githubHost: that.options.githubHost,
+
+ summary: that.options.summary,
+ allNavigation: that.options.navigation,
+ progress: progress,
+
+ _input: _input,
+ content: sections,
+
+ basePath: basePath,
+ staticBase: path.join(basePath, "gitbook"),
+ });
+ })
+ .then(function(html) {
+ return fs.writeFile(
+ output,
+ html
+ );
+ });
+};
+
+// Symlink index.html and copy assets
+Generator.prototype.finish = function() {
+ var that = this;
+
+ return fs.symlink(
+ path.join(that.options.output, 'README.html'),
+ path.join(that.options.output, 'index.html')
+ )
+ .then(function() {
+ return fs.copy(
+ path.join(__dirname, "../../assets/static"),
+ path.join(that.options.output, "gitbook")
+ );
+ });
+};
+
+module.exports = Generator; \ No newline at end of file
diff --git a/lib/generate/index.js b/lib/generate/index.js
index ee5ffc4..95f325c 100644
--- a/lib/generate/index.js
+++ b/lib/generate/index.js
@@ -5,12 +5,24 @@ var path = require("path");
var fs = require("./fs");
var parse = require("../parse");
-var template = require("./template");
-var generate = function(root, output, options) {
- var files, summary, navigation, tpl;
+var generators = {
+ "site": require("./generator_site"),
+ "json": require("./generator_json")
+};
+
+var generate = function(options) {
+ var generator = null;
+ var files;
options = _.defaults(options || {}, {
+ // Folders to use
+ input: null,
+ output: null,
+
+ // Output generator
+ generator: "site",
+
// Book title, keyword, description
title: null,
description: "Book generated using GitBook",
@@ -20,20 +32,24 @@ var generate = function(root, output, options) {
githubHost: 'https://github.com/'
});
- if (!options.github || !options.title) {
- return Q.reject(new Error("Need options.github and options.title"));
+ if (!options.github || !options.title || !options.input || !options.output) {
+ return Q.reject(new Error("Need options: github, title, input, output"));
+ }
+
+ if (!generators[options.generator]) {
+ return Q.reject(new Error("Invalid generator (availables are: "+_.keys(generators).join(", ")));
}
// Clean output folder
- return fs.remove(output)
+ return fs.remove(options.output)
.then(function() {
- return fs.mkdirp(output);
+ return fs.mkdirp(options.output);
})
// List all files in the repository
.then(function() {
- return fs.list(root);
+ return fs.list(options.input);
})
// Check repository is valid
@@ -47,32 +63,18 @@ var generate = function(root, output, options) {
// Get summary
.then(function() {
- return fs.readFile(path.join(root, "SUMMARY.md"), "utf-8")
+ return fs.readFile(path.join(options.input, "SUMMARY.md"), "utf-8")
.then(function(_summary) {
- summary = parse.summary(_summary);
+ options.summary = parse.summary(_summary);
// Parse navigation
- navigation = parse.navigation(summary);
+ options.navigation = parse.navigation(options.summary);
});
})
- // Create template
+ // Create the generator
.then(function() {
- tpl = template({
- root: root,
- output: output,
- locals: {
- title: options.title,
- description: options.description,
-
- githubAuthor: options.github.split("/")[0],
- githubId: options.github,
- githubHost: options.githubHost,
-
- summary: summary,
- allNavigation: navigation
- }
- });
+ generator = new generators[options.generator](options);
})
// Copy file and replace markdown file
@@ -81,49 +83,29 @@ var generate = function(root, output, options) {
_.chain(files)
.map(function(file) {
if (!file) return;
- var _html = file.replace(".md", ".html");
- // Folder
if (file[file.length -1] == "/") {
- return fs.mkdirp(
- path.join(output, file)
- );
- }
-
- // Markdown file (only from the summary)
- else if (path.extname(file) == ".md" && navigation[_html] != null) {
- return tpl(file, file.replace(".md", ".html"));
- }
-
- // Copy file
- else {
- return fs.copy(
- path.join(root, file),
- path.join(output, file)
- );
+ return Q(generator.transferFolder(file));
+ } else if (path.extname(file) == ".md" && options.navigation[file] != null) {
+ return fs.readFile(path.join(options.input, file), "utf-8")
+ .then(function(content) {
+ return Q(generator.convertFile(content, file));
+ });
+ } else {
+ return Q(generator.transferFile(file));
}
})
.value()
);
})
- // Symlink index.html to README.html
- .then(function() {
- return fs.symlink(
- path.join(output, 'README.html'),
- path.join(output, 'index.html')
- );
- })
-
- // Copy assets
+ // Finish gneration
.then(function() {
- return fs.copy(
- path.join(__dirname, "../../assets/static"),
- path.join(output, "gitbook")
- );
+ return generator.finish();
});
};
module.exports = {
+ generators: generators,
folder: generate
};
diff --git a/lib/generate/template.js b/lib/generate/template.js
deleted file mode 100644
index d4e8cae..0000000
--- a/lib/generate/template.js
+++ /dev/null
@@ -1,66 +0,0 @@
-var swig = require('swig');
-var path = require('path');
-var _ = require("lodash");
-
-var parse = require("../parse");
-
-var fs = require('./fs');
-
-swig.setFilter('lines', function(content) {
- return content.split('\n').length;
-});
-
-// return a templeter for page
-var initTemplate = function(options) {
- var tpl = swig.compileFile(path.resolve(__dirname, '../../templates/page.html'));
-
- options = _.defaults(options || {}, {
- // Base folder for input
- root: "./",
-
- // Base folder for output
- output: "./",
-
- // Locals for templates
- locals: {}
- });
-
- return function(input, output, local) {
- var _input = input, _output = output;
- input = path.join(options.root, input);
- output = path.join(options.output, output);
-
- var basePath = path.relative(path.dirname(output), options.output) || ".";
-
- // Read markdown file
- return fs.readFile(input, "utf-8")
-
- // Parse sections
- .then(function(markdown) {
- return parse.page(markdown, {
- repo: options.locals.githubId,
- dir: path.dirname(_input) || '/'
- });
- })
-
- //Calcul template
- .then(function(sections) {
- return tpl(
- _.extend(local || {}, options.locals, {
- _input: _input,
- content: sections,
- basePath: basePath,
- staticBase: path.join(basePath, "gitbook"),
- progress: parse.progress(options.locals.allNavigation, _output)
- })
- );
- })
-
- // Write html
- .then(function(html) {
- return fs.writeFile(output, html);
- })
- }
-};
-
-module.exports = initTemplate; \ No newline at end of file
diff --git a/lib/parse/navigation.js b/lib/parse/navigation.js
index 145c873..bba25ce 100644
--- a/lib/parse/navigation.js
+++ b/lib/parse/navigation.js
@@ -1,15 +1,15 @@
var _ = require('lodash');
// Cleans up an article/chapter object
-// remove 'articles' and '_path' attributes
+// remove 'articles' attributes
function clean(obj) {
- return obj && _.omit(obj, ['articles', '_path']);
+ return obj && _.omit(obj, ['articles']);
}
// Returns from a summary a map of
/*
{
- "file/path.html": {
+ "file/path.md": {
prev: ...,
next: ...,
},
@@ -25,7 +25,7 @@ function navigation(summary, files) {
// Special README nav
var README_NAV = {
- path: 'README.html',
+ path: 'README.md',
title: 'Introduction',
};
@@ -69,7 +69,7 @@ function navigation(summary, files) {
});
// Hack for README.html
- mapping['README.html'] = {
+ mapping['README.md'] = {
title: README_NAV.title,
prev: null,
next: clean(summary.chapters[0]),
diff --git a/lib/parse/summary.js b/lib/parse/summary.js
index 0995c40..7725b59 100644
--- a/lib/parse/summary.js
+++ b/lib/parse/summary.js
@@ -82,10 +82,7 @@ function parseTitle(src, nums) {
level: level,
// Replace .md references with .html
- path: matches[2].replace(/\.md$/, '.html'),
-
- // Original, non normalized path
- _path: matches[2],
+ path: matches[2],
};
}