diff --git a/lib/rss.js b/lib/rss.js index a903c77..40f8516 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -15,6 +15,14 @@ function RSS (options, items) { this.site_url = options.site_url; this.image_url = options.image_url; this.author = options.author; + this.categories = options.categories; + this.pubDate = options.pubDate; + this.docs = options.docs; + this.copyright = options.copyright; + this.language = options.language; + this.managingEditor = options.managingEditor; + this.webMaster = options.webMaster; + this.ttl = options.ttl; this.items = items || []; this.item = function (options) { @@ -61,9 +69,22 @@ function generateXML (data){ channel.push({ generator: 'NodeJS RSS Module' }); channel.push({ 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() } + ifTruePush(data.feed_url, channel, { 'atom:link': { _attr: { href: data.feed_url, rel: 'self', type: 'application/rss+xml' } } }); + ifTruePush(data.author, channel, { 'author': { _cdata: data.author } }); + ifTruePush(data.pubDate, channel, { 'pubDate': new Date(data.pubDate).toGMTString() }); + ifTruePush(data.copyright, channel, { 'copyright': { _cdata: data.copyright } }); + ifTruePush(data.language, channel, { 'language': { _cdata: data.language } }); + ifTruePush(data.managingEditor, channel, { 'managingEditor': { _cdata: data.managingEditor } }); + ifTruePush(data.webMaster, channel, { 'webMaster': { _cdata: data.webMaster } }); + ifTruePush(data.docs, channel, { 'docs': data.docs }); + ifTruePush(data.ttl, channel, { 'ttl': data.ttl }); + if (data.categories) { + data.categories.forEach(function(category) { + ifTruePush(category, channel, { category: { _cdata: category } }); + }); + } + // { updated: new Date().toGMTString() } data.items.forEach(function(item) { var item_values = [ @@ -73,6 +94,10 @@ function generateXML (data){ ifTruePush(item.url, 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 ] }); + item.categories.forEach(function(category) { + ifTruePush(category, item_values, { category: { _cdata: category } }); + }); + 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() }); diff --git a/readme.md b/readme.md index 16860af..f7d61fe 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,15 @@ feed_url: 'http://example.com/rss.xml', site_url: 'http://example.com', image_url: 'http://example.com/icon.png', - author: 'Dylan Greene' + docs: 'http://example.com/rss/docs.html', + author: 'Dylan Greene', + managingEditor: 'Dylan Green', + webMaster: 'Dylan Green', + copyright: '2013 Dylan Green', + language: 'en', + categories: ['Category 1','Category 2','Category 3'], + pubDate: 'May 20, 2012 04:00:00 GMT', + ttl: '60' }); /* loop over data and add to feed */ @@ -33,6 +41,7 @@ description: 'use this for the content. It can include html.', url: 'http://example.com/article4?this&that', // link to the item guid: '1123', // optional - defaults to url + categories: ['Category 1','Category 2','Category 3','Category 4'], // optional - array of item categories author: 'Guest Author', // optional - defaults to feed author property date: 'May 27, 2012' // any format that js Date can parse. enclosure : {url:'...', file:'path-to-file'} // optional @@ -48,7 +57,15 @@ * _feed_url_ Url to the rss feed. * _site_url_ Url to the site that the feed is for. * _image_url_ Optional. Small image for feed readers to use. + * _docs_ Optional. Url to documentation on this feed. * _author_ Who owns this feed. + * _managingEditor_ Optional. Who manages content in this feed. + * _webMaster_ Optional. Who manages feed availability and technical support. + * _copyright_ Optional. Copyright information for this feed. + * _language_ Optional. The language of the content of this feed. + * _categories_ Optional. One or more categories this feed belongs to. + * _pubDate_ Optional. The publication date for content in the feed + * _ttl_ Optional. Number of minutes feed can be cached before refreshing from source. ### Item Options @@ -62,11 +79,12 @@ an have any number of items. Ten to tenty is usually good. * _guid_ A unique string feed readers use to know if an item is new or has already been seen. If you use a guid never change it. If you don't provide a guid then your item urls must be unique. + * _categories_ Optional. If provided, each array item will be added as a category element * _author_ Optional. If included it is the name of the item's creator. If not provided the item author will be the same as the feed author. This is typical except on multi-author blogs. - * _date_ The date and time of when the intem was created. Feed - readers use this to determin the sort order. Some readers will also use it to determin + * _date_ The date and time of when the item was created. Feed + readers use this to determine the sort order. Some readers will also use it to determine if the content should be presented as unread. ### Methods diff --git a/test/test.js b/test/test.js index b0a60ab..0c38b0d 100644 --- a/test/test.js +++ b/test/test.js @@ -22,7 +22,15 @@ module.exports = { feed_url: 'http://example.com/rss.xml', site_url: 'http://example.com', image_url: 'http://example.com/icon.png', - author: 'Dylan Greene' + author: 'Dylan Greene', + categories: ['Category 1','Category 2','Category 3'], + pubDate: 'May 20, 2012 04:00:00 GMT', + docs: 'http://example.com/rss/docs.html', + copyright: '2013 Dylan Green', + language: 'en', + managingEditor: 'Dylan Green', + webMaster: 'Dylan Green', + ttl: '60' }); feed.item({ @@ -50,9 +58,17 @@ module.exports = { url: 'http://example.com/article4?this&that', author: 'Guest Author', date: 'May 27, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 5 & test for categories', + description: 'description 5', + url: 'http://example.com/article5', + categories: ['Category 1','Category 2','Category 3','Category 4'], + author: 'Guest Author', + date: 'May 28, 2012 04:00:00 GMT' }); - var expectedResult = '\n<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT'; + var expectedResult = '\n<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comNodeJS RSS Module' + new Date().toUTCString() +'Sun, 20 May 2012 04:00:00 GMThttp://example.com/rss/docs.html60<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; var result = feed.xml(); test.equal(result.length, expectedResult.length); @@ -66,7 +82,15 @@ module.exports = { description: 'description', feed_url: 'http://example.com/rss.xml', site_url: 'http://example.com', - author: 'Dylan Greene' + author: 'Dylan Greene', + categories: ['Category 1','Category 2','Category 3'], + pubDate: 'May 20, 2012 04:00:00 GMT', + docs: 'http://example.com/rss/docs.html', + copyright: '2013 Dylan Green', + language: 'en', + managingEditor: 'Dylan Green', + webMaster: 'Dylan Green', + ttl: '60' }); feed.item({ @@ -94,9 +118,17 @@ module.exports = { url: 'http://example.com/article4?this&that', author: 'Guest Author', date: 'May 27, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 5 & test for categories', + description: 'description 5', + url: 'http://example.com/article5', + categories: ['Category 1','Category 2','Category 3','Category 4'], + author: 'Guest Author', + date: 'May 28, 2012 04:00:00 GMT' }); - var expectedResult = '\n<![CDATA[title]]>http://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT'; + var expectedResult = '\n<![CDATA[title]]>http://example.comNodeJS RSS Module' + new Date().toUTCString() +'Sun, 20 May 2012 04:00:00 GMThttp://example.com/rss/docs.html60<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; var result = feed.xml(); test.equal(result.length, expectedResult.length);