Commit 1449e2ae authored by Isabella Skořepová's avatar Isabella Skořepová

Allow nunjucks to render multiple templates

parent 32030007
......@@ -158,3 +158,4 @@ var jobs = {
}
module.exports = jobs;
module.exports.jobList = jobList;
......@@ -50,20 +50,65 @@ env.addFilter('relURL', function(filename, dir){
return dir+'/'+filename;
})
var formats = {};
module.exports = function(data, type) {
return new Promise(function(resolve, reject) {
env.getTemplate(type+'.html.nunj', true, function(err, tmpl) {
if(err) reject(new Error('Failed to load template '+type+'.html.nunj'));
resolve(tmpl);
});
}).then(function(tmpl) {
return new Promise(function(resolve, reject){
tmpl.render(data, function(err, val) {
if(err) {
reject(err);
}
resolve(val);
if(formats[type]) resolve(formats[type]);
else {
fs.readdir('theme/templates',function(err, files) {
if(err) return reject(new Error('Error listing files in theme/templates'));
files = files.filter(function(file) {
return file.match(type+'\..*\.nunj');
});
var filteredFiles = [];
var todo = 0;
files.forEach(function(file) {
fs.stat('theme/templates/'+file,function(err, stat) {
if(err) reject(new Error('Error stating theme/templates/'+file));
if(stat.isFile())
filteredFiles.push(file);
todo--;
if(todo <= 0) {
formats[type] = filteredFiles;
resolve(filteredFiles);
}
});
todo++;
});
});
})
}
}).then(function(filelist) {
var promises = [];
filelist.forEach(function(file){
var type = file.replace(/^.*\.([a-z]+)\.nunj/,'$1');
promises.push(new Promise(function(resolve, reject) {
env.getTemplate(file, function(err, tmpl) {
if(err) reject(new Error('Failed to load template '+type+'.html.nunj'));
resolve({tmpl: tmpl, type: type, file: file});
});
}));
});
return Promise.all(promises);
}).then(function(tmpls) {
var promises = [];
tmpls.forEach(function(o) {
promises.push(new Promise(function(resolve, reject){
o.tmpl.render(data, function(err, val) {
if(err) {
//reject(new Error('Error rendering template '+o.file));
reject(err);
}
resolve({val: val, type: o.type});
});
}));
});
return Promise.all(promises);
});
}
module.exports.reset = function() {
formats = {};
}
......@@ -4,6 +4,7 @@
module.exports = function(jobs) {
return jobs.runSequence(
'resetJobs',
'config',
'readFiles',
'parseHugo',
......
var jobs = require('./jobs');
module.exports = function() {
for(var jobname in jobs.jobList) {
if(! jobs.jobList.hasOwnProperty(jobname)) continue;
var job = jobs.jobList[jobname];
if(job.f.reset) {
job.f.reset();
}
}
return Promise.resolve();
}
......@@ -15,6 +15,7 @@ module.exports = function(config) {
['theme', './theme'],
['urls', './urls'],
['writeFiles', './writeFiles'],
['resetJobs', './resetJobs'],
['pipeline','./pipeline']
)
......
......@@ -11,7 +11,14 @@ module.exports = function(obj) {
todo++;
jobs.run('nunjucks', obj, type)
.then(function(data) {
obj.content = data;
data.forEach(function(o){
if(o.type == 'html') obj.content = o.val;
else {
if(!obj.otherContent) obj.otherContent = [];
obj.otherContent.push(o);
}
});
done();
})
.catch(function(e) {reject(e)})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment