From 1449e2aea798d7809beaf93af6d6de455d7e8301 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Sko=C5=99epa?= <jakub@skorepa.info>
Date: Sat, 26 Mar 2016 19:39:26 +0100
Subject: [PATCH] Allow nunjucks to render multiple templates

---
 sitegin/jobs.js      |  1 +
 sitegin/nunjucks.js  | 69 ++++++++++++++++++++++++++++++++++++--------
 sitegin/pipeline.js  |  1 +
 sitegin/resetJobs.js | 12 ++++++++
 sitegin/sitegin.js   |  1 +
 sitegin/theme.js     |  9 +++++-
 6 files changed, 80 insertions(+), 13 deletions(-)
 create mode 100644 sitegin/resetJobs.js

diff --git a/sitegin/jobs.js b/sitegin/jobs.js
index 89e1217f..128290e7 100644
--- a/sitegin/jobs.js
+++ b/sitegin/jobs.js
@@ -158,3 +158,4 @@ var jobs = {
 }
 
 module.exports = jobs;
+module.exports.jobList = jobList;
diff --git a/sitegin/nunjucks.js b/sitegin/nunjucks.js
index 328d4adc..7ad4405c 100644
--- a/sitegin/nunjucks.js
+++ b/sitegin/nunjucks.js
@@ -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 = {};
+}
diff --git a/sitegin/pipeline.js b/sitegin/pipeline.js
index 7bc89858..5c5780d5 100644
--- a/sitegin/pipeline.js
+++ b/sitegin/pipeline.js
@@ -4,6 +4,7 @@
 
 module.exports = function(jobs) {
   return jobs.runSequence(
+    'resetJobs',
     'config',
     'readFiles',
     'parseHugo',
diff --git a/sitegin/resetJobs.js b/sitegin/resetJobs.js
new file mode 100644
index 00000000..e5030f43
--- /dev/null
+++ b/sitegin/resetJobs.js
@@ -0,0 +1,12 @@
+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();
+}
diff --git a/sitegin/sitegin.js b/sitegin/sitegin.js
index 97bb02aa..8b7de6a1 100644
--- a/sitegin/sitegin.js
+++ b/sitegin/sitegin.js
@@ -15,6 +15,7 @@ module.exports = function(config) {
     ['theme', './theme'],
     ['urls', './urls'],
     ['writeFiles', './writeFiles'],
+    ['resetJobs', './resetJobs'],
 
     ['pipeline','./pipeline']
   )
diff --git a/sitegin/theme.js b/sitegin/theme.js
index aab5d9db..441c0e19 100644
--- a/sitegin/theme.js
+++ b/sitegin/theme.js
@@ -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)})
-- 
GitLab