summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/cli/watch.js3
-rw-r--r--lib/models/file.js2
-rw-r--r--lib/models/glossary.js2
-rw-r--r--lib/models/parser.js113
-rw-r--r--lib/models/summary.js2
-rw-r--r--lib/output/generatePage.js4
-rw-r--r--lib/parse/parseStructureFile.js15
-rw-r--r--lib/parsers.js79
8 files changed, 168 insertions, 52 deletions
diff --git a/lib/cli/watch.js b/lib/cli/watch.js
index 0d1ab17..14434ab 100644
--- a/lib/cli/watch.js
+++ b/lib/cli/watch.js
@@ -1,4 +1,3 @@
-var _ = require('lodash');
var path = require('path');
var chokidar = require('chokidar');
@@ -20,7 +19,7 @@ function watch(dir) {
];
// Watch all parsable files
- _.each(parsers.extensions, function(ext) {
+ parsers.extensions.forEach(function(ext) {
toWatch.push('**/*'+ext);
});
diff --git a/lib/models/file.js b/lib/models/file.js
index d1726a7..8ddd4af 100644
--- a/lib/models/file.js
+++ b/lib/models/file.js
@@ -36,7 +36,7 @@ File.prototype.exists = function() {
File.prototype.getType = function() {
var parser = this.getParser();
if (parser) {
- return parser.name;
+ return parser.getName();
} else {
return undefined;
}
diff --git a/lib/models/glossary.js b/lib/models/glossary.js
index bb4407d..0033248 100644
--- a/lib/models/glossary.js
+++ b/lib/models/glossary.js
@@ -48,7 +48,7 @@ Glossary.prototype.toText = function(parser) {
});
}
- return parser.glossary.toText(entries.toJS());
+ return parser.renderGlossary(entries.toJS());
};
diff --git a/lib/models/parser.js b/lib/models/parser.js
new file mode 100644
index 0000000..e776582
--- /dev/null
+++ b/lib/models/parser.js
@@ -0,0 +1,113 @@
+var Immutable = require('immutable');
+var Promise = require('../utils/promise');
+
+var Parser = Immutable.Record({
+ name: String(),
+
+ // List of extensions that can be processed using this parser
+ extensions: Immutable.List(),
+
+ // Parsing functions
+ readme: Function(),
+ langs: Function(),
+ summary: Function(),
+ glossary: Function(),
+ page: Function(),
+ inline: Function()
+});
+
+Parser.prototype.getName = function() {
+ return this.get('name');
+};
+
+Parser.prototype.getExtensions = function() {
+ return this.get('extensions');
+};
+
+// PARSE
+
+Parser.prototype.parseReadme = function(content) {
+ var readme = this.get('readme');
+ return Promise(readme(content));
+};
+
+Parser.prototype.parseSummary = function(content) {
+ var summary = this.get('summary');
+ return Promise(summary(content));
+};
+
+Parser.prototype.parseGlossary = function(content) {
+ var glossary = this.get('glossary');
+ return Promise(glossary(content));
+};
+
+Parser.prototype.preparePage = function(content) {
+ var page = this.get('page');
+ return Promise(page.prepare(content));
+};
+
+Parser.prototype.parsePage = function(content) {
+ var page = this.get('page');
+ return Promise(page(content));
+};
+
+Parser.prototype.parseLanguages = function(content) {
+ var langs = this.get('langs');
+ return Promise(langs(content));
+};
+
+Parser.prototype.parseInline = function(content) {
+ var inline = this.get('inline');
+ return Promise(inline(content));
+};
+
+// TO TEXT
+
+Parser.prototype.renderLanguages = function(content) {
+ var langs = this.get('langs');
+ return Promise(langs.toText(content));
+};
+
+Parser.prototype.renderSummary = function(content) {
+ var summary = this.get('summary');
+ return Promise(summary.toText(content));
+};
+
+Parser.prototype.renderGlossary = function(content) {
+ var glossary = this.get('glossary');
+ return Promise(glossary.toText(content));
+};
+
+/**
+ Test if this parser matches an extension
+
+ @param {String} ext
+ @return {Boolean}
+*/
+Parser.prototype.matchExtension = function(ext) {
+ var exts = this.getExtensions();
+ return exts.includes(ext.toLowerCase());
+};
+
+/**
+ Create a new parser using a module (gitbook-markdown, etc)
+
+ @param {String} name
+ @param {Array<String>} extensions
+ @param {Object} module
+ @return {Parser}
+*/
+Parser.create = function(name, extensions, module) {
+ return new Parser({
+ name: name,
+ extensions: Immutable.List(extensions),
+ readme: module.readme,
+ langs: module.langs,
+ summary: module.summary,
+ glossary: module.glossary,
+ page: module.page,
+ inline: module.inline
+ });
+};
+
+module.exports = Parser;
diff --git a/lib/models/summary.js b/lib/models/summary.js
index 5314bb0..9f1701e 100644
--- a/lib/models/summary.js
+++ b/lib/models/summary.js
@@ -146,7 +146,7 @@ Summary.prototype.toText = function(parser) {
});
}
- return parser.summary.toText({
+ return parser.renderSummary({
parts: parts.toJS()
});
};
diff --git a/lib/output/generatePage.js b/lib/output/generatePage.js
index a93d4b0..27b4eb1 100644
--- a/lib/output/generatePage.js
+++ b/lib/output/generatePage.js
@@ -39,7 +39,7 @@ function generatePage(output, page) {
// Escape code blocks with raw tags
.then(function(currentPage) {
- return parser.page.prepare(currentPage.getContent());
+ return parser.preparePage(currentPage.getContent());
})
// Render templating syntax
@@ -48,7 +48,7 @@ function generatePage(output, page) {
})
// Render page using parser (markdown -> HTML)
- .then(parser.page).get('content')
+ .then(parser.parsePage.bind(parser)).get('content')
// Post processing for templating syntax
.then(function(content) {
diff --git a/lib/parse/parseStructureFile.js b/lib/parse/parseStructureFile.js
index bdb97db..fe8c935 100644
--- a/lib/parse/parseStructureFile.js
+++ b/lib/parse/parseStructureFile.js
@@ -24,9 +24,22 @@ function parseFile(fs, file, type) {
return fs.readAsString(filepath)
.then(function(content) {
+ if (type === 'readme') {
+ return parser.parseReadme(content);
+ } else if (type === 'glossary') {
+ return parser.parseGlossary(content);
+ } else if (type === 'summary') {
+ return parser.parseSummary(content);
+ } else if (type === 'langs') {
+ return parser.parseLanguages(content);
+ } else {
+ throw new Error('Parsing invalid type "' + type + '"');
+ }
+ })
+ .then(function(result) {
return [
file,
- parser[type](content)
+ result
];
});
}
diff --git a/lib/parsers.js b/lib/parsers.js
index a27ddd0..1c9ba4c 100644
--- a/lib/parsers.js
+++ b/lib/parsers.js
@@ -1,69 +1,60 @@
-var _ = require('lodash');
var path = require('path');
+var Immutable = require('immutable');
var markdownParser = require('gitbook-markdown');
var asciidocParser = require('gitbook-asciidoc');
-var Promise = require('./utils/promise');
+var Parser = require('./models/parser');
// 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);
+var parsers = Immutable.List([
+ Parser.create('markdown', ['.md', '.markdown', '.mdown'], markdownParser),
+ Parser.create('asciidoc', ['.adoc', '.asciidoc'], asciidocParser)
+]);
- nparser.page = Promise.wrapfn(parser.page);
- nparser.page.prepare = Promise.wrapfn(parser.page.prepare || _.identity);
+/**
+ Return a specific parser by its name
- nparser.inline = Promise.wrapfn(parser.inline);
-
- return nparser;
-}
-
-// Return a specific parser
+ @param {String} name
+ @return {Parser|undefined}
+*/
function getParser(name) {
- return _.find(PARSERS, {
- name: name
+ return parsers.find(function(parser) {
+ return parser.getName() === name;
});
}
-// Return a specific parser according to an extension
+/**
+ Return a specific parser according to an extension
+
+ @param {String} ext
+ @return {Parser|undefined}
+*/
function getParserByExt(ext) {
- return _.find(PARSERS, function(input) {
- return input.name == ext || _.contains(input.extensions, ext);
+ return parsers.find(function(parser) {
+ return parser.matchExtension(ext);
});
}
-// Return parser for a file
+/**
+ Return parser for a file
+
+ @param {String} ext
+ @return {Parser|undefined}
+*/
function getParserForFile(filename) {
return getParser(path.extname(filename));
}
+// List all parsable extensions
+var extensions = parsers
+ .map(function(parser) {
+ return parser.getExtensions();
+ })
+ .flatten();
+
module.exports = {
- all: PARSERS,
- extensions: _.flatten(_.pluck(PARSERS, 'extensions')),
+ extensions: extensions,
get: getParser,
getByExt: getParserByExt,
getForFile: getParserForFile