From a6d811b39865cbba6441ed9044c8782152aeefc5 Mon Sep 17 00:00:00 2001 From: mamod Date: Sat, 6 Jun 2015 19:40:18 +0300 Subject: [PATCH] v0.0.5 --- gruntfile.js | 59 ++++++++++++++++++++++++++++++++++------------- package.json | 2 +- source/init.js | 15 ++++++++++-- source/layout.hbs | 5 +--- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/gruntfile.js b/gruntfile.js index 8b66a8b..cb6d3b2 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -4,7 +4,6 @@ var assert = require('assert'); var handlebars = require('handlebars'); var copy = require('./copy.js'); var args = process.argv; -var marked = require('marked'); //grab second arg options var template = args[3]; @@ -74,6 +73,10 @@ module.exports = function(grunt) { } }); + grunt.registerTask('Clean', 'Cleaning Task', function(){ + // init.clean(); + }); + grunt.registerTask('Compile', 'Compile Handlebars', function() { //go through each page.hbs, and compile var baseLocation = get_file("pages"); @@ -83,6 +86,7 @@ module.exports = function(grunt) { } (function LoopFiles (location){ + var rel = path.relative(location, baseLocation); var assets = rel.replace('\\', '/'); if (!assets){ @@ -91,8 +95,10 @@ module.exports = function(grunt) { global_handlebars_data.assets = assets; + var currentDest = location.split(baseLocation)[1] || '/'; currentDest += '/'; + var hbs = fs.readdirSync(location); var layoutContent = fs.readFileSync(layout); @@ -107,7 +113,20 @@ module.exports = function(grunt) { } return init.data; })(); - + + /* pages starting with ipage- are special pages created automatically + so remove and unlink if they are found in directory as they will be + created again */ + for (var i = 0; i < hbs.length; i++){ + var hb = hbs[i]; + var re = /^ipage-/; + if (re.test(hb)){ + hbs.splice(i, 1); + --i; + fs.unlinkSync(get_file("pages" + currentDest + hb)); + } + } + for (var i = 0; i < hbs.length; i++){ var hb = hbs[i]; var page = get_file("pages" + currentDest + hb); @@ -120,8 +139,6 @@ module.exports = function(grunt) { LoopFiles(page); continue; } - - var template = handlebars.compile(layoutContent.toString("utf8")); handlebars.registerHelper("config", function(context, options){ var text = context.fn(); @@ -133,35 +150,45 @@ module.exports = function(grunt) { } return ""; }); - - var _marked = []; - handlebars.registerHelper("md", function(context){ - var md = context.fn(); - _marked = marked(md); - return _marked; - }); + + //export back some information about current + //page, so you can use it with handlebars helpers + init.current = { + location : location, + page : page, + layout : layout, + hbs : hbs, + data : page_data + }; var pagecontent = fs.readFileSync(page).toString("utf8"); var p = handlebars.compile(pagecontent); + data.assets = assets; var page_out = p(data); handlebars.registerPartial("content", page_out); - + //split var f = hb.split('.'); if (f[1] !== 'hbs'){ continue; } var filename = f[0] + '.html'; - var out = template(page_data || data); + + var all = page_data || data || {}; + all.assets = assets; + + var template = handlebars.compile(layoutContent.toString("utf8")); + var out = template(all); //FIXME: we compile 2 times just to parse //{{assets}} within global options //there must be a better way to do this template = handlebars.compile(out); - out = template({ assets : assets, _marked : _marked }); - + out = template({ assets : assets }); + fs.writeFileSync(get_file("dist" + currentDest + filename), out); + page_data = null; } })(baseLocation); @@ -206,5 +233,5 @@ module.exports = function(grunt) { //register grunt.registerTask('default', ['watch']); grunt.registerTask('watcher', ['watch']); - grunt.registerTask('compile', ['less','Compile', 'Copy']); + grunt.registerTask('compile', ['less','Compile', 'Copy', 'Clean']); }; diff --git a/package.json b/package.json index e11dc8b..7fcb758 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "temply", - "version": "0.0.4", + "version": "0.0.5", "description": "Temply the stratic html creator", "main": "gruntfile.js", "dependencies": { diff --git a/source/init.js b/source/init.js index 9e7f176..76873f4 100644 --- a/source/init.js +++ b/source/init.js @@ -2,8 +2,19 @@ exports.outLocation = ''; //handlebars object -exports.handlebars = function(Handlebars) { - +exports.handlebars = function(Handlebars){ + + //this gets some information about + //current compiled page + //current.location + //current.layout + //current.hbs + //current.page + //current.data + var current = exports.current; + + //Handlebars.registerHelper('helper', helper); + // Handlebars.registerPartial('partial', partial); }; exports.data = { diff --git a/source/layout.hbs b/source/layout.hbs index 20bfe35..a8b97b3 100644 --- a/source/layout.hbs +++ b/source/layout.hbs @@ -1,8 +1,5 @@ - - - - + {{title}}