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

158 lines
3.9 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');
var _ = require('lodash');
8 years ago
var plugins = require('../plugins');
var file = require('../file');
7 years ago
var db = require('../database');
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
7 years ago
function getTemplateDirs(activePlugins, callback) {
var pluginTemplates = activePlugins.map(function (id) {
if (id.startsWith('nodebb-theme-')) {
return nconf.get('theme_templates_path');
}
if (!plugins.pluginsData[id]) {
return '';
}
7 years ago
return path.join(__dirname, '../../node_modules/', id, plugins.pluginsData[id].templates || 'templates');
}).filter(Boolean);
8 years ago
var themeConfig = require(nconf.get('theme_config'));
var theme = themeConfig.baseTheme;
var themePath;
7 years ago
var themeTemplates = [];
while (theme) {
themePath = path.join(nconf.get('themes_path'), theme);
themeConfig = require(path.join(themePath, 'theme.json'));
themeTemplates.push(path.join(themePath, themeConfig.templates || 'templates'));
theme = themeConfig.baseTheme;
}
themeTemplates.push(nconf.get('base_templates_path'));
themeTemplates = _.uniq(themeTemplates.reverse());
var coreTemplatesPath = nconf.get('core_templates_path');
var templateDirs = _.uniq([coreTemplatesPath].concat(themeTemplates, pluginTemplates));
async.filter(templateDirs, file.exists, callback);
}
function getTemplateFiles(dirs, callback) {
async.waterfall([
function (cb) {
async.map(dirs, function (dir, next) {
file.walk(dir, function (err, files) {
if (err) { return next(err); }
files = files.filter(function (path) {
return path.endsWith('.tpl');
}).map(function (file) {
return {
name: path.relative(dir, file).replace(/\\/g, '/'),
path: file,
};
});
next(null, files);
});
}, cb);
},
function (buckets, cb) {
var dict = {};
buckets.forEach(function (files) {
files.forEach(function (file) {
dict[file.name] = file.path;
});
});
cb(null, dict);
},
], callback);
}
function compile(callback) {
callback = callback || function () {};
10 years ago
8 years ago
async.waterfall([
function (next) {
rimraf(viewsPath, function (err) { next(err); });
},
function (next) {
mkdirp(viewsPath, function (err) { next(err); });
8 years ago
},
7 years ago
function (next) {
db.getSortedSetRange('plugins:active', 0, -1, next);
},
getTemplateDirs,
getTemplateFiles,
function (files, next) {
async.each(Object.keys(files), function (name, next) {
var filePath = files[name];
8 years ago
async.waterfall([
function (next) {
fs.readFile(filePath, 'utf8', next);
8 years ago
},
function (source, next) {
processImports(files, name, source, next);
8 years ago
},
function (source, next) {
mkdirp(path.join(viewsPath, path.dirname(name)), function (err) {
next(err, source);
8 years ago
});
},
function (compiled, next) {
fs.writeFile(path.join(viewsPath, name), compiled, next);
8 years ago
},
], next);
}, next);
},
function (next) {
winston.verbose('[meta/templates] Successfully compiled templates.');
next();
},
], callback);
}
Templates.compile = compile;