summaryrefslogtreecommitdiffstats
path: root/lib/parsers
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-01-27 10:24:06 +0100
committerSamy Pessé <samypesse@gmail.com>2016-01-27 10:24:06 +0100
commitf305d57ab7702c3ca10fd6e32366d19e524ee1f0 (patch)
tree381ccb09c7cedc72cdf8ad6c98b681b72f4d1bc0 /lib/parsers
parent877f2e477b010f9f37a9044606f110a90f077680 (diff)
downloadgitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.zip
gitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.tar.gz
gitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.tar.bz2
Add more classes structures
Diffstat (limited to 'lib/parsers')
-rw-r--r--lib/parsers/asciidoc.js27
-rw-r--r--lib/parsers/html.js90
-rw-r--r--lib/parsers/index.js11
-rw-r--r--lib/parsers/markdown.js6
4 files changed, 134 insertions, 0 deletions
diff --git a/lib/parsers/asciidoc.js b/lib/parsers/asciidoc.js
new file mode 100644
index 0000000..84e619d
--- /dev/null
+++ b/lib/parsers/asciidoc.js
@@ -0,0 +1,27 @@
+var Asciidoctor = require('asciidoctor.js');
+var htmlParser = require('./html');
+
+var asciidoctor = Asciidoctor();
+var opal = asciidoctor.Opal;
+
+var processor = null;
+var useExtensions = true;
+
+if (useExtensions) {
+ processor = asciidoctor.Asciidoctor(true);
+} else {
+ processor = asciidoctor.Asciidoctor();
+}
+
+
+// Convert asciidoc to HTML
+function asciidocToHTML(content) {
+ var options = opal.hash2(['attributes'], {'attributes': 'showtitle'});
+ return processor.$convert(content, options);
+}
+
+
+module.exports = htmlParser.inherits({
+ extensions: ['.adoc', '.asciidoc'],
+ toHTML: asciidocToHTML
+});
diff --git a/lib/parsers/html.js b/lib/parsers/html.js
new file mode 100644
index 0000000..8f4ed34
--- /dev/null
+++ b/lib/parsers/html.js
@@ -0,0 +1,90 @@
+var _ = require('lodash');
+var cheerio = require('cheerio');
+
+// Parse summary and returns a list of sections
+function parseSummary(html) {
+ var sections = [];
+ var $ = cheerio.load(html);
+
+ // Find main container
+ var $body = getContainer($);
+
+ // Extract sections, and parse
+ var $lists = $body.find('> ul, > ol');
+
+ $lists.each(function() {
+ sections.push({
+ articles: parseList($(this), $)
+ });
+ });
+
+ return sections;
+}
+
+// Parse readme and extract title, description
+function parseReadme(html) {
+ var $ = cheerio.load(html);
+
+ // Find main container
+ var $body = getContainer($);
+
+ return {
+ title: $body.find('h1:first-child').text().trim(),
+ description: $body.find('div.paragraph').first().text().trim()
+ };
+}
+
+// Return a page container (html, body tag or directly the root element)
+function getContainer($) {
+ var $body = $('body, html').first();
+ if (!$body) $body = $;
+
+ return $body;
+}
+
+// Parse a ul list and return list of chapters recursvely
+function parseList($ul, $) {
+ var articles = [];
+
+ $ul.children('li').each(function() {
+ var article = {};
+
+ var $li = $(this);
+
+ var $text = $li.find('> p, > span');
+ var $a = $li.find('> a, > p a, > span a');
+
+ article.title = $text.text();
+ if ($a.length > 0) {
+ article.title = $a.first().text();
+ article.ref = $a.attr('href');
+ }
+
+ // Inner list, with children article
+ var $sub = $li.find('> ol, > ul, > .olist > ol');
+ article.articles = parseList($sub, $);
+
+ articles.push(article);
+ });
+
+ return articles;
+}
+
+
+// Inherit from the html parser
+function inherits(opts) {
+ var parser = _.defaults(opts, {
+ toHTML: _.identity
+ });
+
+ parser.readme = _.compose(opts.toHTML, parseReadme);
+ parser.summary = _.compose(opts.toHTML, parseSummary);
+
+ return parser;
+}
+
+
+module.exports = inherits({
+ extensions: ['.html']
+});
+module.exports.inherits = inherits;
diff --git a/lib/parsers/index.js b/lib/parsers/index.js
new file mode 100644
index 0000000..d650386
--- /dev/null
+++ b/lib/parsers/index.js
@@ -0,0 +1,11 @@
+
+var PARSERS = {
+ html: require('./html'),
+ markdown: require('./markdown'),
+ asciidoc: require('./asciidoc')
+};
+
+
+module.exports = {
+
+};
diff --git a/lib/parsers/markdown.js b/lib/parsers/markdown.js
new file mode 100644
index 0000000..aac2858
--- /dev/null
+++ b/lib/parsers/markdown.js
@@ -0,0 +1,6 @@
+
+
+
+module.exports = {
+ extensions: ['.md', '.markdown', '.mdown']
+};