diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-21 10:18:15 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 11:46:14 +0100 |
commit | a81a503ff0bbbf66f6fefb7090f8516071ae817a (patch) | |
tree | 5a255d8453a3c4705083c6a6a4eba2f76fee7acf | |
parent | 11893255d1167382b16871d2ca831c90a40af57d (diff) | |
download | gitbook-a81a503ff0bbbf66f6fefb7090f8516071ae817a.zip gitbook-a81a503ff0bbbf66f6fefb7090f8516071ae817a.tar.gz gitbook-a81a503ff0bbbf66f6fefb7090f8516071ae817a.tar.bz2 |
Add base to parse asciidoc
21 files changed, 402 insertions, 0 deletions
diff --git a/packages/gitbook-asciidoc/.gitignore b/packages/gitbook-asciidoc/.gitignore new file mode 100755 index 0000000..9550e4f --- /dev/null +++ b/packages/gitbook-asciidoc/.gitignore @@ -0,0 +1,28 @@ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# Deployed apps should consider commenting this line out: +# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git +node_modules + +# vim swapfile +*.swp diff --git a/packages/gitbook-asciidoc/README.md b/packages/gitbook-asciidoc/README.md new file mode 100755 index 0000000..359b7c0 --- /dev/null +++ b/packages/gitbook-asciidoc/README.md @@ -0,0 +1,3 @@ +# GitBook Markdown Parser + +This node module uses kramed to parse markdown for gitbook (SUMMARY.md, README.md). diff --git a/packages/gitbook-asciidoc/lib/glossary.js b/packages/gitbook-asciidoc/lib/glossary.js new file mode 100755 index 0000000..dec645d --- /dev/null +++ b/packages/gitbook-asciidoc/lib/glossary.js @@ -0,0 +1,7 @@ +var _ = require('lodash'); + +function parseGlossary(src) { + return []; +} + +module.exports = parseGlossary; diff --git a/packages/gitbook-asciidoc/lib/index.js b/packages/gitbook-asciidoc/lib/index.js new file mode 100755 index 0000000..ac8339a --- /dev/null +++ b/packages/gitbook-asciidoc/lib/index.js @@ -0,0 +1,8 @@ + +module.exports = { + summary: require("./summary"), + glossary: require("./glossary"), + langs: require("./langs"), + readme: require("./readme"), + page: require("./page") +}; diff --git a/packages/gitbook-asciidoc/lib/langs.js b/packages/gitbook-asciidoc/lib/langs.js new file mode 100755 index 0000000..86da75a --- /dev/null +++ b/packages/gitbook-asciidoc/lib/langs.js @@ -0,0 +1,22 @@ +var _ = require("lodash"); +var parseEntries = require("./summary").entries; + + +var parseLangs = function(content) { + var entries = parseEntries(content); + + return _.chain(entries) + .filter(function(entry) { + return Boolean(entry.path); + }) + .map(function(entry) { + return { + title: entry.title, + path: entry.path, + lang: entry.path.replace("/", "") + }; + }) + .value(); +}; + +module.exports = parseLangs; diff --git a/packages/gitbook-asciidoc/lib/page.js b/packages/gitbook-asciidoc/lib/page.js new file mode 100755 index 0000000..2bcfe61 --- /dev/null +++ b/packages/gitbook-asciidoc/lib/page.js @@ -0,0 +1,17 @@ +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 new file mode 100755 index 0000000..6ca1a83 --- /dev/null +++ b/packages/gitbook-asciidoc/lib/readme.js @@ -0,0 +1,18 @@ +var _ = require('lodash'); +var cheerio = require('cheerio'); + +var convert = require('./utils/convert'); + +function parseReadme(src) { + var html = convert(src); + $ = 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 new file mode 100755 index 0000000..67e11f3 --- /dev/null +++ b/packages/gitbook-asciidoc/lib/summary.js @@ -0,0 +1,20 @@ +var _ = require('lodash'); +var cheerio = require('cheerio'); + +var convert = require('./utils/convert'); + +function parseSummary(src, entryPoint) { + var html = convert(src); + $ = cheerio.load(html); + console.log(html); + return []; +} + +function parseEntries (src) { + return []; +} + + +// Exports +module.exports = parseSummary; +module.exports.entries = parseEntries; diff --git a/packages/gitbook-asciidoc/lib/utils/convert.js b/packages/gitbook-asciidoc/lib/utils/convert.js new file mode 100644 index 0000000..8a19638 --- /dev/null +++ b/packages/gitbook-asciidoc/lib/utils/convert.js @@ -0,0 +1,21 @@ +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 new file mode 100755 index 0000000..9bb1959 --- /dev/null +++ b/packages/gitbook-asciidoc/package.json @@ -0,0 +1,38 @@ +{ + "name": "gitbook-asciidoc", + "version": "0.0.0", + "homepage": "https://www.gitbook.com", + "description": "Parse AsciiDoc content for gitbook", + "main": "lib/index.js", + "dependencies": { + "q": "1.0.1", + "lodash": "2.4.1", + "asciidoctor.js": "1.5.2", + "cheerio": "0.18.0" + }, + "devDependencies": { + "mocha": "1.18.2" + }, + "scripts": { + "test": "export TESTING=true; mocha --reporter list" + }, + "repository": { + "type": "git", + "url": "https://github.com/GitbookIO/gitbook-asciidoc.git" + }, + "author": "FriendCode Inc. <contact@gitbook.com>", + "license": "Apache 2", + "bugs": { + "url": "https://github.com/GitbookIO/gitbook-asciidoc/issues" + }, + "contributors": [ + { + "name": "Aaron O'Mullan", + "email": "aaron@gitbook.com" + }, + { + "name": "Samy Pessé", + "email": "samy@gitbook.com" + } + ] +} diff --git a/packages/gitbook-asciidoc/test.js b/packages/gitbook-asciidoc/test.js new file mode 100644 index 0000000..3989494 --- /dev/null +++ b/packages/gitbook-asciidoc/test.js @@ -0,0 +1,6 @@ +var asciidoctor = require('asciidoctor.js')(); +var opal = asciidoctor.Opal; +var processor = asciidoctor.Asciidoctor(); +var asciidocOpts = opal.hash2(['attributes'], {'attributes': 'showtitle'}); + +console.log(processor.$convert('= Test', asciidocOpts));
\ No newline at end of file diff --git a/packages/gitbook-asciidoc/test/fixtures/GLOSSARY.adoc b/packages/gitbook-asciidoc/test/fixtures/GLOSSARY.adoc new file mode 100755 index 0000000..75be225 --- /dev/null +++ b/packages/gitbook-asciidoc/test/fixtures/GLOSSARY.adoc @@ -0,0 +1,32 @@ += Glossary + +== Magic +Sufficiently advanced technology, beyond the understanding of the observer producing a sense of wonder. + +Hello, I am random noise in the middle of this beautiful Glossary. (Really astonishing !) + +== PHP +An atrocious language, invented for the sole purpose of inflicting pain and suffering amongst the proframming wizards of this world. + +== Clojure +Lisp re-invented for hipsters. + +== Go +Go Go Google [Wow](https://www.google.com) + +Fantastic, I love code too ! : + +```py + +def f(x): + return x * 4 + +# Wow this is some really awesome code +# totally mind blowing +# but we don't care, it shouldn't be in our glossary ! +print(f(9)) +``` + +== Gitbook + +Awesome project. Really amazing, I'm really at a loss for words ... diff --git a/packages/gitbook-asciidoc/test/fixtures/LANGS.adoc b/packages/gitbook-asciidoc/test/fixtures/LANGS.adoc new file mode 100755 index 0000000..8ea9e11 --- /dev/null +++ b/packages/gitbook-asciidoc/test/fixtures/LANGS.adoc @@ -0,0 +1,4 @@ += Languages + +. link:en/[English] +. link:en/[French] diff --git a/packages/gitbook-asciidoc/test/fixtures/PAGE.adoc b/packages/gitbook-asciidoc/test/fixtures/PAGE.adoc new file mode 100755 index 0000000..a9afbac --- /dev/null +++ b/packages/gitbook-asciidoc/test/fixtures/PAGE.adoc @@ -0,0 +1,14 @@ += Python basics + +Python is a nice language, you can add stuff. Bla bla bla. + +Some more nice content .... + +[Cool stuff](http://gitbook.io) + +[Link to another Markdown file](./xyz/file.md) + +And look at this pretty picture: + + +Lets go for another exercise but this time with some context : diff --git a/packages/gitbook-asciidoc/test/fixtures/README.adoc b/packages/gitbook-asciidoc/test/fixtures/README.adoc new file mode 100755 index 0000000..12687b6 --- /dev/null +++ b/packages/gitbook-asciidoc/test/fixtures/README.adoc @@ -0,0 +1,6 @@ += This is the title + +This is the book description. + +other content +... diff --git a/packages/gitbook-asciidoc/test/fixtures/SUMMARY.adoc b/packages/gitbook-asciidoc/test/fixtures/SUMMARY.adoc new file mode 100755 index 0000000..8f97fa0 --- /dev/null +++ b/packages/gitbook-asciidoc/test/fixtures/SUMMARY.adoc @@ -0,0 +1,12 @@ += Summary + +. link:chapter-1/README.adoc[Chapter 1] +.. link:chapter-1/ARTICLE1.adoc[Article 1] +.. link:chapter-1/ARTICLE2.adoc[Article 2] +... link:\chapter-1\ARTICLE-1-2-1.adoc[Article 1.2.1] +... link:\chapter-1\ARTICLE-1-2-2.adoc[Article 1.2.2] +. link:chapter-2/README.adoc[Chapter 2] +. link:chapter-3/README.adoc[Chapter 3] +. link:chapter-4/README.adoc[Chapter 4] +.. Unfinished article +. Unfinished Chapter diff --git a/packages/gitbook-asciidoc/test/glossary.js b/packages/gitbook-asciidoc/test/glossary.js new file mode 100755 index 0000000..eb26f4a --- /dev/null +++ b/packages/gitbook-asciidoc/test/glossary.js @@ -0,0 +1,20 @@ +var fs = require('fs'); +var path = require('path'); +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 () { + it('should only get heading + paragraph pairs', function() { + assert.equal(LEXED.length, 5); + }); + + it('should output simple name/description objects', function() { + assert.equal(true, !(LEXED.some(function(e) { + return !Boolean(e.name && e.description); + }))); + }); +}); diff --git a/packages/gitbook-asciidoc/test/langs.js b/packages/gitbook-asciidoc/test/langs.js new file mode 100755 index 0000000..bbf3c68 --- /dev/null +++ b/packages/gitbook-asciidoc/test/langs.js @@ -0,0 +1,20 @@ +var fs = require('fs'); +var path = require('path'); +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 () { + it('should detect paths and titles', function() { + assert.equal(LEXED[0].path,'en/'); + assert.equal(LEXED[0].lang,'en'); + assert.equal(LEXED[0].title,'English'); + + assert.equal(LEXED[1].path,'fr/'); + assert.equal(LEXED[1].lang,'fr'); + assert.equal(LEXED[1].title,'French'); + }); +}); diff --git a/packages/gitbook-asciidoc/test/page.js b/packages/gitbook-asciidoc/test/page.js new file mode 100755 index 0000000..9579725 --- /dev/null +++ b/packages/gitbook-asciidoc/test/page.js @@ -0,0 +1,22 @@ +var fs = require('fs'); +var path = require('path'); +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); + }); + + it('should gen content for normal sections', function() { + assert(LEXED[0].content); + }); +}); diff --git a/packages/gitbook-asciidoc/test/readme.js b/packages/gitbook-asciidoc/test/readme.js new file mode 100755 index 0000000..0bca343 --- /dev/null +++ b/packages/gitbook-asciidoc/test/readme.js @@ -0,0 +1,28 @@ +var fs = require('fs'); +var path = require('path'); +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 () { + + it('should contain a title', function() { + assert(LEXED.title); + }); + + it('should contain a description', function() { + assert(LEXED.description); + }); + + it('should extract the right title', function() { + assert.equal(LEXED.title, "This is the title"); + }); + + it('should extract the right description', function() { + assert.equal(LEXED.description, "This is the book description."); + }); +}); diff --git a/packages/gitbook-asciidoc/test/summary.js b/packages/gitbook-asciidoc/test/summary.js new file mode 100755 index 0000000..5c9bbcc --- /dev/null +++ b/packages/gitbook-asciidoc/test/summary.js @@ -0,0 +1,56 @@ +var fs = require('fs'); +var path = require('path'); +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, 6); + }); + + it('should support articles', function() { + assert.equal(LEXED.chapters[1].articles.length, 2); + assert.equal(LEXED.chapters[2].articles.length, 0); + assert.equal(LEXED.chapters[3].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(LEXED.chapters[4].path); + assert.equal(LEXED.chapters[5].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(LEXED.chapters[5].title); + }); + + it('should normalize paths from .md', function() { + assert.equal(LEXED.chapters[0].path,'README.md'); + assert.equal(LEXED.chapters[1].path,'chapter-1/README.md'); + assert.equal(LEXED.chapters[2].path,'chapter-2/README.md'); + assert.equal(LEXED.chapters[3].path,'chapter-3/README.md'); + }); + + it('should detect levels correctly', function() { + var c = LEXED.chapters; + + assert.equal(c[0].level, '0'); + assert.equal(c[1].level, '1'); + assert.equal(c[2].level, '2'); + assert.equal(c[3].level, '3'); + + assert.equal(c[1].articles[0].level, '1.1'); + assert.equal(c[1].articles[1].level, '1.2'); + assert.equal(c[1].articles[1].articles[0].level, '1.2.1'); + }); +}); |