mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-22 18:43:29 +00:00
Add class BaseGenerator
This commit is contained in:
@@ -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;
|
||||
@@ -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;
|
||||
+20
-47
@@ -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();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user