Skip to content

Instantly share code, notes, and snippets.

@morgondag
Created September 1, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morgondag/0ef4bfe70a79bf541437 to your computer and use it in GitHub Desktop.
Save morgondag/0ef4bfe70a79bf541437 to your computer and use it in GitHub Desktop.
render
var render = require('./app/render.js');
// Build our templates
gulp.task('templates', function() {
render.render(templateData.JS, templateData.CSS);
})
var lang = require('./locales/lang.js');
var Handlebars = require('handlebars');
var fs = require('fs');
var maxDepth = 10;
var allowedExtensions = ['hb', 'hbs', 'handlebars', 'html'];
var isDir = function (filename) {
var stats = fs.statSync(filename);
return stats && stats.isDirectory();
};
var isHandlebars = function (filename) {
return allowedExtensions.indexOf(filename.split('.').pop()) !== -1;
};
var partialName = function (filename, base) {
var name = filename.substr(0, filename.lastIndexOf('.'));
name = name.replace(new RegExp('^' + base + '\\/'), '');
return name.substring(name.charAt(0) === '_' ? 1 : 0);
};
var registerPartial = function (filename, base) {
if (!isHandlebars(filename)) { return; }
var name = partialName(filename, base);
var template = fs.readFileSync(filename, 'utf8');
Handlebars.registerPartial(name, template);
};
var registerPartials = function (dir, base, depth) {
if (depth > maxDepth) { return; }
base = base || dir;
fs.readdirSync(dir).forEach(function (basename) {
var filename = dir + '/' + basename;
if (isDir(filename)) {
registerPartials(filename, base);
} else {
registerPartial(filename, base);
}
});
};
registerPartials('./app/templates/partials','./app/templates/partials',0);
var sites = [];
var getSites = function(){
sites = [];
fs.readdir('./app/templates/', function(error, files){
files.splice(files.indexOf('partials'),1);
for (var i = 0; i < files.length; i++) {
if(files[i].indexOf('.') > -1){
} else {
sites.push(files[i]);
}
};
console.log('got em si', sites)
});
}
//getSites();
var getData = new Promise(function(resolve, reject) {
var getFolders = new Promise(function(resolve, reject) {
var sites = [];
fs.readdir('./app/templates/', function(error, files){
if(error){
reject(Error("getFolders broke"));
}
files.splice(files.indexOf('partials'),1);
for (var i = 0; i < files.length; i++) {
if(files[i].indexOf('.') > -1){
} else {
sites.push(files[i]);
}
};
resolve(sites);
});
});
var getLang = new Promise(function(resolve, reject) {
var langs = [];
fs.readdir('./app/locales', function(error, files){
if(error){
reject(Error("getLang broke"));
}
langs = [];
files.splice(files.indexOf('lang.js'),1);
for (var i = 0; i < files.length; i++) {
langs.push(files[i].split('.')[0]);
}
resolve(langs);
});
});
Promise.all([getFolders,getLang]).then(function(data){
resolve(data);
}).catch(function(err) {
reject(Error("getData broke"));
})
});
module.exports.render = function(js,css){
/*
var hbs = fs.readFileSync('./app/templates/index.hbs', 'utf8');
var template = Handlebars.compile(hbs);
console.log(template);
return true;
*/
return getData.then(function(data){
console.log('all data ready',data)
})
}
module.exports.handlebars = Handlebars;
@GitGhassan
Copy link

omg -> console.log? @morgondag

@morgondag
Copy link
Author

at-least you understood that line ;-p

@hussfelt
Copy link

hussfelt commented Sep 1, 2015

@morgondag @ghassan-s console.log FTW! <3

@morgondag
Copy link
Author

@hussfelt @ghassan-s
Here is the main idea:

render->find-sites(folders)*findLangs(locals)+partials+helpers+js+css+templateData = sites^x

Then we sit as authors for the handelbar views and write fetches for templateData.
as well as client js and client CSS(though scss the ruby shit)

Using the filesystem and folders structure as config on what to render = instant new sites.

K lets get to work:
http://datashat.net/music_for_programming_33-uuav.mp3

@morgondag
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment