view atom.js @ 7:7ab6b48122af

Add support for content in Atom and improve published display.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 02 Nov 2011 09:35:46 -0700
parents 4d7a67349089
children 461a24a5a788
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.e, 'id', id);
		var d8601 = date.to8601();
		article.setAttributeNS(ns.e, '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 = 'mailto:' + 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 atomContent = atom.getChild('content');
		if (atomContent) {
			var contentType = atomContent.getAttribute('type');
			if (/^text$/.test(contentType)) {
				var p = document.createElementNS(ns.xhtml, 'p');
				p.appendChild(document.createTextNode(atomContent.getText()));
				article.appendChild(p);
			} else if (/^html$/.test(contentType)) {
				article.insertAdjacentHTML('beforeend', atomContent.getText()); // FIXME: could be not-well-formed.
			} else if (/^xhtml$/.test(contentType)) {
				// TODO: use a better xml2json lib that allow to json2xml
				/*var div = atomContent.getChild();
				article.appendChild(div.innerXml());*/
			} else {
				var contentSrc = atomContent.getAttribute('src');
				if (contentSrc) {
					if (/^image\//.test(contentType)) {
						var img = document.createElementNS(ns.xhtml, 'img');
						img.setAttributeNS(null, 'src', contentSrc);
						article.appendChild(img);
					} else if (/^audio\//.test(contentType)) {
						var audio = document.createElementNS(ns.xhtml, 'audio');
						audio.setAttributeNS(null, 'src', contentSrc);
						audio.setAttributeNS(null, 'controls', '');
						article.appendChild(audio);
					} else if (/^video\//.test(contentType)) {
						var video = document.createElementNS(ns.xhtml, 'video');
						video.setAttributeNS(null, 'src', contentSrc);
						video.setAttributeNS(null, 'controls', '');
						article.appendChild(video);
					} else {
						var a = document.createElementNS(ns.xhtml, 'a');
						a.setAttributeNS(null, 'href', contentSrc);
						article.appendChild(a);
					}
				}
			}
		}

		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);
}