2015-07-15 19:01:23 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
config = require('../config'),
|
2017-02-03 16:15:11 +03:00
|
|
|
utils = require('../utils'),
|
2015-07-15 19:01:23 +03:00
|
|
|
errors = require('../errors'),
|
2016-10-06 15:27:35 +03:00
|
|
|
i18n = require('../i18n'),
|
2017-03-03 01:05:35 +03:00
|
|
|
settingsCache = require('../settings/cache'),
|
2017-03-02 19:53:48 +03:00
|
|
|
themeList = require('../themes').list,
|
2015-07-15 19:01:23 +03:00
|
|
|
themeHandler;
|
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
themeHandler = {
|
|
|
|
// ### configHbsForContext Middleware
|
|
|
|
// Setup handlebars for the current context (admin or theme)
|
|
|
|
configHbsForContext: function configHbsForContext(req, res, next) {
|
2017-03-13 19:30:35 +03:00
|
|
|
// Static information, same for every request unless the settings change
|
|
|
|
// @TODO: bind this once and then update based on events?
|
2017-02-03 16:15:11 +03:00
|
|
|
var themeData = {
|
|
|
|
title: settingsCache.get('title'),
|
|
|
|
description: settingsCache.get('description'),
|
|
|
|
facebook: settingsCache.get('facebook'),
|
|
|
|
twitter: settingsCache.get('twitter'),
|
|
|
|
timezone: settingsCache.get('activeTimezone'),
|
|
|
|
navigation: settingsCache.get('navigation'),
|
|
|
|
posts_per_page: settingsCache.get('postsPerPage'),
|
|
|
|
icon: settingsCache.get('icon'),
|
|
|
|
cover: settingsCache.get('cover'),
|
|
|
|
logo: settingsCache.get('logo'),
|
|
|
|
amp: settingsCache.get('amp')
|
|
|
|
},
|
2017-03-13 19:30:35 +03:00
|
|
|
labsData = _.cloneDeep(settingsCache.get('labs'));
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2017-03-13 19:30:35 +03:00
|
|
|
// Request-specific information
|
|
|
|
// These things are super dependent on the request, so they need to be in middleware
|
|
|
|
themeData.url = utils.url.urlFor('home', {secure: req.secure}, true);
|
|
|
|
|
|
|
|
// Pass 'secure' flag to the view engine
|
|
|
|
// so that templates can choose to render https or http 'url', see url utility
|
|
|
|
res.locals.secure = req.secure;
|
|
|
|
|
|
|
|
// @TODO: only do this if something changed?
|
2017-02-03 16:15:11 +03:00
|
|
|
hbs.updateTemplateOptions({
|
|
|
|
data: {
|
|
|
|
blog: themeData,
|
|
|
|
labs: labsData
|
|
|
|
}
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
next();
|
|
|
|
},
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-11 23:21:54 +03:00
|
|
|
// ### Activate Theme
|
|
|
|
// Helper for updateActiveTheme
|
2017-03-13 19:30:35 +03:00
|
|
|
activateTheme: function activateTheme(blogApp, activeThemeName) {
|
|
|
|
var themePartialsPath = path.join(config.getContentPath('themes'), activeThemeName, 'partials'),
|
|
|
|
hbsOptions = {
|
|
|
|
partialsDir: [config.get('paths').helperTemplates],
|
|
|
|
onCompile: function onCompile(exhbs, source) {
|
|
|
|
return exhbs.handlebars.compile(source, {preventIndent: true});
|
|
|
|
}
|
|
|
|
};
|
2015-10-11 23:21:54 +03:00
|
|
|
|
2017-03-13 19:30:35 +03:00
|
|
|
fs.stat(themePartialsPath, function stat(err, stats) {
|
2015-10-11 23:21:54 +03:00
|
|
|
// Check that the theme has a partials directory before trying to use it
|
|
|
|
if (!err && stats && stats.isDirectory()) {
|
2017-03-13 19:30:35 +03:00
|
|
|
hbsOptions.partialsDir.push(themePartialsPath);
|
2015-10-11 23:21:54 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-13 19:30:35 +03:00
|
|
|
// reset the asset hash
|
|
|
|
config.set('assetHash', null);
|
|
|
|
// clear the view cache
|
|
|
|
blogApp.cache = {};
|
|
|
|
// Set the views and engine
|
|
|
|
blogApp.set('views', path.join(config.getContentPath('themes'), activeThemeName));
|
2015-10-11 23:21:54 +03:00
|
|
|
blogApp.engine('hbs', hbs.express3(hbsOptions));
|
|
|
|
|
|
|
|
// Set active theme variable on the express server
|
2017-03-13 19:30:35 +03:00
|
|
|
// Note: this is effectively the "mounted" theme, which has been loaded into the express app
|
|
|
|
blogApp.set('activeTheme', activeThemeName);
|
2015-10-11 23:21:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// ### updateActiveTheme
|
|
|
|
// Updates the blogApp's activeTheme variable and subsequently
|
|
|
|
// activates that theme's views with the hbs templating engine if it
|
|
|
|
// is not yet activated.
|
2016-07-21 14:26:16 +03:00
|
|
|
updateActiveTheme: function updateActiveTheme(req, res, next) {
|
2017-03-03 01:05:35 +03:00
|
|
|
var blogApp = req.app,
|
2017-03-13 19:30:35 +03:00
|
|
|
activeThemeName = settingsCache.get('activeTheme'),
|
|
|
|
mountedThemeName = blogApp.get('activeTheme');
|
2016-07-21 14:26:16 +03:00
|
|
|
|
2017-03-13 19:30:35 +03:00
|
|
|
// This means that the theme hasn't been loaded yet i.e. there is no active theme
|
|
|
|
if (!themeList.get(activeThemeName)) {
|
|
|
|
// This is the one place we ACTUALLY throw an error for a missing theme
|
|
|
|
// As it's a request we cannot serve
|
|
|
|
return next(new errors.InternalServerError({
|
|
|
|
message: i18n.t('errors.middleware.themehandler.missingTheme', {theme: activeThemeName})
|
|
|
|
}));
|
|
|
|
|
|
|
|
// If there is an active theme AND it has changed, call activate
|
|
|
|
} else if (activeThemeName !== mountedThemeName) {
|
|
|
|
// This is effectively "mounting" a theme into express, the theme is already "active"
|
|
|
|
themeHandler.activateTheme(blogApp, activeThemeName);
|
2017-03-03 01:05:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
2015-10-11 23:21:54 +03:00
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = themeHandler;
|