diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-05-02 16:12:07 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 12:32:17 +0100 |
commit | c3a417a9834ba3c25ceb20a3cb650fa0384c60d0 (patch) | |
tree | bdbffac34a15636265e834b9bf0c484054426135 | |
parent | 9e88003d7373e2e7efac862494a9a0db4847f617 (diff) | |
download | gitbook-c3a417a9834ba3c25ceb20a3cb650fa0384c60d0.zip gitbook-c3a417a9834ba3c25ceb20a3cb650fa0384c60d0.tar.gz gitbook-c3a417a9834ba3c25ceb20a3cb650fa0384c60d0.tar.bz2 |
Adapt for gitbook v3
-rw-r--r-- | packages/gitbook-html/lib/dom.js | 30 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/glossary.js | 7 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/langs.js | 12 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/page.js | 7 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/readme.js | 7 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/summary.js | 23 | ||||
-rw-r--r-- | packages/gitbook-html/lib/totext.js | 22 | ||||
-rw-r--r-- | packages/gitbook-html/test/helper.js | 2 | ||||
-rwxr-xr-x | packages/gitbook-html/test/langs.js | 4 | ||||
-rwxr-xr-x | packages/gitbook-html/test/summary.js | 16 |
10 files changed, 103 insertions, 27 deletions
diff --git a/packages/gitbook-html/lib/dom.js b/packages/gitbook-html/lib/dom.js index d8f7c84..819ced0 100644 --- a/packages/gitbook-html/lib/dom.js +++ b/packages/gitbook-html/lib/dom.js @@ -1,7 +1,12 @@ var _ = require('lodash'); var cheerio = require('cheerio'); -// Parse an HTML string and return its content +/** + 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(); @@ -9,13 +14,23 @@ function parse(html) { return $el.length > 0? $el : $; } -// Return main element +/** + 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 text node of an element +/** + Return text node of an element + + @param {cheerio.Node} + @return {String} +*/ function textNode($el) { return _.reduce($el.children, function(text, e) { if (e.type == 'text') text += e.data; @@ -23,8 +38,13 @@ function textNode($el) { }, ''); } -// Cleanup a dom -// Remove all divs +/** + 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() { var $div = $(this); diff --git a/packages/gitbook-html/lib/glossary.js b/packages/gitbook-html/lib/glossary.js index 89ff135..26787ab 100755 --- a/packages/gitbook-html/lib/glossary.js +++ b/packages/gitbook-html/lib/glossary.js @@ -1,7 +1,12 @@ var _ = require('lodash'); var dom = require('./dom'); -// HTML -> Glossary +/** + Parse an HTML content into a list of glossary entry + + @param {String} html + @return {Array} +*/ function parseGlossary(html) { var $ = dom.parse(html); diff --git a/packages/gitbook-html/lib/langs.js b/packages/gitbook-html/lib/langs.js index d337e0f..a06d3ee 100755 --- a/packages/gitbook-html/lib/langs.js +++ b/packages/gitbook-html/lib/langs.js @@ -1,10 +1,18 @@ var _ = require('lodash'); var parseSummary = require('./summary'); -// HTML -> Languages +/** + 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) return parts[0].articles; + if (parts.length > 0) { + return parts[0].articles; + } + return []; } diff --git a/packages/gitbook-html/lib/page.js b/packages/gitbook-html/lib/page.js index ae8923e..56d8984 100755 --- a/packages/gitbook-html/lib/page.js +++ b/packages/gitbook-html/lib/page.js @@ -1,7 +1,12 @@ var Q = require('q'); var _ = require('lodash'); -// HTML -> Page +/** + Parse content of a page + + @param {String} html + @return {Object} +*/ function parsePage(html) { return { content: html diff --git a/packages/gitbook-html/lib/readme.js b/packages/gitbook-html/lib/readme.js index 0d179ad..34e447e 100755 --- a/packages/gitbook-html/lib/readme.js +++ b/packages/gitbook-html/lib/readme.js @@ -1,7 +1,12 @@ var _ = require('lodash'); var dom = require('./dom'); -// HTML -> Readme +/** + Parse an HTML content into metadata about a readme + + @param {String} html + @return {Object} +*/ function parseReadme(html) { var $ = dom.parse(html); diff --git a/packages/gitbook-html/lib/summary.js b/packages/gitbook-html/lib/summary.js index 8d716d6..e55719b 100755 --- a/packages/gitbook-html/lib/summary.js +++ b/packages/gitbook-html/lib/summary.js @@ -3,10 +3,14 @@ var dom = require('./dom'); var SELECTOR_LIST = 'ol, ul'; var SELECTOR_LINK = '> a, p > a'; - var BL = '\n'; -// Find a list +/** + Find a list + + @param {cheerio.Node} + @return {cheerio.Node} +*/ function findList($parent) { var $container = $parent.children('.olist'); if ($container.length > 0) $parent = $container.first(); @@ -14,7 +18,13 @@ function findList($parent) { return $parent.children('ul, ol'); } -// Parse a ul list and return list of chapters recursvely +/** + Parse a ul list and return list of chapters recursvely + + @param {cheerio.Node} + @param {cheerio.DOM} + @return {Array} +*/ function parseList($ul, $) { var articles = []; @@ -44,7 +54,12 @@ function parseList($ul, $) { return articles; } -// HTML -> Summary +/** + 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($), $); diff --git a/packages/gitbook-html/lib/totext.js b/packages/gitbook-html/lib/totext.js index 49762d9..64d4898 100644 --- a/packages/gitbook-html/lib/totext.js +++ b/packages/gitbook-html/lib/totext.js @@ -1,5 +1,11 @@ var _ = require('lodash'); +/* + This class is extended by gitbook-markdown and gitbook-asciidoc + to generate back markdown/asciidoc from GitBook metadata. +*/ + + function ToText(markup) { _.extend(this, markup || {}); _.bindAll(this); @@ -14,6 +20,10 @@ ToText.prototype.onText = function(text) { return text; }; +ToText.prototype.onHR = function() { + return '<hr />'; +}; + // ---- TITLES ToText.prototype.onTitleStart = function(level) { @@ -126,7 +136,6 @@ ToText.prototype._summaryPart = function(part) { if (part.title) content += this.onTitleStart(2) + this.onText(part.title) + this.onTitleEnd(2); content += this._summaryArticles(part.articles); - content += this.onSection(); return content; }; @@ -136,8 +145,17 @@ ToText.prototype.summary = function(summary) { content += this.onTitleStart(1) + this.onText('Summary') + this.onTitleEnd(1); content += this.onSection(); - _.each(summary.parts, function(part) { + _.each(summary.parts, function(part, i) { + var next = summary.parts[i + 1]; + content += this._summaryPart(part); + + if (next && !next.title) { + content += this.onHR(); + } else { + content += this.onSection(); + } + }, this); return content; diff --git a/packages/gitbook-html/test/helper.js b/packages/gitbook-html/test/helper.js index 44cfb24..1e310f7 100644 --- a/packages/gitbook-html/test/helper.js +++ b/packages/gitbook-html/test/helper.js @@ -1,6 +1,6 @@ var assert = require("assert"); global.assertObjectsEqual = function(o1, o2) { - assert.equal(JSON.stringify(o1, null, 4), JSON.stringify(o2, null, 4)); + assert.equal(JSON.stringify(o1, null, 4), JSON.stringify(o2, null, 4)); }; diff --git a/packages/gitbook-html/test/langs.js b/packages/gitbook-html/test/langs.js index 6b5e00b..ab002a1 100755 --- a/packages/gitbook-html/test/langs.js +++ b/packages/gitbook-html/test/langs.js @@ -13,10 +13,10 @@ describe('Languages parsing', function () { }); it('should detect paths and titles', function() { - assert.equal(LEXED[0].path,'en/'); + assert.equal(LEXED[0].ref,'en/'); assert.equal(LEXED[0].title,'English'); - assert.equal(LEXED[1].path,'fr/'); + assert.equal(LEXED[1].ref,'fr/'); assert.equal(LEXED[1].title,'French'); }); diff --git a/packages/gitbook-html/test/summary.js b/packages/gitbook-html/test/summary.js index 02104cd..24d22c0 100755 --- a/packages/gitbook-html/test/summary.js +++ b/packages/gitbook-html/test/summary.js @@ -40,11 +40,11 @@ describe('Summary parsing', function () { }); it('should detect paths and titles', function() { - assert(PART.articles[0].path); - assert(PART.articles[1].path); - assert(PART.articles[2].path); - assert(PART.articles[3].path); - assert.equal(PART.articles[4].path, null); + assert(PART.articles[0].ref); + assert(PART.articles[1].ref); + assert(PART.articles[2].ref); + assert(PART.articles[3].ref); + assert.equal(PART.articles[4].ref, null); assert(PART.articles[0].title); assert(PART.articles[1].title); @@ -54,9 +54,9 @@ describe('Summary parsing', function () { }); it('should normalize paths from .md', function() { - assert.equal(PART.articles[0].path,'chapter-1/README.md'); - assert.equal(PART.articles[1].path,'chapter-2/README.md'); - assert.equal(PART.articles[2].path,'chapter-3/README.md'); + assert.equal(PART.articles[0].ref,'chapter-1/README.md'); + assert.equal(PART.articles[1].ref,'chapter-2/README.md'); + assert.equal(PART.articles[2].ref,'chapter-3/README.md'); }); it('should correctly convert it to text', function() { |