You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/meta/templates.js

167 lines
4.2 KiB
JavaScript

8 years ago
'use strict';
10 years ago
8 years ago
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var winston = require('winston');
var async = require('async');
var path = require('path');
var fs = require('fs');
var nconf = require('nconf');
8 years ago
var plugins = require('../plugins');
var file = require('../file');
var viewsPath = nconf.get('views_dir');
8 years ago
var Templates = module.exports;
function processImports(paths, templatePath, source, callback) {
var regex = /<!-- IMPORT (.+?) -->/;
8 years ago
var matches = source.match(regex);
8 years ago
if (!matches) {
return callback(null, source);
}
8 years ago
var partial = '/' + matches[1];
if (paths[partial] && templatePath !== partial) {
fs.readFile(paths[partial], 'utf8', function (err, partialSource) {
if (err) {
return callback(err);
}
8 years ago
source = source.replace(regex, partialSource);
processImports(paths, templatePath, source, callback);
});
} else {
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
source = source.replace(regex, '');
8 years ago
processImports(paths, templatePath, source, callback);
}
}
Templates.processImports = processImports;
8 years ago
Templates.compile = function (callback) {
callback = callback || function () {};
8 years ago
var themeConfig = require(nconf.get('theme_config'));
var baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')];
10 years ago
8 years ago
async.waterfall([
function (next) {
preparePaths(baseTemplatesPaths, next);
},
function (paths, next) {
async.each(Object.keys(paths), function (relativePath, next) {
async.waterfall([
function (next) {
fs.readFile(paths[relativePath], 'utf8', next);
8 years ago
},
function (source, next) {
8 years ago
processImports(paths, relativePath, source, next);
},
function (source, next) {
8 years ago
mkdirp(path.join(viewsPath, path.dirname(relativePath)), function (err) {
next(err, source);
8 years ago
});
},
function (compiled, next) {
fs.writeFile(path.join(viewsPath, relativePath), compiled, next);
},
], next);
}, next);
},
function (next) {
rimraf(path.join(viewsPath, '*.js'), next);
},
8 years ago
function (next) {
winston.verbose('[meta/templates] Successfully compiled templates.');
next();
},
], callback);
};
10 years ago
function getBaseTemplates(theme) {
var baseTemplatesPaths = [];
var baseThemePath;
var baseThemeConfig;
while (theme) {
baseThemePath = path.join(nconf.get('themes_path'), theme);
baseThemeConfig = require(path.join(baseThemePath, 'theme.json'));
baseTemplatesPaths.push(path.join(baseThemePath, baseThemeConfig.templates || 'templates'));
theme = baseThemeConfig.baseTheme;
10 years ago
}
return baseTemplatesPaths.reverse();
}
function preparePaths(baseTemplatesPaths, callback) {
9 years ago
var coreTemplatesPath = nconf.get('core_templates_path');
8 years ago
var pluginTemplates;
async.waterfall([
9 years ago
function (next) {
rimraf(viewsPath, next);
},
function (next) {
mkdirp(viewsPath, next);
},
function (viewsPath, next) {
9 years ago
plugins.fireHook('static:templates.precompile', {}, next);
},
function (next) {
9 years ago
plugins.getTemplates(next);
},
8 years ago
function (_pluginTemplates, next) {
pluginTemplates = _pluginTemplates;
winston.verbose('[meta/templates] Compiling templates');
8 years ago
async.parallel({
coreTpls: function (next) {
file.walk(coreTemplatesPath, next);
},
baseThemes: function (next) {
async.map(baseTemplatesPaths, function (baseTemplatePath, next) {
file.walk(baseTemplatePath, function (err, paths) {
paths = paths.map(function (tpl) {
return {
base: baseTemplatePath,
path: tpl.replace(baseTemplatePath, ''),
};
});
next(err, paths);
});
8 years ago
}, next);
},
}, next);
},
function (data, next) {
var baseThemes = data.baseThemes;
var coreTpls = data.coreTpls;
var paths = {};
coreTpls.forEach(function (el, i) {
paths[coreTpls[i].replace(coreTemplatesPath, '')] = coreTpls[i];
});
baseThemes.forEach(function (baseTpls) {
baseTpls.forEach(function (el, i) {
paths[baseTpls[i].path] = path.join(baseTpls[i].base, baseTpls[i].path);
});
});
for (var tpl in pluginTemplates) {
if (pluginTemplates.hasOwnProperty(tpl)) {
paths[tpl] = pluginTemplates[tpl];
}
}
8 years ago
next(null, paths);
},
], callback);
}