diff options
Diffstat (limited to 'lib/output/website')
-rw-r--r-- | lib/output/website/__tests__/i18n.js | 38 | ||||
-rw-r--r-- | lib/output/website/copyPluginAssets.js | 117 | ||||
-rw-r--r-- | lib/output/website/createTemplateEngine.js | 151 | ||||
-rw-r--r-- | lib/output/website/index.js | 11 | ||||
-rw-r--r-- | lib/output/website/listSearchPaths.js | 23 | ||||
-rw-r--r-- | lib/output/website/onAsset.js | 28 | ||||
-rw-r--r-- | lib/output/website/onFinish.js | 35 | ||||
-rw-r--r-- | lib/output/website/onInit.js | 20 | ||||
-rw-r--r-- | lib/output/website/onPage.js | 76 | ||||
-rw-r--r-- | lib/output/website/options.js | 14 | ||||
-rw-r--r-- | lib/output/website/prepareI18n.js | 30 | ||||
-rw-r--r-- | lib/output/website/prepareResources.js | 54 | ||||
-rw-r--r-- | lib/output/website/state.js | 19 |
13 files changed, 0 insertions, 616 deletions
diff --git a/lib/output/website/__tests__/i18n.js b/lib/output/website/__tests__/i18n.js deleted file mode 100644 index fd610fb..0000000 --- a/lib/output/website/__tests__/i18n.js +++ /dev/null @@ -1,38 +0,0 @@ -var createMockOutput = require('../../__tests__/createMock'); -var prepareI18n = require('../prepareI18n'); -var createTemplateEngine = require('../createTemplateEngine'); - -var WebsiteGenerator = require('../'); - -describe('i18n', function() { - it('should correctly use english as default language', function() { - return createMockOutput(WebsiteGenerator, { - 'README.md': 'Hello World' - }) - .then(function(output) { - return prepareI18n(output); - }) - .then(function(output) { - var engine = createTemplateEngine(output, 'README.md'); - var t = engine.getFilters().get('t'); - - expect(t('SUMMARY_INTRODUCTION')).toEqual('Introduction'); - }); - }); - - it('should correctly use language from book.json', function() { - return createMockOutput(WebsiteGenerator, { - 'README.md': 'Hello World', - 'book.json': JSON.stringify({ language: 'fr' }) - }) - .then(function(output) { - return prepareI18n(output); - }) - .then(function(output) { - var engine = createTemplateEngine(output, 'README.md'); - var t = engine.getFilters().get('t'); - - expect(t('GITBOOK_LINK')).toEqual('PubliƩ avec GitBook'); - }); - }); -}); diff --git a/lib/output/website/copyPluginAssets.js b/lib/output/website/copyPluginAssets.js deleted file mode 100644 index 9150636..0000000 --- a/lib/output/website/copyPluginAssets.js +++ /dev/null @@ -1,117 +0,0 @@ -var path = require('path'); - -var ASSET_FOLDER = require('../../constants/pluginAssetsFolder'); -var Promise = require('../../utils/promise'); -var fs = require('../../utils/fs'); - -/** - Copy all assets from plugins. - Assets are files stored in "_assets" - nd resources declared in the plugin itself. - - @param {Output} - @return {Promise} -*/ -function copyPluginAssets(output) { - var book = output.getBook(); - - // Don't copy plugins assets for language book - // It'll be resolved to the parent folder - if (book.isLanguageBook()) { - return Promise(output); - } - - var plugins = output.getPlugins() - - // We reverse the order of plugins to copy - // so that first plugins can replace assets from other plugins. - .reverse(); - - return Promise.forEach(plugins, function(plugin) { - return copyAssets(output, plugin) - .then(function() { - return copyResources(output, plugin); - }); - }) - .thenResolve(output); -} - -/** - Copy assets from a plugin - - @param {Plugin} - @return {Promise} -*/ -function copyAssets(output, plugin) { - var logger = output.getLogger(); - var pluginRoot = plugin.getPath(); - var options = output.getOptions(); - - var outputRoot = options.get('root'); - var assetOutputFolder = path.join(outputRoot, 'gitbook'); - var prefix = options.get('prefix'); - - var assetFolder = path.join(pluginRoot, ASSET_FOLDER, prefix); - - if (!fs.existsSync(assetFolder)) { - return Promise(); - } - - logger.debug.ln('copy assets from theme', assetFolder); - return fs.copyDir( - assetFolder, - assetOutputFolder, - { - deleteFirst: false, - overwrite: true, - confirm: true - } - ); -} - -/** - Copy resources from a plugin - - @param {Plugin} - @return {Promise} -*/ -function copyResources(output, plugin) { - var logger = output.getLogger(); - - var options = output.getOptions(); - var outputRoot = options.get('root'); - - var state = output.getState(); - var resources = state.getResources(); - - var pluginRoot = plugin.getPath(); - var pluginResources = resources.get(plugin.getName()); - - var assetsFolder = pluginResources.get('assets'); - var assetOutputFolder = path.join(outputRoot, 'gitbook', plugin.getNpmID()); - - if (!assetsFolder) { - return Promise(); - } - - // Resolve assets folder - assetsFolder = path.resolve(pluginRoot, assetsFolder); - if (!fs.existsSync(assetsFolder)) { - logger.warn.ln('assets folder for plugin "' + plugin.getName() + '" doesn\'t exist'); - return Promise(); - } - - logger.debug.ln('copy resources from plugin', assetsFolder); - - return fs.copyDir( - assetsFolder, - assetOutputFolder, - { - deleteFirst: false, - overwrite: true, - confirm: true - } - ); -} - -module.exports = copyPluginAssets; diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js deleted file mode 100644 index 02ec796..0000000 --- a/lib/output/website/createTemplateEngine.js +++ /dev/null @@ -1,151 +0,0 @@ -var path = require('path'); -var nunjucks = require('nunjucks'); -var DoExtension = require('nunjucks-do')(nunjucks); - -var Api = require('../../api'); -var deprecate = require('../../api/deprecate'); -var JSONUtils = require('../../json'); -var LocationUtils = require('../../utils/location'); -var fs = require('../../utils/fs'); -var PathUtils = require('../../utils/path'); -var TemplateEngine = require('../../models/templateEngine'); -var templatesFolder = require('../../constants/templatesFolder'); -var defaultFilters = require('../../constants/defaultFilters'); -var Templating = require('../../templating'); -var listSearchPaths = require('./listSearchPaths'); - -var fileToURL = require('../helper/fileToURL'); -var resolveFileToURL = require('../helper/resolveFileToURL'); - -/** - * Directory for a theme with the templates - */ -function templateFolder(dir) { - return path.join(dir, templatesFolder); -} - -/** - * Create templating engine to render themes - * - * @param {Output} output - * @param {String} currentFile - * @return {TemplateEngine} - */ -function createTemplateEngine(output, currentFile) { - var book = output.getBook(); - var state = output.getState(); - var i18n = state.getI18n(); - var config = book.getConfig(); - var summary = book.getSummary(); - var outputFolder = output.getRoot(); - - // Search paths for templates - var searchPaths = listSearchPaths(output); - var tplSearchPaths = searchPaths.map(templateFolder); - - // Create loader - var loader = new Templating.ThemesLoader(tplSearchPaths); - - // Get languages - var language = config.getValue('language'); - - // Create API context - var context = Api.encodeGlobal(output); - - - /** - * Check if a file exists - * @param {String} fileName - * @return {Boolean} - */ - function fileExists(fileName) { - if (!fileName) { - return false; - } - - var filePath = PathUtils.resolveInRoot(outputFolder, fileName); - return fs.existsSync(filePath); - } - - /** - * Return an article by its path - * @param {String} filePath - * @return {Object|undefined} - */ - function getArticleByPath(filePath) { - var article = summary.getByPath(filePath); - if (!article) return undefined; - - return JSONUtils.encodeSummaryArticle(article); - } - - /** - * Return a page by its path - * @param {String} filePath - * @return {Object|undefined} - */ - function getPageByPath(filePath) { - var page = output.getPage(filePath); - if (!page) return undefined; - - return JSONUtils.encodePage(page, summary); - } - - return TemplateEngine.create({ - loader: loader, - - context: context, - - globals: { - getArticleByPath: getArticleByPath, - getPageByPath: getPageByPath, - fileExists: fileExists - }, - - filters: defaultFilters.merge({ - /** - * Translate a sentence - */ - t: function t(s) { - return i18n.t(language, s); - }, - - /** - * Resolve an absolute file path into a - * relative path. - * it also resolve pages - */ - resolveFile: function(filePath) { - filePath = resolveFileToURL(output, filePath); - return LocationUtils.relativeForFile(currentFile, filePath); - }, - - resolveAsset: function(filePath) { - filePath = LocationUtils.toAbsolute(filePath, '', ''); - filePath = path.join('gitbook', filePath); - filePath = LocationUtils.relativeForFile(currentFile, filePath); - - // Use assets from parent if language book - if (book.isLanguageBook()) { - filePath = path.join('../', filePath); - } - - return LocationUtils.normalize(filePath); - }, - - - fileExists: deprecate.method(book, 'fileExists', fileExists, 'Filter "fileExists" is deprecated, use "fileExists(filename)" '), - getArticleByPath: deprecate.method(book, 'getArticleByPath', fileExists, 'Filter "getArticleByPath" is deprecated, use "getArticleByPath(filename)" '), - - contentURL: function(filePath) { - return fileToURL(output, filePath); - } - }), - - extensions: { - 'DoExtension': new DoExtension() - } - }); -} - -module.exports = createTemplateEngine; diff --git a/lib/output/website/index.js b/lib/output/website/index.js deleted file mode 100644 index 7818a28..0000000 --- a/lib/output/website/index.js +++ /dev/null @@ -1,11 +0,0 @@ - -module.exports = { - name: 'website', - State: require('./state'), - Options: require('./options'), - onInit: require('./onInit'), - onFinish: require('./onFinish'), - onPage: require('./onPage'), - onAsset: require('./onAsset'), - createTemplateEngine: require('./createTemplateEngine') -}; diff --git a/lib/output/website/listSearchPaths.js b/lib/output/website/listSearchPaths.js deleted file mode 100644 index c45f39c..0000000 --- a/lib/output/website/listSearchPaths.js +++ /dev/null @@ -1,23 +0,0 @@ - -/** - List search paths for templates / i18n, etc - - @param {Output} output - @return {List<String>} -*/ -function listSearchPaths(output) { - var book = output.getBook(); - var plugins = output.getPlugins(); - - var searchPaths = plugins - .valueSeq() - .map(function(plugin) { - return plugin.getPath(); - }) - .toList(); - - return searchPaths.unshift(book.getContentRoot()); -} - - -module.exports = listSearchPaths; diff --git a/lib/output/website/onAsset.js b/lib/output/website/onAsset.js deleted file mode 100644 index 69dfc4f..0000000 --- a/lib/output/website/onAsset.js +++ /dev/null @@ -1,28 +0,0 @@ -var path = require('path'); -var fs = require('../../utils/fs'); - -/** - Copy an asset to the output folder - - @param {Output} output - @param {Page} page -*/ -function onAsset(output, asset) { - var book = output.getBook(); - var options = output.getOptions(); - var bookFS = book.getContentFS(); - - var outputFolder = options.get('root'); - var outputPath = path.resolve(outputFolder, asset); - - return fs.ensureFile(outputPath) - .then(function() { - return bookFS.readAsStream(asset) - .then(function(stream) { - return fs.writeStream(outputPath, stream); - }); - }) - .thenResolve(output); -} - -module.exports = onAsset; diff --git a/lib/output/website/onFinish.js b/lib/output/website/onFinish.js deleted file mode 100644 index 5267458..0000000 --- a/lib/output/website/onFinish.js +++ /dev/null @@ -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; diff --git a/lib/output/website/onInit.js b/lib/output/website/onInit.js deleted file mode 100644 index 3465eef..0000000 --- a/lib/output/website/onInit.js +++ /dev/null @@ -1,20 +0,0 @@ -var Promise = require('../../utils/promise'); - -var copyPluginAssets = require('./copyPluginAssets'); -var prepareI18n = require('./prepareI18n'); -var prepareResources = require('./prepareResources'); - -/** - Initialize the generator - - @param {Output} - @return {Output} -*/ -function onInit(output) { - return Promise(output) - .then(prepareI18n) - .then(prepareResources) - .then(copyPluginAssets); -} - -module.exports = onInit; diff --git a/lib/output/website/onPage.js b/lib/output/website/onPage.js deleted file mode 100644 index 5fb40a7..0000000 --- a/lib/output/website/onPage.js +++ /dev/null @@ -1,76 +0,0 @@ -var path = require('path'); -var omit = require('omit-keys'); - -var Templating = require('../../templating'); -var Plugins = require('../../plugins'); -var JSONUtils = require('../../json'); -var LocationUtils = require('../../utils/location'); -var Modifiers = require('../modifiers'); -var writeFile = require('../helper/writeFile'); -var getModifiers = require('../getModifiers'); -var createTemplateEngine = require('./createTemplateEngine'); -var fileToOutput = require('../helper/fileToOutput'); - -/** - * Write a page as a json file - * - * @param {Output} output - * @param {Page} page - */ -function onPage(output, page) { - var options = output.getOptions(); - var prefix = options.get('prefix'); - - var file = page.getFile(); - - var book = output.getBook(); - var plugins = output.getPlugins(); - var state = output.getState(); - var resources = state.getResources(); - - var engine = createTemplateEngine(output, page.getPath()); - - // Output file path - var filePath = fileToOutput(output, file.getPath()); - - // Calcul relative path to the root - var outputDirName = path.dirname(filePath); - var basePath = LocationUtils.normalize(path.relative(outputDirName, './')); - - return Modifiers.modifyHTML(page, getModifiers(output, page)) - .then(function(resultPage) { - // Generate the context - var context = JSONUtils.encodeOutputWithPage(output, resultPage); - context.plugins = { - resources: Plugins.listResources(plugins, resources).toJS() - }; - - context.template = { - getJSContext: function() { - return { - page: omit(context.page, 'content'), - config: context.config, - file: context.file, - gitbook: context.gitbook, - basePath: basePath, - book: { - language: book.getLanguage() - } - }; - } - }; - - // We should probabbly move it to "template" or a "site" namespace - context.basePath = basePath; - - // Render the theme - return Templating.renderFile(engine, prefix + '/page.html', context) - - // Write it to the disk - .then(function(tplOut) { - return writeFile(output, filePath, tplOut.getContent()); - }); - }); -} - -module.exports = onPage; diff --git a/lib/output/website/options.js b/lib/output/website/options.js deleted file mode 100644 index ac9cdad..0000000 --- a/lib/output/website/options.js +++ /dev/null @@ -1,14 +0,0 @@ -var Immutable = require('immutable'); - -var Options = Immutable.Record({ - // Root folder for the output - root: String(), - - // Prefix for generation - prefix: String('website'), - - // Use directory index url instead of "index.html" - directoryIndex: Boolean(true) -}); - -module.exports = Options; diff --git a/lib/output/website/prepareI18n.js b/lib/output/website/prepareI18n.js deleted file mode 100644 index cedd3b9..0000000 --- a/lib/output/website/prepareI18n.js +++ /dev/null @@ -1,30 +0,0 @@ -var path = require('path'); - -var fs = require('../../utils/fs'); -var Promise = require('../../utils/promise'); -var listSearchPaths = require('./listSearchPaths'); - -/** - * Prepare i18n, load translations from plugins and book - * - * @param {Output} - * @return {Promise<Output>} - */ -function prepareI18n(output) { - var state = output.getState(); - var i18n = state.getI18n(); - var searchPaths = listSearchPaths(output); - - searchPaths - .reverse() - .forEach(function(searchPath) { - var i18nRoot = path.resolve(searchPath, '_i18n'); - - if (!fs.existsSync(i18nRoot)) return; - i18n.load(i18nRoot); - }); - - return Promise(output); -} - -module.exports = prepareI18n; diff --git a/lib/output/website/prepareResources.js b/lib/output/website/prepareResources.js deleted file mode 100644 index 4e6835d..0000000 --- a/lib/output/website/prepareResources.js +++ /dev/null @@ -1,54 +0,0 @@ -var is = require('is'); -var Immutable = require('immutable'); -var Promise = require('../../utils/promise'); - -var Api = require('../../api'); - -/** - Prepare plugins resources, add all output corresponding type resources - - @param {Output} - @return {Promise<Output>} -*/ -function prepareResources(output) { - var plugins = output.getPlugins(); - var options = output.getOptions(); - var type = options.get('prefix'); - var state = output.getState(); - var context = Api.encodeGlobal(output); - - var result = Immutable.Map(); - - return Promise.forEach(plugins, function(plugin) { - var pluginResources = plugin.getResources(type); - - return Promise() - .then(function() { - // Apply resources if is a function - if (is.fn(pluginResources)) { - return Promise() - .then(pluginResources.bind(context)); - } - else { - return pluginResources; - } - }) - .then(function(resources) { - result = result.set(plugin.getName(), Immutable.Map(resources)); - }); - }) - .then(function() { - // Set output resources - state = state.merge({ - resources: result - }); - - output = output.merge({ - state: state - }); - - return output; - }); -} - -module.exports = prepareResources;
\ No newline at end of file diff --git a/lib/output/website/state.js b/lib/output/website/state.js deleted file mode 100644 index cb8f750..0000000 --- a/lib/output/website/state.js +++ /dev/null @@ -1,19 +0,0 @@ -var I18n = require('i18n-t'); -var Immutable = require('immutable'); - -var GeneratorState = Immutable.Record({ - i18n: I18n(), - - // List of plugins' resources - resources: Immutable.Map() -}); - -GeneratorState.prototype.getI18n = function() { - return this.get('i18n'); -}; - -GeneratorState.prototype.getResources = function() { - return this.get('resources'); -}; - -module.exports = GeneratorState; |