mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Move gitbook source code to es6 (linted)
This commit is contained in:
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"eslint": "2.10.2",
|
||||
"eslint": "3.4.0",
|
||||
"eslint-config-gitbook": "^1.2.0",
|
||||
"expect": "^1.20.1",
|
||||
"lerna": "2.0.0-beta.26",
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
var path = require('path');
|
||||
var Book = require('../models/book');
|
||||
var createNodeFS = require('../fs/node');
|
||||
|
||||
/**
|
||||
Return a book instance to work on from
|
||||
command line args/kwargs
|
||||
|
||||
@param {Array} args
|
||||
@param {Object} kwargs
|
||||
@return {Book}
|
||||
*/
|
||||
function getBook(args, kwargs) {
|
||||
var input = path.resolve(args[0] || process.cwd());
|
||||
var logLevel = kwargs.log;
|
||||
|
||||
var fs = createNodeFS(input);
|
||||
var book = Book.createForFS(fs);
|
||||
|
||||
return book.setLogLevel(logLevel);
|
||||
}
|
||||
|
||||
module.exports = getBook;
|
||||
@@ -1,17 +0,0 @@
|
||||
var path = require('path');
|
||||
|
||||
/**
|
||||
Return path to output folder
|
||||
|
||||
@param {Array} args
|
||||
@return {String}
|
||||
*/
|
||||
function getOutputFolder(args) {
|
||||
var bookRoot = path.resolve(args[0] || process.cwd());
|
||||
var defaultOutputRoot = path.join(bookRoot, '_book');
|
||||
var outputFolder = args[1]? path.resolve(process.cwd(), args[1]) : defaultOutputRoot;
|
||||
|
||||
return outputFolder;
|
||||
}
|
||||
|
||||
module.exports = getOutputFolder;
|
||||
@@ -1,17 +0,0 @@
|
||||
var path = require('path');
|
||||
|
||||
var options = require('./options');
|
||||
var initBook = require('../init');
|
||||
|
||||
module.exports = {
|
||||
name: 'init [book]',
|
||||
description: 'setup and create files for chapters',
|
||||
options: [
|
||||
options.log
|
||||
],
|
||||
exec: function(args, kwargs) {
|
||||
var bookRoot = path.resolve(process.cwd(), args[0] || './');
|
||||
|
||||
return initBook(bookRoot);
|
||||
}
|
||||
};
|
||||
@@ -1,128 +0,0 @@
|
||||
var events = require('events');
|
||||
var http = require('http');
|
||||
var send = require('send');
|
||||
var util = require('util');
|
||||
var url = require('url');
|
||||
|
||||
var Promise = require('../utils/promise');
|
||||
|
||||
function Server() {
|
||||
this.running = null;
|
||||
this.dir = null;
|
||||
this.port = 0;
|
||||
this.sockets = [];
|
||||
}
|
||||
util.inherits(Server, events.EventEmitter);
|
||||
|
||||
/**
|
||||
Return true if the server is running
|
||||
|
||||
@return {Boolean}
|
||||
*/
|
||||
Server.prototype.isRunning = function() {
|
||||
return !!this.running;
|
||||
};
|
||||
|
||||
/**
|
||||
Stop the server
|
||||
|
||||
@return {Promise}
|
||||
*/
|
||||
Server.prototype.stop = function() {
|
||||
var that = this;
|
||||
if (!this.isRunning()) return Promise();
|
||||
|
||||
var d = Promise.defer();
|
||||
this.running.close(function(err) {
|
||||
that.running = null;
|
||||
that.emit('state', false);
|
||||
|
||||
if (err) d.reject(err);
|
||||
else d.resolve();
|
||||
});
|
||||
|
||||
for (var i = 0; i < this.sockets.length; i++) {
|
||||
this.sockets[i].destroy();
|
||||
}
|
||||
|
||||
return d.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
Start the server
|
||||
|
||||
@return {Promise}
|
||||
*/
|
||||
Server.prototype.start = function(dir, port) {
|
||||
var that = this, pre = Promise();
|
||||
port = port || 8004;
|
||||
|
||||
if (that.isRunning()) pre = this.stop();
|
||||
return pre
|
||||
.then(function() {
|
||||
var d = Promise.defer();
|
||||
|
||||
that.running = http.createServer(function(req, res){
|
||||
// Render error
|
||||
function error(err) {
|
||||
res.statusCode = err.status || 500;
|
||||
res.end(err.message);
|
||||
}
|
||||
|
||||
// Redirect to directory's index.html
|
||||
function redirect() {
|
||||
var resultURL = urlTransform(req.url, function(parsed) {
|
||||
parsed.pathname += '/';
|
||||
return parsed;
|
||||
});
|
||||
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', resultURL);
|
||||
res.end('Redirecting to ' + resultURL);
|
||||
}
|
||||
|
||||
res.setHeader('X-Current-Location', req.url);
|
||||
|
||||
// Send file
|
||||
send(req, url.parse(req.url).pathname, {
|
||||
root: dir
|
||||
})
|
||||
.on('error', error)
|
||||
.on('directory', redirect)
|
||||
.pipe(res);
|
||||
});
|
||||
|
||||
that.running.on('connection', function (socket) {
|
||||
that.sockets.push(socket);
|
||||
socket.setTimeout(4000);
|
||||
socket.on('close', function () {
|
||||
that.sockets.splice(that.sockets.indexOf(socket), 1);
|
||||
});
|
||||
});
|
||||
|
||||
that.running.listen(port, function(err) {
|
||||
if (err) return d.reject(err);
|
||||
|
||||
that.port = port;
|
||||
that.dir = dir;
|
||||
that.emit('state', true);
|
||||
d.resolve();
|
||||
});
|
||||
|
||||
return d.promise;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
urlTransform is a helper function that allows a function to transform
|
||||
a url string in it's parsed form and returns the new url as a string
|
||||
|
||||
@param {String} uri
|
||||
@param {Function} fn
|
||||
@return {String}
|
||||
*/
|
||||
function urlTransform(uri, fn) {
|
||||
return url.format(fn(url.parse(uri)));
|
||||
}
|
||||
|
||||
module.exports = Server;
|
||||
@@ -1,6 +0,0 @@
|
||||
var Immutable = require('immutable');
|
||||
var jsonSchemaDefaults = require('json-schema-defaults');
|
||||
|
||||
var schema = require('./configSchema');
|
||||
|
||||
module.exports = Immutable.fromJS(jsonSchemaDefaults(schema));
|
||||
@@ -1,39 +0,0 @@
|
||||
var extend = require('extend');
|
||||
|
||||
var gitbook = require('../gitbook');
|
||||
var encodeSummary = require('./encodeSummary');
|
||||
var encodeGlossary = require('./encodeGlossary');
|
||||
var encodeReadme = require('./encodeReadme');
|
||||
var encodeLanguages = require('./encodeLanguages');
|
||||
|
||||
/**
|
||||
Encode a book to JSON
|
||||
|
||||
@param {Book}
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeBookToJson(book) {
|
||||
var config = book.getConfig();
|
||||
var language = book.getLanguage();
|
||||
|
||||
var variables = config.getValue('variables', {});
|
||||
|
||||
return {
|
||||
summary: encodeSummary(book.getSummary()),
|
||||
glossary: encodeGlossary(book.getGlossary()),
|
||||
readme: encodeReadme(book.getReadme()),
|
||||
config: book.getConfig().getValues().toJS(),
|
||||
|
||||
languages: book.isMultilingual()? encodeLanguages(book.getLanguages()) : undefined,
|
||||
|
||||
gitbook: {
|
||||
version: gitbook.version,
|
||||
time: gitbook.START_TIME
|
||||
},
|
||||
book: extend({
|
||||
language: language? language : undefined
|
||||
}, variables.toJS())
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = encodeBookToJson;
|
||||
@@ -1,45 +0,0 @@
|
||||
var Templating = require('../templating');
|
||||
var TemplateEngine = require('../models/templateEngine');
|
||||
|
||||
var Api = require('../api');
|
||||
var Plugins = require('../plugins');
|
||||
|
||||
var defaultBlocks = require('../constants/defaultBlocks');
|
||||
var defaultFilters = require('../constants/defaultFilters');
|
||||
|
||||
/**
|
||||
Create template engine for an output.
|
||||
It adds default filters/blocks, then add the ones from plugins
|
||||
|
||||
@param {Output} output
|
||||
@return {TemplateEngine}
|
||||
*/
|
||||
function createTemplateEngine(output) {
|
||||
var plugins = output.getPlugins();
|
||||
var book = output.getBook();
|
||||
var rootFolder = book.getContentRoot();
|
||||
var logger = book.getLogger();
|
||||
|
||||
var filters = Plugins.listFilters(plugins);
|
||||
var blocks = Plugins.listBlocks(plugins);
|
||||
|
||||
// Extend with default
|
||||
blocks = defaultBlocks.merge(blocks);
|
||||
filters = defaultFilters.merge(filters);
|
||||
|
||||
// Create loader
|
||||
var transformFn = Templating.replaceShortcuts.bind(null, blocks);
|
||||
var loader = new Templating.ConrefsLoader(rootFolder, transformFn, logger);
|
||||
|
||||
// Create API context
|
||||
var context = Api.encodeGlobal(output);
|
||||
|
||||
return new TemplateEngine({
|
||||
filters: filters,
|
||||
blocks: blocks,
|
||||
loader: loader,
|
||||
context: context
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = createTemplateEngine;
|
||||
@@ -1,40 +0,0 @@
|
||||
var cheerio = require('cheerio');
|
||||
var tmp = require('tmp');
|
||||
var path = require('path');
|
||||
|
||||
var URL = 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png';
|
||||
|
||||
describe('fetchRemoteImages', function() {
|
||||
var dir;
|
||||
var fetchRemoteImages = require('../fetchRemoteImages');
|
||||
|
||||
beforeEach(function() {
|
||||
dir = tmp.dirSync();
|
||||
});
|
||||
|
||||
it('should download image file', function() {
|
||||
var $ = cheerio.load('<img src="' + URL + '" />');
|
||||
|
||||
return fetchRemoteImages(dir.name, 'index.html', $)
|
||||
.then(function() {
|
||||
var $img = $('img');
|
||||
var src = $img.attr('src');
|
||||
|
||||
expect(dir.name).toHaveFile(src);
|
||||
});
|
||||
});
|
||||
|
||||
it('should download image file and replace with relative path', function() {
|
||||
var $ = cheerio.load('<img src="' + URL + '" />');
|
||||
|
||||
return fetchRemoteImages(dir.name, 'test/index.html', $)
|
||||
.then(function() {
|
||||
var $img = $('img');
|
||||
var src = $img.attr('src');
|
||||
|
||||
expect(dir.name).toHaveFile(path.join('test', src));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
var cheerio = require('cheerio');
|
||||
var tmp = require('tmp');
|
||||
var inlinePng = require('../inlinePng');
|
||||
|
||||
describe('inlinePng', function() {
|
||||
var dir;
|
||||
|
||||
beforeEach(function() {
|
||||
dir = tmp.dirSync();
|
||||
});
|
||||
|
||||
it('should write an inline PNG using data URI as a file', function() {
|
||||
var $ = cheerio.load('<img alt="GitBook Logo 20x20" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUEAYAAADdGcFOAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAAAsTAAALEwEAmpwYAAABWWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNS40LjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp0aWZmPSJodHRwOi8vbnMuYWRvYmUuY29tL3RpZmYvMS4wLyI+CiAgICAgICAgIDx0aWZmOk9yaWVudGF0aW9uPjE8L3RpZmY6T3JpZW50YXRpb24+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgpMwidZAAAF+klEQVRIDY3Wf5CVVR3H8c9z791fyI9dQwdQ4TTI7wEWnQZZAa/mJE4Z0OaKUuN1KoaykZxUGGHay+iIVFMoEYrUPhDCKEKW2ChT8dA0RCSxWi6EW3sYYpcfxq5C+4O9957O+7m7O/qHQ9/XzH1+nHuec57z8wkWTsKw0y6N/LxXN6KzTnEUHi8eP/l3YStSU/MdsYvBbGh8six2YXcbcgc++QkfTQkWz/81KtqDA0hlUoWnsX+5uxe5X365BB9my2bjrHNHccLk16BpS9CExjcmXMDbD6wehdyEjxbjz1uK1zn9qga6dcfnMLXeXY/qjuQqTF4W1MKke8ZgeNhjMCxMPIWSd4OF78C55CFI/1kF6WwXpMqjkAZ/CKniNDrCsmU4lE1YbPlgR2x7R39FF23D4mq3A1+Z35PGTNs1E1XhxcGQOh6HNPwXkK56BVJhOaRg/pvoHXNxHFw410B25EYE2RMvI0i/twFJvXcrFObykEa+DmnQGLwYqR0l2a6JqItaj8C/4E2QxtZCofkC8tF1t8HZc/fAZaLnIF2xEsoEtW1w7vBSSFtfhDTnCki9cSi81Ain1uko2Ld+Dmf2rkUq0/5t+PYbFtPQdkjzNiAXTWtDEF49FgkzJInAVPwNyhzcDOmrdZCm/Rn+ebWtcPs+/U24hmg2XL0rRkPPELh9R8fDtXR2oC/VuZbGaci79Ajkb6lZgfyYtyzy/X9s6T/pO/ZfN/RdNxxIwTWM2wbX8KVmuIaEqmKm6zEondwGpd0SyOy5DrJ//TFkX9kMhd3XQHbEVCSsm4OECV5HIv2p15CwfWPSntoHRbv2Q1HzSvSlSqZwATIuBxk/zZBOBbdB+u9hSKU3Q7pwAjInZkFm6U8hu7MSMqe/Dqn8fUj5GVCmpxK+4N/F1LMa0p5eSOPqIPP7NGSunAI/+R6GnzQzIBt8A1LC/QZ+6HwLst1rITv0n5CtXgSZ78yFTNkR+FdeDZneJkip3fAtsQ5Scilkek7CH9dAmjIWvkK7IXXOh6/IzZDNPQdZXR1TQmdjKv0ZfEu0YKDpNflpyG5aDtnRv8VAuu3dBV+huyBbvgdS97tQNLQc0mfugKy5Cb4BipPIXvsUpK5N8Mvao/Bd3QDZRH9Rrtj3Cl6FHwPFMLmNkKrj8BnHoT+XX6f2wl+XxFS4Ab7C72Dgf7bi+5DpTkNm8kQMpCs/BzIlz8LfPxnzLdh3EjwMX4GX4Ju4GNb9A1L7k/D3J8b6kv2LFCtmCmcgUzoJsr2z4MfwFsh87xikZefg188fYaAhpPUxm3ge/vFnYkoED0HqeQiyJYcwkNGWnoNv6s9C1p1Bf/389VYoCjohW7UfMms3wXdpBv7+FEiPLIHs4DIMNERUNhbSpY3wk6QOsqlCDVx2xCrInMpBmfNPQOnzKxBkkrugdOl9GKigSZZCUWIm/GqwDtLUI5D+WAOlb9wKP0YvQLbjZSjsaYaL/n0/FA3fDtnCGihK5UYjCK+ZDr+TDIKLdm2Fs1UOzo76F5wO74XSZj0S6d7RCMLkCshcXALZxaWQRjXDZQ62oRAdCeG/Ju5HELX2QFH3C0hkRy6GovyfwF58AoVbguOxyB2H7/I34Gf11yANnQSp7Vr4MbQH0vg7kbNNp5AM3UrIVDchnz56B1Jm573wW9gZSFVPwO/hefg5FsIvN09CchtQCIOFw/F5U8ii3CZn4cqo7C8YlXEPYkx9cacZl00+iwnprrtwVdj1Q/gXmAs/pu6LZc9XQOGgSvh19n2cDZN341g2EcfxTEGwH/RewqlMsUfbbWIGLjUG+j/j9nokD1beiOvLS5dhjr30Gu6ZnivgdtM/6VJvY1+6pBHbH+h9CX84vfMxNJtisYVFlys+WNCIZJNmIsjohlhNSQC3f8R55H+y/hjkN8GPR9ndCLJxT4/3n0Px51ay8XQnNrYfDJHf//Fc0oMrEZSeeQGJ7+Z+gKCgLbHNWgXnB9FlYt5JaN38JIINC95EakjtAqQeuUx21c5B6tEFf0fSfbEFQf28Z6D6y+X/H0jf40QQJhYwAAAAAElFTkSuQmCC"/>');
|
||||
|
||||
return inlinePng(dir.name, 'index.html', $)
|
||||
.then(function() {
|
||||
var $img = $('img');
|
||||
var src = $img.attr('src');
|
||||
|
||||
expect(dir.name).toHaveFile(src);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
var cheerio = require('cheerio');
|
||||
var tmp = require('tmp');
|
||||
|
||||
describe('svgToImg', function() {
|
||||
var dir;
|
||||
var svgToImg = require('../svgToImg');
|
||||
|
||||
beforeEach(function() {
|
||||
dir = tmp.dirSync();
|
||||
});
|
||||
|
||||
it('should write svg as a file', function() {
|
||||
var $ = cheerio.load('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1"><rect width="200" height="100" stroke="black" stroke-width="6" fill="green"/></svg>');
|
||||
|
||||
return svgToImg(dir.name, 'index.html', $)
|
||||
.then(function() {
|
||||
var $img = $('img');
|
||||
var src = $img.attr('src');
|
||||
|
||||
expect(dir.name).toHaveFile(src);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
var cheerio = require('cheerio');
|
||||
var tmp = require('tmp');
|
||||
var path = require('path');
|
||||
|
||||
var svgToImg = require('../svgToImg');
|
||||
var svgToPng = require('../svgToPng');
|
||||
|
||||
describe('svgToPng', function() {
|
||||
var dir;
|
||||
|
||||
beforeEach(function() {
|
||||
dir = tmp.dirSync();
|
||||
});
|
||||
|
||||
it('should write svg as png file', function() {
|
||||
var $ = cheerio.load('<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1"><rect width="200" height="100" stroke="black" stroke-width="6" fill="green"/></svg>');
|
||||
var fileName = 'index.html';
|
||||
|
||||
return svgToImg(dir.name, fileName, $)
|
||||
.then(function() {
|
||||
return svgToPng(dir.name, fileName, $);
|
||||
})
|
||||
.then(function() {
|
||||
var $img = $('img');
|
||||
var src = $img.attr('src');
|
||||
|
||||
expect(dir.name).toHaveFile(src);
|
||||
expect(path.extname(src)).toBe('.png');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
var Promise = require('../../utils/promise');
|
||||
var JSONUtils = require('../../json');
|
||||
var Templating = require('../../templating');
|
||||
var writeFile = require('../helper/writeFile');
|
||||
var createTemplateEngine = require('./createTemplateEngine');
|
||||
|
||||
/**
|
||||
Finish the generation, write the languages index
|
||||
|
||||
@param {Output}
|
||||
@return {Output}
|
||||
*/
|
||||
function onFinish(output) {
|
||||
var book = output.getBook();
|
||||
var options = output.getOptions();
|
||||
var prefix = options.get('prefix');
|
||||
|
||||
if (!book.isMultilingual()) {
|
||||
return Promise(output);
|
||||
}
|
||||
|
||||
var filePath = 'index.html';
|
||||
var engine = createTemplateEngine(output, filePath);
|
||||
var context = JSONUtils.encodeOutput(output);
|
||||
|
||||
// Render the theme
|
||||
return Templating.renderFile(engine, prefix + '/languages.html', context)
|
||||
|
||||
// Write it to the disk
|
||||
.then(function(tplOut) {
|
||||
return writeFile(output, filePath, tplOut.getContent());
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = onFinish;
|
||||
@@ -1,29 +0,0 @@
|
||||
var path = require('path');
|
||||
|
||||
var PluginDependency = require('../../models/pluginDependency');
|
||||
var Book = require('../../models/book');
|
||||
var NodeFS = require('../../fs/node');
|
||||
var installPlugin = require('../installPlugin');
|
||||
|
||||
var Parse = require('../../parse');
|
||||
|
||||
describe('installPlugin', function() {
|
||||
var book;
|
||||
|
||||
this.timeout(30000);
|
||||
|
||||
before(function() {
|
||||
var fs = NodeFS(path.resolve(__dirname, '../../../'));
|
||||
var baseBook = Book.createForFS(fs);
|
||||
|
||||
return Parse.parseConfig(baseBook)
|
||||
.then(function(_book) {
|
||||
book = _book;
|
||||
});
|
||||
});
|
||||
|
||||
it('must install a plugin from NPM', function() {
|
||||
var dep = PluginDependency.createFromString('ga');
|
||||
return installPlugin(book, dep);
|
||||
});
|
||||
});
|
||||
@@ -1,22 +0,0 @@
|
||||
var path = require('path');
|
||||
var resolve = require('resolve');
|
||||
|
||||
var DEFAULT_PLUGINS = require('../constants/defaultPlugins');
|
||||
|
||||
/**
|
||||
* Resolve the root folder containing for node_modules
|
||||
* since gitbook can be used as a library and dependency can be flattened.
|
||||
*
|
||||
* @return {String} folderPath
|
||||
*/
|
||||
function locateRootFolder() {
|
||||
var firstDefaultPlugin = DEFAULT_PLUGINS.first();
|
||||
var pluginPath = resolve.sync(firstDefaultPlugin.getNpmID() + '/package.json', {
|
||||
basedir: __dirname
|
||||
});
|
||||
var nodeModules = path.resolve(pluginPath, '../../..');
|
||||
|
||||
return nodeModules;
|
||||
}
|
||||
|
||||
module.exports = locateRootFolder;
|
||||
@@ -1,133 +0,0 @@
|
||||
var is = require('is');
|
||||
var path = require('path');
|
||||
var crc = require('crc');
|
||||
var URI = require('urijs');
|
||||
|
||||
var pathUtil = require('./path');
|
||||
var Promise = require('./promise');
|
||||
var command = require('./command');
|
||||
var fs = require('./fs');
|
||||
|
||||
var GIT_PREFIX = 'git+';
|
||||
|
||||
function Git() {
|
||||
this.tmpDir;
|
||||
this.cloned = {};
|
||||
}
|
||||
|
||||
// Return an unique ID for a combinaison host/ref
|
||||
Git.prototype.repoID = function(host, ref) {
|
||||
return crc.crc32(host+'#'+(ref || '')).toString(16);
|
||||
};
|
||||
|
||||
// Allocate a temporary folder for cloning repos in it
|
||||
Git.prototype.allocateDir = function() {
|
||||
var that = this;
|
||||
|
||||
if (this.tmpDir) return Promise();
|
||||
|
||||
return fs.tmpDir()
|
||||
.then(function(dir) {
|
||||
that.tmpDir = dir;
|
||||
});
|
||||
};
|
||||
|
||||
// Clone a git repository if non existant
|
||||
Git.prototype.clone = function(host, ref) {
|
||||
var that = this;
|
||||
|
||||
return this.allocateDir()
|
||||
|
||||
// Return or clone the git repo
|
||||
.then(function() {
|
||||
// Unique ID for repo/ref combinaison
|
||||
var repoId = that.repoID(host, ref);
|
||||
|
||||
// Absolute path to the folder
|
||||
var repoPath = path.join(that.tmpDir, repoId);
|
||||
|
||||
if (that.cloned[repoId]) return repoPath;
|
||||
|
||||
// Clone repo
|
||||
return command.exec('git clone '+host+' '+repoPath)
|
||||
|
||||
// Checkout reference if specified
|
||||
.then(function() {
|
||||
that.cloned[repoId] = true;
|
||||
|
||||
if (!ref) return;
|
||||
return command.exec('git checkout '+ref, { cwd: repoPath });
|
||||
})
|
||||
.thenResolve(repoPath);
|
||||
});
|
||||
};
|
||||
|
||||
// Get file from a git repo
|
||||
Git.prototype.resolve = function(giturl) {
|
||||
// Path to a file in a git repo?
|
||||
if (!Git.isUrl(giturl)) {
|
||||
if (this.resolveRoot(giturl)) return Promise(giturl);
|
||||
return Promise(null);
|
||||
}
|
||||
if (is.string(giturl)) giturl = Git.parseUrl(giturl);
|
||||
if (!giturl) return Promise(null);
|
||||
|
||||
// Clone or get from cache
|
||||
return this.clone(giturl.host, giturl.ref)
|
||||
.then(function(repo) {
|
||||
return path.resolve(repo, giturl.filepath);
|
||||
});
|
||||
};
|
||||
|
||||
// Return root of git repo from a filepath
|
||||
Git.prototype.resolveRoot = function(filepath) {
|
||||
var relativeToGit, repoId;
|
||||
|
||||
// No git repo cloned, or file is not in a git repository
|
||||
if (!this.tmpDir || !pathUtil.isInRoot(this.tmpDir, filepath)) return null;
|
||||
|
||||
// Extract first directory (is the repo id)
|
||||
relativeToGit = path.relative(this.tmpDir, filepath);
|
||||
repoId = relativeToGit.split(path.sep)[0];
|
||||
if (!repoId) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Return an absolute file
|
||||
return path.resolve(this.tmpDir, repoId);
|
||||
};
|
||||
|
||||
// Check if an url is a git dependency url
|
||||
Git.isUrl = function(giturl) {
|
||||
return (giturl.indexOf(GIT_PREFIX) === 0);
|
||||
};
|
||||
|
||||
// Parse and extract infos
|
||||
Git.parseUrl = function(giturl) {
|
||||
var ref, uri, fileParts, filepath;
|
||||
|
||||
if (!Git.isUrl(giturl)) return null;
|
||||
giturl = giturl.slice(GIT_PREFIX.length);
|
||||
|
||||
uri = new URI(giturl);
|
||||
ref = uri.fragment() || null;
|
||||
uri.fragment(null);
|
||||
|
||||
// Extract file inside the repo (after the .git)
|
||||
fileParts = uri.path().split('.git');
|
||||
filepath = fileParts.length > 1? fileParts.slice(1).join('.git') : '';
|
||||
if (filepath[0] == '/') {
|
||||
filepath = filepath.slice(1);
|
||||
}
|
||||
|
||||
// Recreate pathname without the real filename
|
||||
uri.path(fileParts[0] + '.git');
|
||||
|
||||
return {
|
||||
host: uri.toString(),
|
||||
ref: ref,
|
||||
filepath: filepath
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = Git;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
var gitbook = require('../gitbook');
|
||||
const gitbook = require('../gitbook');
|
||||
|
||||
describe('satisfies', function() {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
var tmp = require('tmp');
|
||||
var initBook = require('../init');
|
||||
const tmp = require('tmp');
|
||||
const initBook = require('../init');
|
||||
|
||||
describe('initBook', function() {
|
||||
|
||||
it('should create a README and SUMMARY for empty book', function() {
|
||||
var dir = tmp.dirSync();
|
||||
const dir = tmp.dirSync();
|
||||
|
||||
return initBook(dir.name)
|
||||
.then(function() {
|
||||
@@ -6,7 +6,7 @@
|
||||
@return {Config}
|
||||
*/
|
||||
function decodeGlobal(config, result) {
|
||||
var values = result.values;
|
||||
const values = result.values;
|
||||
|
||||
delete values.generator;
|
||||
delete values.output;
|
||||
@@ -1,4 +1,4 @@
|
||||
var decodeConfig = require('./decodeConfig');
|
||||
const decodeConfig = require('./decodeConfig');
|
||||
|
||||
/**
|
||||
Decode changes from a JS API to a output object.
|
||||
@@ -9,8 +9,8 @@ var decodeConfig = require('./decodeConfig');
|
||||
@return {Output}
|
||||
*/
|
||||
function decodeGlobal(output, result) {
|
||||
var book = output.getBook();
|
||||
var config = book.getConfig();
|
||||
let book = output.getBook();
|
||||
let config = book.getConfig();
|
||||
|
||||
// Update config
|
||||
config = decodeConfig(config, result.config);
|
||||
@@ -1,4 +1,4 @@
|
||||
var deprecate = require('./deprecate');
|
||||
const deprecate = require('./deprecate');
|
||||
|
||||
/**
|
||||
Decode changes from a JS API to a page object.
|
||||
@@ -10,7 +10,7 @@ var deprecate = require('./deprecate');
|
||||
@return {Page}
|
||||
*/
|
||||
function decodePage(output, page, result) {
|
||||
var originalContent = page.getContent();
|
||||
const originalContent = page.getContent();
|
||||
|
||||
// No returned value
|
||||
// Existing content will be used
|
||||
@@ -1,8 +1,8 @@
|
||||
var is = require('is');
|
||||
var objectPath = require('object-path');
|
||||
const is = require('is');
|
||||
const objectPath = require('object-path');
|
||||
|
||||
var logged = {};
|
||||
var disabled = {};
|
||||
const logged = {};
|
||||
const disabled = {};
|
||||
|
||||
/**
|
||||
Log a deprecated notice
|
||||
@@ -16,7 +16,7 @@ function logNotice(book, key, message) {
|
||||
|
||||
logged[key] = true;
|
||||
|
||||
var logger = book.getLogger();
|
||||
const logger = book.getLogger();
|
||||
logger.warn.ln(message);
|
||||
}
|
||||
|
||||
@@ -48,22 +48,22 @@ function deprecateMethod(book, key, fn, msg) {
|
||||
@return {Function}
|
||||
*/
|
||||
function deprecateField(book, key, instance, property, value, msg) {
|
||||
var store = undefined;
|
||||
let store = undefined;
|
||||
|
||||
var prepare = function() {
|
||||
const prepare = function() {
|
||||
if (!is.undefined(store)) return;
|
||||
|
||||
if (is.fn(value)) store = value();
|
||||
else store = value;
|
||||
};
|
||||
|
||||
var getter = function(){
|
||||
const getter = function() {
|
||||
prepare();
|
||||
|
||||
logNotice(book, key, msg);
|
||||
return store;
|
||||
};
|
||||
var setter = function(v) {
|
||||
const setter = function(v) {
|
||||
prepare();
|
||||
|
||||
logNotice(book, key, msg);
|
||||
@@ -108,7 +108,7 @@ function disableDeprecation(key) {
|
||||
*/
|
||||
function deprecateRenamedMethod(book, key, instance, oldName, newName, msg) {
|
||||
msg = msg || ('"' + oldName + '" is deprecated, use "' + newName + '()" instead');
|
||||
var fn = objectPath.get(instance, newName);
|
||||
const fn = objectPath.get(instance, newName);
|
||||
|
||||
instance[oldName] = deprecateMethod(book, key, fn, msg);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
var objectPath = require('object-path');
|
||||
var deprecate = require('./deprecate');
|
||||
const objectPath = require('object-path');
|
||||
const deprecate = require('./deprecate');
|
||||
|
||||
/**
|
||||
Encode a config object into a JS config api
|
||||
@@ -9,14 +9,14 @@ var deprecate = require('./deprecate');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeConfig(output, config) {
|
||||
var result = {
|
||||
const result = {
|
||||
values: config.getValues().toJS(),
|
||||
|
||||
get: function(key, defaultValue) {
|
||||
get(key, defaultValue) {
|
||||
return objectPath.get(result.values, key, defaultValue);
|
||||
},
|
||||
|
||||
set: function(key, value) {
|
||||
set(key, value) {
|
||||
return objectPath.set(result.values, key, value);
|
||||
}
|
||||
};
|
||||
+45
-43
@@ -1,19 +1,19 @@
|
||||
var path = require('path');
|
||||
var Promise = require('../utils/promise');
|
||||
var PathUtils = require('../utils/path');
|
||||
var fs = require('../utils/fs');
|
||||
const path = require('path');
|
||||
const Promise = require('../utils/promise');
|
||||
const PathUtils = require('../utils/path');
|
||||
const fs = require('../utils/fs');
|
||||
|
||||
var Plugins = require('../plugins');
|
||||
var deprecate = require('./deprecate');
|
||||
var fileToURL = require('../output/helper/fileToURL');
|
||||
var defaultBlocks = require('../constants/defaultBlocks');
|
||||
var gitbook = require('../gitbook');
|
||||
var parsers = require('../parsers');
|
||||
const Plugins = require('../plugins');
|
||||
const deprecate = require('./deprecate');
|
||||
const fileToURL = require('../output/helper/fileToURL');
|
||||
const defaultBlocks = require('../constants/defaultBlocks');
|
||||
const gitbook = require('../gitbook');
|
||||
const parsers = require('../parsers');
|
||||
|
||||
var encodeConfig = require('./encodeConfig');
|
||||
var encodeSummary = require('./encodeSummary');
|
||||
var encodeNavigation = require('./encodeNavigation');
|
||||
var encodePage = require('./encodePage');
|
||||
const encodeConfig = require('./encodeConfig');
|
||||
const encodeSummary = require('./encodeSummary');
|
||||
const encodeNavigation = require('./encodeNavigation');
|
||||
const encodePage = require('./encodePage');
|
||||
|
||||
/**
|
||||
Encode a global context into a JS object
|
||||
@@ -23,14 +23,14 @@ var encodePage = require('./encodePage');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeGlobal(output) {
|
||||
var book = output.getBook();
|
||||
var bookFS = book.getContentFS();
|
||||
var logger = output.getLogger();
|
||||
var outputFolder = output.getRoot();
|
||||
var plugins = output.getPlugins();
|
||||
var blocks = Plugins.listBlocks(plugins);
|
||||
const book = output.getBook();
|
||||
const bookFS = book.getContentFS();
|
||||
const logger = output.getLogger();
|
||||
const outputFolder = output.getRoot();
|
||||
const plugins = output.getPlugins();
|
||||
const blocks = Plugins.listBlocks(plugins);
|
||||
|
||||
var result = {
|
||||
const result = {
|
||||
log: logger,
|
||||
config: encodeConfig(output, book.getConfig()),
|
||||
summary: encodeSummary(output, book.getSummary()),
|
||||
@@ -40,7 +40,7 @@ function encodeGlobal(output) {
|
||||
|
||||
@return {Boolean}
|
||||
*/
|
||||
isMultilingual: function() {
|
||||
isMultilingual() {
|
||||
return book.isMultilingual();
|
||||
},
|
||||
|
||||
@@ -49,7 +49,7 @@ function encodeGlobal(output) {
|
||||
|
||||
@return {Boolean}
|
||||
*/
|
||||
isLanguageBook: function() {
|
||||
isLanguageBook() {
|
||||
return book.isLanguageBook();
|
||||
},
|
||||
|
||||
@@ -59,7 +59,7 @@ function encodeGlobal(output) {
|
||||
@param {String} fileName
|
||||
@return {Promise<Buffer>}
|
||||
*/
|
||||
readFile: function(fileName) {
|
||||
readFile(fileName) {
|
||||
return bookFS.read(fileName);
|
||||
},
|
||||
|
||||
@@ -69,7 +69,7 @@ function encodeGlobal(output) {
|
||||
@param {String} fileName
|
||||
@return {Promise<String>}
|
||||
*/
|
||||
readFileAsString: function(fileName) {
|
||||
readFileAsString(fileName) {
|
||||
return bookFS.readAsString(fileName);
|
||||
},
|
||||
|
||||
@@ -79,7 +79,7 @@ function encodeGlobal(output) {
|
||||
@param {String} fileName
|
||||
@return {String}
|
||||
*/
|
||||
resolve: function(fileName) {
|
||||
resolve(fileName) {
|
||||
return path.resolve(book.getContentRoot(), fileName);
|
||||
},
|
||||
|
||||
@@ -89,8 +89,8 @@ function encodeGlobal(output) {
|
||||
@param {String} filePath
|
||||
@return {String}
|
||||
*/
|
||||
getPageByPath: function(filePath) {
|
||||
var page = output.getPage(filePath);
|
||||
getPageByPath(filePath) {
|
||||
const page = output.getPage(filePath);
|
||||
if (!page) return undefined;
|
||||
|
||||
return encodePage(output, page);
|
||||
@@ -103,8 +103,8 @@ function encodeGlobal(output) {
|
||||
@param {String} text
|
||||
@return {Promise<String>}
|
||||
*/
|
||||
renderBlock: function(type, text) {
|
||||
var parser = parsers.get(type);
|
||||
renderBlock(type, text) {
|
||||
const parser = parsers.get(type);
|
||||
|
||||
return parser.parsePage(text)
|
||||
.get('content');
|
||||
@@ -117,14 +117,15 @@ function encodeGlobal(output) {
|
||||
@param {String} text
|
||||
@return {Promise<String>}
|
||||
*/
|
||||
renderInline: function(type, text) {
|
||||
var parser = parsers.get(type);
|
||||
renderInline(type, text) {
|
||||
const parser = parsers.get(type);
|
||||
|
||||
return parser.parseInline(text)
|
||||
.get('content');
|
||||
},
|
||||
|
||||
template: {
|
||||
|
||||
/**
|
||||
Apply a templating block and returns its result
|
||||
|
||||
@@ -132,13 +133,14 @@ function encodeGlobal(output) {
|
||||
@param {Object} blockData
|
||||
@return {Promise|Object}
|
||||
*/
|
||||
applyBlock: function(name, blockData) {
|
||||
var block = blocks.get(name) || defaultBlocks.get(name);
|
||||
applyBlock(name, blockData) {
|
||||
const block = blocks.get(name) || defaultBlocks.get(name);
|
||||
return Promise(block.applyBlock(blockData, result));
|
||||
}
|
||||
},
|
||||
|
||||
output: {
|
||||
|
||||
/**
|
||||
Name of the generator being used
|
||||
{String}
|
||||
@@ -149,7 +151,7 @@ function encodeGlobal(output) {
|
||||
Return absolute path to the root folder of output
|
||||
@return {String}
|
||||
*/
|
||||
root: function() {
|
||||
root() {
|
||||
return outputFolder;
|
||||
},
|
||||
|
||||
@@ -159,7 +161,7 @@ function encodeGlobal(output) {
|
||||
@param {String} fileName
|
||||
@return {String}
|
||||
*/
|
||||
resolve: function(fileName) {
|
||||
resolve(fileName) {
|
||||
return path.resolve(outputFolder, fileName);
|
||||
},
|
||||
|
||||
@@ -167,7 +169,7 @@ function encodeGlobal(output) {
|
||||
Convert a filepath into an url
|
||||
@return {String}
|
||||
*/
|
||||
toURL: function(filePath) {
|
||||
toURL(filePath) {
|
||||
return fileToURL(output, filePath);
|
||||
},
|
||||
|
||||
@@ -177,10 +179,10 @@ function encodeGlobal(output) {
|
||||
@param {String} fileName
|
||||
@return {Promise}
|
||||
*/
|
||||
hasFile: function(fileName, content) {
|
||||
hasFile(fileName, content) {
|
||||
return Promise()
|
||||
.then(function() {
|
||||
var filePath = PathUtils.resolveInRoot(outputFolder, fileName);
|
||||
const filePath = PathUtils.resolveInRoot(outputFolder, fileName);
|
||||
|
||||
return fs.exists(filePath);
|
||||
});
|
||||
@@ -194,10 +196,10 @@ function encodeGlobal(output) {
|
||||
@param {Buffer} content
|
||||
@return {Promise}
|
||||
*/
|
||||
writeFile: function(fileName, content) {
|
||||
writeFile(fileName, content) {
|
||||
return Promise()
|
||||
.then(function() {
|
||||
var filePath = PathUtils.resolveInRoot(outputFolder, fileName);
|
||||
const filePath = PathUtils.resolveInRoot(outputFolder, fileName);
|
||||
|
||||
return fs.ensureFile(filePath)
|
||||
.then(function() {
|
||||
@@ -215,10 +217,10 @@ function encodeGlobal(output) {
|
||||
@param {Buffer} content
|
||||
@return {Promise}
|
||||
*/
|
||||
copyFile: function(inputFile, outputFile, content) {
|
||||
copyFile(inputFile, outputFile, content) {
|
||||
return Promise()
|
||||
.then(function() {
|
||||
var outputFilePath = PathUtils.resolveInRoot(outputFolder, outputFile);
|
||||
const outputFilePath = PathUtils.resolveInRoot(outputFolder, outputFile);
|
||||
|
||||
return fs.ensureFile(outputFilePath)
|
||||
.then(function() {
|
||||
+12
-12
@@ -1,4 +1,4 @@
|
||||
var Immutable = require('immutable');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
/**
|
||||
Encode an article for next/prev
|
||||
@@ -8,7 +8,7 @@ var Immutable = require('immutable');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeArticle(pages, article) {
|
||||
var articlePath = article.getPath();
|
||||
const articlePath = article.getPath();
|
||||
|
||||
return {
|
||||
path: articlePath,
|
||||
@@ -26,21 +26,21 @@ function encodeArticle(pages, article) {
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeNavigation(output) {
|
||||
var book = output.getBook();
|
||||
var pages = output.getPages();
|
||||
var summary = book.getSummary();
|
||||
var articles = summary.getArticlesAsList();
|
||||
const book = output.getBook();
|
||||
const pages = output.getPages();
|
||||
const summary = book.getSummary();
|
||||
const articles = summary.getArticlesAsList();
|
||||
|
||||
|
||||
var navigation = articles
|
||||
const navigation = articles
|
||||
.map(function(article, i) {
|
||||
var ref = article.getRef();
|
||||
const ref = article.getRef();
|
||||
if (!ref) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var prev = articles.get(i - 1);
|
||||
var next = articles.get(i + 1);
|
||||
const prev = articles.get(i - 1);
|
||||
const next = articles.get(i + 1);
|
||||
|
||||
return [
|
||||
ref,
|
||||
@@ -48,8 +48,8 @@ function encodeNavigation(output) {
|
||||
index: i,
|
||||
title: article.getTitle(),
|
||||
introduction: (i === 0),
|
||||
prev: prev? encodeArticle(pages, prev) : undefined,
|
||||
next: next? encodeArticle(pages, next) : undefined,
|
||||
prev: prev ? encodeArticle(pages, prev) : undefined,
|
||||
next: next ? encodeArticle(pages, next) : undefined,
|
||||
level: article.getLevel()
|
||||
}
|
||||
];
|
||||
@@ -1,6 +1,6 @@
|
||||
var JSONUtils = require('../json');
|
||||
var deprecate = require('./deprecate');
|
||||
var encodeProgress = require('./encodeProgress');
|
||||
const JSONUtils = require('../json');
|
||||
const deprecate = require('./deprecate');
|
||||
const encodeProgress = require('./encodeProgress');
|
||||
|
||||
/**
|
||||
Encode a page in a context to a JS API
|
||||
@@ -10,13 +10,13 @@ var encodeProgress = require('./encodeProgress');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodePage(output, page) {
|
||||
var book = output.getBook();
|
||||
var summary = book.getSummary();
|
||||
var fs = book.getContentFS();
|
||||
var file = page.getFile();
|
||||
const book = output.getBook();
|
||||
const summary = book.getSummary();
|
||||
const fs = book.getContentFS();
|
||||
const file = page.getFile();
|
||||
|
||||
// JS Page is based on the JSON output
|
||||
var result = JSONUtils.encodePage(page, summary);
|
||||
const result = JSONUtils.encodePage(page, summary);
|
||||
|
||||
result.type = file.getType();
|
||||
result.path = file.getPath();
|
||||
+11
-11
@@ -1,5 +1,5 @@
|
||||
var Immutable = require('immutable');
|
||||
var encodeNavigation = require('./encodeNavigation');
|
||||
const Immutable = require('immutable');
|
||||
const encodeNavigation = require('./encodeNavigation');
|
||||
|
||||
/**
|
||||
page.progress is a deprecated property from GitBook v2
|
||||
@@ -9,15 +9,15 @@ var encodeNavigation = require('./encodeNavigation');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeProgress(output, page) {
|
||||
var current = page.getPath();
|
||||
var navigation = encodeNavigation(output);
|
||||
const current = page.getPath();
|
||||
let navigation = encodeNavigation(output);
|
||||
navigation = Immutable.Map(navigation);
|
||||
|
||||
var n = navigation.size;
|
||||
var percent = 0, prevPercent = 0, currentChapter = null;
|
||||
var done = true;
|
||||
const n = navigation.size;
|
||||
let percent = 0, prevPercent = 0, currentChapter = null;
|
||||
let done = true;
|
||||
|
||||
var chapters = navigation
|
||||
const chapters = navigation
|
||||
.map(function(nav, chapterPath) {
|
||||
nav.path = chapterPath;
|
||||
return nav;
|
||||
@@ -46,13 +46,13 @@ function encodeProgress(output, page) {
|
||||
|
||||
return {
|
||||
// Previous percent
|
||||
prevPercent: prevPercent,
|
||||
prevPercent,
|
||||
|
||||
// Current percent
|
||||
percent: percent,
|
||||
percent,
|
||||
|
||||
// List of chapter with progress
|
||||
chapters: chapters,
|
||||
chapters,
|
||||
|
||||
// Current chapter
|
||||
current: currentChapter
|
||||
+11
-10
@@ -1,4 +1,4 @@
|
||||
var encodeSummaryArticle = require('../json/encodeSummaryArticle');
|
||||
const encodeSummaryArticle = require('../json/encodeSummaryArticle');
|
||||
|
||||
/**
|
||||
Encode summary to provide an API to plugin
|
||||
@@ -8,15 +8,16 @@ var encodeSummaryArticle = require('../json/encodeSummaryArticle');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeSummary(output, summary) {
|
||||
var result = {
|
||||
const result = {
|
||||
|
||||
/**
|
||||
Iterate over the summary, it stops when the "iter" returns false
|
||||
|
||||
@param {Function} iter
|
||||
*/
|
||||
walk: function (iter) {
|
||||
walk(iter) {
|
||||
summary.getArticle(function(article) {
|
||||
var jsonArticle = encodeSummaryArticle(article, false);
|
||||
const jsonArticle = encodeSummaryArticle(article, false);
|
||||
|
||||
return iter(jsonArticle);
|
||||
});
|
||||
@@ -28,9 +29,9 @@ function encodeSummary(output, summary) {
|
||||
@param {String} level
|
||||
@return {Object}
|
||||
*/
|
||||
getArticleByLevel: function(level) {
|
||||
var article = summary.getByLevel(level);
|
||||
return (article? encodeSummaryArticle(article) : undefined);
|
||||
getArticleByLevel(level) {
|
||||
const article = summary.getByLevel(level);
|
||||
return (article ? encodeSummaryArticle(article) : undefined);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -39,9 +40,9 @@ function encodeSummary(output, summary) {
|
||||
@param {String} level
|
||||
@return {Object}
|
||||
*/
|
||||
getArticleByPath: function(level) {
|
||||
var article = summary.getByPath(level);
|
||||
return (article? encodeSummaryArticle(article) : undefined);
|
||||
getArticleByPath(level) {
|
||||
const article = summary.getByPath(level);
|
||||
return (article ? encodeSummaryArticle(article) : undefined);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var Modifiers = require('./modifiers');
|
||||
const Modifiers = require('./modifiers');
|
||||
|
||||
module.exports = {
|
||||
Parse: require('./parse'),
|
||||
@@ -1,10 +1,10 @@
|
||||
var Parse = require('../parse');
|
||||
var Output = require('../output');
|
||||
var timing = require('../utils/timing');
|
||||
const Parse = require('../parse');
|
||||
const Output = require('../output');
|
||||
const timing = require('../utils/timing');
|
||||
|
||||
var options = require('./options');
|
||||
var getBook = require('./getBook');
|
||||
var getOutputFolder = require('./getOutputFolder');
|
||||
const options = require('./options');
|
||||
const getBook = require('./getBook');
|
||||
const getOutputFolder = require('./getOutputFolder');
|
||||
|
||||
|
||||
module.exports = {
|
||||
@@ -15,11 +15,11 @@ module.exports = {
|
||||
options.format,
|
||||
options.timing
|
||||
],
|
||||
exec: function(args, kwargs) {
|
||||
var book = getBook(args, kwargs);
|
||||
var outputFolder = getOutputFolder(args);
|
||||
exec(args, kwargs) {
|
||||
const book = getBook(args, kwargs);
|
||||
const outputFolder = getOutputFolder(args);
|
||||
|
||||
var Generator = Output.getGenerator(kwargs.format);
|
||||
const Generator = Output.getGenerator(kwargs.format);
|
||||
|
||||
return Parse.parseBook(book)
|
||||
.then(function(resultBook) {
|
||||
@@ -1,13 +1,13 @@
|
||||
var path = require('path');
|
||||
var tmp = require('tmp');
|
||||
const path = require('path');
|
||||
const tmp = require('tmp');
|
||||
|
||||
var Promise = require('../utils/promise');
|
||||
var fs = require('../utils/fs');
|
||||
var Parse = require('../parse');
|
||||
var Output = require('../output');
|
||||
const Promise = require('../utils/promise');
|
||||
const fs = require('../utils/fs');
|
||||
const Parse = require('../parse');
|
||||
const Output = require('../output');
|
||||
|
||||
var options = require('./options');
|
||||
var getBook = require('./getBook');
|
||||
const options = require('./options');
|
||||
const getBook = require('./getBook');
|
||||
|
||||
|
||||
module.exports = function(format) {
|
||||
@@ -17,37 +17,37 @@ module.exports = function(format) {
|
||||
options: [
|
||||
options.log
|
||||
],
|
||||
exec: function(args, kwargs) {
|
||||
var extension = '.' + format;
|
||||
exec(args, kwargs) {
|
||||
const extension = '.' + format;
|
||||
|
||||
// Output file will be stored in
|
||||
var outputFile = args[1] || ('book' + extension);
|
||||
const outputFile = args[1] || ('book' + extension);
|
||||
|
||||
// Create temporary directory
|
||||
var outputFolder = tmp.dirSync().name;
|
||||
const outputFolder = tmp.dirSync().name;
|
||||
|
||||
var book = getBook(args, kwargs);
|
||||
var logger = book.getLogger();
|
||||
var Generator = Output.getGenerator('ebook');
|
||||
const book = getBook(args, kwargs);
|
||||
const logger = book.getLogger();
|
||||
const Generator = Output.getGenerator('ebook');
|
||||
|
||||
return Parse.parseBook(book)
|
||||
.then(function(resultBook) {
|
||||
return Output.generate(Generator, resultBook, {
|
||||
root: outputFolder,
|
||||
format: format
|
||||
format
|
||||
});
|
||||
})
|
||||
|
||||
// Extract ebook file
|
||||
.then(function(output) {
|
||||
var book = output.getBook();
|
||||
var languages = book.getLanguages();
|
||||
const book = output.getBook();
|
||||
const languages = book.getLanguages();
|
||||
|
||||
if (book.isMultilingual()) {
|
||||
return Promise.forEach(languages.getList(), function(lang) {
|
||||
var langID = lang.getID();
|
||||
const langID = lang.getID();
|
||||
|
||||
var langOutputFile = path.join(
|
||||
const langOutputFile = path.join(
|
||||
path.dirname(outputFile),
|
||||
path.basename(outputFile, extension) + '_' + langID + extension
|
||||
);
|
||||
@@ -0,0 +1,23 @@
|
||||
const path = require('path');
|
||||
const Book = require('../models/book');
|
||||
const createNodeFS = require('../fs/node');
|
||||
|
||||
/**
|
||||
Return a book instance to work on from
|
||||
command line args/kwargs
|
||||
|
||||
@param {Array} args
|
||||
@param {Object} kwargs
|
||||
@return {Book}
|
||||
*/
|
||||
function getBook(args, kwargs) {
|
||||
const input = path.resolve(args[0] || process.cwd());
|
||||
const logLevel = kwargs.log;
|
||||
|
||||
const fs = createNodeFS(input);
|
||||
const book = Book.createForFS(fs);
|
||||
|
||||
return book.setLogLevel(logLevel);
|
||||
}
|
||||
|
||||
module.exports = getBook;
|
||||
@@ -0,0 +1,17 @@
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
Return path to output folder
|
||||
|
||||
@param {Array} args
|
||||
@return {String}
|
||||
*/
|
||||
function getOutputFolder(args) {
|
||||
const bookRoot = path.resolve(args[0] || process.cwd());
|
||||
const defaultOutputRoot = path.join(bookRoot, '_book');
|
||||
const outputFolder = args[1] ? path.resolve(process.cwd(), args[1]) : defaultOutputRoot;
|
||||
|
||||
return outputFolder;
|
||||
}
|
||||
|
||||
module.exports = getOutputFolder;
|
||||
@@ -1,4 +1,4 @@
|
||||
var buildEbook = require('./buildEbook');
|
||||
const buildEbook = require('./buildEbook');
|
||||
|
||||
module.exports = [
|
||||
require('./build'),
|
||||
@@ -0,0 +1,17 @@
|
||||
const path = require('path');
|
||||
|
||||
const options = require('./options');
|
||||
const initBook = require('../init');
|
||||
|
||||
module.exports = {
|
||||
name: 'init [book]',
|
||||
description: 'setup and create files for chapters',
|
||||
options: [
|
||||
options.log
|
||||
],
|
||||
exec(args, kwargs) {
|
||||
const bookRoot = path.resolve(process.cwd(), args[0] || './');
|
||||
|
||||
return initBook(bookRoot);
|
||||
}
|
||||
};
|
||||
@@ -1,8 +1,8 @@
|
||||
var options = require('./options');
|
||||
var getBook = require('./getBook');
|
||||
const options = require('./options');
|
||||
const getBook = require('./getBook');
|
||||
|
||||
var Parse = require('../parse');
|
||||
var Plugins = require('../plugins');
|
||||
const Parse = require('../parse');
|
||||
const Plugins = require('../plugins');
|
||||
|
||||
module.exports = {
|
||||
name: 'install [book]',
|
||||
@@ -10,8 +10,8 @@ module.exports = {
|
||||
options: [
|
||||
options.log
|
||||
],
|
||||
exec: function(args, kwargs) {
|
||||
var book = getBook(args, kwargs);
|
||||
exec(args, kwargs) {
|
||||
const book = getBook(args, kwargs);
|
||||
|
||||
return Parse.parseConfig(book)
|
||||
.then(function(resultBook) {
|
||||
@@ -1,6 +1,6 @@
|
||||
var Logger = require('../utils/logger');
|
||||
const Logger = require('../utils/logger');
|
||||
|
||||
var logOptions = {
|
||||
const logOptions = {
|
||||
name: 'log',
|
||||
description: 'Minimum log level to display',
|
||||
values: Logger.LEVELS
|
||||
@@ -11,14 +11,14 @@ var logOptions = {
|
||||
defaults: 'info'
|
||||
};
|
||||
|
||||
var formatOption = {
|
||||
const formatOption = {
|
||||
name: 'format',
|
||||
description: 'Format to build to',
|
||||
values: ['website', 'json', 'ebook'],
|
||||
defaults: 'website'
|
||||
};
|
||||
|
||||
var timingOption = {
|
||||
const timingOption = {
|
||||
name: 'timing',
|
||||
description: 'Print timing debug information',
|
||||
defaults: false
|
||||
@@ -1,22 +1,22 @@
|
||||
var options = require('./options');
|
||||
var getBook = require('./getBook');
|
||||
const options = require('./options');
|
||||
const getBook = require('./getBook');
|
||||
|
||||
var Parse = require('../parse');
|
||||
const Parse = require('../parse');
|
||||
|
||||
function printBook(book) {
|
||||
var logger = book.getLogger();
|
||||
const logger = book.getLogger();
|
||||
|
||||
var config = book.getConfig();
|
||||
var configFile = config.getFile();
|
||||
const config = book.getConfig();
|
||||
const configFile = config.getFile();
|
||||
|
||||
var summary = book.getSummary();
|
||||
var summaryFile = summary.getFile();
|
||||
const summary = book.getSummary();
|
||||
const summaryFile = summary.getFile();
|
||||
|
||||
var readme = book.getReadme();
|
||||
var readmeFile = readme.getFile();
|
||||
const readme = book.getReadme();
|
||||
const readmeFile = readme.getFile();
|
||||
|
||||
var glossary = book.getGlossary();
|
||||
var glossaryFile = glossary.getFile();
|
||||
const glossary = book.getGlossary();
|
||||
const glossaryFile = glossary.getFile();
|
||||
|
||||
if (configFile.exists()) {
|
||||
logger.info.ln('Configuration file is', configFile.getPath());
|
||||
@@ -36,9 +36,9 @@ function printBook(book) {
|
||||
}
|
||||
|
||||
function printMultingualBook(book) {
|
||||
var logger = book.getLogger();
|
||||
var languages = book.getLanguages();
|
||||
var books = book.getBooks();
|
||||
const logger = book.getLogger();
|
||||
const languages = book.getLanguages();
|
||||
const books = book.getBooks();
|
||||
|
||||
logger.info.ln(languages.size + ' languages');
|
||||
|
||||
@@ -55,14 +55,14 @@ module.exports = {
|
||||
options: [
|
||||
options.log
|
||||
],
|
||||
exec: function(args, kwargs) {
|
||||
var book = getBook(args, kwargs);
|
||||
var logger = book.getLogger();
|
||||
exec(args, kwargs) {
|
||||
const book = getBook(args, kwargs);
|
||||
const logger = book.getLogger();
|
||||
|
||||
return Parse.parseBook(book)
|
||||
.then(function(resultBook) {
|
||||
var rootFolder = book.getRoot();
|
||||
var contentFolder = book.getContentRoot();
|
||||
const rootFolder = book.getRoot();
|
||||
const contentFolder = book.getContentRoot();
|
||||
|
||||
logger.info.ln('Book located in:', rootFolder);
|
||||
if (contentFolder != rootFolder) {
|
||||
@@ -1,24 +1,24 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
var tinylr = require('tiny-lr');
|
||||
var open = require('open');
|
||||
const tinylr = require('tiny-lr');
|
||||
const open = require('open');
|
||||
|
||||
var Parse = require('../parse');
|
||||
var Output = require('../output');
|
||||
var ConfigModifier = require('../modifiers').Config;
|
||||
const Parse = require('../parse');
|
||||
const Output = require('../output');
|
||||
const ConfigModifier = require('../modifiers').Config;
|
||||
|
||||
var Promise = require('../utils/promise');
|
||||
const Promise = require('../utils/promise');
|
||||
|
||||
var options = require('./options');
|
||||
var getBook = require('./getBook');
|
||||
var getOutputFolder = require('./getOutputFolder');
|
||||
var Server = require('./server');
|
||||
var watch = require('./watch');
|
||||
const options = require('./options');
|
||||
const getBook = require('./getBook');
|
||||
const getOutputFolder = require('./getOutputFolder');
|
||||
const Server = require('./server');
|
||||
const watch = require('./watch');
|
||||
|
||||
var server, lrServer, lrPath;
|
||||
let server, lrServer, lrPath;
|
||||
|
||||
function waitForCtrlC() {
|
||||
var d = Promise.defer();
|
||||
const d = Promise.defer();
|
||||
|
||||
process.on('SIGINT', function() {
|
||||
d.resolve();
|
||||
@@ -29,15 +29,15 @@ function waitForCtrlC() {
|
||||
|
||||
|
||||
function generateBook(args, kwargs) {
|
||||
var port = kwargs.port;
|
||||
var outputFolder = getOutputFolder(args);
|
||||
var book = getBook(args, kwargs);
|
||||
var Generator = Output.getGenerator(kwargs.format);
|
||||
var browser = kwargs['browser'];
|
||||
const port = kwargs.port;
|
||||
const outputFolder = getOutputFolder(args);
|
||||
const book = getBook(args, kwargs);
|
||||
const Generator = Output.getGenerator(kwargs.format);
|
||||
const browser = kwargs['browser'];
|
||||
|
||||
var hasWatch = kwargs['watch'];
|
||||
var hasLiveReloading = kwargs['live'];
|
||||
var hasOpen = kwargs['open'];
|
||||
const hasWatch = kwargs['watch'];
|
||||
const hasLiveReloading = kwargs['live'];
|
||||
const hasOpen = kwargs['open'];
|
||||
|
||||
// Stop server if running
|
||||
if (server.isRunning()) console.log('Stopping server');
|
||||
@@ -48,7 +48,7 @@ function generateBook(args, kwargs) {
|
||||
.then(function(resultBook) {
|
||||
if (hasLiveReloading) {
|
||||
// Enable livereload plugin
|
||||
var config = resultBook.getConfig();
|
||||
let config = resultBook.getConfig();
|
||||
config = ConfigModifier.addPlugin(config, 'livereload');
|
||||
resultBook = resultBook.set('config', config);
|
||||
}
|
||||
@@ -64,7 +64,7 @@ function generateBook(args, kwargs) {
|
||||
return server.start(outputFolder, port);
|
||||
})
|
||||
.then(function() {
|
||||
console.log('Serving book on http://localhost:'+port);
|
||||
console.log('Serving book on http://localhost:' + port);
|
||||
|
||||
if (lrPath && hasLiveReloading) {
|
||||
// trigger livereload
|
||||
@@ -76,7 +76,7 @@ function generateBook(args, kwargs) {
|
||||
}
|
||||
|
||||
if (hasOpen) {
|
||||
open('http://localhost:'+port, browser);
|
||||
open('http://localhost:' + port, browser);
|
||||
}
|
||||
})
|
||||
.then(function() {
|
||||
@@ -132,10 +132,10 @@ module.exports = {
|
||||
options.log,
|
||||
options.format
|
||||
],
|
||||
exec: function(args, kwargs) {
|
||||
exec(args, kwargs) {
|
||||
server = new Server();
|
||||
var hasWatch = kwargs['watch'];
|
||||
var hasLiveReloading = kwargs['live'];
|
||||
const hasWatch = kwargs['watch'];
|
||||
const hasLiveReloading = kwargs['live'];
|
||||
|
||||
return Promise()
|
||||
.then(function() {
|
||||
@@ -0,0 +1,127 @@
|
||||
const events = require('events');
|
||||
const http = require('http');
|
||||
const send = require('send');
|
||||
const url = require('url');
|
||||
|
||||
const Promise = require('../utils/promise');
|
||||
|
||||
class Server extends events.EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.running = null;
|
||||
this.dir = null;
|
||||
this.port = 0;
|
||||
this.sockets = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the server is running
|
||||
* @return {Boolean}
|
||||
*/
|
||||
isRunning() {
|
||||
return !!this.running;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the server
|
||||
* @return {Promise}
|
||||
*/
|
||||
stop() {
|
||||
const that = this;
|
||||
if (!this.isRunning()) return Promise();
|
||||
|
||||
const d = Promise.defer();
|
||||
this.running.close(function(err) {
|
||||
that.running = null;
|
||||
that.emit('state', false);
|
||||
|
||||
if (err) d.reject(err);
|
||||
else d.resolve();
|
||||
});
|
||||
|
||||
for (let i = 0; i < this.sockets.length; i++) {
|
||||
this.sockets[i].destroy();
|
||||
}
|
||||
|
||||
return d.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the server
|
||||
* @return {Promise}
|
||||
*/
|
||||
start(dir, port) {
|
||||
const that = this;
|
||||
let pre = Promise();
|
||||
port = port || 8004;
|
||||
|
||||
if (that.isRunning()) pre = this.stop();
|
||||
return pre
|
||||
.then(function() {
|
||||
const d = Promise.defer();
|
||||
|
||||
that.running = http.createServer(function(req, res) {
|
||||
// Render error
|
||||
function error(err) {
|
||||
res.statusCode = err.status || 500;
|
||||
res.end(err.message);
|
||||
}
|
||||
|
||||
// Redirect to directory's index.html
|
||||
function redirect() {
|
||||
const resultURL = urlTransform(req.url, function(parsed) {
|
||||
parsed.pathname += '/';
|
||||
return parsed;
|
||||
});
|
||||
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', resultURL);
|
||||
res.end('Redirecting to ' + resultURL);
|
||||
}
|
||||
|
||||
res.setHeader('X-Current-Location', req.url);
|
||||
|
||||
// Send file
|
||||
send(req, url.parse(req.url).pathname, {
|
||||
root: dir
|
||||
})
|
||||
.on('error', error)
|
||||
.on('directory', redirect)
|
||||
.pipe(res);
|
||||
});
|
||||
|
||||
that.running.on('connection', function(socket) {
|
||||
that.sockets.push(socket);
|
||||
socket.setTimeout(4000);
|
||||
socket.on('close', function() {
|
||||
that.sockets.splice(that.sockets.indexOf(socket), 1);
|
||||
});
|
||||
});
|
||||
|
||||
that.running.listen(port, function(err) {
|
||||
if (err) return d.reject(err);
|
||||
|
||||
that.port = port;
|
||||
that.dir = dir;
|
||||
that.emit('state', true);
|
||||
d.resolve();
|
||||
});
|
||||
|
||||
return d.promise;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* urlTransform is a helper function that allows a function to transform
|
||||
* a url string in it's parsed form and returns the new url as a string
|
||||
*
|
||||
* @param {String} uri
|
||||
* @param {Function} fn
|
||||
* @return {String}
|
||||
*/
|
||||
function urlTransform(uri, fn) {
|
||||
return url.format(fn(url.parse(uri)));
|
||||
}
|
||||
|
||||
module.exports = Server;
|
||||
@@ -1,8 +1,8 @@
|
||||
var path = require('path');
|
||||
var chokidar = require('chokidar');
|
||||
const path = require('path');
|
||||
const chokidar = require('chokidar');
|
||||
|
||||
var Promise = require('../utils/promise');
|
||||
var parsers = require('../parsers');
|
||||
const Promise = require('../utils/promise');
|
||||
const parsers = require('../parsers');
|
||||
|
||||
/**
|
||||
Watch a folder and resolve promise once a file is modified
|
||||
@@ -11,19 +11,19 @@ var parsers = require('../parsers');
|
||||
@return {Promise}
|
||||
*/
|
||||
function watch(dir) {
|
||||
var d = Promise.defer();
|
||||
const d = Promise.defer();
|
||||
dir = path.resolve(dir);
|
||||
|
||||
var toWatch = [
|
||||
const toWatch = [
|
||||
'book.json', 'book.js', '_layouts/**'
|
||||
];
|
||||
|
||||
// Watch all parsable files
|
||||
parsers.extensions.forEach(function(ext) {
|
||||
toWatch.push('**/*'+ext);
|
||||
toWatch.push('**/*' + ext);
|
||||
});
|
||||
|
||||
var watcher = chokidar.watch(toWatch, {
|
||||
const watcher = chokidar.watch(toWatch, {
|
||||
cwd: dir,
|
||||
ignored: '_book/**',
|
||||
ignoreInitial: true
|
||||
+6
-6
@@ -1,10 +1,10 @@
|
||||
var jsonschema = require('jsonschema');
|
||||
var schema = require('../configSchema');
|
||||
const jsonschema = require('jsonschema');
|
||||
const schema = require('../configSchema');
|
||||
|
||||
describe('configSchema', function() {
|
||||
|
||||
function validate(cfg) {
|
||||
var v = new jsonschema.Validator();
|
||||
const v = new jsonschema.Validator();
|
||||
return v.validate(cfg, schema, {
|
||||
propertyName: 'config'
|
||||
});
|
||||
@@ -13,7 +13,7 @@ describe('configSchema', function() {
|
||||
describe('structure', function() {
|
||||
|
||||
it('should accept dot in filename', function() {
|
||||
var result = validate({
|
||||
const result = validate({
|
||||
structure: {
|
||||
readme: 'book-intro.adoc'
|
||||
}
|
||||
@@ -23,7 +23,7 @@ describe('configSchema', function() {
|
||||
});
|
||||
|
||||
it('should accept uppercase in filename', function() {
|
||||
var result = validate({
|
||||
const result = validate({
|
||||
structure: {
|
||||
readme: 'BOOK.adoc'
|
||||
}
|
||||
@@ -33,7 +33,7 @@ describe('configSchema', function() {
|
||||
});
|
||||
|
||||
it('should not accept filepath', function() {
|
||||
var result = validate({
|
||||
const result = validate({
|
||||
structure: {
|
||||
readme: 'folder/myFile.md'
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
const Immutable = require('immutable');
|
||||
const jsonSchemaDefaults = require('json-schema-defaults');
|
||||
|
||||
const schema = require('./configSchema');
|
||||
|
||||
module.exports = Immutable.fromJS(jsonSchemaDefaults(schema));
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
var FILENAME_REGEX = '^[a-zA-Z-._\d,\s]+$';
|
||||
const FILENAME_REGEX = '^[a-zA-Z-._\d,\s]+$';
|
||||
|
||||
module.exports = {
|
||||
'$schema': 'http://json-schema.org/schema#',
|
||||
+7
-7
@@ -1,17 +1,17 @@
|
||||
var Immutable = require('immutable');
|
||||
var TemplateBlock = require('../models/templateBlock');
|
||||
const Immutable = require('immutable');
|
||||
const TemplateBlock = require('../models/templateBlock');
|
||||
|
||||
module.exports = Immutable.Map({
|
||||
html: TemplateBlock({
|
||||
name: 'html',
|
||||
process: function(blk) {
|
||||
process(blk) {
|
||||
return blk;
|
||||
}
|
||||
}),
|
||||
|
||||
code: TemplateBlock({
|
||||
name: 'code',
|
||||
process: function(blk) {
|
||||
process(blk) {
|
||||
return {
|
||||
html: false,
|
||||
body: blk.body
|
||||
@@ -21,7 +21,7 @@ module.exports = Immutable.Map({
|
||||
|
||||
markdown: TemplateBlock({
|
||||
name: 'markdown',
|
||||
process: function(blk) {
|
||||
process(blk) {
|
||||
return this.book.renderInline('markdown', blk.body)
|
||||
.then(function(out) {
|
||||
return { body: out };
|
||||
@@ -31,7 +31,7 @@ module.exports = Immutable.Map({
|
||||
|
||||
asciidoc: TemplateBlock({
|
||||
name: 'asciidoc',
|
||||
process: function(blk) {
|
||||
process(blk) {
|
||||
return this.book.renderInline('asciidoc', blk.body)
|
||||
.then(function(out) {
|
||||
return { body: out };
|
||||
@@ -41,7 +41,7 @@ module.exports = Immutable.Map({
|
||||
|
||||
markup: TemplateBlock({
|
||||
name: 'markup',
|
||||
process: function(blk) {
|
||||
process(blk) {
|
||||
return this.book.renderInline(this.ctx.file.type, blk.body)
|
||||
.then(function(out) {
|
||||
return { body: out };
|
||||
+4
-4
@@ -1,15 +1,15 @@
|
||||
var Immutable = require('immutable');
|
||||
var moment = require('moment');
|
||||
const Immutable = require('immutable');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = Immutable.Map({
|
||||
// Format a date
|
||||
// ex: 'MMMM Do YYYY, h:mm:ss a
|
||||
date: function(time, format) {
|
||||
date(time, format) {
|
||||
return moment(time).format(format);
|
||||
},
|
||||
|
||||
// Relative Time
|
||||
dateFromNow: function(time) {
|
||||
dateFromNow(time) {
|
||||
return moment(time).fromNow();
|
||||
}
|
||||
});
|
||||
+5
-5
@@ -1,7 +1,7 @@
|
||||
var Immutable = require('immutable');
|
||||
var PluginDependency = require('../models/pluginDependency');
|
||||
const Immutable = require('immutable');
|
||||
const PluginDependency = require('../models/pluginDependency');
|
||||
|
||||
var pkg = require('../../package.json');
|
||||
const pkg = require('../../package.json');
|
||||
|
||||
/**
|
||||
* Create a PluginDependency from a dependency of gitbook
|
||||
@@ -9,8 +9,8 @@ var pkg = require('../../package.json');
|
||||
* @return {PluginDependency}
|
||||
*/
|
||||
function createFromDependency(pluginName) {
|
||||
var npmID = PluginDependency.nameToNpmID(pluginName);
|
||||
var version = pkg.dependencies[npmID];
|
||||
const npmID = PluginDependency.nameToNpmID(pluginName);
|
||||
const version = pkg.dependencies[npmID];
|
||||
|
||||
return PluginDependency.create(pluginName, version);
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
var Immutable = require('immutable');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
module.exports = Immutable.List([
|
||||
'js',
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
All GitBook themes plugins name start with this prefix once shorted.
|
||||
*/
|
||||
module.exports = 'theme-';
|
||||
module.exports = 'theme-';
|
||||
+2
-3
@@ -1,7 +1,7 @@
|
||||
var createMockFS = require('../mock');
|
||||
const createMockFS = require('../mock');
|
||||
|
||||
describe('MockFS', function() {
|
||||
var fs = createMockFS({
|
||||
const fs = createMockFS({
|
||||
'README.md': 'Hello World',
|
||||
'SUMMARY.md': '# Summary',
|
||||
'folder': {
|
||||
@@ -79,4 +79,3 @@ describe('MockFS', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
var path = require('path');
|
||||
var is = require('is');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
var Immutable = require('immutable');
|
||||
const path = require('path');
|
||||
const is = require('is');
|
||||
const Buffer = require('buffer').Buffer;
|
||||
const Immutable = require('immutable');
|
||||
|
||||
var FS = require('../models/fs');
|
||||
var error = require('../utils/error');
|
||||
const FS = require('../models/fs');
|
||||
const error = require('../utils/error');
|
||||
|
||||
/**
|
||||
Create a fake filesystem for unit testing GitBook.
|
||||
@@ -13,14 +13,14 @@ var error = require('../utils/error');
|
||||
*/
|
||||
function createMockFS(files) {
|
||||
files = Immutable.fromJS(files);
|
||||
var mtime = new Date();
|
||||
const mtime = new Date();
|
||||
|
||||
function getFile(filePath) {
|
||||
var parts = path.normalize(filePath).split(path.sep);
|
||||
const parts = path.normalize(filePath).split(path.sep);
|
||||
return parts.reduce(function(list, part, i) {
|
||||
if (!list) return null;
|
||||
|
||||
var file;
|
||||
let file;
|
||||
|
||||
if (!part || part === '.') file = list;
|
||||
else file = list.get(part);
|
||||
@@ -41,7 +41,7 @@ function createMockFS(files) {
|
||||
}
|
||||
|
||||
function fsReadFile(filePath) {
|
||||
var file = getFile(filePath);
|
||||
const file = getFile(filePath);
|
||||
if (!is.string(file)) {
|
||||
throw error.FileNotFoundError({
|
||||
filename: filePath
|
||||
@@ -52,7 +52,7 @@ function createMockFS(files) {
|
||||
}
|
||||
|
||||
function fsStatFile(filePath) {
|
||||
var file = getFile(filePath);
|
||||
const file = getFile(filePath);
|
||||
if (!file) {
|
||||
throw error.FileNotFoundError({
|
||||
filename: filePath
|
||||
@@ -60,12 +60,12 @@ function createMockFS(files) {
|
||||
}
|
||||
|
||||
return {
|
||||
mtime: mtime
|
||||
mtime
|
||||
};
|
||||
}
|
||||
|
||||
function fsReadDir(filePath) {
|
||||
var dir = getFile(filePath);
|
||||
const dir = getFile(filePath);
|
||||
if (!dir || is.string(dir)) {
|
||||
throw error.FileNotFoundError({
|
||||
filename: filePath
|
||||
@@ -85,10 +85,10 @@ function createMockFS(files) {
|
||||
|
||||
return FS.create({
|
||||
root: '',
|
||||
fsExists: fsExists,
|
||||
fsReadFile: fsReadFile,
|
||||
fsStatFile: fsStatFile,
|
||||
fsReadDir: fsReadDir
|
||||
fsExists,
|
||||
fsReadFile,
|
||||
fsStatFile,
|
||||
fsReadDir
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
var path = require('path');
|
||||
var Immutable = require('immutable');
|
||||
var fresh = require('fresh-require');
|
||||
const path = require('path');
|
||||
const Immutable = require('immutable');
|
||||
const fresh = require('fresh-require');
|
||||
|
||||
var fs = require('../utils/fs');
|
||||
var FS = require('../models/fs');
|
||||
const fs = require('../utils/fs');
|
||||
const FS = require('../models/fs');
|
||||
|
||||
function fsReadDir(folder) {
|
||||
return fs.readdir(folder)
|
||||
@@ -14,7 +14,7 @@ function fsReadDir(folder) {
|
||||
.map(function(file) {
|
||||
if (file == '.' || file == '..') return;
|
||||
|
||||
var stat = fs.statSync(path.join(folder, file));
|
||||
const stat = fs.statSync(path.join(folder, file));
|
||||
if (stat.isDirectory()) file = file + path.sep;
|
||||
return file;
|
||||
})
|
||||
@@ -30,13 +30,13 @@ function fsLoadObject(filename) {
|
||||
|
||||
module.exports = function createNodeFS(root) {
|
||||
return FS.create({
|
||||
root: root,
|
||||
root,
|
||||
|
||||
fsExists: fs.exists,
|
||||
fsReadFile: fs.readFile,
|
||||
fsStatFile: fs.stat,
|
||||
fsReadDir: fsReadDir,
|
||||
fsLoadObject: fsLoadObject,
|
||||
fsReadDir,
|
||||
fsLoadObject,
|
||||
fsReadAsStream: fs.readStream
|
||||
});
|
||||
};
|
||||
@@ -1,10 +1,10 @@
|
||||
var semver = require('semver');
|
||||
var pkg = require('../package.json');
|
||||
const semver = require('semver');
|
||||
const pkg = require('../package.json');
|
||||
|
||||
var VERSION = pkg.version;
|
||||
var VERSION_STABLE = VERSION.replace(/\-(\S+)/g, '');
|
||||
const VERSION = pkg.version;
|
||||
const VERSION_STABLE = VERSION.replace(/\-(\S+)/g, '');
|
||||
|
||||
var START_TIME = new Date();
|
||||
const START_TIME = new Date();
|
||||
|
||||
/**
|
||||
Verify that this gitbook version satisfies a requirement
|
||||
@@ -23,6 +23,6 @@ function satisfies(condition) {
|
||||
|
||||
module.exports = {
|
||||
version: pkg.version,
|
||||
satisfies: satisfies,
|
||||
START_TIME: START_TIME
|
||||
satisfies,
|
||||
START_TIME
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
var extend = require('extend');
|
||||
const extend = require('extend');
|
||||
|
||||
var common = require('./browser');
|
||||
const common = require('./browser');
|
||||
|
||||
module.exports = extend({
|
||||
initBook: require('./init'),
|
||||
@@ -1,12 +1,12 @@
|
||||
var path = require('path');
|
||||
const path = require('path');
|
||||
|
||||
var createNodeFS = require('./fs/node');
|
||||
var fs = require('./utils/fs');
|
||||
var Promise = require('./utils/promise');
|
||||
var File = require('./models/file');
|
||||
var Readme = require('./models/readme');
|
||||
var Book = require('./models/book');
|
||||
var Parse = require('./parse');
|
||||
const createNodeFS = require('./fs/node');
|
||||
const fs = require('./utils/fs');
|
||||
const Promise = require('./utils/promise');
|
||||
const File = require('./models/file');
|
||||
const Readme = require('./models/readme');
|
||||
const Book = require('./models/book');
|
||||
const Parse = require('./parse');
|
||||
|
||||
/**
|
||||
Initialize folder structure for a book
|
||||
@@ -17,38 +17,38 @@ var Parse = require('./parse');
|
||||
@return {Promise}
|
||||
*/
|
||||
function initBook(rootFolder) {
|
||||
var extension = '.md';
|
||||
const extension = '.md';
|
||||
|
||||
return fs.mkdirp(rootFolder)
|
||||
|
||||
// Parse the summary and readme
|
||||
.then(function() {
|
||||
var fs = createNodeFS(rootFolder);
|
||||
var book = Book.createForFS(fs);
|
||||
const bookFS = createNodeFS(rootFolder);
|
||||
const book = Book.createForFS(bookFS);
|
||||
|
||||
return Parse.parseReadme(book)
|
||||
|
||||
// Setup default readme if doesn't found one
|
||||
.fail(function() {
|
||||
var readmeFile = File.createWithFilepath('README' + extension);
|
||||
var readme = Readme.create(readmeFile);
|
||||
const readmeFile = File.createWithFilepath('README' + extension);
|
||||
const readme = Readme.create(readmeFile);
|
||||
return book.setReadme(readme);
|
||||
});
|
||||
})
|
||||
.then(Parse.parseSummary)
|
||||
|
||||
.then(function(book) {
|
||||
var logger = book.getLogger();
|
||||
var summary = book.getSummary();
|
||||
var summaryFile = summary.getFile();
|
||||
var summaryFilename = summaryFile.getPath() || ('SUMMARY' + extension);
|
||||
const logger = book.getLogger();
|
||||
const summary = book.getSummary();
|
||||
const summaryFile = summary.getFile();
|
||||
const summaryFilename = summaryFile.getPath() || ('SUMMARY' + extension);
|
||||
|
||||
var articles = summary.getArticlesAsList();
|
||||
const articles = summary.getArticlesAsList();
|
||||
|
||||
// Write pages
|
||||
return Promise.forEach(articles, function(article) {
|
||||
var articlePath = article.getPath();
|
||||
var filePath = articlePath? path.join(rootFolder, articlePath) : null;
|
||||
const articlePath = article.getPath();
|
||||
const filePath = articlePath ? path.join(rootFolder, articlePath) : null;
|
||||
if (!filePath) {
|
||||
return;
|
||||
}
|
||||
@@ -64,7 +64,7 @@ function initBook(rootFolder) {
|
||||
|
||||
// Write summary
|
||||
.then(function() {
|
||||
var filePath = path.join(rootFolder, summaryFilename);
|
||||
const filePath = path.join(rootFolder, summaryFilename);
|
||||
|
||||
return fs.ensureFile(filePath)
|
||||
.then(function() {
|
||||
@@ -0,0 +1,39 @@
|
||||
const extend = require('extend');
|
||||
|
||||
const gitbook = require('../gitbook');
|
||||
const encodeSummary = require('./encodeSummary');
|
||||
const encodeGlossary = require('./encodeGlossary');
|
||||
const encodeReadme = require('./encodeReadme');
|
||||
const encodeLanguages = require('./encodeLanguages');
|
||||
|
||||
/**
|
||||
Encode a book to JSON
|
||||
|
||||
@param {Book}
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeBookToJson(book) {
|
||||
const config = book.getConfig();
|
||||
const language = book.getLanguage();
|
||||
|
||||
const variables = config.getValue('variables', {});
|
||||
|
||||
return {
|
||||
summary: encodeSummary(book.getSummary()),
|
||||
glossary: encodeGlossary(book.getGlossary()),
|
||||
readme: encodeReadme(book.getReadme()),
|
||||
config: book.getConfig().getValues().toJS(),
|
||||
|
||||
languages: book.isMultilingual() ? encodeLanguages(book.getLanguages()) : undefined,
|
||||
|
||||
gitbook: {
|
||||
version: gitbook.version,
|
||||
time: gitbook.START_TIME
|
||||
},
|
||||
book: extend({
|
||||
language: language ? language : undefined
|
||||
}, variables.toJS())
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = encodeBookToJson;
|
||||
+5
-5
@@ -1,6 +1,6 @@
|
||||
var encodeBook = require('./encodeBook');
|
||||
var encodePage = require('./encodePage');
|
||||
var encodeFile = require('./encodeFile');
|
||||
const encodeBook = require('./encodeBook');
|
||||
const encodePage = require('./encodePage');
|
||||
const encodeFile = require('./encodeFile');
|
||||
|
||||
/**
|
||||
* Return a JSON representation of a book with a specific file
|
||||
@@ -10,9 +10,9 @@ var encodeFile = require('./encodeFile');
|
||||
* @return {Object}
|
||||
*/
|
||||
function encodeBookWithPage(book, page) {
|
||||
var file = page.getFile();
|
||||
const file = page.getFile();
|
||||
|
||||
var result = encodeBook(book);
|
||||
const result = encodeBook(book);
|
||||
result.page = encodePage(page, book.getSummary());
|
||||
result.file = encodeFile(file);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeFileToJson(file) {
|
||||
var filePath = file.getPath();
|
||||
const filePath = file.getPath();
|
||||
if (!filePath) {
|
||||
return undefined;
|
||||
}
|
||||
+4
-4
@@ -1,5 +1,5 @@
|
||||
var encodeFile = require('./encodeFile');
|
||||
var encodeGlossaryEntry = require('./encodeGlossaryEntry');
|
||||
const encodeFile = require('./encodeFile');
|
||||
const encodeGlossaryEntry = require('./encodeGlossaryEntry');
|
||||
|
||||
/**
|
||||
Encode a glossary to JSON
|
||||
@@ -8,8 +8,8 @@ var encodeGlossaryEntry = require('./encodeGlossaryEntry');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeGlossary(glossary) {
|
||||
var file = glossary.getFile();
|
||||
var entries = glossary.getEntries();
|
||||
const file = glossary.getFile();
|
||||
const entries = glossary.getEntries();
|
||||
|
||||
return {
|
||||
file: encodeFile(file),
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
var encodeFile = require('./encodeFile');
|
||||
const encodeFile = require('./encodeFile');
|
||||
|
||||
/**
|
||||
Encode a languages listing to JSON
|
||||
@@ -7,8 +7,8 @@ var encodeFile = require('./encodeFile');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeLanguages(languages) {
|
||||
var file = languages.getFile();
|
||||
var list = languages.getList();
|
||||
const file = languages.getFile();
|
||||
const list = languages.getList();
|
||||
|
||||
return {
|
||||
file: encodeFile(file),
|
||||
+5
-5
@@ -1,4 +1,4 @@
|
||||
var encodeBook = require('./encodeBook');
|
||||
const encodeBook = require('./encodeBook');
|
||||
|
||||
/**
|
||||
* Encode an output to JSON
|
||||
@@ -7,11 +7,11 @@ var encodeBook = require('./encodeBook');
|
||||
* @return {Object}
|
||||
*/
|
||||
function encodeOutputToJson(output) {
|
||||
var book = output.getBook();
|
||||
var generator = output.getGenerator();
|
||||
var options = output.getOptions();
|
||||
const book = output.getBook();
|
||||
const generator = output.getGenerator();
|
||||
const options = output.getOptions();
|
||||
|
||||
var result = encodeBook(book);
|
||||
const result = encodeBook(book);
|
||||
|
||||
result.output = {
|
||||
name: generator
|
||||
+6
-6
@@ -1,6 +1,6 @@
|
||||
var encodeOutput = require('./encodeOutput');
|
||||
var encodePage = require('./encodePage');
|
||||
var encodeFile = require('./encodeFile');
|
||||
const encodeOutput = require('./encodeOutput');
|
||||
const encodePage = require('./encodePage');
|
||||
const encodeFile = require('./encodeFile');
|
||||
|
||||
/**
|
||||
* Return a JSON representation of a book with a specific file
|
||||
@@ -10,10 +10,10 @@ var encodeFile = require('./encodeFile');
|
||||
* @return {Object}
|
||||
*/
|
||||
function encodeOutputWithPage(output, page) {
|
||||
var file = page.getFile();
|
||||
var book = output.getBook();
|
||||
const file = page.getFile();
|
||||
const book = output.getBook();
|
||||
|
||||
var result = encodeOutput(output);
|
||||
const result = encodeOutput(output);
|
||||
result.page = encodePage(page, book.getSummary());
|
||||
result.file = encodeFile(file);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var encodeSummaryArticle = require('./encodeSummaryArticle');
|
||||
const encodeSummaryArticle = require('./encodeSummaryArticle');
|
||||
|
||||
/**
|
||||
Return a JSON representation of a page
|
||||
@@ -8,23 +8,23 @@ var encodeSummaryArticle = require('./encodeSummaryArticle');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodePage(page, summary) {
|
||||
var file = page.getFile();
|
||||
var attributes = page.getAttributes();
|
||||
var article = summary.getByPath(file.getPath());
|
||||
const file = page.getFile();
|
||||
const attributes = page.getAttributes();
|
||||
const article = summary.getByPath(file.getPath());
|
||||
|
||||
var result = attributes.toJS();
|
||||
const result = attributes.toJS();
|
||||
|
||||
if (article) {
|
||||
result.title = article.getTitle();
|
||||
result.level = article.getLevel();
|
||||
result.depth = article.getDepth();
|
||||
|
||||
var nextArticle = summary.getNextArticle(article);
|
||||
const nextArticle = summary.getNextArticle(article);
|
||||
if (nextArticle) {
|
||||
result.next = encodeSummaryArticle(nextArticle);
|
||||
}
|
||||
|
||||
var prevArticle = summary.getPrevArticle(article);
|
||||
const prevArticle = summary.getPrevArticle(article);
|
||||
if (prevArticle) {
|
||||
result.previous = encodeSummaryArticle(prevArticle);
|
||||
}
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
var encodeFile = require('./encodeFile');
|
||||
const encodeFile = require('./encodeFile');
|
||||
|
||||
/**
|
||||
Encode a readme to JSON
|
||||
@@ -7,7 +7,7 @@ var encodeFile = require('./encodeFile');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeReadme(readme) {
|
||||
var file = readme.getFile();
|
||||
const file = readme.getFile();
|
||||
|
||||
return {
|
||||
file: encodeFile(file)
|
||||
+4
-4
@@ -1,5 +1,5 @@
|
||||
var encodeFile = require('./encodeFile');
|
||||
var encodeSummaryPart = require('./encodeSummaryPart');
|
||||
const encodeFile = require('./encodeFile');
|
||||
const encodeSummaryPart = require('./encodeSummaryPart');
|
||||
|
||||
/**
|
||||
Encode a summary to JSON
|
||||
@@ -8,8 +8,8 @@ var encodeSummaryPart = require('./encodeSummaryPart');
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeSummary(summary) {
|
||||
var file = summary.getFile();
|
||||
var parts = summary.getParts();
|
||||
const file = summary.getFile();
|
||||
const parts = summary.getParts();
|
||||
|
||||
return {
|
||||
file: encodeFile(file),
|
||||
+2
-2
@@ -6,7 +6,7 @@
|
||||
@return {Object}
|
||||
*/
|
||||
function encodeSummaryArticle(article, recursive) {
|
||||
var articles = undefined;
|
||||
let articles = undefined;
|
||||
if (recursive !== false) {
|
||||
articles = article.getArticles()
|
||||
.map(encodeSummaryArticle)
|
||||
@@ -21,7 +21,7 @@ function encodeSummaryArticle(article, recursive) {
|
||||
url: article.getUrl(),
|
||||
path: article.getPath(),
|
||||
ref: article.getRef(),
|
||||
articles: articles
|
||||
articles
|
||||
};
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
var encodeSummaryArticle = require('./encodeSummaryArticle');
|
||||
const encodeSummaryArticle = require('./encodeSummaryArticle');
|
||||
|
||||
/**
|
||||
Encode a SummaryPart to JSON
|
||||
+16
-17
@@ -1,8 +1,8 @@
|
||||
var Immutable = require('immutable');
|
||||
var Config = require('../config');
|
||||
const Immutable = require('immutable');
|
||||
const Config = require('../config');
|
||||
|
||||
describe('Config', function() {
|
||||
var config = Config.createWithValues({
|
||||
const config = Config.createWithValues({
|
||||
hello: {
|
||||
world: 1,
|
||||
test: 'Hello',
|
||||
@@ -12,32 +12,32 @@ describe('Config', function() {
|
||||
|
||||
describe('getValue', function() {
|
||||
it('must return value as immutable', function() {
|
||||
var value = config.getValue('hello');
|
||||
const value = config.getValue('hello');
|
||||
expect(Immutable.Map.isMap(value)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('must return deep value', function() {
|
||||
var value = config.getValue('hello.world');
|
||||
const value = config.getValue('hello.world');
|
||||
expect(value).toBe(1);
|
||||
});
|
||||
|
||||
it('must return default value if non existant', function() {
|
||||
var value = config.getValue('hello.nonExistant', 'defaultValue');
|
||||
const value = config.getValue('hello.nonExistant', 'defaultValue');
|
||||
expect(value).toBe('defaultValue');
|
||||
});
|
||||
|
||||
it('must not return default value for falsy values', function() {
|
||||
var value = config.getValue('hello.isFalse', 'defaultValue');
|
||||
const value = config.getValue('hello.isFalse', 'defaultValue');
|
||||
expect(value).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setValue', function() {
|
||||
it('must set value as immutable', function() {
|
||||
var testConfig = config.setValue('hello', {
|
||||
const testConfig = config.setValue('hello', {
|
||||
'cool': 1
|
||||
});
|
||||
var value = testConfig.getValue('hello');
|
||||
const value = testConfig.getValue('hello');
|
||||
|
||||
expect(Immutable.Map.isMap(value)).toBeTruthy();
|
||||
expect(value.size).toBe(1);
|
||||
@@ -45,9 +45,9 @@ describe('Config', function() {
|
||||
});
|
||||
|
||||
it('must set deep value', function() {
|
||||
var testConfig = config.setValue('hello.world', 2);
|
||||
var hello = testConfig.getValue('hello');
|
||||
var world = testConfig.getValue('hello.world');
|
||||
const testConfig = config.setValue('hello.world', 2);
|
||||
const hello = testConfig.getValue('hello');
|
||||
const world = testConfig.getValue('hello.world');
|
||||
|
||||
expect(Immutable.Map.isMap(hello)).toBeTruthy();
|
||||
expect(hello.size).toBe(3);
|
||||
@@ -58,11 +58,11 @@ describe('Config', function() {
|
||||
|
||||
describe('toReducedVersion', function() {
|
||||
it('must only return diffs for simple values', function() {
|
||||
var _config = Config.createWithValues({
|
||||
const _config = Config.createWithValues({
|
||||
gitbook: '3.0.0'
|
||||
});
|
||||
|
||||
var reducedVersion = _config.toReducedVersion();
|
||||
const reducedVersion = _config.toReducedVersion();
|
||||
|
||||
expect(reducedVersion.toJS()).toEqual({
|
||||
gitbook: '3.0.0'
|
||||
@@ -70,13 +70,13 @@ describe('Config', function() {
|
||||
});
|
||||
|
||||
it('must only return diffs for deep values', function() {
|
||||
var _config = Config.createWithValues({
|
||||
const _config = Config.createWithValues({
|
||||
structure: {
|
||||
readme: 'intro.md'
|
||||
}
|
||||
});
|
||||
|
||||
var reducedVersion = _config.toReducedVersion();
|
||||
const reducedVersion = _config.toReducedVersion();
|
||||
|
||||
expect(reducedVersion.toJS()).toEqual({
|
||||
structure: {
|
||||
@@ -87,4 +87,3 @@ describe('Config', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+7
-8
@@ -1,9 +1,9 @@
|
||||
var File = require('../file');
|
||||
var Glossary = require('../glossary');
|
||||
var GlossaryEntry = require('../glossaryEntry');
|
||||
const File = require('../file');
|
||||
const Glossary = require('../glossary');
|
||||
const GlossaryEntry = require('../glossaryEntry');
|
||||
|
||||
describe('Glossary', function() {
|
||||
var glossary = Glossary.createFromEntries(File(), [
|
||||
const glossary = Glossary.createFromEntries(File(), [
|
||||
{
|
||||
name: 'Hello World',
|
||||
description: 'Awesome!'
|
||||
@@ -16,13 +16,13 @@ describe('Glossary', function() {
|
||||
|
||||
describe('createFromEntries', function() {
|
||||
it('must add all entries', function() {
|
||||
var entries = glossary.getEntries();
|
||||
const entries = glossary.getEntries();
|
||||
expect(entries.size).toBe(2);
|
||||
});
|
||||
|
||||
it('must add entries as GlossaryEntries', function() {
|
||||
var entries = glossary.getEntries();
|
||||
var entry = entries.get('hello-world');
|
||||
const entries = glossary.getEntries();
|
||||
const entry = entries.get('hello-world');
|
||||
expect(entry instanceof GlossaryEntry).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -37,4 +37,3 @@ describe('Glossary', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+2
-3
@@ -1,9 +1,9 @@
|
||||
var GlossaryEntry = require('../glossaryEntry');
|
||||
const GlossaryEntry = require('../glossaryEntry');
|
||||
|
||||
describe('GlossaryEntry', function() {
|
||||
describe('getID', function() {
|
||||
it('must return a normalized ID', function() {
|
||||
var entry = new GlossaryEntry({
|
||||
const entry = new GlossaryEntry({
|
||||
name: 'Hello World'
|
||||
});
|
||||
|
||||
@@ -12,4 +12,3 @@ describe('GlossaryEntry', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+4
-5
@@ -1,11 +1,11 @@
|
||||
var Immutable = require('immutable');
|
||||
var Page = require('../page');
|
||||
const Immutable = require('immutable');
|
||||
const Page = require('../page');
|
||||
|
||||
describe('Page', function() {
|
||||
|
||||
describe('toText', function() {
|
||||
it('must not prepend frontmatter if no attributes', function() {
|
||||
var page = Page().merge({
|
||||
const page = Page().merge({
|
||||
content: 'Hello World'
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ describe('Page', function() {
|
||||
});
|
||||
|
||||
it('must prepend frontmatter if attributes', function() {
|
||||
var page = Page().merge({
|
||||
const page = Page().merge({
|
||||
content: 'Hello World',
|
||||
attributes: Immutable.fromJS({
|
||||
hello: 'world'
|
||||
@@ -25,4 +25,3 @@ describe('Page', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+4
-5
@@ -1,15 +1,15 @@
|
||||
describe('Plugin', function() {
|
||||
var Plugin = require('../plugin');
|
||||
const Plugin = require('../plugin');
|
||||
|
||||
describe('createFromString', function() {
|
||||
it('must parse name', function() {
|
||||
var plugin = Plugin.createFromString('hello');
|
||||
const plugin = Plugin.createFromString('hello');
|
||||
expect(plugin.getName()).toBe('hello');
|
||||
expect(plugin.getVersion()).toBe('*');
|
||||
});
|
||||
|
||||
it('must parse version', function() {
|
||||
var plugin = Plugin.createFromString('hello@1.0.0');
|
||||
const plugin = Plugin.createFromString('hello@1.0.0');
|
||||
expect(plugin.getName()).toBe('hello');
|
||||
expect(plugin.getVersion()).toBe('1.0.0');
|
||||
});
|
||||
@@ -17,11 +17,10 @@ describe('Plugin', function() {
|
||||
|
||||
describe('isLoaded', function() {
|
||||
it('must return false for empty plugin', function() {
|
||||
var plugin = Plugin.createFromString('hello');
|
||||
const plugin = Plugin.createFromString('hello');
|
||||
expect(plugin.isLoaded()).toBe(false);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+11
-11
@@ -1,29 +1,29 @@
|
||||
var Immutable = require('immutable');
|
||||
var PluginDependency = require('../pluginDependency');
|
||||
const Immutable = require('immutable');
|
||||
const PluginDependency = require('../pluginDependency');
|
||||
|
||||
describe('PluginDependency', function() {
|
||||
describe('createFromString', function() {
|
||||
it('must parse name', function() {
|
||||
var plugin = PluginDependency.createFromString('hello');
|
||||
const plugin = PluginDependency.createFromString('hello');
|
||||
expect(plugin.getName()).toBe('hello');
|
||||
expect(plugin.getVersion()).toBe('*');
|
||||
});
|
||||
|
||||
it('must parse state', function() {
|
||||
var plugin = PluginDependency.createFromString('-hello');
|
||||
const plugin = PluginDependency.createFromString('-hello');
|
||||
expect(plugin.getName()).toBe('hello');
|
||||
expect(plugin.isEnabled()).toBe(false);
|
||||
});
|
||||
|
||||
describe('Version', function() {
|
||||
it('must parse version', function() {
|
||||
var plugin = PluginDependency.createFromString('hello@1.0.0');
|
||||
const plugin = PluginDependency.createFromString('hello@1.0.0');
|
||||
expect(plugin.getName()).toBe('hello');
|
||||
expect(plugin.getVersion()).toBe('1.0.0');
|
||||
});
|
||||
|
||||
it('must parse semver', function() {
|
||||
var plugin = PluginDependency.createFromString('hello@>=4.0.0');
|
||||
const plugin = PluginDependency.createFromString('hello@>=4.0.0');
|
||||
expect(plugin.getName()).toBe('hello');
|
||||
expect(plugin.getVersion()).toBe('>=4.0.0');
|
||||
});
|
||||
@@ -31,13 +31,13 @@ describe('PluginDependency', function() {
|
||||
|
||||
describe('GIT Version', function() {
|
||||
it('must handle HTTPS urls', function() {
|
||||
var plugin = PluginDependency.createFromString('hello@git+https://github.com/GitbookIO/plugin-ga.git');
|
||||
const plugin = PluginDependency.createFromString('hello@git+https://github.com/GitbookIO/plugin-ga.git');
|
||||
expect(plugin.getName()).toBe('hello');
|
||||
expect(plugin.getVersion()).toBe('git+https://github.com/GitbookIO/plugin-ga.git');
|
||||
});
|
||||
|
||||
it('must handle SSH urls', function() {
|
||||
var plugin = PluginDependency.createFromString('hello@git+ssh://samy@github.com/GitbookIO/plugin-ga.git');
|
||||
const plugin = PluginDependency.createFromString('hello@git+ssh://samy@github.com/GitbookIO/plugin-ga.git');
|
||||
expect(plugin.getName()).toBe('hello');
|
||||
expect(plugin.getVersion()).toBe('git+ssh://samy@github.com/GitbookIO/plugin-ga.git');
|
||||
});
|
||||
@@ -45,7 +45,7 @@ describe('PluginDependency', function() {
|
||||
|
||||
describe('listToArray', function() {
|
||||
it('must create an array from a list of plugin dependencies', function() {
|
||||
var list = PluginDependency.listToArray(Immutable.List([
|
||||
const list = PluginDependency.listToArray(Immutable.List([
|
||||
PluginDependency.createFromString('hello@1.0.0'),
|
||||
PluginDependency.createFromString('noversion'),
|
||||
PluginDependency.createFromString('-disabled')
|
||||
@@ -61,14 +61,14 @@ describe('PluginDependency', function() {
|
||||
|
||||
describe('listFromArray', function() {
|
||||
it('must create an array from a list of plugin dependencies', function() {
|
||||
var arr = Immutable.fromJS([
|
||||
const arr = Immutable.fromJS([
|
||||
'hello@1.0.0',
|
||||
{
|
||||
'name': 'plugin-ga',
|
||||
'version': 'git+ssh://samy@github.com/GitbookIO/plugin-ga.git'
|
||||
}
|
||||
]);
|
||||
var list = PluginDependency.listFromArray(arr);
|
||||
const list = PluginDependency.listFromArray(arr);
|
||||
|
||||
expect(list.first().getName()).toBe('hello');
|
||||
expect(list.first().getVersion()).toBe('1.0.0');
|
||||
+10
-11
@@ -1,9 +1,9 @@
|
||||
|
||||
describe('Summary', function() {
|
||||
var File = require('../file');
|
||||
var Summary = require('../summary');
|
||||
const File = require('../file');
|
||||
const Summary = require('../summary');
|
||||
|
||||
var summary = Summary.createFromParts(File(), [
|
||||
const summary = Summary.createFromParts(File(), [
|
||||
{
|
||||
articles: [
|
||||
{
|
||||
@@ -30,21 +30,21 @@ describe('Summary', function() {
|
||||
|
||||
describe('createFromEntries', function() {
|
||||
it('must add all parts', function() {
|
||||
var parts = summary.getParts();
|
||||
const parts = summary.getParts();
|
||||
expect(parts.size).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getByLevel', function() {
|
||||
it('can return a Part', function() {
|
||||
var part = summary.getByLevel('1');
|
||||
const part = summary.getByLevel('1');
|
||||
|
||||
expect(part).toBeDefined();
|
||||
expect(part.getArticles().size).toBe(4);
|
||||
});
|
||||
|
||||
it('can return a Part (2)', function() {
|
||||
var part = summary.getByLevel('2');
|
||||
const part = summary.getByLevel('2');
|
||||
|
||||
expect(part).toBeDefined();
|
||||
expect(part.getTitle()).toBe('Test');
|
||||
@@ -52,7 +52,7 @@ describe('Summary', function() {
|
||||
});
|
||||
|
||||
it('can return an Article', function() {
|
||||
var article = summary.getByLevel('1.1');
|
||||
const article = summary.getByLevel('1.1');
|
||||
|
||||
expect(article).toBeDefined();
|
||||
expect(article.getTitle()).toBe('My First Article');
|
||||
@@ -61,21 +61,21 @@ describe('Summary', function() {
|
||||
|
||||
describe('getByPath', function() {
|
||||
it('return correct article', function() {
|
||||
var article = summary.getByPath('README.md');
|
||||
const article = summary.getByPath('README.md');
|
||||
|
||||
expect(article).toBeDefined();
|
||||
expect(article.getTitle()).toBe('My First Article');
|
||||
});
|
||||
|
||||
it('return correct article', function() {
|
||||
var article = summary.getByPath('article.md');
|
||||
const article = summary.getByPath('article.md');
|
||||
|
||||
expect(article).toBeDefined();
|
||||
expect(article.getTitle()).toBe('My Second Article');
|
||||
});
|
||||
|
||||
it('return undefined if not found', function() {
|
||||
var article = summary.getByPath('NOT_EXISTING.md');
|
||||
const article = summary.getByPath('NOT_EXISTING.md');
|
||||
|
||||
expect(article).toBeFalsy();
|
||||
});
|
||||
@@ -91,4 +91,3 @@ describe('Summary', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+10
-11
@@ -1,15 +1,15 @@
|
||||
var SummaryArticle = require('../summaryArticle');
|
||||
var File = require('../file');
|
||||
const SummaryArticle = require('../summaryArticle');
|
||||
const File = require('../file');
|
||||
|
||||
describe('SummaryArticle', function() {
|
||||
describe('createChildLevel', function() {
|
||||
it('must create the right level', function() {
|
||||
var article = SummaryArticle.create({}, '1.1');
|
||||
const article = SummaryArticle.create({}, '1.1');
|
||||
expect(article.createChildLevel()).toBe('1.1.1');
|
||||
});
|
||||
|
||||
it('must create the right level when has articles', function() {
|
||||
var article = SummaryArticle.create({
|
||||
const article = SummaryArticle.create({
|
||||
articles: [
|
||||
{
|
||||
title: 'Test'
|
||||
@@ -22,32 +22,31 @@ describe('SummaryArticle', function() {
|
||||
|
||||
describe('isFile', function() {
|
||||
it('must return true when exactly the file', function() {
|
||||
var article = SummaryArticle.create({
|
||||
const article = SummaryArticle.create({
|
||||
ref: 'hello.md'
|
||||
}, '1.1');
|
||||
var file = File.createWithFilepath('hello.md');
|
||||
const file = File.createWithFilepath('hello.md');
|
||||
|
||||
expect(article.isFile(file)).toBe(true);
|
||||
});
|
||||
|
||||
it('must return true when path is not normalized', function() {
|
||||
var article = SummaryArticle.create({
|
||||
const article = SummaryArticle.create({
|
||||
ref: '/hello.md'
|
||||
}, '1.1');
|
||||
var file = File.createWithFilepath('hello.md');
|
||||
const file = File.createWithFilepath('hello.md');
|
||||
|
||||
expect(article.isFile(file)).toBe(true);
|
||||
});
|
||||
|
||||
it('must return false when has anchor', function() {
|
||||
var article = SummaryArticle.create({
|
||||
const article = SummaryArticle.create({
|
||||
ref: 'hello.md#world'
|
||||
}, '1.1');
|
||||
var file = File.createWithFilepath('hello.md');
|
||||
const file = File.createWithFilepath('hello.md');
|
||||
|
||||
expect(article.isFile(file)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+3
-4
@@ -1,14 +1,14 @@
|
||||
var SummaryPart = require('../summaryPart');
|
||||
const SummaryPart = require('../summaryPart');
|
||||
|
||||
describe('SummaryPart', function() {
|
||||
describe('createChildLevel', function() {
|
||||
it('must create the right level', function() {
|
||||
var article = SummaryPart.create({}, '1');
|
||||
const article = SummaryPart.create({}, '1');
|
||||
expect(article.createChildLevel()).toBe('1.1');
|
||||
});
|
||||
|
||||
it('must create the right level when has articles', function() {
|
||||
var article = SummaryPart.create({
|
||||
const article = SummaryPart.create({
|
||||
articles: [
|
||||
{
|
||||
title: 'Test'
|
||||
@@ -20,4 +20,3 @@ describe('SummaryPart', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+37
-37
@@ -1,13 +1,13 @@
|
||||
var nunjucks = require('nunjucks');
|
||||
var Immutable = require('immutable');
|
||||
var Promise = require('../../utils/promise');
|
||||
const nunjucks = require('nunjucks');
|
||||
const Immutable = require('immutable');
|
||||
const Promise = require('../../utils/promise');
|
||||
|
||||
describe('TemplateBlock', function() {
|
||||
var TemplateBlock = require('../templateBlock');
|
||||
const TemplateBlock = require('../templateBlock');
|
||||
|
||||
describe('create', function() {
|
||||
it('must initialize a simple TemplateBlock from a function', function() {
|
||||
var templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
const templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
return {
|
||||
body: '<p>Hello, World!</p>',
|
||||
parse: true
|
||||
@@ -34,7 +34,7 @@ describe('TemplateBlock', function() {
|
||||
|
||||
describe('getShortcuts', function() {
|
||||
it('must return undefined if no shortcuts', function() {
|
||||
var templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
const templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
return {
|
||||
body: '<p>Hello, World!</p>',
|
||||
parse: true
|
||||
@@ -45,8 +45,8 @@ describe('TemplateBlock', function() {
|
||||
});
|
||||
|
||||
it('must return complete shortcut', function() {
|
||||
var templateBlock = TemplateBlock.create('sayhello', {
|
||||
process: function(block) {
|
||||
const templateBlock = TemplateBlock.create('sayhello', {
|
||||
process(block) {
|
||||
return '<p>Hello, World!</p>';
|
||||
},
|
||||
shortcuts: {
|
||||
@@ -56,7 +56,7 @@ describe('TemplateBlock', function() {
|
||||
}
|
||||
});
|
||||
|
||||
var shortcut = templateBlock.getShortcuts();
|
||||
const shortcut = templateBlock.getShortcuts();
|
||||
|
||||
expect(shortcut).toBeDefined();
|
||||
expect(shortcut.getStart()).toEqual('$');
|
||||
@@ -68,28 +68,28 @@ describe('TemplateBlock', function() {
|
||||
|
||||
describe('toNunjucksExt()', function() {
|
||||
it('should replace by block anchor', function() {
|
||||
var templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
const templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
return 'Hello';
|
||||
});
|
||||
|
||||
var blocks = {};
|
||||
let blocks = {};
|
||||
|
||||
// Create a fresh Nunjucks environment
|
||||
var env = new nunjucks.Environment(null, { autoescape: false });
|
||||
const env = new nunjucks.Environment(null, { autoescape: false });
|
||||
|
||||
// Add template block to environement
|
||||
var Ext = templateBlock.toNunjucksExt({}, blocks);
|
||||
const Ext = templateBlock.toNunjucksExt({}, blocks);
|
||||
env.addExtension(templateBlock.getExtensionName(), new Ext());
|
||||
|
||||
// Render a template using the block
|
||||
var src = '{% sayhello %}{% endsayhello %}';
|
||||
const src = '{% sayhello %}{% endsayhello %}';
|
||||
return Promise.nfcall(env.renderString.bind(env), src)
|
||||
.then(function(res) {
|
||||
blocks = Immutable.fromJS(blocks);
|
||||
expect(blocks.size).toBe(1);
|
||||
|
||||
var blockId = blocks.keySeq().get(0);
|
||||
var block = blocks.get(blockId);
|
||||
const blockId = blocks.keySeq().get(0);
|
||||
const block = blocks.get(blockId);
|
||||
|
||||
expect(res).toBe('{{-%' + blockId + '%-}}');
|
||||
expect(block.get('body')).toBe('Hello');
|
||||
@@ -98,7 +98,7 @@ describe('TemplateBlock', function() {
|
||||
});
|
||||
|
||||
it('must create a valid nunjucks extension', function() {
|
||||
var templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
const templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
return {
|
||||
body: '<p>Hello, World!</p>',
|
||||
parse: true
|
||||
@@ -106,14 +106,14 @@ describe('TemplateBlock', function() {
|
||||
});
|
||||
|
||||
// Create a fresh Nunjucks environment
|
||||
var env = new nunjucks.Environment(null, { autoescape: false });
|
||||
const env = new nunjucks.Environment(null, { autoescape: false });
|
||||
|
||||
// Add template block to environement
|
||||
var Ext = templateBlock.toNunjucksExt();
|
||||
const Ext = templateBlock.toNunjucksExt();
|
||||
env.addExtension(templateBlock.getExtensionName(), new Ext());
|
||||
|
||||
// Render a template using the block
|
||||
var src = '{% sayhello %}{% endsayhello %}';
|
||||
const src = '{% sayhello %}{% endsayhello %}';
|
||||
return Promise.nfcall(env.renderString.bind(env), src)
|
||||
.then(function(res) {
|
||||
expect(res).toBe('<p>Hello, World!</p>');
|
||||
@@ -121,22 +121,22 @@ describe('TemplateBlock', function() {
|
||||
});
|
||||
|
||||
it('must apply block arguments correctly', function() {
|
||||
var templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
const templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
return {
|
||||
body: '<'+block.kwargs.tag+'>Hello, '+block.kwargs.name+'!</'+block.kwargs.tag+'>',
|
||||
body: '<' + block.kwargs.tag + '>Hello, ' + block.kwargs.name + '!</' + block.kwargs.tag + '>',
|
||||
parse: true
|
||||
};
|
||||
});
|
||||
|
||||
// Create a fresh Nunjucks environment
|
||||
var env = new nunjucks.Environment(null, { autoescape: false });
|
||||
const env = new nunjucks.Environment(null, { autoescape: false });
|
||||
|
||||
// Add template block to environement
|
||||
var Ext = templateBlock.toNunjucksExt();
|
||||
const Ext = templateBlock.toNunjucksExt();
|
||||
env.addExtension(templateBlock.getExtensionName(), new Ext());
|
||||
|
||||
// Render a template using the block
|
||||
var src = '{% sayhello name="Samy", tag="p" %}{% endsayhello %}';
|
||||
const src = '{% sayhello name="Samy", tag="p" %}{% endsayhello %}';
|
||||
return Promise.nfcall(env.renderString.bind(env), src)
|
||||
.then(function(res) {
|
||||
expect(res).toBe('<p>Hello, Samy!</p>');
|
||||
@@ -144,7 +144,7 @@ describe('TemplateBlock', function() {
|
||||
});
|
||||
|
||||
it('must accept an async function', function() {
|
||||
var templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
const templateBlock = TemplateBlock.create('sayhello', function(block) {
|
||||
return Promise()
|
||||
.then(function() {
|
||||
return {
|
||||
@@ -155,14 +155,14 @@ describe('TemplateBlock', function() {
|
||||
});
|
||||
|
||||
// Create a fresh Nunjucks environment
|
||||
var env = new nunjucks.Environment(null, { autoescape: false });
|
||||
const env = new nunjucks.Environment(null, { autoescape: false });
|
||||
|
||||
// Add template block to environement
|
||||
var Ext = templateBlock.toNunjucksExt();
|
||||
const Ext = templateBlock.toNunjucksExt();
|
||||
env.addExtension(templateBlock.getExtensionName(), new Ext());
|
||||
|
||||
// Render a template using the block
|
||||
var src = '{% sayhello %}Samy{% endsayhello %}';
|
||||
const src = '{% sayhello %}Samy{% endsayhello %}';
|
||||
return Promise.nfcall(env.renderString.bind(env), src)
|
||||
.then(function(res) {
|
||||
expect(res).toBe('Hello Samy');
|
||||
@@ -170,36 +170,36 @@ describe('TemplateBlock', function() {
|
||||
});
|
||||
|
||||
it('must handle nested blocks', function() {
|
||||
var templateBlock = new TemplateBlock({
|
||||
const templateBlock = new TemplateBlock({
|
||||
name: 'yoda',
|
||||
blocks: Immutable.List(['start', 'end']),
|
||||
process: function(block) {
|
||||
var nested = {};
|
||||
process(block) {
|
||||
const nested = {};
|
||||
|
||||
block.blocks.forEach(function(blk) {
|
||||
nested[blk.name] = blk.body.trim();
|
||||
});
|
||||
|
||||
return {
|
||||
body: '<p class="yoda">'+nested.end+' '+nested.start+'</p>',
|
||||
body: '<p class="yoda">' + nested.end + ' ' + nested.start + '</p>',
|
||||
parse: true
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Create a fresh Nunjucks environment
|
||||
var env = new nunjucks.Environment(null, { autoescape: false });
|
||||
const env = new nunjucks.Environment(null, { autoescape: false });
|
||||
|
||||
// Add template block to environement
|
||||
var Ext = templateBlock.toNunjucksExt();
|
||||
const Ext = templateBlock.toNunjucksExt();
|
||||
env.addExtension(templateBlock.getExtensionName(), new Ext());
|
||||
|
||||
// Render a template using the block
|
||||
var src = '{% yoda %}{% start %}this sentence should be{% end %}inverted{% endyoda %}';
|
||||
const src = '{% yoda %}{% start %}this sentence should be{% end %}inverted{% endyoda %}';
|
||||
return Promise.nfcall(env.renderString.bind(env), src)
|
||||
.then(function(res) {
|
||||
expect(res).toBe('<p class="yoda">inverted this sentence should be</p>');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
+14
-14
@@ -1,40 +1,40 @@
|
||||
|
||||
describe('TemplateBlock', function() {
|
||||
var TemplateEngine = require('../templateEngine');
|
||||
const TemplateEngine = require('../templateEngine');
|
||||
|
||||
describe('create', function() {
|
||||
it('must initialize with a list of filters', function() {
|
||||
var engine = TemplateEngine.create({
|
||||
const engine = TemplateEngine.create({
|
||||
filters: {
|
||||
hello: function(name) {
|
||||
hello(name) {
|
||||
return 'Hello ' + name + '!';
|
||||
}
|
||||
}
|
||||
});
|
||||
var env = engine.toNunjucks();
|
||||
var res = env.renderString('{{ "Luke"|hello }}');
|
||||
const env = engine.toNunjucks();
|
||||
const res = env.renderString('{{ "Luke"|hello }}');
|
||||
|
||||
expect(res).toBe('Hello Luke!');
|
||||
});
|
||||
|
||||
it('must initialize with a list of globals', function() {
|
||||
var engine = TemplateEngine.create({
|
||||
const engine = TemplateEngine.create({
|
||||
globals: {
|
||||
hello: function(name) {
|
||||
hello(name) {
|
||||
return 'Hello ' + name + '!';
|
||||
}
|
||||
}
|
||||
});
|
||||
var env = engine.toNunjucks();
|
||||
var res = env.renderString('{{ hello("Luke") }}');
|
||||
const env = engine.toNunjucks();
|
||||
const res = env.renderString('{{ hello("Luke") }}');
|
||||
|
||||
expect(res).toBe('Hello Luke!');
|
||||
});
|
||||
|
||||
it('must pass context to filters and blocks', function() {
|
||||
var engine = TemplateEngine.create({
|
||||
const engine = TemplateEngine.create({
|
||||
filters: {
|
||||
hello: function(name) {
|
||||
hello(name) {
|
||||
return 'Hello ' + name + ' ' + this.lastName + '!';
|
||||
}
|
||||
},
|
||||
@@ -42,10 +42,10 @@ describe('TemplateBlock', function() {
|
||||
lastName: 'Skywalker'
|
||||
}
|
||||
});
|
||||
var env = engine.toNunjucks();
|
||||
var res = env.renderString('{{ "Luke"|hello }}');
|
||||
const env = engine.toNunjucks();
|
||||
const res = env.renderString('{{ "Luke"|hello }}');
|
||||
|
||||
expect(res).toBe('Hello Luke Skywalker!');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,17 +1,17 @@
|
||||
var path = require('path');
|
||||
var Immutable = require('immutable');
|
||||
const path = require('path');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
var Logger = require('../utils/logger');
|
||||
const Logger = require('../utils/logger');
|
||||
|
||||
var FS = require('./fs');
|
||||
var Config = require('./config');
|
||||
var Readme = require('./readme');
|
||||
var Summary = require('./summary');
|
||||
var Glossary = require('./glossary');
|
||||
var Languages = require('./languages');
|
||||
var Ignore = require('./ignore');
|
||||
const FS = require('./fs');
|
||||
const Config = require('./config');
|
||||
const Readme = require('./readme');
|
||||
const Summary = require('./summary');
|
||||
const Glossary = require('./glossary');
|
||||
const Languages = require('./languages');
|
||||
const Ignore = require('./ignore');
|
||||
|
||||
var Book = Immutable.Record({
|
||||
const Book = Immutable.Record({
|
||||
// Logger for outptu message
|
||||
logger: Logger(),
|
||||
|
||||
@@ -81,9 +81,9 @@ Book.prototype.getLanguage = function() {
|
||||
@return {FS}
|
||||
*/
|
||||
Book.prototype.getContentFS = function() {
|
||||
var fs = this.getFS();
|
||||
var config = this.getConfig();
|
||||
var rootFolder = config.getValue('root');
|
||||
const fs = this.getFS();
|
||||
const config = this.getConfig();
|
||||
const rootFolder = config.getValue('root');
|
||||
|
||||
if (rootFolder) {
|
||||
return FS.reduceScope(fs, rootFolder);
|
||||
@@ -98,7 +98,7 @@ Book.prototype.getContentFS = function() {
|
||||
@return {String}
|
||||
*/
|
||||
Book.prototype.getRoot = function() {
|
||||
var fs = this.getFS();
|
||||
const fs = this.getFS();
|
||||
return fs.getRoot();
|
||||
};
|
||||
|
||||
@@ -108,7 +108,7 @@ Book.prototype.getRoot = function() {
|
||||
@return {String}
|
||||
*/
|
||||
Book.prototype.getContentRoot = function() {
|
||||
var fs = this.getContentFS();
|
||||
const fs = this.getContentFS();
|
||||
return fs.getRoot();
|
||||
};
|
||||
|
||||
@@ -119,8 +119,8 @@ Book.prototype.getContentRoot = function() {
|
||||
@return {Page|undefined}
|
||||
*/
|
||||
Book.prototype.isFileIgnored = function(filename) {
|
||||
var ignore = this.getIgnore();
|
||||
var language = this.getLanguage();
|
||||
const ignore = this.getIgnore();
|
||||
const language = this.getLanguage();
|
||||
|
||||
// Ignore is always relative to the root of the main book
|
||||
if (language) {
|
||||
@@ -137,8 +137,8 @@ Book.prototype.isFileIgnored = function(filename) {
|
||||
@return {Page|undefined}
|
||||
*/
|
||||
Book.prototype.isContentFileIgnored = function(filename) {
|
||||
var config = this.getConfig();
|
||||
var rootFolder = config.getValue('root');
|
||||
const config = this.getConfig();
|
||||
const rootFolder = config.getValue('root');
|
||||
|
||||
if (rootFolder) {
|
||||
filename = path.join(rootFolder, filename);
|
||||
@@ -182,7 +182,7 @@ Book.prototype.isLanguageBook = function() {
|
||||
@return {Book}
|
||||
*/
|
||||
Book.prototype.getLanguageBook = function(language) {
|
||||
var books = this.getBooks();
|
||||
const books = this.getBooks();
|
||||
return books.get(language);
|
||||
};
|
||||
|
||||
@@ -194,7 +194,7 @@ Book.prototype.getLanguageBook = function(language) {
|
||||
@return {Book}
|
||||
*/
|
||||
Book.prototype.addLanguageBook = function(language, book) {
|
||||
var books = this.getBooks();
|
||||
let books = this.getBooks();
|
||||
books = books.set(language, book);
|
||||
|
||||
return this.set('books', books);
|
||||
@@ -259,7 +259,7 @@ Book.prototype.setLogLevel = function(level) {
|
||||
*/
|
||||
Book.createForFS = function createForFS(fs) {
|
||||
return new Book({
|
||||
fs: fs
|
||||
fs
|
||||
});
|
||||
};
|
||||
|
||||
@@ -269,15 +269,15 @@ Book.createForFS = function createForFS(fs) {
|
||||
*/
|
||||
Book.prototype.getDefaultExt = function() {
|
||||
// Inferring sources
|
||||
var clues = [
|
||||
const clues = [
|
||||
this.getReadme(),
|
||||
this.getSummary(),
|
||||
this.getGlossary()
|
||||
];
|
||||
|
||||
// List their extensions
|
||||
var exts = clues.map(function (clue) {
|
||||
var file = clue.getFile();
|
||||
const exts = clues.map(function(clue) {
|
||||
const file = clue.getFile();
|
||||
if (file.exists()) {
|
||||
return file.getParser().getExtensions().first();
|
||||
} else {
|
||||
@@ -288,7 +288,7 @@ Book.prototype.getDefaultExt = function() {
|
||||
exts.push('.md');
|
||||
|
||||
// Choose the first non null
|
||||
return exts.find(function (e) { return e !== null; });
|
||||
return exts.find(function(e) { return e !== null; });
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -298,7 +298,7 @@ Book.prototype.getDefaultExt = function() {
|
||||
@return {String}
|
||||
*/
|
||||
Book.prototype.getDefaultReadmePath = function(absolute) {
|
||||
var defaultPath = 'README'+this.getDefaultExt();
|
||||
const defaultPath = 'README' + this.getDefaultExt();
|
||||
if (absolute) {
|
||||
return path.join(this.getContentRoot(), defaultPath);
|
||||
} else {
|
||||
@@ -313,7 +313,7 @@ Book.prototype.getDefaultReadmePath = function(absolute) {
|
||||
@return {String}
|
||||
*/
|
||||
Book.prototype.getDefaultSummaryPath = function(absolute) {
|
||||
var defaultPath = 'SUMMARY'+this.getDefaultExt();
|
||||
const defaultPath = 'SUMMARY' + this.getDefaultExt();
|
||||
if (absolute) {
|
||||
return path.join(this.getContentRoot(), defaultPath);
|
||||
} else {
|
||||
@@ -328,7 +328,7 @@ Book.prototype.getDefaultSummaryPath = function(absolute) {
|
||||
@return {String}
|
||||
*/
|
||||
Book.prototype.getDefaultGlossaryPath = function(absolute) {
|
||||
var defaultPath = 'GLOSSARY'+this.getDefaultExt();
|
||||
const defaultPath = 'GLOSSARY' + this.getDefaultExt();
|
||||
if (absolute) {
|
||||
return path.join(this.getContentRoot(), defaultPath);
|
||||
} else {
|
||||
@@ -344,8 +344,8 @@ Book.prototype.getDefaultGlossaryPath = function(absolute) {
|
||||
@return {Book}
|
||||
*/
|
||||
Book.createFromParent = function createFromParent(parent, language) {
|
||||
var ignore = parent.getIgnore();
|
||||
var config = parent.getConfig();
|
||||
const ignore = parent.getIgnore();
|
||||
let config = parent.getConfig();
|
||||
|
||||
// Set language in configuration
|
||||
config = config.setValue('language', language);
|
||||
@@ -353,10 +353,10 @@ Book.createFromParent = function createFromParent(parent, language) {
|
||||
return new Book({
|
||||
// Inherits config. logegr and list of ignored files
|
||||
logger: parent.getLogger(),
|
||||
config: config,
|
||||
ignore: ignore,
|
||||
config,
|
||||
ignore,
|
||||
|
||||
language: language,
|
||||
language,
|
||||
fs: FS.reduceScope(parent.getContentFS(), language)
|
||||
});
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
var is = require('is');
|
||||
var Immutable = require('immutable');
|
||||
const is = require('is');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
var File = require('./file');
|
||||
var PluginDependency = require('./pluginDependency');
|
||||
var configDefault = require('../constants/configDefault');
|
||||
var reducedObject = require('../utils/reducedObject');
|
||||
const File = require('./file');
|
||||
const PluginDependency = require('./pluginDependency');
|
||||
const configDefault = require('../constants/configDefault');
|
||||
const reducedObject = require('../utils/reducedObject');
|
||||
|
||||
var Config = Immutable.Record({
|
||||
const Config = Immutable.Record({
|
||||
file: File(),
|
||||
values: configDefault
|
||||
}, 'Config');
|
||||
@@ -51,7 +51,7 @@ Config.prototype.setFile = function(file) {
|
||||
* @return {Mixed}
|
||||
*/
|
||||
Config.prototype.getValue = function(keyPath, def) {
|
||||
var values = this.getValues();
|
||||
const values = this.getValues();
|
||||
keyPath = Config.keyToKeyPath(keyPath);
|
||||
|
||||
if (!values.hasIn(keyPath)) {
|
||||
@@ -72,7 +72,7 @@ Config.prototype.setValue = function(keyPath, value) {
|
||||
|
||||
value = Immutable.fromJS(value);
|
||||
|
||||
var values = this.getValues();
|
||||
let values = this.getValues();
|
||||
values = values.setIn(keyPath, value);
|
||||
|
||||
return this.set('values', values);
|
||||
@@ -83,7 +83,7 @@ Config.prototype.setValue = function(keyPath, value) {
|
||||
* @return {List<PluginDependency>}
|
||||
*/
|
||||
Config.prototype.getPluginDependencies = function() {
|
||||
var plugins = this.getValue('plugins');
|
||||
const plugins = this.getValue('plugins');
|
||||
|
||||
if (is.string(plugins)) {
|
||||
return PluginDependency.listFromString(plugins);
|
||||
@@ -98,7 +98,7 @@ Config.prototype.getPluginDependencies = function() {
|
||||
* @return {PluginDependency}
|
||||
*/
|
||||
Config.prototype.getPluginDependency = function(name) {
|
||||
var plugins = this.getPluginDependencies();
|
||||
const plugins = this.getPluginDependencies();
|
||||
|
||||
return plugins.find(function(dep) {
|
||||
return dep.getName() === name;
|
||||
@@ -111,7 +111,7 @@ Config.prototype.getPluginDependency = function(name) {
|
||||
* @return {Config}
|
||||
*/
|
||||
Config.prototype.setPluginDependencies = function(deps) {
|
||||
var plugins = PluginDependency.listToArray(deps);
|
||||
const plugins = PluginDependency.listToArray(deps);
|
||||
|
||||
return this.setValue('plugins', plugins);
|
||||
};
|
||||
@@ -135,7 +135,7 @@ Config.prototype.updateValues = function(values) {
|
||||
* @returns {Config}
|
||||
*/
|
||||
Config.prototype.mergeValues = function(values) {
|
||||
var currentValues = this.getValues();
|
||||
let currentValues = this.getValues();
|
||||
values = Immutable.fromJS(values);
|
||||
|
||||
currentValues = currentValues.mergeDeep(values);
|
||||
@@ -151,7 +151,7 @@ Config.prototype.mergeValues = function(values) {
|
||||
*/
|
||||
Config.create = function(file, values) {
|
||||
return new Config({
|
||||
file: file,
|
||||
file,
|
||||
values: Immutable.fromJS(values)
|
||||
});
|
||||
};
|
||||
@@ -1,9 +1,9 @@
|
||||
var path = require('path');
|
||||
var Immutable = require('immutable');
|
||||
const path = require('path');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
var parsers = require('../parsers');
|
||||
const parsers = require('../parsers');
|
||||
|
||||
var File = Immutable.Record({
|
||||
const File = Immutable.Record({
|
||||
// Path of the file, relative to the FS
|
||||
path: String(),
|
||||
|
||||
@@ -34,7 +34,7 @@ File.prototype.exists = function() {
|
||||
@return {String}
|
||||
*/
|
||||
File.prototype.getType = function() {
|
||||
var parser = this.getParser();
|
||||
const parser = this.getParser();
|
||||
if (parser) {
|
||||
return parser.getName();
|
||||
} else {
|
||||
@@ -1,13 +1,13 @@
|
||||
var path = require('path');
|
||||
var Immutable = require('immutable');
|
||||
var stream = require('stream');
|
||||
const path = require('path');
|
||||
const Immutable = require('immutable');
|
||||
const stream = require('stream');
|
||||
|
||||
var File = require('./file');
|
||||
var Promise = require('../utils/promise');
|
||||
var error = require('../utils/error');
|
||||
var PathUtil = require('../utils/path');
|
||||
const File = require('./file');
|
||||
const Promise = require('../utils/promise');
|
||||
const error = require('../utils/error');
|
||||
const PathUtil = require('../utils/path');
|
||||
|
||||
var FS = Immutable.Record({
|
||||
const FS = Immutable.Record({
|
||||
root: String(),
|
||||
|
||||
fsExists: Function(),
|
||||
@@ -35,27 +35,25 @@ FS.prototype.getRoot = function() {
|
||||
@return {Boolean}
|
||||
*/
|
||||
FS.prototype.isInScope = function(filename) {
|
||||
var rootPath = this.getRoot();
|
||||
const rootPath = this.getRoot();
|
||||
filename = path.join(rootPath, filename);
|
||||
|
||||
return PathUtil.isInRoot(rootPath, filename);
|
||||
};
|
||||
|
||||
/**
|
||||
Resolve a file in this FS
|
||||
|
||||
@param {String}
|
||||
@return {String}
|
||||
*/
|
||||
FS.prototype.resolve = function() {
|
||||
var rootPath = this.getRoot();
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var filename = path.join.apply(path, [rootPath].concat(args));
|
||||
* Resolve a file in this FS
|
||||
* @param {String}
|
||||
* @return {String}
|
||||
*/
|
||||
FS.prototype.resolve = function(...args) {
|
||||
const rootPath = this.getRoot();
|
||||
let filename = path.join(rootPath, ...args);
|
||||
filename = path.normalize(filename);
|
||||
|
||||
if (!this.isInScope(filename)) {
|
||||
throw error.FileOutOfScopeError({
|
||||
filename: filename,
|
||||
filename,
|
||||
root: this.root
|
||||
});
|
||||
}
|
||||
@@ -64,47 +62,44 @@ FS.prototype.resolve = function() {
|
||||
};
|
||||
|
||||
/**
|
||||
Check if a file exists, run a Promise(true) if that's the case, Promise(false) otherwise
|
||||
|
||||
@param {String} filename
|
||||
@return {Promise<Boolean>}
|
||||
*/
|
||||
* Check if a file exists, run a Promise(true) if that's the case, Promise(false) otherwise
|
||||
* @param {String} filename
|
||||
* @return {Promise<Boolean>}
|
||||
*/
|
||||
FS.prototype.exists = function(filename) {
|
||||
var that = this;
|
||||
const that = this;
|
||||
|
||||
return Promise()
|
||||
.then(function() {
|
||||
filename = that.resolve(filename);
|
||||
var exists = that.get('fsExists');
|
||||
const exists = that.get('fsExists');
|
||||
|
||||
return exists(filename);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
Read a file and returns a promise with the content as a buffer
|
||||
|
||||
@param {String} filename
|
||||
@return {Promise<Buffer>}
|
||||
*/
|
||||
* Read a file and returns a promise with the content as a buffer
|
||||
* @param {String} filename
|
||||
* @return {Promise<Buffer>}
|
||||
*/
|
||||
FS.prototype.read = function(filename) {
|
||||
var that = this;
|
||||
const that = this;
|
||||
|
||||
return Promise()
|
||||
.then(function() {
|
||||
filename = that.resolve(filename);
|
||||
var read = that.get('fsReadFile');
|
||||
const read = that.get('fsReadFile');
|
||||
|
||||
return read(filename);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
Read a file as a string (utf-8)
|
||||
|
||||
@param {String} filename
|
||||
@return {Promise<String>}
|
||||
*/
|
||||
* Read a file as a string (utf-8)
|
||||
* @param {String} filename
|
||||
* @return {Promise<String>}
|
||||
*/
|
||||
FS.prototype.readAsString = function(filename, encoding) {
|
||||
encoding = encoding || 'utf8';
|
||||
|
||||
@@ -115,15 +110,14 @@ FS.prototype.readAsString = function(filename, encoding) {
|
||||
};
|
||||
|
||||
/**
|
||||
Read file as a stream
|
||||
|
||||
@param {String} filename
|
||||
@return {Promise<Stream>}
|
||||
*/
|
||||
* Read file as a stream
|
||||
* @param {String} filename
|
||||
* @return {Promise<Stream>}
|
||||
*/
|
||||
FS.prototype.readAsStream = function(filename) {
|
||||
var that = this;
|
||||
var filepath = that.resolve(filename);
|
||||
var fsReadAsStream = this.get('fsReadAsStream');
|
||||
const that = this;
|
||||
const filepath = that.resolve(filename);
|
||||
const fsReadAsStream = this.get('fsReadAsStream');
|
||||
|
||||
if (fsReadAsStream) {
|
||||
return Promise(fsReadAsStream(filepath));
|
||||
@@ -131,7 +125,7 @@ FS.prototype.readAsStream = function(filename) {
|
||||
|
||||
return this.read(filename)
|
||||
.then(function(buf) {
|
||||
var bufferStream = new stream.PassThrough();
|
||||
const bufferStream = new stream.PassThrough();
|
||||
bufferStream.end(buf);
|
||||
|
||||
return bufferStream;
|
||||
@@ -139,18 +133,17 @@ FS.prototype.readAsStream = function(filename) {
|
||||
};
|
||||
|
||||
/**
|
||||
Read stat infos about a file
|
||||
|
||||
@param {String} filename
|
||||
@return {Promise<File>}
|
||||
*/
|
||||
* Read stat infos about a file
|
||||
* @param {String} filename
|
||||
* @return {Promise<File>}
|
||||
*/
|
||||
FS.prototype.statFile = function(filename) {
|
||||
var that = this;
|
||||
const that = this;
|
||||
|
||||
return Promise()
|
||||
.then(function() {
|
||||
var filepath = that.resolve(filename);
|
||||
var stat = that.get('fsStatFile');
|
||||
const filepath = that.resolve(filename);
|
||||
const stat = that.get('fsStatFile');
|
||||
|
||||
return stat(filepath);
|
||||
})
|
||||
@@ -160,19 +153,19 @@ FS.prototype.statFile = function(filename) {
|
||||
};
|
||||
|
||||
/**
|
||||
List files/directories in a directory.
|
||||
Directories ends with '/'
|
||||
* List files/directories in a directory.
|
||||
* Directories ends with '/'
|
||||
|
||||
@param {String} dirname
|
||||
@return {Promise<List<String>>}
|
||||
*/
|
||||
* @param {String} dirname
|
||||
* @return {Promise<List<String>>}
|
||||
*/
|
||||
FS.prototype.readDir = function(dirname) {
|
||||
var that = this;
|
||||
const that = this;
|
||||
|
||||
return Promise()
|
||||
.then(function() {
|
||||
var dirpath = that.resolve(dirname);
|
||||
var readDir = that.get('fsReadDir');
|
||||
const dirpath = that.resolve(dirname);
|
||||
const readDir = that.get('fsReadDir');
|
||||
|
||||
return readDir(dirpath);
|
||||
})
|
||||
@@ -182,12 +175,12 @@ FS.prototype.readDir = function(dirname) {
|
||||
};
|
||||
|
||||
/**
|
||||
List only files in a diretcory
|
||||
Directories ends with '/'
|
||||
|
||||
@param {String} dirname
|
||||
@return {Promise<List<String>>}
|
||||
*/
|
||||
* List only files in a diretcory
|
||||
* Directories ends with '/'
|
||||
*
|
||||
* @param {String} dirname
|
||||
* @return {Promise<List<String>>}
|
||||
*/
|
||||
FS.prototype.listFiles = function(dirname) {
|
||||
return this.readDir(dirname)
|
||||
.then(function(files) {
|
||||
@@ -196,21 +189,21 @@ FS.prototype.listFiles = function(dirname) {
|
||||
};
|
||||
|
||||
/**
|
||||
List all files in a directory
|
||||
|
||||
@param {String} dirName
|
||||
@param {Function(dirName)} filterFn: call it for each file/directory to test if it should stop iterating
|
||||
@return {Promise<List<String>>}
|
||||
*/
|
||||
* List all files in a directory
|
||||
*
|
||||
* @param {String} dirName
|
||||
* @param {Function(dirName)} filterFn: call it for each file/directory to test if it should stop iterating
|
||||
* @return {Promise<List<String>>}
|
||||
*/
|
||||
FS.prototype.listAllFiles = function(dirName, filterFn) {
|
||||
var that = this;
|
||||
const that = this;
|
||||
dirName = dirName || '.';
|
||||
|
||||
return this.readDir(dirName)
|
||||
.then(function(files) {
|
||||
return Promise.reduce(files, function(out, file) {
|
||||
var isDirectory = pathIsFolder(file);
|
||||
var newDirName = path.join(dirName, file);
|
||||
const isDirectory = pathIsFolder(file);
|
||||
const newDirName = path.join(dirName, file);
|
||||
|
||||
if (filterFn && filterFn(newDirName) === false) {
|
||||
return out;
|
||||
@@ -229,13 +222,13 @@ FS.prototype.listAllFiles = function(dirName, filterFn) {
|
||||
};
|
||||
|
||||
/**
|
||||
Find a file in a folder (case insensitive)
|
||||
Return the found filename
|
||||
|
||||
@param {String} dirname
|
||||
@param {String} filename
|
||||
@return {Promise<String>}
|
||||
*/
|
||||
* Find a file in a folder (case insensitive)
|
||||
* Return the found filename
|
||||
*
|
||||
* @param {String} dirname
|
||||
* @param {String} filename
|
||||
* @return {Promise<String>}
|
||||
*/
|
||||
FS.prototype.findFile = function(dirname, filename) {
|
||||
return this.listFiles(dirname)
|
||||
.then(function(files) {
|
||||
@@ -246,20 +239,20 @@ FS.prototype.findFile = function(dirname, filename) {
|
||||
};
|
||||
|
||||
/**
|
||||
Load a JSON file
|
||||
By default, fs only supports JSON
|
||||
|
||||
@param {String} filename
|
||||
@return {Promise<Object>}
|
||||
*/
|
||||
* Load a JSON file
|
||||
* By default, fs only supports JSON
|
||||
*
|
||||
* @param {String} filename
|
||||
* @return {Promise<Object>}
|
||||
*/
|
||||
FS.prototype.loadAsObject = function(filename) {
|
||||
var that = this;
|
||||
var fsLoadObject = this.get('fsLoadObject');
|
||||
const that = this;
|
||||
const fsLoadObject = this.get('fsLoadObject');
|
||||
|
||||
return this.exists(filename)
|
||||
.then(function(exists) {
|
||||
if (!exists) {
|
||||
var err = new Error('Module doesn\'t exist');
|
||||
const err = new Error('Module doesn\'t exist');
|
||||
err.code = 'MODULE_NOT_FOUND';
|
||||
|
||||
throw err;
|
||||
@@ -277,22 +270,22 @@ FS.prototype.loadAsObject = function(filename) {
|
||||
};
|
||||
|
||||
/**
|
||||
Create a FS instance
|
||||
|
||||
@param {Object} def
|
||||
@return {FS}
|
||||
*/
|
||||
* Create a FS instance
|
||||
*
|
||||
* @param {Object} def
|
||||
* @return {FS}
|
||||
*/
|
||||
FS.create = function create(def) {
|
||||
return new FS(def);
|
||||
};
|
||||
|
||||
/**
|
||||
Create a new FS instance with a reduced scope
|
||||
|
||||
@param {FS} fs
|
||||
@param {String} scope
|
||||
@return {FS}
|
||||
*/
|
||||
* Create a new FS instance with a reduced scope
|
||||
*
|
||||
* @param {FS} fs
|
||||
* @param {String} scope
|
||||
* @return {FS}
|
||||
*/
|
||||
FS.reduceScope = function reduceScope(fs, scope) {
|
||||
return fs.set('root', path.join(fs.getRoot(), scope));
|
||||
};
|
||||
@@ -300,8 +293,8 @@ FS.reduceScope = function reduceScope(fs, scope) {
|
||||
|
||||
// .readdir return files/folder as a list of string, folder ending with '/'
|
||||
function pathIsFolder(filename) {
|
||||
var lastChar = filename[filename.length - 1];
|
||||
const lastChar = filename[filename.length - 1];
|
||||
return lastChar == '/' || lastChar == '\\';
|
||||
}
|
||||
|
||||
module.exports = FS;
|
||||
module.exports = FS;
|
||||
@@ -1,11 +1,11 @@
|
||||
var Immutable = require('immutable');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
var error = require('../utils/error');
|
||||
var File = require('./file');
|
||||
var GlossaryEntry = require('./glossaryEntry');
|
||||
var parsers = require('../parsers');
|
||||
const error = require('../utils/error');
|
||||
const File = require('./file');
|
||||
const GlossaryEntry = require('./glossaryEntry');
|
||||
const parsers = require('../parsers');
|
||||
|
||||
var Glossary = Immutable.Record({
|
||||
const Glossary = Immutable.Record({
|
||||
file: File(),
|
||||
entries: Immutable.OrderedMap()
|
||||
});
|
||||
@@ -25,8 +25,8 @@ Glossary.prototype.getEntries = function() {
|
||||
@return {GlossaryEntry}
|
||||
*/
|
||||
Glossary.prototype.getEntry = function(name) {
|
||||
var entries = this.getEntries();
|
||||
var id = GlossaryEntry.nameToID(name);
|
||||
const entries = this.getEntries();
|
||||
const id = GlossaryEntry.nameToID(name);
|
||||
|
||||
return entries.get(id);
|
||||
};
|
||||
@@ -37,10 +37,10 @@ Glossary.prototype.getEntry = function(name) {
|
||||
@return {Promise<String>}
|
||||
*/
|
||||
Glossary.prototype.toText = function(parser) {
|
||||
var file = this.getFile();
|
||||
var entries = this.getEntries();
|
||||
const file = this.getFile();
|
||||
const entries = this.getEntries();
|
||||
|
||||
parser = parser? parsers.getByExt(parser) : file.getParser();
|
||||
parser = parser ? parsers.getByExt(parser) : file.getParser();
|
||||
|
||||
if (!parser) {
|
||||
throw error.FileNotParsableError({
|
||||
@@ -60,8 +60,8 @@ Glossary.prototype.toText = function(parser) {
|
||||
@return {Glossary}
|
||||
*/
|
||||
Glossary.addEntry = function addEntry(glossary, entry) {
|
||||
var id = entry.getID();
|
||||
var entries = glossary.getEntries();
|
||||
const id = entry.getID();
|
||||
let entries = glossary.getEntries();
|
||||
|
||||
entries = entries.set(id, entry);
|
||||
return glossary.set('entries', entries);
|
||||
@@ -75,9 +75,9 @@ Glossary.addEntry = function addEntry(glossary, entry) {
|
||||
@return {Glossary}
|
||||
*/
|
||||
Glossary.addEntryByName = function addEntryByName(glossary, name, description) {
|
||||
var entry = new GlossaryEntry({
|
||||
name: name,
|
||||
description: description
|
||||
const entry = new GlossaryEntry({
|
||||
name,
|
||||
description
|
||||
});
|
||||
|
||||
return Glossary.addEntry(glossary, entry);
|
||||
@@ -100,7 +100,7 @@ Glossary.createFromEntries = function createFromEntries(file, entries) {
|
||||
});
|
||||
|
||||
return new Glossary({
|
||||
file: file,
|
||||
file,
|
||||
entries: Immutable.OrderedMap(entries)
|
||||
});
|
||||
};
|
||||
+3
-3
@@ -1,11 +1,11 @@
|
||||
var Immutable = require('immutable');
|
||||
var slug = require('github-slugid');
|
||||
const Immutable = require('immutable');
|
||||
const slug = require('github-slugid');
|
||||
|
||||
/*
|
||||
A definition represents an entry in the glossary
|
||||
*/
|
||||
|
||||
var GlossaryEntry = Immutable.Record({
|
||||
const GlossaryEntry = Immutable.Record({
|
||||
name: String(),
|
||||
description: String()
|
||||
});
|
||||
@@ -1,10 +1,10 @@
|
||||
var Immutable = require('immutable');
|
||||
var IgnoreMutable = require('ignore');
|
||||
const Immutable = require('immutable');
|
||||
const IgnoreMutable = require('ignore');
|
||||
|
||||
/*
|
||||
Immutable version of node-ignore
|
||||
*/
|
||||
var Ignore = Immutable.Record({
|
||||
const Ignore = Immutable.Record({
|
||||
ignore: new IgnoreMutable()
|
||||
}, 'Ignore');
|
||||
|
||||
@@ -19,7 +19,7 @@ Ignore.prototype.getIgnore = function() {
|
||||
@return {Boolean}
|
||||
*/
|
||||
Ignore.prototype.isFileIgnored = function(filename) {
|
||||
var ignore = this.getIgnore();
|
||||
const ignore = this.getIgnore();
|
||||
return ignore.filter([filename]).length == 0;
|
||||
};
|
||||
|
||||
@@ -30,8 +30,8 @@ Ignore.prototype.isFileIgnored = function(filename) {
|
||||
@return {Ignore}
|
||||
*/
|
||||
Ignore.prototype.add = function(rule) {
|
||||
var ignore = this.getIgnore();
|
||||
var newIgnore = new IgnoreMutable();
|
||||
const ignore = this.getIgnore();
|
||||
const newIgnore = new IgnoreMutable();
|
||||
|
||||
newIgnore.add(ignore);
|
||||
newIgnore.add(rule);
|
||||
@@ -1,7 +1,7 @@
|
||||
var path = require('path');
|
||||
var Immutable = require('immutable');
|
||||
const path = require('path');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
var Language = Immutable.Record({
|
||||
const Language = Immutable.Record({
|
||||
title: String(),
|
||||
path: String()
|
||||
});
|
||||
@@ -1,9 +1,9 @@
|
||||
var Immutable = require('immutable');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
var File = require('./file');
|
||||
var Language = require('./language');
|
||||
const File = require('./file');
|
||||
const Language = require('./language');
|
||||
|
||||
var Languages = Immutable.Record({
|
||||
const Languages = Immutable.Record({
|
||||
file: File(),
|
||||
list: Immutable.OrderedMap()
|
||||
});
|
||||
@@ -52,7 +52,7 @@ Languages.prototype.getCount = function() {
|
||||
@return {Language}
|
||||
*/
|
||||
Languages.createFromList = function(file, langs) {
|
||||
var list = Immutable.OrderedMap();
|
||||
let list = Immutable.OrderedMap();
|
||||
|
||||
langs.forEach(function(lang) {
|
||||
lang = Language({
|
||||
@@ -63,8 +63,8 @@ Languages.createFromList = function(file, langs) {
|
||||
});
|
||||
|
||||
return Languages({
|
||||
file: file,
|
||||
list: list
|
||||
file,
|
||||
list
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
var Immutable = require('immutable');
|
||||
const Immutable = require('immutable');
|
||||
|
||||
var Book = require('./book');
|
||||
var LocationUtils = require('../utils/location');
|
||||
const Book = require('./book');
|
||||
const LocationUtils = require('../utils/location');
|
||||
|
||||
var Output = Immutable.Record({
|
||||
const Output = Immutable.Record({
|
||||
book: Book(),
|
||||
|
||||
// Name of the generator being used
|
||||
@@ -62,7 +62,7 @@ Output.prototype.getState = function() {
|
||||
Output.prototype.getPage = function(filePath) {
|
||||
filePath = LocationUtils.normalize(filePath);
|
||||
|
||||
var pages = this.getPages();
|
||||
const pages = this.getPages();
|
||||
return pages.get(filePath);
|
||||
};
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user