summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/lib/totext.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-12-22 13:12:16 +0100
committerSamy Pessé <samypesse@gmail.com>2016-12-22 13:12:16 +0100
commit97f2c333a87b9d939b5a7dc2884590c971b53291 (patch)
treea22824b02d84a89e59c458c8af7d3494561d43f6 /packages/gitbook-html/lib/totext.js
parent627e6dd866f77ff497a21f0b706490b82e40ea0e (diff)
downloadgitbook-97f2c333a87b9d939b5a7dc2884590c971b53291.zip
gitbook-97f2c333a87b9d939b5a7dc2884590c971b53291.tar.gz
gitbook-97f2c333a87b9d939b5a7dc2884590c971b53291.tar.bz2
Import and adapt gitbook-html
Refactor to remove lodash and q as dependencies
Diffstat (limited to 'packages/gitbook-html/lib/totext.js')
-rw-r--r--packages/gitbook-html/lib/totext.js353
1 files changed, 201 insertions, 152 deletions
diff --git a/packages/gitbook-html/lib/totext.js b/packages/gitbook-html/lib/totext.js
index 368d62e..0baa48a 100644
--- a/packages/gitbook-html/lib/totext.js
+++ b/packages/gitbook-html/lib/totext.js
@@ -1,181 +1,230 @@
-var _ = require('lodash');
+'use strict';
+
+var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
+
+function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*
This class is extended by gitbook-markdown and gitbook-asciidoc
to generate back markdown/asciidoc from GitBook metadata.
*/
+var ToText = function () {
+ function ToText(markup) {
+ _classCallCheck(this, ToText);
-function ToText(markup) {
- if (!(this instanceof ToText)) {
- return new ToText(markup);
+ Object.assign(this, markup);
}
- _.extend(this, markup || {});
- _.bindAll(this, _.functionsIn(this));
-};
-
-// Break line
-ToText.prototype.onBL = function() {
- return '\n';
-};
-
-ToText.prototype.onText = function(text) {
- return text;
-};
-
-ToText.prototype.onHR = function() {
- return '<hr />';
-};
-
-// ---- 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 that = this;
- var content = '';
-
- content += that.onTitleStart(1) + that.onText('Glossary') + that.onTitleEnd(1);
- content += that.onSection();
-
- _.each(glossary, function(entry) {
- content += that.onTitleStart(2) + that.onText(entry.name) + that.onTitleEnd(2);
- content += that.onParagraphStart();
- content += that.onText(entry.description);
- content += that.onParagraphEnd();
- content += that.onSection();
- });
-
- return content;
-};
-
-// ------ SUMMARY
-
-ToText.prototype._summaryArticle = function(article, level) {
- var content = '';
-
- content += this.onListItemStart(level);
-
- if (article.ref) content += this.onLinkStart(article.ref)
- content += this.onText(article.title)
- if (article.ref) content += this.onLinkEnd(article.ref);
- content += this.onBL();
-
- if (article.articles && article.articles.length > 0) {
- content += this._summaryArticles(article.articles, level + 1);
- }
+ // Break line
- content += this.onListItemEnd(level);
- return content;
-};
-ToText.prototype._summaryArticles = function(articles, level) {
- var that = this;
- var content = '';
+ _createClass(ToText, [{
+ key: 'onBL',
+ value: function onBL() {
+ return '\n';
+ }
+ }, {
+ key: 'onText',
+ value: function onText(text) {
+ return text;
+ }
+ }, {
+ key: 'onHR',
+ value: function onHR() {
+ return '<hr />';
+ }
- level = level || 0;
+ // ---- TITLES
- content += that.onListStart(level);
- _.each(articles, function(article) {
- content += that._summaryArticle(article, level);
- });
- content += that.onListEnd(level);
+ }, {
+ key: 'onTitleStart',
+ value: function onTitleStart(level) {
+ return '<h' + level + '>';
+ }
+ }, {
+ key: 'onTitleEnd',
+ value: function onTitleEnd(level) {
+ return '</h' + level + '>';
+ }
- return content;
-};
-ToText.prototype._summaryPart = function(part) {
- var content = '';
+ // ---- PARAGRAPHS / SECTIONS
- if (part.title) content += this.onTitleStart(2) + this.onText(part.title) + this.onTitleEnd(2);
+ }, {
+ key: 'onParagraphStart',
+ value: function onParagraphStart() {
+ return '<p>';
+ }
+ }, {
+ key: 'onParagraphEnd',
+ value: function onParagraphEnd() {
+ return '</p>';
+ }
+ }, {
+ key: 'onSection',
+ value: function onSection() {
+ return this.onBL();
+ }
- content += this._summaryArticles(part.articles);
+ // ---- LINKS
- return content;
-};
+ }, {
+ key: 'onLinkStart',
+ value: function onLinkStart(href) {
+ return '<a href="' + href + '">';
+ }
+ }, {
+ key: 'onLinkEnd',
+ value: function onLinkEnd(href) {
+ return '</a>';
+ }
-ToText.prototype.summary = function(summary) {
- var that = this;
- var content = '';
+ // ---- LISTS
- content += that.onTitleStart(1) + that.onText('Summary') + that.onTitleEnd(1);
- content += that.onSection();
+ }, {
+ key: 'onListItemStart',
+ value: function onListItemStart(level) {
+ return this._spaces((level + 1) * 4) + '<li>';
+ }
+ }, {
+ key: 'onListItemEnd',
+ value: function onListItemEnd(level) {
+ return this._spaces((level + 1) * 4) + '</li>' + this.onBL();
+ }
+ }, {
+ key: 'onListStart',
+ value: function onListStart(level) {
+ return this._spaces(level * 4) + '<ul>' + this.onBL();
+ }
+ }, {
+ key: 'onListEnd',
+ value: function onListEnd(level) {
+ return this._spaces(level * 4) + '</ul>' + this.onBL();
+ }
+
+ // ------ LANGS
- _.each(summary.parts, function(part, i) {
- var next = summary.parts[i + 1];
+ }, {
+ key: 'langs',
+ value: function langs(languages) {
+ var content = '';
+ content += this.onTitleStart(1) + this.onText('Languages') + this.onTitleEnd(1);
+ content += this.onSection();
- content += that._summaryPart(part);
+ content += this._summaryArticles(languages);
- if (next && !next.title) {
- content += that.onBL() + that.onHR() + that.onBL();
- } else {
- content += that.onSection();
+ return content;
}
- });
+ // ------ GLOSSARY
- return content;
-};
+ }, {
+ key: 'glossary',
+ value: function glossary(_glossary) {
+ var _this = this;
-// ---- Utilities
+ var content = '';
-ToText.prototype._spaces = function(n, s) {
- return Array(n + 1).join(s || ' ');
-}
+ content += this.onTitleStart(1) + this.onText('Glossary') + this.onTitleEnd(1);
+ content += this.onSection();
-module.exports = ToText;
+ _glossary.forEach(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();
+ });
+
+ return content;
+ }
+
+ // ------ SUMMARY
+
+ }, {
+ key: '_summaryArticle',
+ value: function _summaryArticle(article, level) {
+ var content = '';
+
+ content += this.onListItemStart(level);
+
+ if (article.ref) content += this.onLinkStart(article.ref);
+ content += this.onText(article.title);
+ if (article.ref) content += this.onLinkEnd(article.ref);
+ content += this.onBL();
+ if (article.articles && article.articles.length > 0) {
+ content += this._summaryArticles(article.articles, level + 1);
+ }
+
+ content += this.onListItemEnd(level);
+
+ return content;
+ }
+ }, {
+ key: '_summaryArticles',
+ value: function _summaryArticles(articles, level) {
+ var _this2 = this;
+
+ var content = '';
+
+ level = level || 0;
+
+ content += this.onListStart(level);
+ articles.forEach(function (article) {
+ content += _this2._summaryArticle(article, level);
+ });
+ content += this.onListEnd(level);
+
+ return content;
+ }
+ }, {
+ key: '_summaryPart',
+ value: function _summaryPart(part) {
+ var content = '';
+
+ if (part.title) content += this.onTitleStart(2) + this.onText(part.title) + this.onTitleEnd(2);
+
+ content += this._summaryArticles(part.articles);
+
+ return content;
+ }
+ }, {
+ key: 'summary',
+ value: function summary(_summary) {
+ var _this3 = this;
+
+ var content = '';
+
+ content += this.onTitleStart(1) + this.onText('Summary') + this.onTitleEnd(1);
+ content += this.onSection();
+
+ _summary.parts.forEach(function (part, i) {
+ var next = _summary.parts[i + 1];
+
+ content += _this3._summaryPart(part);
+
+ if (next && !next.title) {
+ content += _this3.onBL() + _this3.onHR() + _this3.onBL();
+ } else {
+ content += _this3.onSection();
+ }
+ });
+
+ return content;
+ }
+
+ // ---- Utilities
+
+ }, {
+ key: '_spaces',
+ value: function _spaces(n, s) {
+ return Array(n + 1).join(s || ' ');
+ }
+ }]);
+
+ return ToText;
+}();
+
+module.exports = ToText;
+//# sourceMappingURL=totext.js.map \ No newline at end of file