comparison 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
comparison
equal deleted inserted replaced
13:161d4ea1c3f8 14:03be0717d3f8
12 var Element = xmpp.Element; 12 var Element = xmpp.Element;
13 var JID = require('./jid'); 13 var JID = require('./jid');
14 var ns = require('./ns'); 14 var ns = require('./ns');
15 var forms = require('./forms'); 15 var forms = require('./forms');
16 require('./date'); 16 require('./date');
17 var Atom = require('./atom');
17 18
18 var received = {}; 19 var received = {};
19 20
20 var cl = new xmpp.Client(config); 21 var cl = new xmpp.Client(config);
21 22
146 if (payload) 147 if (payload)
147 return handlePubSub(payload, stanza.attrs.from); 148 return handlePubSub(payload, stanza.attrs.from);
148 } 149 }
149 }); 150 });
150 151
151 var parseAtom = function(atom, id, jid) {
152 var article = new Element('article', {'e:id': id});
153
154 var avatar = article.c('aside').c('img')
155 article.up();
156
157 try {
158 var title = atom.getChild('title', ns.atom).getText();
159 if (title)
160 article.c('h2').t(title).up();
161 } catch (e) { }
162
163 var footer = article.c('footer');
164 article.up();
165
166 var author = atom.getChild('author', ns.atom);
167 if (author) {
168 footer.t('By ');
169 var name = author.getChild('name', ns.atom).getText();
170
171 try{
172 var uri = author.getChild('uri', ns.atom).getText();
173 footer.c('cite').c('a', {href: uri}).t(name).up();
174 avatar.attrs.src = config.avatarRoot + uri.substring(5);
175 avatar.attrs.alt = uri.substring(5);
176 } catch (e) {
177 footer.c('cite').t(name);
178 }
179
180 try {
181 var email = author.getChild('email', ns.atom).getText();
182 footer.t(' (').c('a', {href: 'mailto:' + email}).t('email').up().t(')');
183 } catch (e) { }
184 footer.up();
185 }
186
187 var published = (function() {
188 try {
189 var elem = atom.getChild('published', ns.atom);
190 var iso8601 = elem.getText();
191 var d = (new Date).set8601(iso8601);
192 var relative = d.getRelative();
193
194 return {iso8601: iso8601, relative: relative};
195 } catch (e) {
196 var d = new Date;
197 return {iso8601: d.to8601(), relative: d.getRelative()};
198 }
199 })();
200
201 if (author)
202 footer.t(', ');
203 footer.c('time', {datetime: published.iso8601}).t(published.relative).up();
204 article.attrs['e:date'] = published.iso8601;
205
206 try {
207 var summary = atom.getChild('summary', ns.atom).getText();
208 if (summary)
209 article.c('p').t(summary).up();
210 } catch (e) { }
211
212 try {
213 var links = atom.getChildren('link');
214 for (var i in links) {
215 var link = links[i];
216
217 if (link.attrs.rel !== 'replies')
218 continue;
219
220 if (link.attrs.title !== 'comments')
221 continue;
222
223 var href = new JID;
224 href.uri = link.attrs.href;
225
226 article.c('a', {href: '?jid=' + href.bare + ';node=' + href.query.node + ';origjid=' + jid.bare + ';orignode=' + jid.resource + ';origitem=' + id}).t('Comments !');
227 break;
228 }
229 } catch (e) { }
230
231 return article;
232 };
233
234 var generatePage = function(jid) { 152 var generatePage = function(jid) {
235 var r = received[jid.full]; 153 var r = received[jid.full];
236 var s = r.wait; 154 var s = r.wait;
237 var form = r.form; 155 var form = r.form;
238 var data = r.data; 156 var data = r.data;
249 167
250 var makePage = function(res, jid, form, data, noscript) { 168 var makePage = function(res, jid, form, data, noscript) {
251 var body = '</div>'; 169 var body = '</div>';
252 170
253 for (var id in data) { 171 for (var id in data) {
254 var item = data[id]; 172 var payload = data[id];
255 var article = parseAtom(item, id, jid); 173 var article = new Atom({id: id, ns: payload.attrs['xmlns'], payload: payload}); // , jid ?
256 body = article + body; 174 body = article.html + body;
257 } 175 }
258 body = '<div e:jid="' + jid.full + '">' + body; 176 body = '<div e:jid="' + jid.full + '">' + body;
259 177
260 home(res, form['pubsub#title'], form['pubsub#description'], body, 'Node created the <time>' + form['pubsub#creation_date'] + '</time> by <cite>' + form['pubsub#creator'] + '</cite> with <a href="http://linkmauve.fr/dev/eldonilo/blog">Eldonilo blog</a>.', noscript); 178 home(res, form['pubsub#title'], form['pubsub#description'], body, 'Node created the <time>' + form['pubsub#creation_date'] + '</time> by <cite>' + form['pubsub#creator'] + '</cite> with <a href="http://linkmauve.fr/dev/eldonilo/blog">Eldonilo blog</a>.', noscript);
261 }; 179 };