diff options
Diffstat (limited to 'lib')
-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 |
4 files changed, 31 insertions, 4 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); |