Skip to content

Commit

Permalink
merging from master
Browse files Browse the repository at this point in the history
  • Loading branch information
Kellen Presley committed Dec 8, 2009
2 parents c5d15ae + 7647812 commit 6843876
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 46 deletions.
29 changes: 13 additions & 16 deletions picard/lib/picard/haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ Haml.parse = function (text) {
}
mode = 'ELEMENT';

classes = selector.match(/\.[^\.#]+/g),
var classes = selector.match(/\.[^\.#]+/g),
ids = selector.match(/#[^\.#]+/g),
tag = selector.match(/^%([^\.#]+)/g);
tag = selector.match(/^%([^\.#]+)/g),
plugin = selector.match(/^:([^\.#]+)/g);
tag = tag ? tag[0].substr(1) : (plugin ? null : 'div');
plugin = plugin ? plugin[0].substr(1) : null;
Expand Down Expand Up @@ -272,10 +272,10 @@ Haml.parse = function (text) {

function process_plugins() {
var contents, i;
switch (element[0]) {
case ':if':
var condition = element[1].condition;
contents = element[2];
switch (element[0].plugin) {
case 'if':
var condition = element[0].condition
contents = element[1]
for (i in element) {
if (element.hasOwnProperty(i)) {
delete element[i];
Expand All @@ -291,17 +291,14 @@ Haml.parse = function (text) {
element.length = new_element.length;
}
break;
case ':foreach':
case 'foreach':
var array, key, value, key_name, value_name;
array = element[1].array;
key_name = element[1].key;
value_name = element[1].value;
contents = element[2];
for (i in element) {
if (element.hasOwnProperty(i)) {
delete element[i];
}
}

array = element[0].array
for (var i in array[0]) { key_name = i }
value_name = element[0].value
contents = element[1]

element.length = 0;
for (key in array) {
if (array.hasOwnProperty(key)) {
Expand Down
6 changes: 3 additions & 3 deletions picard/lib/picard/request_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var request_extensions = {

extract_form_params: function(chunk){
if( chunk == undefined ) { return }
var chunks = chunk.split('&')
var chunks = decodeURIComponent(chunk).split('&')
for(var i in chunks){
var k_v = chunks[i].split('=')
this[k_v[0]] = k_v[1]
Expand Down Expand Up @@ -36,9 +36,9 @@ var request_extensions = {
this.on_screen(scope)
},

serve_static: function(){
serve_static: function(file){
var request = this
var filename = picard.env.root + picard.env.public + this.uri.path
var filename = file || picard.env.root + picard.env.public + this.uri.path

// non-blocking static file access
posix.cat(filename, 'binary').addCallback(function(content){
Expand Down
74 changes: 50 additions & 24 deletions sample_app/app.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
require('./config/env')

var layout = "application"

get('/thing/:foo/:bar', function(params){
return { text: params.foo + params.bar }
})

get('/layout', function(){
return { template: 'partial_test', layout: layout }
})

get('/partial', function(){
return { template: 'partial_test' }
})
// var layout = "application"
//
// get('/thing/:foo/:bar', function(params){
// return { text: params.foo + params.bar }
// })
//
// get('/layout', function(){
// return { template: 'partial_test', layout: layout }
// })
//
// get('/partial', function(){
// return { template: 'partial_test' }
// })
//
// get('/', function(){
// return { text: 'Hello Universe' }
//
// get('/power_haml', function(p){
//
// return {
// template: 'kellen.haml',
// todolist: [ { description: 'foo' }, { description: 'bar' } ]
// }
//
// })

get('/', function(){
return { text: 'Hello Universe' }
})

get('/foo/:bar', function(params){
return { text: params.bar }
})

get('/haml', function(){
var scope = {
template: 'index',
return {
//template: 'index.haml',
template: 'index',
print_date: function () {
return (new Date()).toDateString();
},
current_user: {
name: "Jean-Luc Picard",
bio: "Captain of the USS Enterprise"
}
};
return scope
}
})

var commands = [
{ command: 'Make it so' },
{ command: 'You have the bridge, Number One' }
]

get('/json', function(){
return {
type: 'application/json',
body: JSON.stringify(
[ { command_1: 'Make it so' },
{ command_2: 'You have the bridge, Number One' } ] )
body: JSON.stringify(commands)
}
})

get('/advanced_haml', function(){
return {
template: 'advanced.haml',
commands: commands // defined above
}
})

Expand Down Expand Up @@ -103,6 +121,14 @@ get('/cookie', function(request){
return { text: '<h1>' + request.cookie('hobby').value + '</h1>' } // will render 'literature'
})

get('/foo/:bar', function(params){
return { text: params.bar }
})

get('/multiple/:thing/:stuff', function(params){
return { text: params.thing + " " + params.stuff }
})

// Below we make a GET request to /haml (to simulate an http service call).
// Rather than block other processing while waiting for a response,
// we attach event listeners to the request without returning a value from our callback.
Expand Down
6 changes: 3 additions & 3 deletions sample_app/spec/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

it 'should do gets' do
res = Curl::Easy.perform(base_url + '/')
res.body_str.should == "Hello Universe"
res.body_str.should eql("Hello Universe")
res.header_str.should include('200')
end

it 'should do gets with normal params' do
res = Curl::Easy.perform(base_url + '/foo/bar')
res.body_str.should == "bar"
res.body_str.should eql("bar")
res.header_str.should include('200')
end

Expand All @@ -25,7 +25,7 @@

it 'should render json' do
res = Curl::Easy.perform(base_url + '/json')
res.body_str.should include("[{\"command_1\":\"Make it so\"},{\"command_2\":\"You have the bridge, Number One\"}]")
res.body_str.should include("[{\"command\":\"Make it so\"},{\"command\":\"You have the bridge, Number One\"}]")
res.header_str.should include('application/json')
res.header_str.should include('200')
end
Expand Down
21 changes: 21 additions & 0 deletions sample_app/spec/picard_specific_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
res.body_str.should include('404')
res.header_str.should include('404')
end

it 'should allow multple URL params' do
res = Curl::Easy.perform(base_url + '/multiple/bar/baz')
res.body_str.should eql("bar baz")
end

describe 'cookies' do
before do
Expand All @@ -30,4 +35,20 @@
end
end

describe 'advanced haml' do
before do
@res = Curl::Easy.perform(base_url + '/advanced_haml')
end

it 'should allow for the "if" plugin' do
@res.body_str.should include("This will show up!")
@res.body_str.should_not include("This will not show up!")
end

it 'should allow for the "foreach" plugin' do
@res.body_str.should include("<li>Make it so</li>")
@res.body_str.should include("<li>You have the bridge, Number One</li>")
end
end

end
11 changes: 11 additions & 0 deletions sample_app/views/advanced.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#page Advanced Haml Usage

:if{ condition: true }
%p This will show up!

:if{ condition: 0 > 1 }
%p This will not show up!

%ul.todolist
:foreach{ array: commands, value: "item" }
%li= item.command

0 comments on commit 6843876

Please sign in to comment.