diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-20 15:31:29 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 11:46:22 +0100 |
commit | 147679a42257660d70a9ef6ac6216fabf1d67582 (patch) | |
tree | 4dfd4a809c8351b9eea4ebfcc222878a30bec0ad | |
parent | d7b5c6a14d823cd067c7309240326d18a495674a (diff) | |
download | gitbook-147679a42257660d70a9ef6ac6216fabf1d67582.zip gitbook-147679a42257660d70a9ef6ac6216fabf1d67582.tar.gz gitbook-147679a42257660d70a9ef6ac6216fabf1d67582.tar.bz2 |
Use gitbook-html as base parser
-rwxr-xr-x | packages/gitbook-asciidoc/lib/glossary.js | 39 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/lib/index.js | 11 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/lib/langs.js | 21 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/lib/page.js | 17 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/lib/readme.js | 18 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/lib/summary.js | 83 | ||||
-rw-r--r-- | packages/gitbook-asciidoc/lib/toasciidoc.js | 43 | ||||
-rw-r--r-- | packages/gitbook-asciidoc/lib/tohtml.js | 13 | ||||
-rw-r--r-- | packages/gitbook-asciidoc/lib/utils/convert.js | 21 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/package.json | 7 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/test/glossary.js | 10 | ||||
-rw-r--r-- | packages/gitbook-asciidoc/test/helper.js | 2 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/test/langs.js | 10 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/test/page.js | 18 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/test/readme.js | 10 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/test/summary.js | 54 |
16 files changed, 123 insertions, 254 deletions
diff --git a/packages/gitbook-asciidoc/lib/glossary.js b/packages/gitbook-asciidoc/lib/glossary.js deleted file mode 100755 index 90ec61f..0000000 --- a/packages/gitbook-asciidoc/lib/glossary.js +++ /dev/null @@ -1,39 +0,0 @@ -var _ = require('lodash'); -var cheerio = require('cheerio'); - -var convert = require('./utils/convert'); - -function parseGlossary(src) { - var html = convert(src); - var $ = cheerio.load(html); - - var entries = []; - - $("h2").each(function() { - var $h2 = $(this); - var $p = $h2.next().find("p"); - - var entry = {}; - - entry.name = $h2.text(); - entry.description = $p.text(); - - entries.push(entry); - }); - - return entries; -} - - -function glossaryToText(glossary) { - var bl = "\n"; - - var body = _.map(glossary, function(entry) { - return "== "+entry.name+bl+bl+entry.description; - }).join(bl+bl); - - return "= Glossary"+bl+bl+body; -} - -module.exports = parseGlossary; -module.exports.toText = glossaryToText; diff --git a/packages/gitbook-asciidoc/lib/index.js b/packages/gitbook-asciidoc/lib/index.js index a7c478c..f9314ba 100755 --- a/packages/gitbook-asciidoc/lib/index.js +++ b/packages/gitbook-asciidoc/lib/index.js @@ -1,8 +1,5 @@ +var htmlParser = require('gitbook-html'); +var toHTML = require('./tohtml'); +var toAsciidoc = require('./toasciidoc'); -module.exports = { - summary: require("./summary"), - glossary: require("./glossary"), - langs: require("./langs"), - readme: require("./readme"), - page: require("./page") -}; +module.exports = htmlParser.createParser(toHTML, toAsciidoc); diff --git a/packages/gitbook-asciidoc/lib/langs.js b/packages/gitbook-asciidoc/lib/langs.js deleted file mode 100755 index d42f5dc..0000000 --- a/packages/gitbook-asciidoc/lib/langs.js +++ /dev/null @@ -1,21 +0,0 @@ -var _ = require("lodash"); -var parseEntries = require("./summary").entries; - - -var parseLangs = function(content) { - return parseEntries(content); -}; - -function langsToText(langs) { - var bl = "\n"; - var content = "= Languages"+bl+bl; - - _.each(langs, function(lang) { - content = content + ". link:"+lang.path+"["+lang.title+"]"+bl; - }); - - return content; -} - -module.exports = parseLangs; -module.exports.toText = langsToText; diff --git a/packages/gitbook-asciidoc/lib/page.js b/packages/gitbook-asciidoc/lib/page.js deleted file mode 100755 index 2bcfe61..0000000 --- a/packages/gitbook-asciidoc/lib/page.js +++ /dev/null @@ -1,17 +0,0 @@ -var Q = require('q'); -var _ = require('lodash'); - -var convert = require('./utils/convert'); - -function parsePage(src) { - return { - sections: [ - { - type: "normal", - content: convert(src) - } - ] - }; -} - -module.exports = parsePage; diff --git a/packages/gitbook-asciidoc/lib/readme.js b/packages/gitbook-asciidoc/lib/readme.js deleted file mode 100755 index bdcd3d3..0000000 --- a/packages/gitbook-asciidoc/lib/readme.js +++ /dev/null @@ -1,18 +0,0 @@ -var _ = require('lodash'); -var cheerio = require('cheerio'); - -var convert = require('./utils/convert'); - -function parseReadme(src) { - var html = convert(src); - var $ = cheerio.load(html); - - return { - title: $("h1:first-child").text().trim(), - description: $("div.paragraph").first().text().trim() - }; -} - - -// Exports -module.exports = parseReadme; diff --git a/packages/gitbook-asciidoc/lib/summary.js b/packages/gitbook-asciidoc/lib/summary.js deleted file mode 100755 index 2c9ada0..0000000 --- a/packages/gitbook-asciidoc/lib/summary.js +++ /dev/null @@ -1,83 +0,0 @@ -var _ = require('lodash'); -var cheerio = require('cheerio'); - -var convert = require('./utils/convert'); - - -// parse a ul list and return list of chapters recursvely -function parseList($ul, $) { - var articles = []; - - $ul.children("li").each(function() { - var article = {}; - - var $li = $(this); - var $p = $li.children("p"); - - article.title = $p.text(); - - // Parse link - var $a = $p.children("a"); - if ($a.length > 0) { - article.title = $a.first().text(); - article.path = $a.attr("href").replace(/\\/g, '/').replace(/^\/+/, '') - } - - // Sub articles - var $sub = $li.children(".olist").children("ol"); - article.articles = parseList($sub, $); - - articles.push(article); - }); - - return articles; -} - -function parseSummary(src) { - var chapters = parseEntries(src); - - return { - chapters: chapters - }; -} - -function parseEntries (src) { - var html = convert(src); - var $ = cheerio.load(html); - - var chapters = parseList($("ol").first(), $); - return chapters; -} - -function summaryToText(summary) { - var bl = "\n"; - var content = "= Summary"+bl+bl; - - var _base = function(article) { - if (article.path) { - return "link:"+article.path+"["+article.title+"]"; - } else { - return article.title; - } - }; - - var convertArticle = function(article, d) { - content = content + Array(d+2).join(".") + " " + _base(article)+bl; - _.each(article.articles, function(_article) { - convertArticle(_article, d + 1); - }); - }; - - _.each(summary.chapters, function(chapter) { - convertArticle(chapter, 0); - }); - - content = content+bl; - - return content; -}; - - -module.exports = parseSummary; -module.exports.entries = parseEntries; -module.exports.toText = summaryToText; diff --git a/packages/gitbook-asciidoc/lib/toasciidoc.js b/packages/gitbook-asciidoc/lib/toasciidoc.js new file mode 100644 index 0000000..cb32921 --- /dev/null +++ b/packages/gitbook-asciidoc/lib/toasciidoc.js @@ -0,0 +1,43 @@ + +// Return N time a string +function ns(s, n) { + return Array(n + 1).join(s); +} + +module.exports = { + onTitleStart: function(level) { + return ns('=', level) + ' '; + }, + onTitleEnd: function(level) { + return this.onBL(); + }, + + onParagraphStart: function() { + return this.onSection(); + }, + onParagraphEnd: function() { + return this.onSection(); + }, + + onLinkStart: function(href) { + return 'link:' + href + '['; + }, + onLinkEnd: function() { + return ']'; + }, + + onListStart: function(level) { + return ''; + }, + onListEnd: function() { + return ''; + }, + + onListItemStart: function(level) { + return ns('.', level + 1) + ' '; + }, + onListItemEnd: function() { + return ''; + }, +}; + diff --git a/packages/gitbook-asciidoc/lib/tohtml.js b/packages/gitbook-asciidoc/lib/tohtml.js new file mode 100644 index 0000000..80cbb89 --- /dev/null +++ b/packages/gitbook-asciidoc/lib/tohtml.js @@ -0,0 +1,13 @@ +var asciidoctor = require('asciidoctor.js')(); +var opal = asciidoctor.Opal; +var processor = asciidoctor.Asciidoctor(true); + + +function asciidocToHTML(content) { + var options = opal.hash2(['attributes'], {'attributes': 'showtitle'}); + + var html = processor.$convert(content, options); + return html; +}; + +module.exports = asciidocToHTML; diff --git a/packages/gitbook-asciidoc/lib/utils/convert.js b/packages/gitbook-asciidoc/lib/utils/convert.js deleted file mode 100644 index 8a19638..0000000 --- a/packages/gitbook-asciidoc/lib/utils/convert.js +++ /dev/null @@ -1,21 +0,0 @@ -var asciidoctor = require('asciidoctor.js')(); -var opal = asciidoctor.Opal; - -var processor = null; -var useExtensions = true; - -if (useExtensions) { - processor = asciidoctor.Asciidoctor(true); -} else { - processor = asciidoctor.Asciidoctor(); -} - - -var convert = function(content) { - var options = opal.hash2(['attributes'], {'attributes': 'showtitle'}); - - var html = processor.$convert(content, options); - return html; -}; - -module.exports = convert; diff --git a/packages/gitbook-asciidoc/package.json b/packages/gitbook-asciidoc/package.json index 7e62bf4..05e18b4 100755 --- a/packages/gitbook-asciidoc/package.json +++ b/packages/gitbook-asciidoc/package.json @@ -5,16 +5,15 @@ "description": "Parse AsciiDoc content for gitbook", "main": "lib/index.js", "dependencies": { - "q": "^1.1.2", "lodash": "^3.2.0", - "asciidoctor.js": "1.5.3-preview.1", - "cheerio": "^0.19.0" + "asciidoctor.js": "1.5.5-1", + "gitbook-html": "1.0.0" }, "devDependencies": { "mocha": "2.3.2" }, "scripts": { - "test": "export TESTING=true; mocha --reporter list" + "test": "export TESTING=true; mocha --reporter list --bail" }, "repository": { "type": "git", diff --git a/packages/gitbook-asciidoc/test/glossary.js b/packages/gitbook-asciidoc/test/glossary.js index 529bfba..abef367 100755 --- a/packages/gitbook-asciidoc/test/glossary.js +++ b/packages/gitbook-asciidoc/test/glossary.js @@ -4,10 +4,14 @@ var assert = require('assert'); var glossary = require('../').glossary; -var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/GLOSSARY.adoc'), 'utf8'); -var LEXED = glossary(CONTENT); - describe('Glossary parsing', function () { + var LEXED; + + before(function() { + var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/GLOSSARY.adoc'), 'utf8'); + LEXED = glossary(CONTENT); + }); + it('should only get heading + paragraph pairs', function() { assert.equal(LEXED.length, 5); }); diff --git a/packages/gitbook-asciidoc/test/helper.js b/packages/gitbook-asciidoc/test/helper.js index 44cfb24..1e310f7 100644 --- a/packages/gitbook-asciidoc/test/helper.js +++ b/packages/gitbook-asciidoc/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-asciidoc/test/langs.js b/packages/gitbook-asciidoc/test/langs.js index 178c3cc..94111e3 100755 --- a/packages/gitbook-asciidoc/test/langs.js +++ b/packages/gitbook-asciidoc/test/langs.js @@ -4,10 +4,14 @@ var assert = require('assert'); var langs = require('../').langs; -var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/LANGS.adoc'), 'utf8'); -var LEXED = langs(CONTENT); - describe('Languages parsing', function () { + var LEXED; + + before(function() { + var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/LANGS.adoc'), 'utf8'); + LEXED = langs(CONTENT); + }); + it('should detect paths and titles', function() { assert.equal(LEXED[0].path,'en/'); assert.equal(LEXED[0].title,'English'); diff --git a/packages/gitbook-asciidoc/test/page.js b/packages/gitbook-asciidoc/test/page.js index 9579725..3a28c5f 100755 --- a/packages/gitbook-asciidoc/test/page.js +++ b/packages/gitbook-asciidoc/test/page.js @@ -4,19 +4,15 @@ var assert = require('assert'); var page = require('../').page; -function loadPage (name, options) { - var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/' + name + '.adoc'), 'utf8'); - return page(CONTENT, options).sections; -} - -var LEXED = loadPage('PAGE'); - describe('Page parsing', function() { - it('should detect sections', function() { - assert.equal(LEXED.length, 1); + var LEXED; + + before(function() { + var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/PAGE.adoc'), 'utf8'); + LEXED = page(CONTENT); }); - it('should gen content for normal sections', function() { - assert(LEXED[0].content); + it('should gen content', function() { + assert(LEXED.content); }); }); diff --git a/packages/gitbook-asciidoc/test/readme.js b/packages/gitbook-asciidoc/test/readme.js index 0bca343..2f4f601 100755 --- a/packages/gitbook-asciidoc/test/readme.js +++ b/packages/gitbook-asciidoc/test/readme.js @@ -4,11 +4,13 @@ var assert = require('assert'); var readme = require('../').readme; - -var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/README.adoc'), 'utf8'); -var LEXED = readme(CONTENT); - describe('Readme parsing', function () { + var LEXED; + + before(function() { + var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/README.adoc'), 'utf8'); + LEXED = readme(CONTENT); + }); it('should contain a title', function() { assert(LEXED.title); diff --git a/packages/gitbook-asciidoc/test/summary.js b/packages/gitbook-asciidoc/test/summary.js index d363f3f..981967a 100755 --- a/packages/gitbook-asciidoc/test/summary.js +++ b/packages/gitbook-asciidoc/test/summary.js @@ -4,38 +4,48 @@ var assert = require('assert'); var summary = require('../').summary; -var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/SUMMARY.adoc'), 'utf8'); -var LEXED = summary(CONTENT); - describe('Summary parsing', function () { - it('should detect chapters', function() { - assert.equal(LEXED.chapters.length, 5); + var LEXED, PART; + + before(function() { + var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/SUMMARY.adoc'), 'utf8'); + LEXED = summary(CONTENT); + PART = LEXED.parts[0]; + // todo: add support for parts in asciidoc + }); + + it('should detect parts', function() { + assert.equal(LEXED.parts.length, 1); + }); + + it('should detect articles', function() { + assert.equal(PART.articles.length, 5); }); it('should support articles', function() { - assert.equal(LEXED.chapters[0].articles.length, 2); - assert.equal(LEXED.chapters[1].articles.length, 0); - assert.equal(LEXED.chapters[2].articles.length, 0); + assert.equal(PART.articles[0].articles.length, 2); + assert.equal(PART.articles[1].articles.length, 0); + assert.equal(PART.articles[2].articles.length, 0); }); it('should detect paths and titles', function() { - assert(LEXED.chapters[0].path); - assert(LEXED.chapters[1].path); - assert(LEXED.chapters[2].path); - assert(LEXED.chapters[3].path); - assert.equal(LEXED.chapters[4].path, null); - - assert(LEXED.chapters[0].title); - assert(LEXED.chapters[1].title); - assert(LEXED.chapters[2].title); - assert(LEXED.chapters[3].title); - assert(LEXED.chapters[4].title); + 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].title); + assert(PART.articles[1].title); + assert(PART.articles[2].title); + assert(PART.articles[3].title); + assert(PART.articles[4].title); }); it('should normalize paths from .md', function() { - assert.equal(LEXED.chapters[0].path,'chapter-1/README.adoc'); - assert.equal(LEXED.chapters[1].path,'chapter-2/README.adoc'); - assert.equal(LEXED.chapters[2].path,'chapter-3/README.adoc'); + assert.equal(PART.articles[0].path,'chapter-1/README.adoc'); + assert.equal(PART.articles[1].path,'chapter-2/README.adoc'); + assert.equal(PART.articles[2].path,'chapter-3/README.adoc'); }); it('should correctly convert it to text', function() { |