diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-12-22 13:12:16 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 13:12:16 +0100 |
commit | 97f2c333a87b9d939b5a7dc2884590c971b53291 (patch) | |
tree | a22824b02d84a89e59c458c8af7d3494561d43f6 /packages/gitbook-html/lib | |
parent | 627e6dd866f77ff497a21f0b706490b82e40ea0e (diff) | |
download | gitbook-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')
-rw-r--r-- | packages/gitbook-html/lib/dom.js | 50 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/glossary.js | 20 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/index.js | 56 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/langs.js | 14 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/page.js | 13 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/readme.js | 16 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/summary.js | 72 | ||||
-rw-r--r-- | packages/gitbook-html/lib/totext.js | 353 |
8 files changed, 328 insertions, 266 deletions
diff --git a/packages/gitbook-html/lib/dom.js b/packages/gitbook-html/lib/dom.js index 819ced0..96d370e 100644 --- a/packages/gitbook-html/lib/dom.js +++ b/packages/gitbook-html/lib/dom.js @@ -1,52 +1,49 @@ -var _ = require('lodash'); +'use strict'; + var cheerio = require('cheerio'); /** - Parse an HTML string and return its content - - @param {String} - @return {cheerio.DOM} -*/ + * Parse an HTML string and return its content. + * @param {String} + * @return {cheerio.DOM} + */ function parse(html) { var $ = cheerio.load(html); var $el = $('html, body').first(); - return $el.length > 0? $el : $; + return $el.length > 0 ? $el : $; } /** - Return main element for a DOM - - @param {cheerio.DOM} - @return {cheerio.Node} -*/ + * Return main element for a DOM. + * @param {cheerio.DOM} + * @return {cheerio.Node} + */ function root($) { var $el = $('html, body, > div').first(); - return $el.length > 0? $el : $.root(); + return $el.length > 0 ? $el : $.root(); } /** - Return text node of an element - - @param {cheerio.Node} - @return {String} -*/ + * Return text node of an element. + * @param {cheerio.Node} + * @return {String} + */ function textNode($el) { - return _.reduce($el.children, function(text, e) { + return $el.children.reduce(function (text, e) { if (e.type == 'text') text += e.data; return text; }, ''); } /** - Cleanup a DOM by removing all useless divs - - @param {cheerio.Node} - @param {cheerio.DOM} - @return {cheerio.Node} -*/ + * Cleanup a DOM by removing all useless divs. + * @param {cheerio.Node} + * @param {cheerio.DOM} + * @return {cheerio.Node} + */ function cleanup($el, $) { - $el.find('div').each(function() { + $el.find('div').each(function () { var $div = $(this); cleanup($div, $); @@ -62,3 +59,4 @@ module.exports = { root: root, cleanup: cleanup }; +//# sourceMappingURL=dom.js.map
\ No newline at end of file diff --git a/packages/gitbook-html/lib/glossary.js b/packages/gitbook-html/lib/glossary.js index 26787ab..c8c5bf5 100755 --- a/packages/gitbook-html/lib/glossary.js +++ b/packages/gitbook-html/lib/glossary.js @@ -1,21 +1,22 @@ -var _ = require('lodash'); +'use strict'; + var dom = require('./dom'); /** - Parse an HTML content into a list of glossary entry - - @param {String} html - @return {Array} -*/ + * Parse an HTML content into a list of glossary entry. + * + * @param {String} html + * @return {Array} entries + */ function parseGlossary(html) { var $ = dom.parse(html); var entries = []; - $('h2').each(function() { + $('h2').each(function () { var $heading = $(this); - var $next = $heading.next() - var $p = $next.is('p')? $next.first() : $next.find('p').first(); + var $next = $heading.next(); + var $p = $next.is('p') ? $next.first() : $next.find('p').first(); var entry = {}; @@ -29,3 +30,4 @@ function parseGlossary(html) { } module.exports = parseGlossary; +//# sourceMappingURL=glossary.js.map
\ No newline at end of file diff --git a/packages/gitbook-html/lib/index.js b/packages/gitbook-html/lib/index.js index 2658914..b6fa18c 100755 --- a/packages/gitbook-html/lib/index.js +++ b/packages/gitbook-html/lib/index.js @@ -1,4 +1,5 @@ -var _ = require('lodash'); +'use strict'; + var ToText = require('./totext'); var htmlParser = { @@ -11,22 +12,26 @@ var htmlParser = { // Compose a function with a transform function for the first argument only function compose(toHTML, fn) { - return function() { - var args = _.toArray(arguments); - args[0] = toHTML(args[0]); + return function () { + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } - return fn.apply(null, args); - } + args[0] = toHTML(args[0]); + return fn.apply(undefined, args); + }; } -// Create a GitBook parser from an HTML converter -function createParser(toHTML, toText) { - if (_.isFunction(toHTML)) { - toHTML = { - inline: toHTML, - block: toHTML - }; - } +/** + * Create a GitBook parser from an HTML converter. + * @param {Object} toHTML + * {Function} [toHTML.inline] + * {Function} [toHTML.block] + * @param {Object} toText + * @return {[type]} [description] + */ +function createParser(toHTML) { + var toText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var parser = { summary: compose(toHTML.block, htmlParser.summary), @@ -38,12 +43,27 @@ function createParser(toHTML, toText) { }; var _toText = new ToText(toText); - parser.summary.toText =_toText.summary; - parser.langs.toText =_toText.langs; - parser.glossary.toText =_toText.glossary; + + parser.summary.toText = function (summary) { + return _toText.summary(summary); + }; + parser.langs.toText = function (langs) { + return _toText.langs(langs); + }; + parser.glossary.toText = function (glossary) { + return _toText.glossary(glossary); + }; return parser; } -module.exports = createParser(_.identity); +module.exports = createParser({ + block: function block(html) { + return html; + }, + inline: function inline(html) { + return html; + } +}); module.exports.createParser = createParser; +//# sourceMappingURL=index.js.map
\ No newline at end of file diff --git a/packages/gitbook-html/lib/langs.js b/packages/gitbook-html/lib/langs.js index a06d3ee..f799b50 100755 --- a/packages/gitbook-html/lib/langs.js +++ b/packages/gitbook-html/lib/langs.js @@ -1,12 +1,12 @@ -var _ = require('lodash'); +'use strict'; + var parseSummary = require('./summary'); /** - Parse an HTML content into a list of language - - @param {String} html - @return {Array} -*/ + * Parse an HTML content into a list of language. + * @param {String} html + * @return {Array} + */ function parseLangs(content) { var parts = parseSummary(content).parts; if (parts.length > 0) { @@ -17,4 +17,4 @@ function parseLangs(content) { } module.exports = parseLangs; - +//# sourceMappingURL=langs.js.map
\ No newline at end of file diff --git a/packages/gitbook-html/lib/page.js b/packages/gitbook-html/lib/page.js index 56d8984..25fd637 100755 --- a/packages/gitbook-html/lib/page.js +++ b/packages/gitbook-html/lib/page.js @@ -1,12 +1,10 @@ -var Q = require('q'); -var _ = require('lodash'); +"use strict"; /** - Parse content of a page - - @param {String} html - @return {Object} -*/ + * Parse content of a page. + * @param {String} html + * @return {Object} + */ function parsePage(html) { return { content: html @@ -14,3 +12,4 @@ function parsePage(html) { } module.exports = parsePage; +//# sourceMappingURL=page.js.map
\ No newline at end of file diff --git a/packages/gitbook-html/lib/readme.js b/packages/gitbook-html/lib/readme.js index 34e447e..771b525 100755 --- a/packages/gitbook-html/lib/readme.js +++ b/packages/gitbook-html/lib/readme.js @@ -1,12 +1,13 @@ -var _ = require('lodash'); +'use strict'; + var dom = require('./dom'); /** - Parse an HTML content into metadata about a readme - - @param {String} html - @return {Object} -*/ + * Parse an HTML content into metadata about a readme + * + * @param {String} html + * @return {Object} + */ function parseReadme(html) { var $ = dom.parse(html); @@ -16,6 +17,5 @@ function parseReadme(html) { }; } - -// Exports module.exports = parseReadme; +//# sourceMappingURL=readme.js.map
\ No newline at end of file diff --git a/packages/gitbook-html/lib/summary.js b/packages/gitbook-html/lib/summary.js index 4b263c9..a7d05a3 100755 --- a/packages/gitbook-html/lib/summary.js +++ b/packages/gitbook-html/lib/summary.js @@ -1,18 +1,16 @@ -var _ = require('lodash'); +'use strict'; + var dom = require('./dom'); var SELECTOR_LIST = 'ol, ul'; var SELECTOR_LINK = '> a, p > a'; var SELECTOR_PART = 'h2, h3, h4'; -var BL = '\n'; - /** - Find a list - - @param {cheerio.Node} - @return {cheerio.Node} -*/ + * Find a list. + * @param {cheerio.Node} + * @return {cheerio.Node} + */ function findList($parent) { var $container = $parent.children('.olist'); if ($container.length > 0) $parent = $container.first(); @@ -21,22 +19,21 @@ function findList($parent) { } /** - Parse a ul list and return list of chapters recursvely - - @param {cheerio.Node} - @param {cheerio.DOM} - @return {Array} -*/ + * Parse a ul list and return list of chapters recursvely. + * @param {cheerio.Node} + * @param {cheerio.DOM} + * @return {Array} + */ function parseList($ul, $) { var articles = []; - $ul.children('li').each(function() { + $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))).trim(); + article.title = ($p.text() || dom.textNode($li.get(0))).trim(); // Parse link var $a = $li.find(SELECTOR_LINK); @@ -57,11 +54,10 @@ function parseList($ul, $) { } /** - Find all parts and their corresponding lists - - @param {cheerio.Node} - @param {cheerio.DOM} - @return {Array<{title: String, list: cheerio.Node}>} + * Find all parts and their corresponding lists. + * @param {cheerio.Node} + * @param {cheerio.DOM} + * @return {Array<{title: String, list: cheerio.Node}>} */ function findParts($parent, $) { // Find parts and lists @@ -82,8 +78,8 @@ function findParts($parent, $) { title: getPartTitle(el, $), list: null }; - - } else { // It is a list + } else { + // It is a list if (previousPart !== null) { previousPart.list = el; } else { @@ -106,32 +102,29 @@ function findParts($parent, $) { } /** - True if the element is a part - - @param el - @return {Boolean} - */ + * True if the element is a part. + * @param el + * @return {Boolean} + */ function isPartNode(el) { return SELECTOR_PART.indexOf(el.name) !== -1; } /** - Parse the title of a part element - - @param el - @param {cheerio.DOM} $ - @return {String} + * Parse the title of a part element. + * @param el + * @param {cheerio.DOM} $ + * @return {String} */ function getPartTitle(el, $) { return $(el).text().trim(); } /** - Parse an HTML content into a tree of articles/parts - - @param {String} html - @return {Object} -*/ + * Parse an HTML content into a tree of articles/parts. + * @param {String} html + * @return {Object} + */ function parseSummary(html) { var $ = dom.parse(html); var $root = dom.cleanup(dom.root($), $); @@ -140,7 +133,7 @@ function parseSummary(html) { // Parse each list var parsedParts = []; - var part; + var part = void 0; for (var i = 0; i < parts.length; ++i) { part = parts[i]; parsedParts.push({ @@ -155,3 +148,4 @@ function parseSummary(html) { } module.exports = parseSummary; +//# sourceMappingURL=summary.js.map
\ No newline at end of file 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 |