Mercurial > eldonilo > blog
diff server.js @ 14:03be0717d3f8
Use the same code for client- and server-side Atom parsing.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 03 Nov 2011 16:42:00 -0700 |
parents | 360186772aa3 |
children | 5149a856d9dd |
line wrap: on
line diff
--- a/server.js +++ b/server.js @@ -14,6 +14,7 @@ var JID = require('./jid'); var ns = require('./ns'); var forms = require('./forms'); require('./date'); +var Atom = require('./atom'); var received = {}; @@ -148,89 +149,6 @@ cl.on('stanza', function(stanza) { } }); -var parseAtom = function(atom, id, jid) { - var article = new Element('article', {'e:id': id}); - - var avatar = article.c('aside').c('img') - article.up(); - - try { - var title = atom.getChild('title', ns.atom).getText(); - if (title) - article.c('h2').t(title).up(); - } catch (e) { } - - var footer = article.c('footer'); - article.up(); - - var author = atom.getChild('author', ns.atom); - if (author) { - footer.t('By '); - var name = author.getChild('name', ns.atom).getText(); - - try{ - var uri = author.getChild('uri', ns.atom).getText(); - footer.c('cite').c('a', {href: uri}).t(name).up(); - avatar.attrs.src = config.avatarRoot + uri.substring(5); - avatar.attrs.alt = uri.substring(5); - } catch (e) { - footer.c('cite').t(name); - } - - try { - var email = author.getChild('email', ns.atom).getText(); - footer.t(' (').c('a', {href: 'mailto:' + email}).t('email').up().t(')'); - } catch (e) { } - footer.up(); - } - - var published = (function() { - try { - var elem = atom.getChild('published', ns.atom); - var iso8601 = elem.getText(); - var d = (new Date).set8601(iso8601); - var relative = d.getRelative(); - - return {iso8601: iso8601, relative: relative}; - } catch (e) { - var d = new Date; - return {iso8601: d.to8601(), relative: d.getRelative()}; - } - })(); - - if (author) - footer.t(', '); - footer.c('time', {datetime: published.iso8601}).t(published.relative).up(); - article.attrs['e:date'] = published.iso8601; - - try { - var summary = atom.getChild('summary', ns.atom).getText(); - if (summary) - article.c('p').t(summary).up(); - } catch (e) { } - - try { - var links = atom.getChildren('link'); - for (var i in links) { - var link = links[i]; - - if (link.attrs.rel !== 'replies') - continue; - - if (link.attrs.title !== 'comments') - continue; - - var href = new JID; - href.uri = link.attrs.href; - - article.c('a', {href: '?jid=' + href.bare + ';node=' + href.query.node + ';origjid=' + jid.bare + ';orignode=' + jid.resource + ';origitem=' + id}).t('Comments !'); - break; - } - } catch (e) { } - - return article; -}; - var generatePage = function(jid) { var r = received[jid.full]; var s = r.wait; @@ -251,9 +169,9 @@ var makePage = function(res, jid, form, var body = '</div>'; for (var id in data) { - var item = data[id]; - var article = parseAtom(item, id, jid); - body = article + body; + var payload = data[id]; + var article = new Atom({id: id, ns: payload.attrs['xmlns'], payload: payload}); // , jid ? + body = article.html + body; } body = '<div e:jid="' + jid.full + '">' + body;