comparison atom.js @ 0:f62b5c395a48

Initial commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 04 Jun 2011 05:02:47 +0200
parents
children 82905edac9d8
comparison
equal deleted inserted replaced
-1:000000000000 0:f62b5c395a48
1 'use strict';
2
3 parsers[ns.atom] = function(id, xml) {
4 var toDate = function(atom) {
5 if (!atom)
6 return new Date();
7
8 var last = atom.getChild('updated');
9 if (!last) {
10 last = atom.getChild('published');
11 if (!last)
12 return new Date();
13 }
14
15 // var d = new Date(last); // FIXME: don't work in obsolete browsers
16 var d = new Date();
17 d.set8601(last);
18
19 return d;
20 };
21
22 var toHTML = function(atom, date) {
23 var article = document.createElementNS(ns.xhtml, 'article');
24
25 article.setAttributeNS(ns.idq, 'id', id);
26 var d8601 = date.to8601();
27 article.setAttributeNS(ns.idq, 'date', d8601);
28
29 var aside = document.createElementNS(ns.xhtml, 'aside');
30 article.appendChild(aside);
31
32 var atomTitle = atom.getChild('title');
33 if (atomTitle) {
34 var title = document.createElementNS(ns.xhtml, 'h2');
35 title.appendChild(document.createTextNode(atomTitle));
36 article.appendChild(title);
37 }
38
39 var footer = document.createElementNS(ns.xhtml, 'footer');
40
41 var atomAuthor = atom.getChild('author', ns.atom);
42 if (atomAuthor) {
43 var atomName = atomAuthor.getChild('name');
44
45 var atomURI = atomAuthor.getChild('uri');
46 var cite = document.createElementNS(ns.xhtml, 'cite');
47 if (atomURI) {
48 var a = document.createElementNS(ns.xhtml, 'a');
49 a.href = atomURI;
50 var atomJID = new JID;
51 atomJID.uri = atomURI;
52
53 a.appendChild(document.createTextNode(atomName? atomName: atomJID.bare));
54 cite.appendChild(a);
55
56 var img = document.createElementNS(ns.xhtml, 'img');
57 img.src = 'http://linkmauve.fr/avatar/' + atomJID.bare;
58 aside.appendChild(img);
59 } else
60 cite.appendChild(document.createTextNode(atomName));
61
62 footer.appendChild(document.createTextNode('By '));
63 footer.appendChild(cite);
64
65 var atomEmail = atomAuthor.getChild('email');
66 if (atomEmail) {
67 footer.appendChild(document.createTextNode(' ('));
68 var a = document.createElementNS(ns.xhtml, 'a');
69 a.href = atomEmail;
70 a.appendChild(document.createTextNode('email'));
71 footer.appendChild(a);
72 footer.appendChild(document.createTextNode(')'));
73 }
74
75 article.appendChild(footer);
76 }
77
78 footer.innerHTML += ', <time datetime="' + d8601 + '">' + date.getRelative() + '</time>';
79
80 var atomSummary = atom.getChild('summary');
81 if (atomSummary) {
82 var p = document.createElementNS(ns.xhtml, 'p');
83 p.appendChild(document.createTextNode(atomSummary));
84 article.appendChild(p);
85 }
86
87 var atomLinks = atom.getChildren('link');
88 for (var i in atomLinks) {
89 var atomLink = atomLinks[i];
90
91 if (atomLink['@rel'] !== 'replies')
92 continue;
93
94 if (atomLink['@title'] !== 'comments')
95 continue;
96
97 var href = new JID;
98 href.uri = atomLink['@href'];
99
100 article.innerHTML += '<a href="?jid=' + href.bare + ';node=' + href.query.node + ';comments=' + params.jid + '/' + params.node + '">Comments !</a>';
101 }
102
103 return article;
104 };
105
106 this.xml = xml;
107 this.date = toDate(xml);
108 this.html = toHTML(xml, this.date);
109 }