Add method renderInline and renderBlock to book

This commit is contained in:
Samy Pessé
2016-03-18 11:10:58 +01:00
parent bc0c8dbcf3
commit 82d8c3e2d9
5 changed files with 33 additions and 6 deletions
+18 -1
View File
@@ -320,7 +320,7 @@ Book.prototype.findParsableFile = function(filename) {
if (!realFilepath) return null;
return {
parser: parsers.get(ext),
parser: parsers.getByExt(ext),
path: realFilepath
};
});
@@ -359,6 +359,23 @@ Book.prototype.isInLanguageBook = function(filename) {
});
};
// ----- Parser Methods
// Render a markup string in inline mode
Book.prototype.renderInline = function(type, src) {
var parser = parsers.get(type);
return parser.inline(src)
.get('content');
};
// Render a markup string in block mode
Book.prototype.renderBlock = function(type, src) {
var parser = parsers.get(type);
return parser.page(src)
.get('content');
};
// ----- DEPRECATED METHODS
Book.prototype.contentLink = error.deprecateMethod(function(s) {
+1 -1
View File
@@ -41,7 +41,7 @@ function Page(book, filename) {
// Can we parse it?
extension = path.extname(this.path);
this.parser = parsers.get(extension);
this.parser = parsers.getByExt(extension);
if (!this.parser) throw error.ParsingError(new Error('Can\'t parse file "'+this.path+'"'));
this.type = this.parser.name;
+11 -1
View File
@@ -37,11 +37,20 @@ function createParser(parser, base) {
nparser.page = Promise.wrapfn(parser.page);
nparser.page.prepare = Promise.wrapfn(parser.page.prepare || _.identity);
nparser.inline = Promise.wrapfn(parser.inline);
return nparser;
}
// Return a specific parser
function getParser(name) {
return _.find(PARSERS, {
name: name
});
}
// Return a specific parser according to an extension
function getParser(ext) {
function getParserByExt(ext) {
return _.find(PARSERS, function(input) {
return input.name == ext || _.contains(input.extensions, ext);
});
@@ -56,5 +65,6 @@ module.exports = {
all: PARSERS,
extensions: _.flatten(_.pluck(PARSERS, 'extensions')),
get: getParser,
getByExt: getParserByExt,
getForFile: getParserForFile
};
+1 -1
View File
@@ -80,7 +80,7 @@ TemplateEngine.prototype.bindContext = function(func) {
// Interpolate a string content to replace shortcuts according to the filetype
TemplateEngine.prototype.interpolate = function(filepath, source) {
var parser = parsers.get(path.extname(filepath));
var parser = parsers.getByExt(path.extname(filepath));
var type = parser? parser.name : null;
return this.applyShortcuts(type, source);
+2 -2
View File
@@ -19,8 +19,8 @@
"escape-string-regexp": "1.0.5",
"eslint": "^2.2.0",
"front-matter": "2.0.6",
"gitbook-asciidoc": "1.0.2",
"gitbook-markdown": "1.0.3",
"gitbook-asciidoc": "1.1.0",
"gitbook-markdown": "1.2.0",
"gitbook-plugin-fontsettings": "1.0.2",
"gitbook-plugin-highlight": "2.0.0",
"gitbook-plugin-livereload": "0.0.1",