Skip to content

Instantly share code, notes, and snippets.

@mikevalstar
Created April 17, 2012 17:58
Show Gist options
  • Save mikevalstar/2407843 to your computer and use it in GitHub Desktop.
Save mikevalstar/2407843 to your computer and use it in GitHub Desktop.
RSS 2.0 Jade Template
res.header('Content-Type', 'application/xml');
res.render('rss2.0.template.jade', {
dateFormat: GetRFC822Date
, channel: {
title: "Mike Valstar's Blog"
, link: "http://mikevalstar.com"
, description: "WEB DEVELOPMENT, PROGRAMMING, RANDOM THOUGHTS"
}
, items: [
{title: "Sample title", link: "http://mikevalstar.com/1", description: "description", pubDate: new Date(), guid: "http://mikevalstar.com/1"},
{title: "Sample title", link: "http://mikevalstar.com/2", description: "description", pubDate: new Date(), guid: "http://mikevalstar.com/2"},
{title: "Sample title", link: "http://mikevalstar.com/3", description: "description", pubDate: new Date(), guid: "http://mikevalstar.com/3"},
{title: "Sample title", link: "http://mikevalstar.com/4", description: "description", pubDate: new Date(), guid: "http://mikevalstar.com/4"},
]
, layout: false
});
function GetRFC822Date(oDate){
// allow for empty request
if(typeof(oDate) == "undefined" || !oDate) oDate = new Date();
//Pads numbers with a preceding 0 if the number is less than 10.
var LZ = function(val){ return (parseInt(val) < 10) ? "0" + val : val; }
/* accepts the client's time zone offset from GMT in minutes as a parameter. returns the timezone offset in the format [+|-}DDDD */
var getTZOString = function(timezoneOffset){
var hours = Math.floor(timezoneOffset/60);
var modMin = Math.abs(timezoneOffset%60);
var s = new String();
s += (hours > 0) ? "-" : "+";
var absHours = Math.abs(hours)
s += (absHours < 10) ? "0" + absHours :absHours;
s += ((modMin == 0) ? "00" : modMin);
return(s);
}
var aMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var aDays = new Array( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var dtm = new String();
dtm = aDays[oDate.getDay()] + ", ";
dtm += LZ(oDate.getDate()) + " ";
dtm += aMonths[oDate.getMonth()] + " ";
dtm += oDate.getFullYear() + " ";
dtm += LZ(oDate.getHours()) + ":";
dtm += LZ(oDate.getMinutes()) + ":";
dtm += LZ(oDate.getSeconds()) + " " ;
dtm += getTZOString(oDate.getTimezoneOffset());
return dtm;
}
//- Not supported:
//- Channel -> cloud
//- Category Domains
!!! xml
rss(version="2.0")
channel
//- Required channel elements
title=channel.title
link=channel.link
description=channel.description
//- Optional Cheannel Elements
- if(typeof(channel.ttl) != 'undefined')
ttl=channel.ttl
- if(typeof(channel.rating) != 'undefined')
rating=channel.rating
- if(typeof(channel.skipHours) != 'undefined')
skipHours=channel.skipHours
- if(typeof(channel.skipDays) != 'undefined')
skipDays=channel.skipDays
- if(typeof(channel.language) != 'undefined')
language=channel.language
- if(typeof(channel.copyright) != 'undefined')
copyright=channel.copyright
- if(typeof(channel.generator) != 'undefined')
generator=channel.generator
- if(typeof(channel.managingEditor) != 'undefined')
managingEditor=channel.managingEditor
- if(typeof(channel.webMaster) != 'undefined')
webMaster=channel.webMaster
//- Image object: {url, title, link, width, height}
- if(typeof(channel.image) != 'undefined')
image
url=channel.image.url
title=channel.image.title
link=channel.image.link
- if(typeof(channel.image.width) != 'undefined')
width=channel.image.width
- if(typeof(channel.image.height) != 'undefined')
height=channel.image.height
//- textInput object: {name, title, description, link}
- if(typeof(channel.textInput) != 'undefined')
textInput
name=channel.textInput.name
title=channel.textInput.title
description=channel.textInput.description
link=channel.textInput.link
//- Category(ies): string or array
- if(typeof(channel.category) == 'object' && channel.category.length){
- channel.category.forEach(function(cat){
category=cat
- });
- }else if(typeof(channel.category) != 'undefined'){
category=channel.category
- }
//- docs (required but using default)
- if(typeof(channel.docs) == 'undefined'){
docs http://blogs.law.harvard.edu/tech/rss
- }else{
docs=channel.docs
- }
//- pubDate / lastBuildDate - default to now
- if(typeof(channel.pubDate) == 'undefined'){
pubDate=dateFormat()
- }else{
pubDate=dateFormat(channel.pubDate)
- }
- if(typeof(channel.lastBuildDate) == 'undefined'){
lastBuildDate=dateFormat()
- }else{
lastBuildDate=dateFormat(channel.lastBuildDate)
- }
//- Items
- items.forEach(function(item){
item
//- Required items
title=item.title
link=item.link
description=item.description
pubDate=dateFormat(item.pubDate)
//- Optional Elements
- if(typeof(item.author) != 'undefined')
author=item.author
- if(typeof(item.comments) != 'undefined')
comments=item.comments
- if(typeof(item.enclosure) != 'undefined')
enclosure(url=item.enclosure, length=item.enclosure_length, type=item.enclosure_type)
- if(typeof(item.source) != 'undefined')
source(url=item.source_title)=item.source
//- Category(ies): string or array
- if(typeof(item.category) == 'object' && item.category.length){
- item.category.forEach(function(cat){
category=cat
- });
- }else if(typeof(item.category) != 'undefined'){
category=item.category
- }
//- guid with optional permalink reference
- if(typeof(item.isPermaLink) != 'undefined' && item.isPermaLink){
guid(isPermaLink="true")=item.guid
- } else {
guid=item.guid
- }
- });
@yuchi
Copy link

yuchi commented May 17, 2012

This is a very complete example! But could be done more easily:

instead of

- if(typeof(channel.textInput) != 'undefined')
  // something goes here

you can also do

if channel.textInput
  // something goes here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment