Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ctypes now work

  • Loading branch information...
commit c9396b79bcce5808e2776b4534e8c9567f5c06a5 1 parent cc15984
@thecolorblue authored
Showing with 75 additions and 14 deletions.
  1. +23 −5 murray/ctypes/blogpost.js
  2. +51 −7 murray/index.js
  3. +1 −2  server.js
View
28 murray/ctypes/blogpost.js
@@ -1,8 +1,26 @@
-exports.Form = [
-{type:'text',name:'title'},
-{type:'text',name:'body'},
-{type:'submit',name:'blogpost'}];
-exports.View = '';
+exports.Form = ' \
+<form method="post" action="/create/post"><fieldset> \
+ <p> \
+ <label for="title">title:</label> \
+ <input type="text" name="title"/> \
+ </p> \
+ <p> \
+ <label for="title">body:</label> \
+ <textarea cols=20 rows=4 name="body">Enter the body of your post here.</textarea> \
+ </p> \
+ <fieldset> \
+ <p> \
+ <input type="submit" name="submit" value="blogpost"/> \
+ </p> \
+ </fieldset> \
+</form>';
+exports.View = '<div style="position:relative;" class="post"> \
+ <h3 class="title"><: title :></h3> \
+ <h4 class="author"><: author :></h4> \
+ <p class="body"><: body :></p> \
+ <p class="date"><: date :></p> \
+ <p class="pid"><: pid :></p> \
+</div>';
exports.Meta = {
title:'blogpost',
engineer:'Brad Davis'
View
58 murray/index.js
@@ -27,6 +27,7 @@ fs.readdir(pluginfolder,function(err,files){
}
}
});
+var htmlTemplate = '<!DOCTYPE html><html><head></head><body><: posts :><: sidbar :><: forms :></body></html>';
var contenttype = {};
var ctype = {};
@@ -42,18 +43,15 @@ fs.readdir(cfolder,function(err,files){
}
}
for (var n in contenttype){
- console.log(contenttype[n]);
if(contenttype[n].form != ''){
var cform = {};
cform.title = contenttype[n].Meta.title;
cform.form = contenttype[n].Form;
+ cform.view = contenttype[n].View;
ctype[cform.title] = cform;
- console.log(cform);
}
}
- console.log(ctype);
});
-
/*
* Get Posts
* pulls all of the posts in 'posts' collection
@@ -68,6 +66,14 @@ exports.getposts = function(req,res,options,callback){
}
filters = options;
}
+ if(filters.form != undefined){
+ console.log('looking for form');
+ var content = {};
+ content.forms = ctype[filters.form].form;
+ substitute(htmlTemplate, content,function(html){
+ res.end(html);
+ });
+ } else {
var config = {'limit':8,'sort':[['date', -1]]};
db.open(function(err, db){
db.collection('posts', function(err, collection){
@@ -79,9 +85,15 @@ exports.getposts = function(req,res,options,callback){
var logged = false;
}
if(callback != ''){
- console.log(posted);
- console.log(sidebar);
- res.render('index.jade', {posts: posted, logged: logged, sidebar:sidebar,ctype:ctype});
+ var parts = {};
+ forPosts(posted,function(array){
+ parts.posts = array.join('<hr/>');
+ console.log(parts);
+ substitute(htmlTemplate,parts,function(html){
+ res.writeHead(200,{'Content-Type':'text/html'});
+ res.end(html);
+ });
+ });
} else {
callback();
}
@@ -90,6 +102,7 @@ exports.getposts = function(req,res,options,callback){
});
});
});
+ }
};
/*
* Create Post
@@ -192,6 +205,37 @@ exports.plugins = function(folder,callback){
});
};
/*
+ * Substitute
+ * takes template (string), array of content, and callback
+ * returns template with content
+ */
+function substitute(string,array,callback){
+ var re = /<:\s(\w+)\s:>/g;
+ var searchString = string;
+ var result = searchString.match(re);
+ for (i=0;i<result.length;i++){
+ var reg = /\s(\w+)\s/;
+ var tarray = reg.exec(result[i]);
+ var templateName = tarray[1];
+ var replacement = array[templateName];
+ searchString = searchString.replace(result[i],replacement);
+ }
+ callback(searchString);
+};
+/* Simple For Each */
+function forPosts(array,callback){
+ var renderedPosts = [];
+ for (i=0;i<array.length;i++){
+ var type = array[i].submit;
+ var view = ctype[type].view;
+ substitute(view,array[i],function(rendered){
+ renderedPosts[i] = rendered;
+ });
+ }
+ callback(renderedPosts);
+};
+
+/*
* Create User
* Creates hash of password
* Creates entry in murray.users with username and hash
View
3  server.js
@@ -57,8 +57,7 @@ app.post('/create/user', function(req,res){
});
app.get('/login', function(req,res){
- console.log(murray);
- res.render('loginform.jade',{posts: '', logged: '',sidebar:sidebar});
+ res.render('loginform.jade',{posts: '', logged: '',sidebar:''});
});
app.post('/login', function(req,res){
murray.login(req,res);
Please sign in to comment.
Something went wrong with that request. Please try again.