gitbook-html
Advanced tools
Comparing version 1.1.0 to 1.2.0
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) { | ||
@@ -12,3 +17,8 @@ var $ = cheerio.load(html); | ||
// Return main element | ||
/** | ||
Return main element for a DOM | ||
@param {cheerio.DOM} | ||
@return {cheerio.Node} | ||
*/ | ||
function root($) { | ||
@@ -19,3 +29,8 @@ var $el = $('html, body, > div').first(); | ||
// Return text node of an element | ||
/** | ||
Return text node of an element | ||
@param {cheerio.Node} | ||
@return {String} | ||
*/ | ||
function textNode($el) { | ||
@@ -28,4 +43,9 @@ return _.reduce($el.children, function(text, e) { | ||
// 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, $) { | ||
@@ -32,0 +52,0 @@ $el.find('div').each(function() { |
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) { | ||
@@ -6,0 +11,0 @@ var $ = dom.parse(html); |
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 []; | ||
@@ -9,0 +17,0 @@ } |
var Q = require('q'); | ||
var _ = require('lodash'); | ||
// HTML -> Page | ||
/** | ||
Parse content of a page | ||
@param {String} html | ||
@return {Object} | ||
*/ | ||
function parsePage(html) { | ||
@@ -6,0 +11,0 @@ return { |
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) { | ||
@@ -6,0 +11,0 @@ var $ = dom.parse(html); |
@@ -6,6 +6,10 @@ var _ = require('lodash'); | ||
var SELECTOR_LINK = '> a, p > a'; | ||
var BL = '\n'; | ||
// Find a list | ||
/** | ||
Find a list | ||
@param {cheerio.Node} | ||
@return {cheerio.Node} | ||
*/ | ||
function findList($parent) { | ||
@@ -18,3 +22,9 @@ var $container = $parent.children('.olist'); | ||
// 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, $) { | ||
@@ -35,3 +45,3 @@ var articles = []; | ||
article.title = $a.first().text(); | ||
article.path = $a.attr('href').replace(/\\/g, '/').replace(/^\/+/, '') | ||
article.ref = $a.attr('href').replace(/\\/g, '/').replace(/^\/+/, '') | ||
} | ||
@@ -50,3 +60,8 @@ | ||
// HTML -> Summary | ||
/** | ||
Parse an HTML content into a tree of articles/parts | ||
@param {String} html | ||
@return {Object} | ||
*/ | ||
function parseSummary(html) { | ||
@@ -53,0 +68,0 @@ var $ = dom.parse(html); |
var _ = require('lodash'); | ||
/* | ||
This class is extended by gitbook-markdown and gitbook-asciidoc | ||
to generate back markdown/asciidoc from GitBook metadata. | ||
*/ | ||
function ToText(markup) { | ||
@@ -17,2 +23,6 @@ _.extend(this, markup || {}); | ||
ToText.prototype.onHR = function() { | ||
return '<hr />'; | ||
}; | ||
// ---- TITLES | ||
@@ -99,5 +109,5 @@ | ||
if (article.path) content += this.onLinkStart(article.path) | ||
if (article.ref) content += this.onLinkStart(article.ref) | ||
content += this.onText(article.title) | ||
if (article.path) content += this.onLinkEnd(article.path); | ||
if (article.ref) content += this.onLinkEnd(article.ref); | ||
content += this.onBL(); | ||
@@ -131,3 +141,2 @@ | ||
content += this._summaryArticles(part.articles); | ||
content += this.onSection(); | ||
@@ -142,4 +151,13 @@ return content; | ||
_.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); | ||
@@ -146,0 +164,0 @@ |
{ | ||
"name": "gitbook-html", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"homepage": "https://www.gitbook.com", | ||
@@ -5,0 +5,0 @@ "description": "Parse HTML content for gitbook", |
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)); | ||
}; | ||
@@ -16,6 +16,6 @@ var fs = require('fs'); | ||
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'); | ||
@@ -22,0 +22,0 @@ }); |
@@ -43,7 +43,7 @@ var fs = require('fs'); | ||
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); | ||
@@ -58,5 +58,5 @@ assert(PART.articles[0].title); | ||
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'); | ||
}); | ||
@@ -63,0 +63,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18725
478