diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-08-18 17:25:26 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-08-18 17:25:26 -0700 |
commit | 90320fd26c60fce5615be09e0c03c486b567e8bd (patch) | |
tree | d84c142fa40b8e3aa28c8ec289779ab8b756f27a /lib/generate/site | |
parent | 4f9b474fdd87dada179efe86300c96278e263be5 (diff) | |
download | gitbook-90320fd26c60fce5615be09e0c03c486b567e8bd.zip gitbook-90320fd26c60fce5615be09e0c03c486b567e8bd.tar.gz gitbook-90320fd26c60fce5615be09e0c03c486b567e8bd.tar.bz2 |
Add glossary indexing
Diffstat (limited to 'lib/generate/site')
-rw-r--r-- | lib/generate/site/glossary_indexer.js | 103 | ||||
-rw-r--r-- | lib/generate/site/index.js | 8 |
2 files changed, 110 insertions, 1 deletions
diff --git a/lib/generate/site/glossary_indexer.js b/lib/generate/site/glossary_indexer.js new file mode 100644 index 0000000..8f8434d --- /dev/null +++ b/lib/generate/site/glossary_indexer.js @@ -0,0 +1,103 @@ +var _ = require("lodash"); + +var kramed = require('kramed'); +var textRenderer = require('marked-text-renderer'); + +var entryId = require('../../parse/glossary').entryId; + + +function Indexer(glossary) { + if(!(this instanceof Indexer)) { + return new Indexer(glossary); + } + + _.bindAll(this); + + this.glossary = glossary || []; + + this.glossaryTerms = _.pluck(this.glossary, "id"); + + // Regex for searching for terms through body + this.termsRegex = new RegExp( + // Match any of the terms + "("+ + this.glossaryTerms.map(regexEscape).join('|') + + ")", + + // Flags + "gi" + ); + + // debug + console.log('term regex =', this.termsRegex); + + // page url => terms + this.idx = { + /* + "a/b.html": ["one word", "second word"] + */ + }; + + // term => page urls + this.invertedIdx = { + /* + "word1": ["page1.html", "page2.html"] + */ + }; + + // Use text renderer + this.renderer = textRenderer(); +} + +Indexer.prototype.text = function(nodes) { + // Copy section + var section = _.toArray(nodes); + + // kramed's Render expects this, we don't use it yet + section.links = {}; + + var options = _.extend({}, kramed.defaults, { + renderer: this.renderer + }); + + return kramed.parser(section, options); +}; + +// Add page to glossary index +Indexer.prototype.add = function(sections, url) { + if(!(this.glossary && this.glossary.length > 0)) { + console.log('Glossary =', this.glossary); + console.log('No glossary to match'); + return; + } + + var textblob = + _.where(sections, { type: 'normal' }) + .map(this.text) + .join('\n'); + + var matches = _(textblob.match(this.termsRegex) || []) + .map(entryId) + .uniq() + .value(); + + // Add idx for book + this.idx[url] = matches; + + // Add to inverted idx + matches.forEach(function(match) { + if(!this.invertedIdx[match]) { + this.invertedIdx[match] = []; + } + this.invertedIdx[match].push(url); + }.bind(this)); +}; + + + +function regexEscape(s) { + return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); +} + +// Exports +module.exports = Indexer; diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js index 1846da1..588ffdb 100644 --- a/lib/generate/site/index.js +++ b/lib/generate/site/index.js @@ -9,7 +9,7 @@ var parse = require("../../parse"); var BaseGenerator = require("../generator"); var links = require("../../utils/links"); var indexer = require('./search_indexer'); - +var glossaryIndexer = require('./glossary_indexer'); var Generator = function() { @@ -85,7 +85,13 @@ Generator.prototype._writeTemplate = function(tpl, options, output, interpolate) }; Generator.prototype.indexPage = function(lexed, pagePath) { + // Setup glossary indexer if not yet setup + if(!this.glossaryIndexer) { + this.glossaryIndexer = glossaryIndexer(this.options.glossary); + } + this.indexer.add(lexed, pagePath); + this.glossaryIndexer.add(lexed, pagePath); return Q(); }; |