Permalink
Browse files

Refactored code to decouple rendering pipeline from template object

  • Loading branch information...
jalleyne committed Nov 21, 2013
1 parent 726042b commit 2a1edf0e479ee2e1ed870da8bcf0f5f9858de3d0
Showing with 463 additions and 354 deletions.
  1. +7 −8 README.md
  2. +1 −1 index.js
  3. +15 −1 lib/config.js
  4. +35 −30 lib/i18n.js
  5. +19 −61 lib/module.js
  6. +1 −22 lib/pebble.js
  7. +190 −0 lib/renderer.js
  8. +118 −56 lib/roq.js
  9. +55 −28 lib/router.js
  10. +8 −135 lib/template.js
  11. 0 test/app.config.js
  12. +4 −3 test/i18n.js
  13. +10 −9 test/regression.js
View
@@ -7,7 +7,7 @@ roq
Web application development platform and CMS build on node.js, express and handlebars.
# Components:
## Components:
Roq: Development platform
-- Pebbles: Data files
@@ -17,23 +17,23 @@ Chisel: CMS
Cronica: Deployment and versioning
# INSTALL:
## Install:
npm install roq -g
# SCAFFOLD:
## Scaffold:
roq mynewroqapp.com
# TEST:
## Test:
npm test
or
make test
# TODO:
## ToDo:
* Client library dependencies
* Client library optimizations
@@ -53,13 +53,12 @@ or
* Integrate handlebars partials into modules
# BUILT ON
## Built On
* [Express 3.0](http://expressjs.com)
* [Handlebars 1.1.2](http://handlebarsjs.com/)
* [Handlebars 1.1.2](http://handlebarsjs.com/)
# License
## License
Roq Platform is released under the MIT license.
View
@@ -17,7 +17,7 @@
/**
* ======== Module dependencies ============ */
var c = require('./lib/common')
var common = require('./lib/common')
, pkg = require('./package.json')
, util = require('util')
, fs = require('fs');
View
@@ -2,12 +2,26 @@ var _ = require('underscore');
var baseConfig = {
cache : true,
port : 8709
port : 8709,
paths : {
'static' : '/app/static',
'i18n' : '/app/i18n',
'content' : '/app/content',
'modules' : '/app/modules',
'templates' : '/app/templates'
},
modules : {
safeStringRendering : true
}
};
module.exports = {
test : _.extend(baseConfig,{
cache : false
}),
development : _.extend(baseConfig,{
cache : false
}),
View
@@ -9,42 +9,47 @@ var fs = require('fs')
var locales = {}
, getLocales = function(){
return locales;
}
, loadPhrases = function( dir){
var files = fs.readdirSync(dir)
, phrases = {}
;
for( var i in files){
phrases[
path.basename( files[ i], '.json')
] = JSON.parse(
fs.readFileSync( dir + '/' + files[ i], {
encoding : 'utf-8'
})
);
}
return phrases;
}
function loadLocale( dir){
var files = fs.readdirSync(dir)
, phrases = {}
;
for( var i in files){
phrases[
path.basename( files[ i], '.json')
] = JSON.parse(
fs.readFileSync( dir + '/' + files[ i], {
encoding : 'utf-8'
})
);
, load = function( dir){
var dirs = fs.readdirSync( dir)
, locale
;
for( var i in dirs){
locale = dirs[ i];
locales[ locale] = new Polyglot({
phrases : loadPhrases( dir + '/' + dirs[ i]),
locale : locale
});
}
return locales;
}
return phrases;
}
;
var localeDir = global.roq.options.appRoot + '/app/i18n'
, locales = fs.readdirSync( localeDir)
, locale
;
for( var i in locales){
locale = locales[ i];
locales[ locale] = new Polyglot({
phrases : loadLocale( localeDir + '/' + locales[ i]),
locale : locale
});
}
module.exports = locales;
module.exports.load = load;
module.exports.loadPhrases = loadPhrases;
View
@@ -6,8 +6,6 @@ var fs = require('fs')
, path = require('path')
, _ = require('underscore')
, format = require('util').format
, modulesDir = global.roq.options.appRoot + '/app/modules'
;
@@ -26,21 +24,17 @@ var fs = require('fs')
/**
*
*/
function Module( type, name, renderer, helpers){
function Module( type, name, dir){
this.name = name;
this.type = type;
this.defaultHelpers = helpers;
this.moduleHelpers = {};
this.renderer = renderer;
var _modulesPath = format('%s/%s', modulesDir, type)
, _view = format('%s/%s.%s', _modulesPath, 'html', 'hbs')
, _model = format('%s/%s.%s', _modulesPath, type, 'peb')
, _controller = format('%s/%s.%s', _modulesPath, type, 'js')
;
var _modulePath = format('%s/%s', dir, type)
, _view = format('%s/%s.%s', _modulePath, 'html', 'hbs')
, _model = format('%s/%s.%s', _modulePath, type, 'peb')
, _controller = format('%s/%s.%s', _modulePath, type, 'js')
;
this.modulesPath = _modulesPath;
/**
* Load view
@@ -59,7 +53,17 @@ function Module( type, name, renderer, helpers){
*/
if( !this.loadController( _controller)
&& !this.view){
console.error( 'unable to load module `%s` with name `%s` from `%s`', type, name, _modulesPath);
console.error( 'unable to load module `%s` with name `%s` from `%s`', type, name, _modulePath);
}
/**
*
*/
if( this.controller){
if( this.controller.helpers){
}
}
return this;
@@ -68,22 +72,12 @@ function Module( type, name, renderer, helpers){
/**
*
*/
Module.prototype.setRenderer = function( renderer){
this.renderer = renderer;
};
/**
*
*/
Module.prototype.loadView = function( file){
if( fs.existsSync(file)){
return this.view = fs.readFileSync( file, {
encoding:'utf8'});
return this.view = fs.readFileSync( file, 'utf8');
}else {
return false;
}
@@ -96,8 +90,7 @@ Module.prototype.loadView = function( file){
*/
Module.prototype.loadModel = function( file){
if( fs.existsSync(file)){
return this.model = fs.readFileSync( file, {
encoding:'utf8'});
return this.model = fs.readFileSync( file, 'utf8');
}else {
return false;
}
@@ -118,41 +111,6 @@ Module.prototype.loadController = function( file){
/**
*
*/
Module.prototype.render = function(data){
if( this.view){
try {
this.locals = _.extend(
data.locals, {
__modules__ : data.modules
}
);
this.data = data;
this.modules = data.modules;
return this.renderer.compile( this.view)
( this.locals, {
helpers : this.defaultHelpers,
partials : {},
data : this.locals
});
}catch( err){
console.error( 'unable to compile module `%s` with name `%s`', this.type, this.name);
}
}
return '';
};
/**
*
*/
View
@@ -5,15 +5,6 @@
var fs = require('fs')
, path = require('path')
, i18n = require('./i18n')
, Template = require('./template')
;
var contentDir = global.roq.options.appRoot + '/app/content'
;
@@ -34,8 +25,6 @@ function Pebble(uri, name){
if( uri){
this.uri = uri;
this.loadContent();
this.loadTemplate();
this.template.setData( this.rawContent);
}
return this;
@@ -47,14 +36,9 @@ Pebble.prototype.parse = function(){
};
Pebble.prototype.loadTemplate = function(){
this.template = new Template( this.rawContent.template);
};
Pebble.prototype.loadContent = function(){
this.rawContent = JSON.parse(
this.data = JSON.parse(
fs.readFileSync( this.uri,{
encoding:'utf8'
})
@@ -68,11 +52,6 @@ Pebble.prototype.save = function(){
Pebble.prototype.render = function(){
return this.template.render( this.rawContent);
};
module.exports = Pebble;
Oops, something went wrong.

0 comments on commit 2a1edf0

Please sign in to comment.