summaryrefslogtreecommitdiffstats
path: root/lib/generate/site
diff options
context:
space:
mode:
Diffstat (limited to 'lib/generate/site')
-rw-r--r--lib/generate/site/glossary_indexer.js101
-rw-r--r--lib/generate/site/index.js314
-rw-r--r--lib/generate/site/search_indexer.js71
3 files changed, 0 insertions, 486 deletions
diff --git a/lib/generate/site/glossary_indexer.js b/lib/generate/site/glossary_indexer.js
deleted file mode 100644
index 46ac9a4..0000000
--- a/lib/generate/site/glossary_indexer.js
+++ /dev/null
@@ -1,101 +0,0 @@
-var _ = require("lodash");
-var kramed = require('kramed');
-var textRenderer = require('kramed-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"
- );
-
- // 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)) {
- 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));
-};
-
-// Dump index as a string
-Indexer.prototype.dump = function() {
- return JSON.stringify(this.idx);
-};
-
-
-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
deleted file mode 100644
index dfdbf68..0000000
--- a/lib/generate/site/index.js
+++ /dev/null
@@ -1,314 +0,0 @@
-var util = require("util");
-var path = require("path");
-var Q = require("q");
-var _ = require("lodash");
-var swig = require("../template");
-
-var fs = require("../fs");
-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() {
- BaseGenerator.apply(this, arguments);
-
- // Attach methods to instance
- _.bindAll(this);
-
- this.styles = ["website"];
- this.revision = Date.now();
- this.indexer = indexer();
-};
-util.inherits(Generator, BaseGenerator);
-
-// Add template loading to load
-Generator.prototype.load = function() {
- var that = this;
-
- return BaseGenerator.prototype.load.apply(this)
- .then(function() {
- return that.loadStyles();
- })
- .then(function() {
- return that.loadTemplates();
- });
-};
-
-// Load all styles
-Generator.prototype.loadStyles = function() {
- var that = this;
- this.styles = _.chain(this.styles)
- .map(function(style) {
- var stylePath = that.options.styles[style];
- if (fs.existsSync(path.resolve(that.options.input, stylePath))) {
- return stylePath;
- }
- return null;
- })
- .compact()
- .value();
-};
-
-// Load all templates
-Generator.prototype.loadTemplates = function() {
- this.template = swig.compileFile(
- this.plugins.template("site:page") || path.resolve(this.options.theme, 'templates/website/page.html')
- );
- this.langsTemplate = swig.compileFile(
- this.plugins.template("site:langs") || path.resolve(this.options.theme, 'templates/website/langs.html')
- );
- this.glossaryTemplate = swig.compileFile(
- this.plugins.template("site:glossary") || path.resolve(this.options.theme, 'templates/website/glossary.html')
- );
-};
-
-// Generate a template
-Generator.prototype._writeTemplate = function(tpl, options, output, interpolate) {
- var that = this;
-
- interpolate = interpolate || _.identity;
- return Q()
- .then(function(sections) {
- return tpl(_.extend({
- styles: that.styles,
-
- revision: that.revision,
-
- title: that.options.title,
- description: that.options.description,
-
- glossary: that.options.glossary,
-
- summary: that.options.summary,
- allNavigation: that.options.navigation,
-
- plugins: that.plugins,
- pluginsConfig: JSON.stringify(that.options.pluginsConfig),
- htmlSnippet: _.partialRight(that.plugins.html, that, options),
-
- options: that.options
- }, options));
- })
- .then(interpolate)
- .then(function(html) {
- return fs.writeFile(
- output,
- html
- );
- });
-};
-
-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();
-};
-
-// Convert a markdown file into a normalized data set
-Generator.prototype.prepareFile = function(content, _input) {
- var that = this;
-
- var input = path.join(this.options.input, _input);
-
- var page = {
- path: _input,
- rawPath: input,
- content: content,
- progress: parse.progress(this.options.navigation, _input)
- };
-
- var _callHook = function(name) {
- return that.callHook(name, page)
- .then(function(_page) {
- page = _page;
- return page;
- });
- };
-
- return Q()
- .then(function() {
- // Send content to plugins
- return _callHook("page:before");
- })
- .then(function() {
- // Lex, parse includes and get
- // Get HTML generated sections
- return parse.page(page.content, {
- // Local files path
- dir: path.dirname(_input) || '/',
-
- // Output directory
- outdir: path.dirname(_input) || '/',
-
- // Includer for templating
- includer: parse.includer(that.options.variables, [
- path.dirname(_input) || '/',
- path.join(that.options.input, '_includes'),
- ], path.join, fs.readFileSync)
- });
- })
- .then(function(parsed) {
- page.lexed = parsed.lexed;
- page.sections = parsed.sections;
-
- // Use plugin hook
- return _callHook("page");
- })
- .then(function() {
- return page;
- });
-};
-
-// Convert a markdown file to html
-Generator.prototype.convertFile = function(content, _input) {
- var that = this;
-
- var _output = _input.replace(".md", ".html");
- if (_output == "README.html") _output = "index.html";
- var output = path.join(this.options.output, _output);
- var basePath = path.relative(path.dirname(output), this.options.output) || ".";
-
- // Bug fix for issue #493 which would occur when relative-links are 2-level or more deep in win32
- if (process.platform === 'win32') {
- basePath = basePath.replace(/\\/g, '/');
- }
-
- return this.prepareFile(content, _input)
- .then(function(page) {
- // Index page in search
- return that.indexPage(page.lexed, _output).thenResolve(page);
- })
- .then(function(page) {
- // Write file
- return that._writeTemplate(that.template, {
- progress: page.progress,
-
- _input: page.path,
- content: page.sections,
-
- basePath: basePath,
- staticBase: links.join(basePath, "gitbook"),
- }, output, function(html) {
- page.content = html;
-
- return that.callHook("page:after", page).get("content")
- });
- });
-};
-
-// Generate languages index
-Generator.prototype.langsIndex = function(langs) {
- var that = this;
- var basePath = ".";
-
- return this._writeTemplate(this.langsTemplate, {
- langs: langs.list,
-
- basePath: basePath,
- staticBase: path.join(basePath, "gitbook"),
- }, path.join(this.options.output, "index.html"))
- .then(function() {
- // Copy assets
- return that.copyAssets();
- });
-};
-
-// Generate glossary
-Generator.prototype.writeGlossary = function() {
- var that = this;
- var basePath = ".";
-
- // No glossary
- if (!this.glossaryIndexer) return Q();
-
- // Transform the glossary to get term, description, files
- var glossary = _.chain(this.glossaryIndexer.invertedIdx)
- .map(function(links, id) {
- var term = _.find(that.options.glossary, { 'id': id });
-
- return {
- id: id,
- name: term.name,
- description: term.description,
- files: _.chain(links)
- .map(function(link) {
- var name = link.slice(0, -5);
-
- if (name == "index") {
- name = "README";
- }
- return that.options.navigation[name+".md"];
- })
- .sortBy("percent")
- .value()
- }
- })
- .sortBy("name")
- .value();
-
- return this._writeTemplate(this.glossaryTemplate, {
- glossaryIndex: glossary,
- basePath: basePath,
- staticBase: path.join(basePath, "gitbook"),
- }, path.join(this.options.output, "GLOSSARY.html"));
-};
-
-// Copy assets
-Generator.prototype.copyAssets = function() {
- var that = this;
-
- // Copy gitbook assets
- return fs.copy(
- path.join(that.options.theme, "assets"),
- path.join(that.options.output, "gitbook")
- )
-
- // Copy plugins assets
- .then(function() {
- return Q.all(
- _.map(that.plugins.list, function(plugin) {
- var pluginAssets = path.join(that.options.output, "gitbook/plugins/", plugin.name);
- return plugin.copyAssets(pluginAssets, {
- base: that.pluginAssetsBase
- });
- })
- );
- });
-};
-
-// Dump search index to disk
-Generator.prototype.writeSearchIndex = function() {
- return fs.writeFile(
- path.join(this.options.output, 'search_index.json'),
- this.indexer.dump()
- );
-};
-
-// Dump glossary index to disk
-Generator.prototype.writeGlossaryIndex = function() {
- if (!this.glossaryIndexer) return Q();
-
- return fs.writeFile(
- path.join(this.options.output, 'glossary_index.json'),
- JSON.stringify(this.options.glossary)
- );
-};
-
-
-Generator.prototype.finish = function() {
- return this.copyAssets()
- .then(this.copyCover)
- .then(this.writeGlossary)
- .then(this.writeGlossaryIndex)
- .then(this.writeSearchIndex);
-};
-
-module.exports = Generator;
diff --git a/lib/generate/site/search_indexer.js b/lib/generate/site/search_indexer.js
deleted file mode 100644
index 7cfe29a..0000000
--- a/lib/generate/site/search_indexer.js
+++ /dev/null
@@ -1,71 +0,0 @@
-var Q = require("q");
-var _ = require("lodash");
-
-var lunr = require('lunr');
-var kramed = require('kramed');
-var textRenderer = require('kramed-text-renderer');
-
-
-function Indexer() {
- if(!(this instanceof Indexer)) {
- return new Indexer();
- }
-
- _.bindAll(this);
-
- // Setup lunr index
- this.idx = lunr(function () {
- this.ref('url');
-
- this.field('title', { boost: 10 });
- this.field('body');
- });
-
- 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);
-};
-
-Indexer.prototype.addSection = function(path, section) {
- var url = [path, section.id].join('#');
-
- var title = this.text(
- _.filter(section, {'type': 'heading'})
- );
-
- var body = this.text(
- _.omit(section, {'type': 'heading'})
- );
-
- // Add to lunr index
- this.idx.add({
- url: url,
- title: title,
- body: body,
- });
-};
-
-Indexer.prototype.add = function(lexedPage, url) {
- var sections = lexedPage;
-
- _.map(sections, _.partial(this.addSection, url));
-};
-
-Indexer.prototype.dump = function() {
- return JSON.stringify(this.idx);
-};
-
-// Exports
-module.exports = Indexer;