view atom.js @ 1:82905edac9d8

Fix some things.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 12 Jul 2011 17:47:32 +0200
parents f62b5c395a48
children eb5b6d3ab09b
line wrap: on
line source

'use strict';

parsers[ns.atom] = function(id, xml) {
	var toDate = function(atom) {
		if (!atom)
			return new Date();

		var last = atom.getChild('updated');
		if (!last) {
			last = atom.getChild('published');
			if (!last)
				return new Date();
		}

		// var d = new Date(last); // FIXME: don't work in obsolete browsers
		var d = new Date();
		d.set8601(last);

		return d;
	};

	var toHTML = function(atom, date) {
		var article = document.createElementNS(ns.xhtml, 'article');

		article.setAttributeNS(ns.idq, 'id', id);
		var d8601 = date.to8601();
		article.setAttributeNS(ns.idq, 'date', d8601);

		var aside = document.createElementNS(ns.xhtml, 'aside');
		article.appendChild(aside);

		var atomTitle = atom.getChild('title');
		if (atomTitle) {
			var title = document.createElementNS(ns.xhtml, 'h2');
			title.appendChild(document.createTextNode(atomTitle));
			article.appendChild(title);
		}

		var footer = document.createElementNS(ns.xhtml, 'footer');

		var atomAuthor = atom.getChild('author', ns.atom);
		if (atomAuthor) {
			var atomName = atomAuthor.getChild('name');

			var atomURI = atomAuthor.getChild('uri');
			var cite = document.createElementNS(ns.xhtml, 'cite');
			if (atomURI) {
				var a = document.createElementNS(ns.xhtml, 'a');
				a.href = atomURI;
				var atomJID = new JID;
				atomJID.uri = atomURI;

				a.appendChild(document.createTextNode(atomName? atomName: atomJID.bare));
				cite.appendChild(a);

				var img = document.createElementNS(ns.xhtml, 'img');
				img.src = 'http://eldonilo.linkmauve.fr/avatar/' + atomJID.bare;
				aside.appendChild(img);
			} else
				cite.appendChild(document.createTextNode(atomName));

			footer.appendChild(document.createTextNode('By '));
			footer.appendChild(cite);

			var atomEmail = atomAuthor.getChild('email');
			if (atomEmail) {
				footer.appendChild(document.createTextNode(' ('));
				var a = document.createElementNS(ns.xhtml, 'a');
				a.href = atomEmail;
				a.appendChild(document.createTextNode('email'));
				footer.appendChild(a);
				footer.appendChild(document.createTextNode(')'));
			}

			article.appendChild(footer);
		}

		footer.innerHTML += ', <time datetime="' + d8601 + '">' + date.getRelative() + '</time>';

		var atomSummary = atom.getChild('summary');
		if (atomSummary) {
			var p = document.createElementNS(ns.xhtml, 'p');
			p.appendChild(document.createTextNode(atomSummary));
			article.appendChild(p);
		}

		var atomLinks = atom.getChildren('link');
		for (var i in atomLinks) {
			var atomLink = atomLinks[i];

			if (atomLink['@rel'] !== 'replies')
				continue;

			if (atomLink['@title'] !== 'comments')
				continue;

			var href = new JID;
			href.uri = atomLink['@href'];

			article.innerHTML += '<a href="?jid=' + href.bare + ';node=' + href.query.node + ';comments=' + params.jid + '/' + params.node + '">Comments !</a>';
		}

		return article;
	};

	this.xml = xml;
	this.date = toDate(xml);
	this.html = toHTML(xml, this.date);
}