FABE: Adding new entries

For this step we are going to need an html editor so we can write articles prettily formatted. There are plenty of editors already in the wild, but some are such a pain to install and use that I decided to go with Yahoo Rich Text Editor, in the name of simplicity.

Nothing to download, just point to their servers and they do everything for us. What's not to love?

Ok, back to architecture astronauting for a moment, we are going to need three scripts, in order to write, preview and publish, named accordingly newpost.jss, preview.jss and publish.jss

The first script, newpost.jss, does nothing out of this world, just shows the template defined in newpost.html, which is the one that calls the rich text editor from Yahoo.

/* newpost.jss */

var view,data={};
view = new Template().process('newpost',data);
response.write(view);

Now, preview.jss gets the data from the form we just edited and presents it in another template for a final decision whether to publish it or not.

/* preview.jss */

var view,data={};
data.title   = request.post.xtitle;
data.content = request.post.xcontent;
view = new Template().process('preview',data);
response.write(view);

Once we are happy with our article and its formatting, we can publish it with the following script, which writes the necessary data to our database and runs a feeder program which generates our feed.xml for those avid readers who consume our rants in their feed readers of choice.

/* publish.jss */

var model,feed,post;
model = require('./models.jss');
feed  = require('./feeder.jss');

post={
  title   : request.post.xtitle,
  date    : new Date().format('Y-m-d H:i:s'),
  url     : cleanUrl(request.post.xtitle),
  content : cleanContent(request.post.xcontent),
  summary : cleanSummary(request.post.xcontent)
}

model.newPost(post);  // save post
feed.updateFeed();    // update feed

response.status(303); 
response.header({'Location':'.'}); // redirect to main page

// Utils
function cleanUrl(url){     return url.replace(/[^a-z^A-Z^0-9]/gi,'').toLowerCase(); }
function cleanContent(txt){ return HTML.unescape(txt).replace(/'/g,"\'"); }
function cleanSummary(txt){ return HTML.unescape(txt).replace(/'/g,"\'").replace(/<[a-zA-Z\/][^>]*>/gi,'').substr(0,200)+'...'; }

You can grab the templates for posting and previewing from here:

  • newpost.html
  • preview.html
  • I know for sure one thing got you thinking, yep, the feed generator, and all I can tell you is its sheer simplicity will leave you speachless.

    But more on the feeder, next...