diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-01-27 21:56:00 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-01-27 21:56:00 +0100 |
commit | 8d277e9108afa6a027c61feb581a2150958f8571 (patch) | |
tree | 708a272a939c3e2089a2280d1358a4a9566e412e | |
parent | f305d57ab7702c3ca10fd6e32366d19e524ee1f0 (diff) | |
download | gitbook-8d277e9108afa6a027c61feb581a2150958f8571.zip gitbook-8d277e9108afa6a027c61feb581a2150958f8571.tar.gz gitbook-8d277e9108afa6a027c61feb581a2150958f8571.tar.bz2 |
Add parsing of the glossary
-rw-r--r-- | lib/backbone/article.js | 2 | ||||
-rw-r--r-- | lib/backbone/glossary.js | 51 | ||||
-rw-r--r-- | lib/backbone/page.js | 4 | ||||
-rw-r--r-- | lib/generators/base.js | 2 | ||||
-rw-r--r-- | lib/generators/json.js | 5 | ||||
-rw-r--r-- | lib/index.js | 1 | ||||
-rw-r--r-- | lib/output.js | 7 | ||||
-rw-r--r-- | lib/parsers/asciidoc.js | 27 | ||||
-rw-r--r-- | lib/parsers/html.js | 90 | ||||
-rw-r--r-- | lib/parsers/index.js | 11 | ||||
-rw-r--r-- | lib/parsers/markdown.js | 6 |
11 files changed, 68 insertions, 138 deletions
diff --git a/lib/backbone/article.js b/lib/backbone/article.js index 68d8236..299df25 100644 --- a/lib/backbone/article.js +++ b/lib/backbone/article.js @@ -16,7 +16,7 @@ function Article(title, ref, articles) { this.articles = _.map(articles || [], function(article) { if (article instanceof Article) return article; return new Article(article.title, article.ref, article.articles); - }) + }); } // Return true if has children diff --git a/lib/backbone/glossary.js b/lib/backbone/glossary.js index aba83cf..b25d7c7 100644 --- a/lib/backbone/glossary.js +++ b/lib/backbone/glossary.js @@ -1,8 +1,57 @@ +var _ = require('lodash'); +var util = require('util'); +var BackboneFile = require('./file'); -function Glossary() { +/* +A glossary entry is represented by a name and a short description +An unique id for the entry is generated using its name +*/ +function GlossaryEntry(name, description) { if (!(this instanceof Glossary)) return new Glossary(); + + this.name = name; + this.description = description; + + Object.defineProperty(this, 'id', { + get: _.bind(this.getId, this) + }); +} + +// Normalizes a glossary entry's name to create an ID +GlossaryEntry.prototype.getId = function() { + return this.name.toLowerCase() + .replace(/[\/\\\?\%\*\:\;\|\"\'\\<\\>\#\$\(\)\!\.\@]/g, '') + .replace(/ /g, '_') + .trim(); +}; + + +function Glossary() { + BackboneFile.apply(this, arguments); + + this.entries = []; } +util.inherits(Glossary, BackboneFile); Glossary.prototype.type = 'glossary'; +// Parse the readme content +Glossary.prototype.parse = function(content) { + var that = this; + + return this.parser.glossary(content) + .then(function(entries) { + that.entries = _.map(entries, function(entry) { + return new GlossaryEntry(entry.name, entry.description); + }); + }); +}; + +// Return an entry by its id +Glossary.prototype.get = function(id) { + return _.find(this.entries, { + id: id + }); +}; + module.exports = Glossary; diff --git a/lib/backbone/page.js b/lib/backbone/page.js index fdfe3f7..33bd636 100644 --- a/lib/backbone/page.js +++ b/lib/backbone/page.js @@ -10,6 +10,10 @@ function Page(book, filename) { this.filename = filename; } +// Return the filename of the page with another extension +Page.prototype.withExtension = function(ext) { + return +}; module.exports = Page; diff --git a/lib/generators/base.js b/lib/generators/base.js index 554a389..0fc0be9 100644 --- a/lib/generators/base.js +++ b/lib/generators/base.js @@ -11,7 +11,7 @@ Generator.prototype.prepare = function() { }; // Copy an asset file (non-parsable), ex: images, etc -Generator.prototype.writeFile = function(filename) { +Generator.prototype.writeAsset = function(filename) { }; diff --git a/lib/generators/json.js b/lib/generators/json.js index c5a87fe..820574b 100644 --- a/lib/generators/json.js +++ b/lib/generators/json.js @@ -6,7 +6,12 @@ function JSONGenerator() { } util.inherits(JSONGenerator, Generator); +// Write a page (parsable file) +Generator.prototype.writePage = function(page) { + return this.output.writeFile(page.withExtension('.json')); + +}; diff --git a/lib/index.js b/lib/index.js index 13a572d..59346f2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,5 @@ var Book = require('./book'); +var Output = require('./output'); module.exports = { Book: Book diff --git a/lib/output.js b/lib/output.js index 2f40fb6..1c694a1 100644 --- a/lib/output.js +++ b/lib/output.js @@ -11,6 +11,11 @@ function Output(book, type) { this.generator = new generators[type](this, type); } +// Write a file to the output folder +Output.prototype.writeFile = function(filename, buf) { + +}; + // Start the generation, for a parsed book Output.prototype.generate = function() { var that = this; @@ -33,7 +38,7 @@ Output.prototype.generate = function() { if (isPage) { return that.generator.writePage(that.book.getPage(filename)); } else { - return that.generator.writeFile(filename); + return that.generator.writeAsset(filename); } }); }) diff --git a/lib/parsers/asciidoc.js b/lib/parsers/asciidoc.js deleted file mode 100644 index 84e619d..0000000 --- a/lib/parsers/asciidoc.js +++ /dev/null @@ -1,27 +0,0 @@ -var Asciidoctor = require('asciidoctor.js'); -var htmlParser = require('./html'); - -var asciidoctor = Asciidoctor(); -var opal = asciidoctor.Opal; - -var processor = null; -var useExtensions = true; - -if (useExtensions) { - processor = asciidoctor.Asciidoctor(true); -} else { - processor = asciidoctor.Asciidoctor(); -} - - -// Convert asciidoc to HTML -function asciidocToHTML(content) { - var options = opal.hash2(['attributes'], {'attributes': 'showtitle'}); - return processor.$convert(content, options); -} - - -module.exports = htmlParser.inherits({ - extensions: ['.adoc', '.asciidoc'], - toHTML: asciidocToHTML -}); diff --git a/lib/parsers/html.js b/lib/parsers/html.js deleted file mode 100644 index 8f4ed34..0000000 --- a/lib/parsers/html.js +++ /dev/null @@ -1,90 +0,0 @@ -var _ = require('lodash'); -var cheerio = require('cheerio'); - -// Parse summary and returns a list of sections -function parseSummary(html) { - var sections = []; - var $ = cheerio.load(html); - - // Find main container - var $body = getContainer($); - - // Extract sections, and parse - var $lists = $body.find('> ul, > ol'); - - $lists.each(function() { - sections.push({ - articles: parseList($(this), $) - }); - }); - - return sections; -} - -// Parse readme and extract title, description -function parseReadme(html) { - var $ = cheerio.load(html); - - // Find main container - var $body = getContainer($); - - return { - title: $body.find('h1:first-child').text().trim(), - description: $body.find('div.paragraph').first().text().trim() - }; -} - -// Return a page container (html, body tag or directly the root element) -function getContainer($) { - var $body = $('body, html').first(); - if (!$body) $body = $; - - return $body; -} - -// 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 $text = $li.find('> p, > span'); - var $a = $li.find('> a, > p a, > span a'); - - article.title = $text.text(); - if ($a.length > 0) { - article.title = $a.first().text(); - article.ref = $a.attr('href'); - } - - // Inner list, with children article - var $sub = $li.find('> ol, > ul, > .olist > ol'); - article.articles = parseList($sub, $); - - articles.push(article); - }); - - return articles; -} - - -// Inherit from the html parser -function inherits(opts) { - var parser = _.defaults(opts, { - toHTML: _.identity - }); - - parser.readme = _.compose(opts.toHTML, parseReadme); - parser.summary = _.compose(opts.toHTML, parseSummary); - - return parser; -} - - -module.exports = inherits({ - extensions: ['.html'] -}); -module.exports.inherits = inherits; diff --git a/lib/parsers/index.js b/lib/parsers/index.js deleted file mode 100644 index d650386..0000000 --- a/lib/parsers/index.js +++ /dev/null @@ -1,11 +0,0 @@ - -var PARSERS = { - html: require('./html'), - markdown: require('./markdown'), - asciidoc: require('./asciidoc') -}; - - -module.exports = { - -}; diff --git a/lib/parsers/markdown.js b/lib/parsers/markdown.js deleted file mode 100644 index aac2858..0000000 --- a/lib/parsers/markdown.js +++ /dev/null @@ -1,6 +0,0 @@ - - - -module.exports = { - extensions: ['.md', '.markdown', '.mdown'] -}; |