mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-20 17:43:24 +00:00
Complete json generator
This commit is contained in:
+2
-2
@@ -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,
|
||||
|
||||
+12
-10
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
+29
-16
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user