summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/lib
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-html/lib')
-rw-r--r--packages/gitbook-html/lib/dom.js23
-rwxr-xr-xpackages/gitbook-html/lib/glossary.js38
-rwxr-xr-xpackages/gitbook-html/lib/index.js8
-rwxr-xr-xpackages/gitbook-html/lib/langs.js24
-rwxr-xr-xpackages/gitbook-html/lib/page.js9
-rwxr-xr-xpackages/gitbook-html/lib/readme.js16
-rwxr-xr-xpackages/gitbook-html/lib/summary.js86
7 files changed, 204 insertions, 0 deletions
diff --git a/packages/gitbook-html/lib/dom.js b/packages/gitbook-html/lib/dom.js
new file mode 100644
index 0000000..2c2eaf7
--- /dev/null
+++ b/packages/gitbook-html/lib/dom.js
@@ -0,0 +1,23 @@
+var _ = require('lodash');
+var cheerio = require('cheerio');
+
+// Parse an HTML string and return its content
+function parse(html) {
+ var $ = cheerio.load('<div>'+html+'</div>');
+ var $el = $('html, body').first();
+
+ return $el.length > 0? $el : $;
+}
+
+// Return text node of an element
+function textNode($el) {
+ return _.reduce($el.children, function(text, e) {
+ if (e.type == 'text') text += e.data;
+ return text;
+ }, '');
+}
+
+module.exports = {
+ parse: parse,
+ textNode: textNode
+};
diff --git a/packages/gitbook-html/lib/glossary.js b/packages/gitbook-html/lib/glossary.js
new file mode 100755
index 0000000..648ba1a
--- /dev/null
+++ b/packages/gitbook-html/lib/glossary.js
@@ -0,0 +1,38 @@
+var _ = require('lodash');
+var dom = require('./dom');
+
+// HTML -> Glossary
+function parseGlossary(html) {
+ var $ = dom.parse(html);
+
+ var entries = [];
+
+ $("h2").each(function() {
+ var $heading = $(this);
+ var $p = $heading.next();
+
+ var entry = {};
+
+ entry.name = $heading.text();
+ entry.description = $p.text();
+
+ entries.push(entry);
+ });
+
+ return entries;
+}
+
+// Glossary -> HTML
+function glossaryToText(glossary) {
+ var bl = '\n';
+
+ var body = _.map(glossary, function(entry) {
+ return '<h2>' + entry.name + '</h2>' + bl + bl
+ + '<p>' + entry.description + '</p>';
+ }).join(bl+bl);
+
+ return '<h1>Glossary</h1>'+bl+bl+body;
+}
+
+module.exports = parseGlossary;
+module.exports.toText = glossaryToText;
diff --git a/packages/gitbook-html/lib/index.js b/packages/gitbook-html/lib/index.js
new file mode 100755
index 0000000..a7c478c
--- /dev/null
+++ b/packages/gitbook-html/lib/index.js
@@ -0,0 +1,8 @@
+
+module.exports = {
+ summary: require("./summary"),
+ glossary: require("./glossary"),
+ langs: require("./langs"),
+ readme: require("./readme"),
+ page: require("./page")
+};
diff --git a/packages/gitbook-html/lib/langs.js b/packages/gitbook-html/lib/langs.js
new file mode 100755
index 0000000..1042dcb
--- /dev/null
+++ b/packages/gitbook-html/lib/langs.js
@@ -0,0 +1,24 @@
+var _ = require('lodash');
+var parseEntries = require('./summary').entries;
+
+// HTML -> Languages
+function parseLangs(content) {
+ return parseEntries(content);
+}
+
+// Languages -> HTML
+function langsToText(langs) {
+ var bl = '\n';
+ var content = '<h1>Languages</h1>'+bl+bl;
+
+ content += '<ul>' + bl;
+ _.each(langs, function(lang) {
+ content = content + ' <li><a href="'+lang.path+'">'+lang.title+'</a></li>'+bl;
+ });
+ content += '</ul>' + bl;
+
+ return content;
+}
+
+module.exports = parseLangs;
+module.exports.toText = langsToText;
diff --git a/packages/gitbook-html/lib/page.js b/packages/gitbook-html/lib/page.js
new file mode 100755
index 0000000..e687050
--- /dev/null
+++ b/packages/gitbook-html/lib/page.js
@@ -0,0 +1,9 @@
+var Q = require('q');
+var _ = require('lodash');
+
+// HTML -> HTML
+function parsePage(src) {
+ return src;
+}
+
+module.exports = parsePage;
diff --git a/packages/gitbook-html/lib/readme.js b/packages/gitbook-html/lib/readme.js
new file mode 100755
index 0000000..0d179ad
--- /dev/null
+++ b/packages/gitbook-html/lib/readme.js
@@ -0,0 +1,16 @@
+var _ = require('lodash');
+var dom = require('./dom');
+
+// HTML -> Readme
+function parseReadme(html) {
+ var $ = dom.parse(html);
+
+ return {
+ title: $('h1:first-child').text().trim(),
+ description: $('div.paragraph,p').first().text().trim()
+ };
+}
+
+
+// Exports
+module.exports = parseReadme;
diff --git a/packages/gitbook-html/lib/summary.js b/packages/gitbook-html/lib/summary.js
new file mode 100755
index 0000000..1e2d63d
--- /dev/null
+++ b/packages/gitbook-html/lib/summary.js
@@ -0,0 +1,86 @@
+var _ = require('lodash');
+var dom = require('./dom');
+
+
+// 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);
+
+ // Get text for the entry
+ var $p = $li.children('> p');
+ article.title = $p.text() || dom.textNode($li.get(0));
+
+ // Parse link
+ var $a = $li.find('> a, > p > a');
+ if ($a.length > 0) {
+ article.title = $a.first().text();
+ article.path = $a.attr('href').replace(/\\/g, '/').replace(/^\/+/, '')
+ }
+
+ // Sub articles
+ var $sub = $li.children('> .olist > ol, > ol, > ul');
+ article.articles = parseList($sub, $);
+
+ articles.push(article);
+ });
+
+ return articles;
+}
+
+// Return a list of entries in a div
+function parseEntries (html) {
+ var $ = dom.parse(html);
+ var chapters = parseList($("> ol, > ul").first(), $);
+ return chapters;
+}
+
+// HTML -> Summary
+function parseSummary(src) {
+ var chapters = parseEntries(src);
+
+ return {
+ chapters: chapters
+ };
+}
+
+// Summary -> HTML
+function summaryToText(summary) {
+ var bl = '\n';
+
+ var _base = function(article) {
+ if (article.path) {
+ return '<a href="'+article.path+'">'+article.title+'</a>';
+ } else {
+ return article.title;
+ }
+ };
+
+ var convertArticle = function(article, d) {
+ var content = Array(d+2).join(' ') + '<li>' + _base(article);
+
+ if (article.articles.length > 0) {
+ content += convertArticles(article.articles, d);
+ }
+ return content + '</li>' + bl;
+ };
+
+ var convertArticles = function(articles, d) {
+ var content = '<ul>' + bl;
+ _.each(articles, function(_article) {
+ content += convertArticle(_article, d + 1);
+ });
+ return content + '<ul>' + bl;
+ }
+
+ return '<h1>Summary</h1>'+ bl+bl + convertArticles(summary.chapters, 0) + bl;
+};
+
+
+module.exports = parseSummary;
+module.exports.entries = parseEntries;
+module.exports.toText = summaryToText;