diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-03-18 11:10:58 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-03-18 11:10:58 +0100 |
commit | 82d8c3e2d954f06fb97644ae0934dbd4c3df8cb3 (patch) | |
tree | e63cf2c4d5ff7cfb8f820213e10173e88330eea9 | |
parent | bc0c8dbcf3ef060d99cafb27b9a414a3009c4a5f (diff) | |
download | gitbook-82d8c3e2d954f06fb97644ae0934dbd4c3df8cb3.zip gitbook-82d8c3e2d954f06fb97644ae0934dbd4c3df8cb3.tar.gz gitbook-82d8c3e2d954f06fb97644ae0934dbd4c3df8cb3.tar.bz2 |
Add method renderInline and renderBlock to book
-rw-r--r-- | lib/book.js | 19 | ||||
-rw-r--r-- | lib/page/index.js | 2 | ||||
-rw-r--r-- | lib/parsers.js | 12 | ||||
-rw-r--r-- | lib/template/index.js | 2 | ||||
-rw-r--r-- | package.json | 4 |
5 files changed, 33 insertions, 6 deletions
diff --git a/lib/book.js b/lib/book.js index dac73f8..82f4440 100644 --- a/lib/book.js +++ b/lib/book.js @@ -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) { diff --git a/lib/page/index.js b/lib/page/index.js index 0377f35..bcd83e6 100644 --- a/lib/page/index.js +++ b/lib/page/index.js @@ -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; diff --git a/lib/parsers.js b/lib/parsers.js index 6899865..a27ddd0 100644 --- a/lib/parsers.js +++ b/lib/parsers.js @@ -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 }; diff --git a/lib/template/index.js b/lib/template/index.js index 078dd6c..5af2089 100644 --- a/lib/template/index.js +++ b/lib/template/index.js @@ -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); diff --git a/package.json b/package.json index 98ea0bb..9b45a44 100644 --- a/package.json +++ b/package.json @@ -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", |