Skip to content

Commit

Permalink
Refactored code to decouple rendering pipeline from template object
Browse files Browse the repository at this point in the history
  • Loading branch information
jalleyne committed Nov 21, 2013
1 parent 726042b commit 2a1edf0
Show file tree
Hide file tree
Showing 13 changed files with 463 additions and 354 deletions.
15 changes: 7 additions & 8 deletions README.md
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -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');
Expand Down
16 changes: 15 additions & 1 deletion lib/config.js
Expand Up @@ -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
}),
Expand Down
65 changes: 35 additions & 30 deletions lib/i18n.js
Expand Up @@ -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;
80 changes: 19 additions & 61 deletions lib/module.js
Expand Up @@ -6,8 +6,6 @@ var fs = require('fs')
, path = require('path')
, _ = require('underscore')
, format = require('util').format

, modulesDir = global.roq.options.appRoot + '/app/modules'
;


Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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 '';
};






/**
*
*/
Expand Down
23 changes: 1 addition & 22 deletions lib/pebble.js
Expand Up @@ -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'
;


Expand All @@ -34,8 +25,6 @@ function Pebble(uri, name){
if( uri){
this.uri = uri;
this.loadContent();
this.loadTemplate();
this.template.setData( this.rawContent);
}

return this;
Expand All @@ -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'
})
Expand All @@ -68,11 +52,6 @@ Pebble.prototype.save = function(){



Pebble.prototype.render = function(){
return this.template.render( this.rawContent);
};



module.exports = Pebble;

0 comments on commit 2a1edf0

Please sign in to comment.