diff options
| author | Jules Laplace <jules@okfoc.us> | 2012-07-18 00:02:21 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2012-07-18 00:02:21 -0400 |
| commit | c4338d2ae878a167c409e91dea6d1783fc7e30ba (patch) | |
| tree | 1e54fac722ac3153f9180a5a8332f2b19e11c00c /static/js/src/text.js | |
| parent | d891a7ae1b205716c086363fba17a3249a665deb (diff) | |
put away back
Diffstat (limited to 'static/js/src/text.js')
| -rw-r--r-- | static/js/src/text.js | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/static/js/src/text.js b/static/js/src/text.js new file mode 100644 index 0000000..9420ba0 --- /dev/null +++ b/static/js/src/text.js @@ -0,0 +1,166 @@ + +// this doesn't properly deal with eg, .gov.uk .co.ck etc +function parseDomain(host){ + var chunks = host.split(".") + if (chunks.length == 1) return chunks[0] + else return chunks[chunks.length - 2] +} + +function escapeHtml(txt) { + if (!txt) { return ""; } + // txt = annoyingCaps(txt) + return $("<span>").text(txt).html() +} + + +URLRegex = /((\b(http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi; +PicRegex = /\.(jpg|jpeg|png|gif|bmp|svg|fid)$/i; +RecipRegex = /(^|\s)@\w+/g; +TopicRegex = /(^|\s)#\w+/g; + + +function getImagesAsArray(text) { + var imgs = [] + var urls = text.match(URLRegex) + if (urls === null) return imgs + for (var i = 0; i<urls.length; i++){ + var url = urls[i] + var urlWithoutParams = url.replace(/\?.*$/i, ""); + if (PicRegex.test(urlWithoutParams)) + imgs.push(url) + } + return imgs +} + +function topicReplace(text) { + text = $.trim(text).toLowerCase(); + var topicLabel = text.substring(1); + return ' <a target="_blank" href="'+ RootDomain + 't/' + topicLabel + '">' + text + '</a> '; +} + +function recipientReplace(atText, recips) { + recips = recips || []; + + var space = ''; + if (atText[0] == ' ') { + atText = atText.slice(1); + space = ' '; + } + + var nick = atText.slice(1).toLowerCase(); + var matchedRecip; + for (var i = 0; i < recips.length; i++) { + if (recips[i].toLowerCase() == nick) { + matchedRecip = recips[i]; + break; + } + } + + if (matchedRecip) { + return space + '<a target="_blank" href="' + RootDomain + matchedRecip + '">@' + matchedRecip + '</a>'; + } else { + return space + atText; + } +} + +function linkify(text, recips) { + LastMsgContainsImage = false; + var recipWrapper = function(text) { return recipientReplace(text, recips); }; + return text + .replace(URLRegex, linkReplace) + .replace(RecipRegex, recipWrapper) + .replace(TopicRegex, topicReplace); +} + +// use this in escapeHtml to turn everyone's text lIkE tHiS +function annoyingCaps(text){ + var chunks = text.split(" ") + for(var i=0; i<chunks.length; i++){ + var chunk = chunks[i] + if (!chunk.length || chunk.substr(0,4) == 'http') continue; + var letters=chunk.split("") + for(var j = 0; j<letters.length; j++){ + if (j % 2) letters[j] = letters[j].toUpperCase() + else letters[j] = letters[j].toLowerCase() + } + chunks[i] = letters.join("") + } + return chunks.join(" ") +} + + +function imgClickHandler() { + // Ugly hack. Don't open new links in chat, only in logs. + // Ugly hack mkII: ensure middle-click opens images in new tab + // c.f. http://code.google.com/p/chromium/issues/detail?id=255 + if ($.browser.webkit && event.button != 0) { + event.stopPropagation(); + } else { + return $('#chatrap').length == 0; + } +} + +// durty hack to use a global to check this... but otherwise i'd have to rewrite the String.replace function? :/ +var LastMsgContainsImage = false +function linkReplace(url) { + var lowerurl = url.toLowerCase(); + if (lowerurl.indexOf('http://') == 0 || lowerurl.indexOf('https://') == 0 || lowerurl.indexOf('ftp://') == 0) + linkUrl = url; + else + linkUrl = 'http://' + url; + + var uri = parseUri(url) + var type = getUriType(uri) + + if (type == 'image') { + LastMsgContainsImage = true; + return "<a target='_blank' href='" + linkUrl + "' class='img-wrapper' onclick='return imgClickHandler()'><img src='" + linkUrl + "' class='unbound'></a>"; + } else if (type == 'youtube') { + Youtube.startAnimation(); + return "<a target='_blank' class='youtube' href='" + linkUrl + "'>" + + "<img class='youtube-thumb' width='130' height='97' src='"+Youtube.nextThumbUrl(uri.queryKey.v)+"'>" + + "<img class='youtube-controls' src='/static/img/youtube.controls.png'></a>" + } else if (type == 'midi' || type == 'wav') { + return '<embed src="'+linkUrl+'" loop="false" autostart="false" volume="80" width="150" height="20" style="vertical-align:bottom"> <a href="'+linkUrl+'">'+uri.file+'</a>' + } else + return "<a target='_blank' href='" + linkUrl + "'>" + url + "</a>"; + +} + + +function getUriType(uri){ + if (PicRegex.test(uri.file.toLowerCase())) + return "image"; + + var domain = parseDomain(uri.host) + + if (domain == "gstatic" && uri.path == "/images" && 'q' in uri.queryKey) + return "image"; + + // actual image url = uri.queryKey['q'].split(":").slice(2).join(":") but often the original image is broken... + + + if (domain == "youtube" && ('v' in uri.queryKey || uri.anchor.indexOf('v') != -1)) + return "youtube"; + + if (uri.path.substr(-4) == ".mid" || uri.path.substr(-5) == ".midi") + return "midi" + + if (uri.path.substr(-4) == ".wav") + return "wav" + + + return "link"; +} + +function linkifyWithoutImage(text) { + LastMsgContainsImage = false + return text.replace(URLRegex, linkReplaceWithoutImage); +} + +function linkReplaceWithoutImage(url){ + var urlWithoutParams = url.replace(/\?.*$/i, ""); + linkUrl = url.indexOf('http://') == 0 ? url : 'http://' + url; + + return "<a target='_blank' href='" + linkUrl + "'>" + url + "</a>" +} |
