changeset 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 29becdd02dea
children 461a24a5a788
files atom.js blog.js server.js theme.css
diffstat 4 files changed, 69 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/atom.js
+++ b/atom.js
@@ -84,6 +84,45 @@ parsers[ns.atom] = function(id, xml) {
 			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];
--- a/blog.js
+++ b/blog.js
@@ -31,15 +31,15 @@ if (params.node)
 var received = {};
 var messages = document.getElementById('messages');
 
-var html = function(name, id) {
-	return received[name][id].html;
-}
+var updateMessage = function(name, id) {
+	var html = function(name, id) {
+		return received[name][id].html;
+	}
 
-var date = function(name, id) {
-	return received[name][id].date;
-}
+	var date = function(name, id) {
+		return received[name][id].date;
+	}
 
-var updateMessage = function(name, id) {
 	var divs = messages.getElementsByTagNameNS(ns.xhtml, 'div');
 	var container = null;
 
--- a/server.js
+++ b/server.js
@@ -149,7 +149,7 @@ cl.on('stanza', function(stanza) {
 });
 
 var parseAtom = function(atom, id, jid) {
-	var article = new Element('article', {'e:id': id, 'e:date': '2011-06-02T10:59:39Z'});
+	var article = new Element('article', {'e:id': id});
 
 	var avatar = article.c('aside').c('img')
 	article.up();
@@ -183,12 +183,24 @@ var parseAtom = function(atom, id, jid) 
 		footer.up();
 	}
 
-	var published = atom.getChild('published', ns.atom).getText();
-	if (published) {
-		if (author)
-			footer.t(', ');
-		footer.c('time', {datetime: published}).t((new Date).set8601(published).getRelative()).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();
--- a/theme.css
+++ b/theme.css
@@ -13,6 +13,10 @@ body {
 	display: none;
 }
 
+img, video {
+	max-width: 540px;
+}
+
 a {
 	color: #999;
 	text-decoration: none;