diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-26 09:41:26 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-26 09:41:26 +0100 |
commit | d3d64f636c859f7f01a64f7774cf70bd8ccdc562 (patch) | |
tree | 4f7731f37c3a793d187b0ab1cd77680e69534c6c /lib/parsers.js | |
parent | 4cb9cbb5ae3aa8f9211ffa3ac5e3d34232c0ca4f (diff) | |
parent | eef072693b17526347c37b66078a5059c71caa31 (diff) | |
download | gitbook-d3d64f636c859f7f01a64f7774cf70bd8ccdc562.zip gitbook-d3d64f636c859f7f01a64f7774cf70bd8ccdc562.tar.gz gitbook-d3d64f636c859f7f01a64f7774cf70bd8ccdc562.tar.bz2 |
Merge pull request #1109 from GitbookIO/3.0.0
Version 3.0.0
Diffstat (limited to 'lib/parsers.js')
-rw-r--r-- | lib/parsers.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/parsers.js b/lib/parsers.js new file mode 100644 index 0000000..6899865 --- /dev/null +++ b/lib/parsers.js @@ -0,0 +1,60 @@ +var _ = require('lodash'); +var path = require('path'); + +var markdownParser = require('gitbook-markdown'); +var asciidocParser = require('gitbook-asciidoc'); + +var Promise = require('./utils/promise'); + +// This list is ordered by priority of parsers to use +var PARSERS = [ + createParser(markdownParser, { + name: 'markdown', + extensions: ['.md', '.markdown', '.mdown'] + }), + createParser(asciidocParser, { + name: 'asciidoc', + extensions: ['.adoc', '.asciidoc'] + }) +]; + + +// Prepare and compose a parser +function createParser(parser, base) { + var nparser = base; + + nparser.glossary = Promise.wrapfn(parser.glossary); + nparser.glossary.toText = Promise.wrapfn(parser.glossary.toText); + + nparser.summary = Promise.wrapfn(parser.summary); + nparser.summary.toText = Promise.wrapfn(parser.summary.toText); + + nparser.langs = Promise.wrapfn(parser.langs); + nparser.langs.toText = Promise.wrapfn(parser.langs.toText); + + nparser.readme = Promise.wrapfn(parser.readme); + + nparser.page = Promise.wrapfn(parser.page); + nparser.page.prepare = Promise.wrapfn(parser.page.prepare || _.identity); + + return nparser; +} + +// Return a specific parser according to an extension +function getParser(ext) { + return _.find(PARSERS, function(input) { + return input.name == ext || _.contains(input.extensions, ext); + }); +} + +// Return parser for a file +function getParserForFile(filename) { + return getParser(path.extname(filename)); +} + +module.exports = { + all: PARSERS, + extensions: _.flatten(_.pluck(PARSERS, 'extensions')), + get: getParser, + getForFile: getParserForFile +}; |