summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-20 15:30:27 +0100
committerSamy Pessé <samypesse@gmail.com>2016-12-22 12:32:14 +0100
commitc1c17a273ae45c653a0c7d2cc64fabbb5b42e224 (patch)
tree526eff188d5799762191b46d2dfb8009b6c57398
parent21fe8961d7ac91a49c49c28052fc2de4f251f409 (diff)
downloadgitbook-c1c17a273ae45c653a0c7d2cc64fabbb5b42e224.zip
gitbook-c1c17a273ae45c653a0c7d2cc64fabbb5b42e224.tar.gz
gitbook-c1c17a273ae45c653a0c7d2cc64fabbb5b42e224.tar.bz2
Improve gitbook -> text
-rwxr-xr-xpackages/gitbook-html/lib/glossary.js13
-rwxr-xr-xpackages/gitbook-html/lib/index.js26
-rwxr-xr-xpackages/gitbook-html/lib/langs.js16
-rwxr-xr-xpackages/gitbook-html/lib/summary.js51
-rw-r--r--packages/gitbook-html/lib/totext.js151
5 files changed, 171 insertions, 86 deletions
diff --git a/packages/gitbook-html/lib/glossary.js b/packages/gitbook-html/lib/glossary.js
index 9d3799b..89ff135 100755
--- a/packages/gitbook-html/lib/glossary.js
+++ b/packages/gitbook-html/lib/glossary.js
@@ -23,17 +23,4 @@ function parseGlossary(html) {
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
index 0e67c94..1c2b0e0 100755
--- a/packages/gitbook-html/lib/index.js
+++ b/packages/gitbook-html/lib/index.js
@@ -1,11 +1,12 @@
var _ = require('lodash');
+var ToText = require('./totext');
var htmlParser = {
- summary: require("./summary"),
- glossary: require("./glossary"),
- langs: require("./langs"),
- readme: require("./readme"),
- page: require("./page")
+ summary: require('./summary'),
+ glossary: require('./glossary'),
+ langs: require('./langs'),
+ readme: require('./readme'),
+ page: require('./page')
};
// Compose a function with a transform function for the first args
@@ -19,15 +20,22 @@ function compose(toHTML, fn) {
}
// Create a GitBook parser
-function createParser(toHTML) {
- return {
+function createParser(toHTML, toText) {
+ var parser = {
summary: compose(toHTML, htmlParser.summary),
glossary: compose(toHTML, htmlParser.glossary),
langs: compose(toHTML, htmlParser.langs),
readme: compose(toHTML, htmlParser.readme),
page: compose(toHTML, htmlParser.page)
- }
+ };
+
+ var _toText = new ToText(toText);
+ parser.summary.toText =_toText.summary;
+ parser.langs.toText =_toText.langs;
+ parser.glossary.toText =_toText.glossary;
+
+ return parser;
}
-module.exports = htmlParser;
+module.exports = createParser(_.identity);
module.exports.createParser = createParser;
diff --git a/packages/gitbook-html/lib/langs.js b/packages/gitbook-html/lib/langs.js
index 270a9f6..035091b 100755
--- a/packages/gitbook-html/lib/langs.js
+++ b/packages/gitbook-html/lib/langs.js
@@ -6,19 +6,5 @@ function parseLangs(content) {
return parseSummary(content).parts[0].articles;
}
-// 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/summary.js b/packages/gitbook-html/lib/summary.js
index 607062e..9b6b688 100755
--- a/packages/gitbook-html/lib/summary.js
+++ b/packages/gitbook-html/lib/summary.js
@@ -24,7 +24,7 @@ function parseList($ul, $) {
// Get text for the entry
var $p = $li.children('p');
- article.title = $p.text() || dom.textNode($li.get(0));
+ article.title = ($p.text() || dom.textNode($li.get(0))).trim();
// Parse link
var $a = $li.find(SELECTOR_LINK);
@@ -37,6 +37,7 @@ function parseList($ul, $) {
var $sub = findList($li);
article.articles = parseList($sub, $);
+ if (!article.title) return;
articles.push(article);
});
@@ -64,52 +65,4 @@ function parseSummary(html) {
};
}
-// Summary -> HTML
-function textPrefix(d) {
- return Array(d*4).join(' ');
-}
-
-function articleToText(article, d) {
- var prefix = textPrefix(d);
- var content = prefix + '<li>';
-
- if (article.path) {
- content += '<a href="'+article.path+'">'+article.title+'</a>';
- } else {
- content += article.title;
- }
-
- if (article.articles.length > 0) {
- content += BL + articlesToText(article.articles, d) + prefix;
- }
- content += '</li>' + BL;
-
- return content;
-}
-
-function articlesToText(articles, d) {
- var prefix = textPrefix(d);
- var content = prefix + '<ul>' + BL;
- _.each(articles, function(_article) {
- content += articleToText(_article, d + 1);
- });
- return content + '</ul>' + BL;
-}
-
-function partsToText(part) {
- return articlesToText(part.articles, 0) + BL + BL;
-}
-
-function summaryToText(summary) {
- var content = '<h1>Summary</h1>' + BL;
-
- _.each(summary.parts, function(part) {
- content += partsToText(part);
- });
-
- return content + BL;
-};
-
-
module.exports = parseSummary;
-module.exports.toText = summaryToText;
diff --git a/packages/gitbook-html/lib/totext.js b/packages/gitbook-html/lib/totext.js
new file mode 100644
index 0000000..13b1cd8
--- /dev/null
+++ b/packages/gitbook-html/lib/totext.js
@@ -0,0 +1,151 @@
+var _ = require('lodash');
+
+function ToText(markup) {
+ _.extend(this, markup || {});
+ _.bindAll(this);
+};
+
+// Break line
+ToText.prototype.onBL = function() {
+ return '\n';
+};
+
+ToText.prototype.onText = function(text) {
+ return text;
+};
+
+// ---- TITLES
+
+ToText.prototype.onTitleStart = function(level) {
+ return '<h'+level+'>';
+};
+ToText.prototype.onTitleEnd = function(level) {
+ return '</h'+level+'>';
+};
+
+// ---- PARAGRAPHS / SECTIONS
+ToText.prototype.onParagraphStart = function() {
+ return '<p>';
+};
+ToText.prototype.onParagraphEnd = function() {
+ return '</p>';
+};
+
+
+ToText.prototype.onSection = function() {
+ return this.onBL();
+};
+
+// ---- LINKS
+ToText.prototype.onLinkStart = function(href) {
+ return '<a href="' + href + '">';
+};
+ToText.prototype.onLinkEnd = function(href) {
+ return '</a>';
+};
+
+// ---- LISTS
+ToText.prototype.onListItemStart = function(level) {
+ return this._spaces((level + 1) * 4) + '<li>';
+};
+ToText.prototype.onListItemEnd = function(level) {
+ return this._spaces((level + 1) * 4) + '</li>' + this.onBL();
+};
+ToText.prototype.onListStart = function(level) {
+ return this._spaces(level * 4) + '<ul>' + this.onBL();
+};
+ToText.prototype.onListEnd = function(level) {
+ return this._spaces(level * 4) + '</ul>' + this.onBL();
+};
+
+// ------ LANGS
+
+ToText.prototype.langs = function(languages) {
+ var content = '';
+ content += this.onTitleStart(1) + this.onText('Languages') + this.onTitleEnd(1);
+ content += this.onSection();
+
+ content += this._summaryArticles(languages);
+
+ return content;
+};
+
+// ------ GLOSSARY
+
+ToText.prototype.glossary = function(glossary) {
+ var content = '';
+ content += this.onTitleStart(1) + this.onText('Glossary') + this.onTitleEnd(1);
+ content += this.onSection();
+
+ _.each(glossary, function(entry) {
+ content += this.onTitleStart(2) + this.onText(entry.name) + this.onTitleEnd(2);
+ content += this.onParagraphStart();
+ content += this.onText(entry.description);
+ content += this.onParagraphEnd();
+ content += this.onSection();
+ }, this);
+
+ return content;
+};
+
+// ------ SUMMARY
+
+ToText.prototype._summaryArticle = function(article, level) {
+ var content = '';
+
+ content += this.onListItemStart(level);
+
+ if (article.path) content += this.onLinkStart(article.path)
+ content += this.onText(article.title)
+ if (article.path) content += this.onLinkEnd(article.path);
+ content += this.onBL();
+
+ if (article.articles && article.articles.length > 0) {
+ content += this._summaryArticles(article.articles, level + 1);
+ }
+
+ content += this.onListItemEnd(level);
+
+ return content;
+};
+ToText.prototype._summaryArticles = function(articles, level) {
+ var content = '';
+ level = level || 0;
+
+ content += this.onListStart(level);
+ _.each(articles, function(article) {
+ content += this._summaryArticle(article, level);
+ }, this);
+ content += this.onListEnd(level);
+
+ return content;
+};
+ToText.prototype._summaryPart = function(part) {
+ var content = '';
+
+ content += this._summaryArticles(part.articles);
+ content += this.onSection();
+
+ return content;
+};
+
+ToText.prototype.summary = function(summary) {
+ var content = '';
+ content += this.onTitleStart(1) + this.onText('Summary') + this.onTitleEnd(1);
+ content += this.onSection();
+
+ _.each(summary.parts, function(part) {
+ content += this._summaryPart(part);
+ }, this);
+
+ return content;
+};
+
+// ---- Utilities
+
+ToText.prototype._spaces = function(n, s) {
+ return Array(n + 1).join(s || ' ');
+}
+
+module.exports = ToText;
+