From da5494fdf1216a5bcb5bd15b4ce7f40891dc9ed1 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 22 Feb 2016 14:20:50 -0500 Subject: [PATCH] allow recursive base templates ex. custom theme based off lavender which is based off vanilla --- src/meta/templates.js | 73 ++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/src/meta/templates.js b/src/meta/templates.js index 9a5aa5c72b..06e4efbe74 100644 --- a/src/meta/templates.js +++ b/src/meta/templates.js @@ -12,7 +12,8 @@ var mkdirp = require('mkdirp'), plugins = require('../plugins'), utils = require('../../public/src/utils'), - Templates = {}; + Templates = {}, + searchIndex = {}; Templates.compile = function(callback) { callback = callback || function() {}; @@ -27,21 +28,32 @@ Templates.compile = function(callback) { return callback(); } - var coreTemplatesPath = nconf.get('core_templates_path'), - baseTemplatesPath = nconf.get('base_templates_path'), - viewsPath = nconf.get('views_dir'), - themeTemplatesPath = nconf.get('theme_templates_path'), - themeConfig = require(nconf.get('theme_config')); + compile(callback); +}; - if (themeConfig.baseTheme) { - var pathToBaseTheme = path.join(nconf.get('themes_path'), themeConfig.baseTheme); - baseTemplatesPath = require(path.join(pathToBaseTheme, 'theme.json')).templates; - if (!baseTemplatesPath){ - baseTemplatesPath = path.join(pathToBaseTheme, 'templates'); - } +function getBaseTemplates(theme) { + var baseTemplatesPaths = [], + baseThemePath, 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; } + return baseTemplatesPaths.reverse(); +} + +function compile(callback) { + var coreTemplatesPath = nconf.get('core_templates_path'), + themeConfig = require(nconf.get('theme_config')), + baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')], + viewsPath = nconf.get('views_dir'); + + plugins.getTemplates(function(err, pluginTemplates) { if (err) { return callback(err); @@ -54,27 +66,33 @@ Templates.compile = function(callback) { coreTpls: function(next) { utils.walk(coreTemplatesPath, next); }, - baseTpls: function(next) { - utils.walk(baseTemplatesPath, next); + baseThemes: function(next) { + async.map(baseTemplatesPaths, function(baseTemplatePath, next) { + utils.walk(baseTemplatePath, function(err, paths) { + paths = paths.map(function(tpl) { + return { + base: baseTemplatePath, + path: tpl.replace(baseTemplatePath, '') + }; + }); + + next(err, paths); + }); + }, next); } }, function(err, data) { - var coreTpls = data.coreTpls, - baseTpls = data.baseTpls, + var baseThemes = data.baseThemes, + coreTpls = data.coreTpls, paths = {}; - if (!baseTpls) { - winston.warn('[meta/templates] Could not find base template files at: ' + baseTemplatesPath); - } - - coreTpls = !coreTpls ? [] : coreTpls.map(function(tpl) { return tpl.replace(coreTemplatesPath, ''); }); - baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); }); - coreTpls.forEach(function(el, i) { - paths[coreTpls[i]] = path.join(coreTemplatesPath, coreTpls[i]); + paths[coreTpls[i].replace(coreTemplatesPath, '')] = path.join(coreTemplatesPath, coreTpls[i]); }); - baseTpls.forEach(function(el, i) { - paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[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) { @@ -125,9 +143,8 @@ Templates.compile = function(callback) { }); }); }); -}; +} -var searchIndex = {}; function addIndex(path, file) { searchIndex[path] = file;