it's working, even have working tests

This commit is contained in:
Dylan Greene
2011-04-14 01:41:13 -04:00
parent 416979ce72
commit f3b5a85fe4
3 changed files with 210 additions and 85 deletions

View File

@@ -5,102 +5,71 @@
var XML = require('xml'),
log = require('logging').from(__filename);
function RSS () {
function RSS (options, items) {
options = options || {};
var site = {
title: '',
url: '',
description: ''
};
this.title = options.title || 'Untitled RSS Feed';
this.description = options.description || '';
this.feed_url = options.feed_url;
this.site_url = options.site_url;
this.image_url = options.image_url;
this.author = options.author;
this.items = items || [];
var feed = {
url: ''
};
var author = {
};
var rss = {
title: '',
description: '',
link: '',
author: {
name: 'string',
uri: 'uri',
email: 'email'
},
image: {
url: 'url',
title: 'text',
link: 'url'
},
items: []
this.item = function (options) {
options = options || {};
var item = {
title: options.title || 'No title',
description: options.description || '',
url: options.url,
guid: options.guid,
categories: options.categories || [],
author: options.author,
date: options.date
};
Object.defineProperty(this, "items", {
get : function(){ return rss.items; },
set : function(new_items){ rss.items = new_items; },
enumerable : true
});
this.item = function (item) {
item = item || {
title: 'cdata',
link: 'url',
source: 'source',
description: 'cdata',
categories: ['category'],
guid: { permaLink: true, guid: 'guid'},
comments: 'comments',
author: 'author',
date: new Date()
};
rss.items.push(item);
this.items.push(item);
return this;
};
this.xml = function(indent) {
return '<?xml version="1.0" encoding="UTF-8"?>\n' + XML(generateXML(rss), indent);
return '<?xml version="1.0" encoding="UTF-8"?>\n'
+ XML(generateXML(this), indent);
}
}
function ifTruePush(bool, array, data) {
if (bool) {
array.push(data);
}
}
function generateXML (rss){
function generateXML (data){
// todo: xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
var channel = [
{ title: rss.title },
{ description: rss.description },
{ title: { _cdata: data.title } },
{ description: { _cdata: data.description || data.title } },
{ link: data.site_url || 'http://github.com/dylan/node-rss' },
{ generator: 'NodeJS RSS Module' },
{ 'atom:link': { _attr: { href: rss.url, rel: 'self', type: 'application/rss+xml' } } },
{ link: rss.link },
{ updated: new Date().toISOString() },
{ author: [
{ name: rss.author.name},
{ uri: rss.author.uri },
{ email: rss.author.email }
]}
];
{ lastBuildDate: new Date().toGMTString() }
];
ifTruePush(data.feed_url, channel, { 'atom:link': { _attr: { href: data.feed_url, rel: 'self', type: 'application/rss+xml' } } });
// { updated: new Date().toGMTString() }
rss.items.forEach(function(item) {
data.items.forEach(function(item) {
var item_values = [
{ title: { _cdata: item.title } }
];
ifTruePush(item.description, item_values, { description: { _cdata: item.description } });
ifTruePush(item.link, item_values, { link: item.url });
ifTruePush(item.link || item.guid || item.title, item_values, { guid: [ { _attr: { isPermaLink: !item.guid && !!item.url } }, item.guid || item.url || item.title ] });
//TODO: categories
channel.push({ item: [
{ title: { _cdata: item.title } },
{ link: item.link },
{ guid: [ { _attr: { isPermaLink: item.guid.permaLink } }, item.guid.guid ] },
{ comments: item.comments },
{ description: { _cdata: item.description } },
{ 'dc:creator': { _cdata: item.author } },
{ pubDate: item.date.toISOString() }
]});
ifTruePush(item.author || data.author, item_values, { 'dc:creator': { _cdata: item.author || data.author } });
ifTruePush(item.date, item_values, { pubDate: new Date(item.date).toGMTString() });
channel.push({ item: item_values });
});
return { rss: [