summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-asciidoc/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-20 15:31:29 +0100
committerSamy Pessé <samypesse@gmail.com>2016-12-22 11:46:22 +0100
commit147679a42257660d70a9ef6ac6216fabf1d67582 (patch)
tree4dfd4a809c8351b9eea4ebfcc222878a30bec0ad /packages/gitbook-asciidoc/lib
parentd7b5c6a14d823cd067c7309240326d18a495674a (diff)
downloadgitbook-147679a42257660d70a9ef6ac6216fabf1d67582.zip
gitbook-147679a42257660d70a9ef6ac6216fabf1d67582.tar.gz
gitbook-147679a42257660d70a9ef6ac6216fabf1d67582.tar.bz2
Use gitbook-html as base parser
Diffstat (limited to 'packages/gitbook-asciidoc/lib')
-rwxr-xr-xpackages/gitbook-asciidoc/lib/glossary.js39
-rwxr-xr-xpackages/gitbook-asciidoc/lib/index.js11
-rwxr-xr-xpackages/gitbook-asciidoc/lib/langs.js21
-rwxr-xr-xpackages/gitbook-asciidoc/lib/page.js17
-rwxr-xr-xpackages/gitbook-asciidoc/lib/readme.js18
-rwxr-xr-xpackages/gitbook-asciidoc/lib/summary.js83
-rw-r--r--packages/gitbook-asciidoc/lib/toasciidoc.js43
-rw-r--r--packages/gitbook-asciidoc/lib/tohtml.js13
-rw-r--r--packages/gitbook-asciidoc/lib/utils/convert.js21
9 files changed, 60 insertions, 206 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;