From c4338d2ae878a167c409e91dea6d1783fc7e30ba Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 18 Jul 2012 00:02:21 -0400 Subject: put away back --- static/js/src/text.js | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 static/js/src/text.js (limited to 'static/js/src/text.js') 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 $("").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' + text + ' '; +} + +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 + '@' + matchedRecip + ''; + } 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"; + } else if (type == 'youtube') { + Youtube.startAnimation(); + return "" + + "" + + "" + } else if (type == 'midi' || type == 'wav') { + return ' '+uri.file+'' + } else + return "" + url + ""; + +} + + +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 "" + url + "" +} -- cgit v1.2.3-70-g09d2