Comparing version 0.1.2 to 0.1.3
145
lib/feed.js
var xml = require('xml'); | ||
var moment = require('moment'); | ||
var generatorName = 'Feed for Node.js'; | ||
// ============================================================================ | ||
// Feed Main Module | ||
// ============================================================================ | ||
function Feed(options, items) { | ||
function Feed(options) { | ||
this.title = options.title; | ||
this.description = options.description; | ||
this.link = options.link; | ||
//this.author = options.author; | ||
//this.copyright = options.copyright; | ||
//this.language = options.language; // fr-CA, en-US... | ||
this.items = items || []; | ||
this.image = options.image; | ||
this.copyright = options.copyright; | ||
this.author = options.author; | ||
this.items = []; | ||
this.categories = []; | ||
this.item = function (options) { | ||
@@ -29,10 +33,19 @@ options = options || {}; | ||
this.category = function (category) { | ||
this.categories.push(category); | ||
return this; | ||
} | ||
this.render = function (format) { | ||
switch(format) { | ||
case 'rss-2.0': | ||
return rss20(this); | ||
return rss_2_0(this); | ||
break; | ||
case 'atom-1.0': | ||
return atom_1_0(this); | ||
break; | ||
default: | ||
return rss20(this); | ||
return rss_2_0(this); | ||
} | ||
@@ -43,14 +56,72 @@ }; | ||
// ============================================================================ | ||
// Atom 1.0 [application/atom+xml] | ||
// ============================================================================ | ||
function atom_1_0(options) { | ||
var items = [], | ||
container = []; | ||
var entries = options.items; | ||
var categories = options.categories; | ||
var author = options.author; | ||
var doctype = '<?xml version="1.0" encoding="utf-8"?>\n'; | ||
var d = new Date(); | ||
container.push({ feed: [] }); | ||
container[0].feed.push({ _attr: { xmlns: 'http://www.w3.org/2005/Atom' }}); | ||
container[0].feed.push({ title: options.title }); | ||
container[0].feed.push({ subtitle: options.description }); | ||
container[0].feed.push({ id: options.link }); | ||
container[0].feed.push({ updated: RFC3339(d) }); | ||
container[0].feed.push({ author: [ | ||
{ name: author.name }, | ||
{ email: author.email }, | ||
{ uri: author.link } | ||
]}); | ||
if(options.image) | ||
container[0].feed.push({ logo: options.image }); | ||
if(options.copyright) | ||
container[0].feed.push({ rights: options.copyright }); | ||
container[0].feed.push({ generator: generatorName }); | ||
// Categories | ||
for(var key in categories) { | ||
container[0].feed.push({ category: [{ _attr: { term: categories[key]}}] }); | ||
} | ||
for(key in entries) { | ||
container[0].feed.push({ entry: [ | ||
{ title: { _cdata: entries[key].title }}, | ||
{ id: entries[key].link }, | ||
{ updated: RFC3339(entries[key].date) }, | ||
{ summary: { _cdata: entries[key].description }} | ||
]}); | ||
} | ||
return doctype + xml(container, true); | ||
} | ||
// ============================================================================ | ||
// Real Simple Syndication 2.0 (RSS 2.0) | ||
// ============================================================================ | ||
function rss20(options) { | ||
var items = []; | ||
var channel = []; | ||
var rss = [{ | ||
rss: [ | ||
{ _attr: { version: '2.0' }}, | ||
{ channel: channel } | ||
] | ||
}]; | ||
function rss_2_0(options) { | ||
var items = [], | ||
channel = [], | ||
container = []; | ||
var items = options.items; | ||
var categories = options.categories; | ||
var author = options.author; | ||
var doctype = '<?xml version="1.0" encoding="utf-8"?>\n'; | ||
container.push({ rss: [] }); | ||
container[0].rss.push({ _attr: { version: '2.0' } }); | ||
container[0].rss.push({ channel: channel }); | ||
// Required RSS 2.0 channel elements | ||
@@ -60,17 +131,41 @@ channel.push({ title: options.title }); | ||
channel.push({ link: options.link }); | ||
channel.push({ author: author.email + ' (' + author.name + ')' }); | ||
// Required RSS 2.0 item elements | ||
for(item in options.items) { | ||
if(options.image) | ||
channel.push({ image: options.image }); | ||
if(options.copyright) | ||
channel.push({ copyright: options.copyright }); | ||
channel.push({ generator: generatorName }); | ||
// Categories | ||
for(var key in categories) { | ||
channel.push({ category: categories[key] }); | ||
} | ||
// Items | ||
for(var key in items) { | ||
channel.push({ item: [ | ||
{ title: { _cdata: options.items[item].title }}, | ||
{ link: options.items[item].link }, | ||
{ pubDate: moment(options.items[item].date).format("ddd, DD MMM YYYY HH:mm:ss ") + 'GMT' }, | ||
{ description: { _cdata: options.items[item].description }} | ||
{ title: { _cdata: items[key].title }}, | ||
{ link: items[key].link }, | ||
{ pubDate: items[key].date.toUTCString() }, | ||
{ description: { _cdata: items[key].description }} | ||
]}); | ||
} | ||
return '<?xml version="1.0"?>\n' + | ||
xml(rss, true); | ||
return doctype + xml(container, true); | ||
} | ||
function RFC3339(d) { | ||
function pad(n) { return n < 10 ? '0' + n : n } | ||
return d.getUTCFullYear()+'-' | ||
+ pad(d.getUTCMonth()+1)+'-' | ||
+ pad(d.getUTCDate())+'T' | ||
+ pad(d.getUTCHours())+':' | ||
+ pad(d.getUTCMinutes())+':' | ||
+ pad(d.getUTCSeconds())+'Z' | ||
} | ||
module.exports = Feed; |
{ | ||
"name": "feed", | ||
"version": "0.1.2", | ||
"description": "Feed is the simplest content syndication wrapper for Atom and RSS 2.0.", | ||
"version": "0.1.3", | ||
"description": "Feed is the simplest content syndication wrapper, letting you generate Atom 1.0 and RSS 2.0 feeds in no time!", | ||
"homepage": "https://github.com/jpmonette/feed", | ||
@@ -23,4 +23,3 @@ "author": "Jean-Philippe Monette", | ||
"dependencies": { | ||
"xml": ">= 0.0.5", | ||
"moment": ">= 1.5.0" | ||
"xml": ">= 0.0.5" | ||
}, | ||
@@ -27,0 +26,0 @@ "engines": { |
@@ -7,3 +7,3 @@ # Feed for Node.js | ||
$ npm install feed | ||
$ npm install feed | ||
@@ -14,25 +14,53 @@ ## Examples | ||
var Feed = require('feed'); | ||
```js | ||
var Feed = require('feed'); | ||
``` | ||
To insert the data you want to syndicate: | ||
var feed = new Feed({ | ||
title: 'My Blog Name', | ||
link: 'http://example.com/', | ||
description: 'This is my blog description' | ||
}); | ||
```js | ||
var feed = new Feed({ | ||
title: 'Feed Title', | ||
description: 'This is my personnal feed!', | ||
link: 'http://example.com/', | ||
image: 'http://example.com/image.png', | ||
copyright: 'All rights reserved 2013, John Doe', | ||
author: { | ||
name: 'John Doe', | ||
email: 'johndoe@example.com', | ||
link: 'https://example.com/johndoe' | ||
} | ||
}); | ||
``` | ||
To insert every posts you want to (as many as you want), use the function `item` as followed: | ||
for(post in posts) { | ||
feed.item({ | ||
title: post.title, | ||
link: post.url, | ||
description: post.description, | ||
date: posts.date | ||
}); | ||
} | ||
```js | ||
for(post in posts) { | ||
feed.item({ | ||
title: post.title, | ||
link: post.url, | ||
description: post.description, | ||
date: posts.date | ||
}); | ||
} | ||
``` | ||
To output a RSS 2.0 feed: | ||
feed.render('rss-2.0'); | ||
```js | ||
feed.render('rss-2.0'); | ||
``` | ||
To output an Atom 1.0 feed: | ||
```js | ||
feed.render('atom-2.0'); | ||
``` | ||
Yes, it's that simple :)! | ||
## Additional information | ||
This module is still a work in progress. If you have any suggestion, feel free to send me a message or a pull request :)! |
6468
1
131
64
- Removedmoment@>= 1.5.0
- Removedmoment@2.30.1(transitive)