summaryrefslogtreecommitdiffstats
path: root/lib/parsers.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-20 16:02:51 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-20 16:02:51 +0100
commita22539a91eea4101d244cdaeb5c61e31376ff4bc (patch)
treefb3cd337b3082134b797e51431ee40c3a7101ecc /lib/parsers.js
parent89378c3883140ce6e6c74e71651270f19b1ee441 (diff)
downloadgitbook-a22539a91eea4101d244cdaeb5c61e31376ff4bc.zip
gitbook-a22539a91eea4101d244cdaeb5c61e31376ff4bc.tar.gz
gitbook-a22539a91eea4101d244cdaeb5c61e31376ff4bc.tar.bz2
Add new gitbook-markdown and gitbook-asciidoc
Diffstat (limited to 'lib/parsers.js')
-rw-r--r--lib/parsers.js60
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
+};