From 9dce855113b9217201cbf1cac851da44d3238fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 09:45:04 -0700 Subject: [PATCH 01/11] Add class BaseGenerator --- lib/generate/generator.js | 32 ++++++++++++++++ lib/generate/generator_site.js | 32 ++++++++++++++++ lib/generate/index.js | 67 ++++++++++------------------------ 3 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 lib/generate/generator.js create mode 100644 lib/generate/generator_site.js diff --git a/lib/generate/generator.js b/lib/generate/generator.js new file mode 100644 index 000000000..fcd0ad6c1 --- /dev/null +++ b/lib/generate/generator.js @@ -0,0 +1,32 @@ +var Q = require("q"); + +var BaseGenerator = function(options) { + this.options = options; +}; + +BaseGenerator.prototype.convertFile = function(input) { + return Q.reject(new Error("Could not convert "+input)); +}; + +BaseGenerator.prototype.transferFile = function(input) { + console.log("tranfer file", input); + /*return fs.copy( + path.join(root, file), + path.join(output, file) + );*/ +}; + +BaseGenerator.prototype.transferFolder = function(input) { + console.log("tranfer folder", input); + /*return fs.mkdirp( + path.join(output, file) + );*/ +}; + +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_site.js b/lib/generate/generator_site.js new file mode 100644 index 000000000..5d17b0c76 --- /dev/null +++ b/lib/generate/generator_site.js @@ -0,0 +1,32 @@ +var BaseGenerator = require("./generator"); +var util = require("util"); + +var Generator = function() { + BaseGenerator.apply(this, arguments); +}; +util.inherits(Generator, BaseGenerator); + +Generator.prototype.convertFile = function(input) { + console.log("convert file", input) +}; + +Generator.prototype.finish = function() { + console.log("finish generation"); + // Symlink index.html to README.html + /*.then(function() { + return fs.symlink( + path.join(output, 'README.html'), + path.join(output, 'index.html') + ); + }) + + // Copy assets + .then(function() { + return fs.copy( + path.join(__dirname, "../../assets/static"), + path.join(output, "gitbook") + ); + });*/ +}; + +module.exports = Generator; \ No newline at end of file diff --git a/lib/generate/index.js b/lib/generate/index.js index ee5ffc46e..a32ba1a00 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -7,10 +7,17 @@ var fs = require("./fs"); var parse = require("../parse"); var template = require("./template"); +var generators = { + "site": require("./generator_site") +}; + var generate = function(root, output, options) { + var generator = null; var files, summary, navigation, tpl; options = _.defaults(options || {}, { + generator: "site", + // Book title, keyword, description title: null, description: "Book generated using GitBook", @@ -24,6 +31,10 @@ var generate = function(root, output, options) { return Q.reject(new Error("Need options.github and options.title")); } + if (!generators[options.generator]) { + return Q.reject(new Error("Invalid generator (availables are: "+_.keys(generators).join(", "))); + } + // Clean output folder return fs.remove(output) @@ -56,23 +67,9 @@ var generate = function(root, output, options) { }); }) - // 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](); }) // Copy file and replace markdown file @@ -81,46 +78,22 @@ 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 generator.transferFolder(file); + } else if (path.extname(file) == ".md" && navigation[file] != null) { + return generator.convertFile(file); + } else { + return generator.transferFile(file); } }) .value() ); }) - // Symlink index.html to README.html + // Finish gneration .then(function() { - return fs.symlink( - path.join(output, 'README.html'), - path.join(output, 'index.html') - ); - }) - - // Copy assets - .then(function() { - return fs.copy( - path.join(__dirname, "../../assets/static"), - path.join(output, "gitbook") - ); + return generator.finish(); }); }; From a52a4bed1a01f954d6faa06b502f5fe311790cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 09:47:06 -0700 Subject: [PATCH 02/11] Change normalization of parser to give link in .md --- lib/parse/navigation.js | 6 +++--- lib/parse/summary.js | 5 +---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/parse/navigation.js b/lib/parse/navigation.js index 145c87396..aeb2716f7 100644 --- a/lib/parse/navigation.js +++ b/lib/parse/navigation.js @@ -9,7 +9,7 @@ function clean(obj) { // 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 0995c4055..7725b5901 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], }; } From 91359fab1e79f8d371e72b6efcb469fb89c3be8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 09:47:34 -0700 Subject: [PATCH 03/11] _path is not used anymore --- lib/parse/navigation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/parse/navigation.js b/lib/parse/navigation.js index aeb2716f7..bba25cec8 100644 --- a/lib/parse/navigation.js +++ b/lib/parse/navigation.js @@ -1,9 +1,9 @@ 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 From 3da53d56a2ac0b6652b52b74a974747d9df8662e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 09:53:05 -0700 Subject: [PATCH 04/11] Add option -g to choice generator Add base json generator --- bin/gitbook.js | 5 ++++- lib/generate/generator_json.js | 15 +++++++++++++++ lib/generate/index.js | 4 +++- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 lib/generate/generator_json.js diff --git a/bin/gitbook.js b/bin/gitbook.js index 6e29986b1..7baad4842 100755 --- a/bin/gitbook.js +++ b/bin/gitbook.js @@ -10,6 +10,7 @@ var fs = require('fs'); var pkg = require('../package.json'); var generate = require("../lib/generate"); var parse = require("../lib/parse"); +var generators = require("../lib/generate").generators; var utils = require('./utils'); @@ -25,6 +26,7 @@ prog .command('build [source_dir]') .description('Build a gitbook from a directory') .option('-o, --output ', 'Path to output directory, defaults to ./_book') +.option('-g, --generator ', 'Change generator, defaults to site, availables are: '+_.keys(generators).join(", ")) .option('-t, --title ', 'Name of the book to generate, defaults to repo name') .option('-i, --intro ', 'Description of the book to generate') .option('-g, --github ', 'ID of github repo like : username/repo') @@ -60,7 +62,8 @@ prog { title: title, description: options.intro, - github: githubID + github: githubID, + generator: options.generator } ); }) diff --git a/lib/generate/generator_json.js b/lib/generate/generator_json.js new file mode 100644 index 000000000..6a039a68a --- /dev/null +++ b/lib/generate/generator_json.js @@ -0,0 +1,15 @@ +var BaseGenerator = require("./generator"); +var util = require("util"); + +var Generator = function() { + BaseGenerator.apply(this, arguments); +}; +util.inherits(Generator, BaseGenerator); + +Generator.prototype.convertFile = function(input) { +}; + +Generator.prototype.finish = function() { +}; + +module.exports = Generator; \ No newline at end of file diff --git a/lib/generate/index.js b/lib/generate/index.js index a32ba1a00..7cc7b6bf4 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -8,7 +8,8 @@ var parse = require("../parse"); var template = require("./template"); var generators = { - "site": require("./generator_site") + "site": require("./generator_site"), + "json": require("./generator_json") }; var generate = function(root, output, options) { @@ -98,5 +99,6 @@ var generate = function(root, output, options) { }; module.exports = { + generators: generators, folder: generate }; From 88c480ba4f59a77e448bb88ede36fca1470aca0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 10:27:45 -0700 Subject: [PATCH 05/11] Complete json generator --- bin/gitbook.js | 4 +-- lib/generate/generator.js | 22 +++++++++-------- lib/generate/generator_json.js | 36 +++++++++++++++++++++++++-- lib/generate/index.js | 45 ++++++++++++++++++++++------------ 4 files changed, 77 insertions(+), 30 deletions(-) diff --git a/bin/gitbook.js b/bin/gitbook.js index 7baad4842..d57e7852c 100755 --- a/bin/gitbook.js +++ b/bin/gitbook.js @@ -57,9 +57,9 @@ prog var title = options.title || utils.titleCase(repo); return generate.folder( - dir, - outputDir, { + input: dir, + output: outputDir, title: title, description: options.intro, github: githubID, diff --git a/lib/generate/generator.js b/lib/generate/generator.js index fcd0ad6c1..216bf3e75 100644 --- a/lib/generate/generator.js +++ b/lib/generate/generator.js @@ -1,26 +1,28 @@ +var path = require("path"); var Q = require("q"); +var fs = require("./fs"); var BaseGenerator = function(options) { this.options = options; }; -BaseGenerator.prototype.convertFile = function(input) { +BaseGenerator.prototype.convertFile = function(content, input) { return Q.reject(new Error("Could not convert "+input)); }; BaseGenerator.prototype.transferFile = function(input) { - console.log("tranfer file", input); - /*return fs.copy( - path.join(root, file), - path.join(output, file) - );*/ + console.log("Copying", input); + return fs.copy( + path.join(this.options.input, input), + path.join(this.options.output, input) + ); }; BaseGenerator.prototype.transferFolder = function(input) { - console.log("tranfer folder", input); - /*return fs.mkdirp( - path.join(output, file) - );*/ + console.log("Creating directory", input); + return fs.mkdirp( + path.join(this.options.output, input) + ); }; BaseGenerator.prototype.finish = function() { diff --git a/lib/generate/generator_json.js b/lib/generate/generator_json.js index 6a039a68a..b331bfd4c 100644 --- a/lib/generate/generator_json.js +++ b/lib/generate/generator_json.js @@ -1,15 +1,47 @@ -var BaseGenerator = require("./generator"); 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.convertFile = function(input) { +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/index.js b/lib/generate/index.js index 7cc7b6bf4..1606bd530 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -12,11 +12,16 @@ var generators = { "json": require("./generator_json") }; -var generate = function(root, output, options) { +var generate = function(options) { var generator = null; - var files, summary, navigation, tpl; + var files; options = _.defaults(options || {}, { + // Folders to use + input: null, + output: null, + + // Output generator generator: "site", // Book title, keyword, description @@ -28,8 +33,8 @@ 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]) { @@ -37,15 +42,15 @@ var generate = function(root, output, options) { } // 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 @@ -59,18 +64,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 the generator .then(function() { - generator = new generators[options.generator](); + generator = new generators[options.generator](options); }) // Copy file and replace markdown file @@ -81,11 +86,14 @@ var generate = function(root, output, options) { if (!file) return; if (file[file.length -1] == "/") { - return generator.transferFolder(file); - } else if (path.extname(file) == ".md" && navigation[file] != null) { - return generator.convertFile(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 generator.transferFile(file); + return Q(generator.transferFile(file)); } }) .value() @@ -95,7 +103,12 @@ var generate = function(root, output, options) { // Finish gneration .then(function() { return generator.finish(); - }); + }) + + .fail(function(err) { + console.log(err); + return Q.reject(err); + }) }; module.exports = { From f656390783922952cc1f6fe8b0a5d0dc902fa92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 10:39:27 -0700 Subject: [PATCH 06/11] Base for new site generator --- lib/generate/generator_site.js | 83 ++++++++++++++++++++++++++++------ lib/generate/index.js | 2 +- 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/lib/generate/generator_site.js b/lib/generate/generator_site.js index 5d17b0c76..1cc86143a 100644 --- a/lib/generate/generator_site.js +++ b/lib/generate/generator_site.js @@ -1,32 +1,87 @@ -var BaseGenerator = require("./generator"); 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"); + +// Init swig filter lines for returning the count of lines in a code section +swig.setFilter('lines', function(content) { + return content.split('\n').length; +}); var Generator = function() { BaseGenerator.apply(this, arguments); + + // Load base template + this.template = swig.compileFile(path.resolve(__dirname, '../../templates/page.html')); }; util.inherits(Generator, BaseGenerator); -Generator.prototype.convertFile = function(input) { - console.log("convert file", input) + +// 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) || "."; + + console.log("Converting ", _input); + 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() { - console.log("finish generation"); - // Symlink index.html to README.html - /*.then(function() { - return fs.symlink( - path.join(output, 'README.html'), - path.join(output, 'index.html') - ); - }) + var that = this; - // Copy assets + 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(output, "gitbook") + 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 1606bd530..b6782f078 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -106,7 +106,7 @@ var generate = function(options) { }) .fail(function(err) { - console.log(err); + console.log(err.stack || err.message || err); return Q.reject(err); }) }; From e0d96b316390660557c1eac72314cdd659ba58e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 10:45:36 -0700 Subject: [PATCH 07/11] Fix links in templates --- lib/generate/generator_site.js | 7 ++++++- templates/includes/book/footer.html | 4 ++-- templates/includes/book/progress.html | 2 +- templates/includes/book/summary.html | 4 ++-- templates/layout.html | 4 ++-- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/generate/generator_site.js b/lib/generate/generator_site.js index 1cc86143a..6e1903da9 100644 --- a/lib/generate/generator_site.js +++ b/lib/generate/generator_site.js @@ -7,11 +7,16 @@ var fs = require("./fs"); var parse = require("../parse"); var BaseGenerator = require("./generator"); -// Init swig filter lines for returning the count of lines in a code section +// 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); diff --git a/templates/includes/book/footer.html b/templates/includes/book/footer.html index 48f4bf90f..1112b14a2 100644 --- a/templates/includes/book/footer.html +++ b/templates/includes/book/footer.html @@ -1,10 +1,10 @@
{% for p in progress.chapters %} - + {% endfor %}
\ No newline at end of file diff --git a/templates/includes/book/summary.html b/templates/includes/book/summary.html index d74fb1842..996ff3d72 100644 --- a/templates/includes/book/summary.html +++ b/templates/includes/book/summary.html @@ -16,7 +16,7 @@ {% for item in summary.chapters %}
  • {% if item.path %} - + {{ item.level }}) {{ item.title }} {% else %} @@ -27,7 +27,7 @@ {% for article in item.articles %}
  • {% if article.path %} - + {{ article.level }}) {{ article.title }} {% else %} diff --git a/templates/layout.html b/templates/layout.html index c0f52cb75..47184b3e5 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -13,10 +13,10 @@ {% if progress.current.next and progress.current.next.path %} - + {% endif %} {% if progress.current.prev and progress.current.prev.path %} - + {% endif %} From 0a6194340f56a3b640ee438972762fc72178c6ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 10:46:11 -0700 Subject: [PATCH 08/11] Remove file template.js --- lib/generate/index.js | 1 - lib/generate/template.js | 66 ---------------------------------------- 2 files changed, 67 deletions(-) delete mode 100644 lib/generate/template.js diff --git a/lib/generate/index.js b/lib/generate/index.js index b6782f078..15b1bef4a 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -5,7 +5,6 @@ var path = require("path"); var fs = require("./fs"); var parse = require("../parse"); -var template = require("./template"); var generators = { "site": require("./generator_site"), diff --git a/lib/generate/template.js b/lib/generate/template.js deleted file mode 100644 index d4e8cae25..000000000 --- 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 From 5125e04814e0f6bbd5533b5755d9a483f3a4ab73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 10:47:44 -0700 Subject: [PATCH 09/11] Update tests --- test/navigation.js | 64 +++++++++++++++++++++++----------------------- test/summary.js | 9 +++---- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/test/navigation.js b/test/navigation.js index 23d98ac7f..b0e5b98dc 100644 --- a/test/navigation.js +++ b/test/navigation.js @@ -13,57 +13,57 @@ var LEXED = summary(CONTENT); describe('Summary navigation', function() { it('should provide next & prev entries for a file', function() { var nav = navigation(LEXED, [ - 'README.html', - 'chapter-1/README.html', - 'chapter-1/ARTICLE1.html', - 'chapter-1/ARTICLE2.html', - 'chapter-2/README.html', + 'README.md', + 'chapter-1/README.md', + 'chapter-1/ARTICLE1.md', + 'chapter-1/ARTICLE2.md', + 'chapter-2/README.md', ]); // Make sure it found the files we gave it - assert(nav['README.html']); - assert(nav['chapter-1/README.html']); - assert(nav['chapter-1/ARTICLE1.html']); - assert(nav['chapter-1/ARTICLE2.html']); - assert(nav['chapter-2/README.html']); + assert(nav['README.md']); + assert(nav['chapter-1/README.md']); + assert(nav['chapter-1/ARTICLE1.md']); + assert(nav['chapter-1/ARTICLE2.md']); + assert(nav['chapter-2/README.md']); - assert.equal(nav['README.html'].prev, null); - assert.equal(nav['README.html'].next.path, 'chapter-1/README.html'); + assert.equal(nav['README.md'].prev, null); + assert.equal(nav['README.md'].next.path, 'chapter-1/README.md'); - assert.equal(nav['chapter-1/README.html'].prev.path, 'README.html'); - assert.equal(nav['chapter-1/README.html'].next.path, 'chapter-1/ARTICLE1.html'); + assert.equal(nav['chapter-1/README.md'].prev.path, 'README.md'); + assert.equal(nav['chapter-1/README.md'].next.path, 'chapter-1/ARTICLE1.md'); - assert.equal(nav['chapter-1/ARTICLE1.html'].prev.path, 'chapter-1/README.html'); - assert.equal(nav['chapter-1/ARTICLE1.html'].next.path, 'chapter-1/ARTICLE2.html'); + assert.equal(nav['chapter-1/ARTICLE1.md'].prev.path, 'chapter-1/README.md'); + assert.equal(nav['chapter-1/ARTICLE1.md'].next.path, 'chapter-1/ARTICLE2.md'); - assert.equal(nav['chapter-1/ARTICLE2.html'].prev.path, 'chapter-1/ARTICLE1.html'); - assert.equal(nav['chapter-1/ARTICLE2.html'].next.path, 'chapter-2/README.html'); + assert.equal(nav['chapter-1/ARTICLE2.md'].prev.path, 'chapter-1/ARTICLE1.md'); + assert.equal(nav['chapter-1/ARTICLE2.md'].next.path, 'chapter-2/README.md'); - assert.equal(nav['chapter-2/README.html'].prev.path, 'chapter-1/ARTICLE2.html'); - assert.equal(nav['chapter-2/README.html'].next.path, 'chapter-3/README.html'); + assert.equal(nav['chapter-2/README.md'].prev.path, 'chapter-1/ARTICLE2.md'); + assert.equal(nav['chapter-2/README.md'].next.path, 'chapter-3/README.md'); }); it('should give full tree, when not limited', function() { var nav = navigation(LEXED); - assert(nav['README.html']); - assert(nav['chapter-1/README.html']); - assert(nav['chapter-1/ARTICLE1.html']); - assert(nav['chapter-1/ARTICLE2.html']); - assert(nav['chapter-2/README.html']); - assert(nav['chapter-3/README.html']); + assert(nav['README.md']); + assert(nav['chapter-1/README.md']); + assert(nav['chapter-1/ARTICLE1.md']); + assert(nav['chapter-1/ARTICLE2.md']); + assert(nav['chapter-2/README.md']); + assert(nav['chapter-3/README.md']); }); it('should detect levels correctly', function() { var nav = navigation(LEXED); - assert.equal(nav['README.html'].level, '0'); - assert.equal(nav['chapter-1/README.html'].level, '1'); - assert.equal(nav['chapter-1/ARTICLE1.html'].level, '1.1'); - assert.equal(nav['chapter-1/ARTICLE2.html'].level, '1.2'); - assert.equal(nav['chapter-2/README.html'].level, '2'); - assert.equal(nav['chapter-3/README.html'].level, '3'); + assert.equal(nav['README.md'].level, '0'); + assert.equal(nav['chapter-1/README.md'].level, '1'); + assert.equal(nav['chapter-1/ARTICLE1.md'].level, '1.1'); + assert.equal(nav['chapter-1/ARTICLE2.md'].level, '1.2'); + assert.equal(nav['chapter-2/README.md'].level, '2'); + assert.equal(nav['chapter-3/README.md'].level, '3'); }); it('should not accept null paths', function() { diff --git a/test/summary.js b/test/summary.js index 51169d585..d1ef3dd39 100644 --- a/test/summary.js +++ b/test/summary.js @@ -37,12 +37,9 @@ describe('Summary parsing', function () { }); it('should normalize paths from .md to .html', function() { - assert.equal(LEXED.chapters[0].path,'chapter-1/README.html'); - assert.equal(LEXED.chapters[0]._path,'chapter-1/README.md'); - assert.equal(LEXED.chapters[1].path,'chapter-2/README.html'); - assert.equal(LEXED.chapters[1]._path,'chapter-2/README.md'); - assert.equal(LEXED.chapters[2].path,'chapter-3/README.html'); - assert.equal(LEXED.chapters[2]._path,'chapter-3/README.md'); + assert.equal(LEXED.chapters[0].path,'chapter-1/README.md'); + assert.equal(LEXED.chapters[1].path,'chapter-2/README.md'); + assert.equal(LEXED.chapters[2].path,'chapter-3/README.md'); }); it('should detect levels correctly', function() { From 26701a2a73a6ea82d350123458b0ba42bfe7d897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 10:50:59 -0700 Subject: [PATCH 10/11] Remove logs from generation --- lib/generate/generator.js | 2 -- lib/generate/generator_site.js | 3 +-- lib/generate/index.js | 7 +------ 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/lib/generate/generator.js b/lib/generate/generator.js index 216bf3e75..3fe080e90 100644 --- a/lib/generate/generator.js +++ b/lib/generate/generator.js @@ -11,7 +11,6 @@ BaseGenerator.prototype.convertFile = function(content, input) { }; BaseGenerator.prototype.transferFile = function(input) { - console.log("Copying", input); return fs.copy( path.join(this.options.input, input), path.join(this.options.output, input) @@ -19,7 +18,6 @@ BaseGenerator.prototype.transferFile = function(input) { }; BaseGenerator.prototype.transferFolder = function(input) { - console.log("Creating directory", input); return fs.mkdirp( path.join(this.options.output, input) ); diff --git a/lib/generate/generator_site.js b/lib/generate/generator_site.js index 6e1903da9..7d40a1a4b 100644 --- a/lib/generate/generator_site.js +++ b/lib/generate/generator_site.js @@ -36,8 +36,7 @@ Generator.prototype.convertFile = function(content, _input) { 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) || "."; - - console.log("Converting ", _input); + return Q() .then(function() { return parse.page(content, { diff --git a/lib/generate/index.js b/lib/generate/index.js index 15b1bef4a..95f325ce4 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -102,12 +102,7 @@ var generate = function(options) { // Finish gneration .then(function() { return generator.finish(); - }) - - .fail(function(err) { - console.log(err.stack || err.message || err); - return Q.reject(err); - }) + }); }; module.exports = { From 5cae311edf62e5c86edd9083a8d927730710c2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Fri, 4 Apr 2014 10:55:48 -0700 Subject: [PATCH 11/11] Log error in command line tool --- bin/gitbook.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/bin/gitbook.js b/bin/gitbook.js index d57e7852c..3abeafdd1 100755 --- a/bin/gitbook.js +++ b/bin/gitbook.js @@ -1,6 +1,7 @@ #! /usr/bin/env node // Requires +var Q = require('q'); var _ = require('lodash'); var path = require('path'); var prog = require('commander'); @@ -14,6 +15,10 @@ var generators = require("../lib/generate").generators; var utils = require('./utils'); +var logError = function(err) { + console.log(err.message || err); + return Q.reject(err); +}; // General options prog @@ -69,9 +74,7 @@ prog }) .then(function(output) { console.log("Successfuly built !"); - }, function(err) { - throw err; - }) + }, logError) .then(_.constant(outputDir)); }); @@ -88,15 +91,13 @@ prog .then(function(outputDir) { console.log(); console.log('Starting server ...'); - return utils.serveDir(outputDir, options.port); + return utils.serveDir(outputDir, options.port) + .fail(logError); }) .then(function() { console.log('Serving book on http://localhost:'+options.port); console.log(); console.log('Press CTRL+C to quit ...'); - }) - .fail(function(err) { - console.error(err); }); });