Permalink
Browse files

Add URL parsing for messages.

  • Loading branch information...
1 parent 1a9a688 commit a72859ea9917de8eae1b80aff98b8269c2ef5b45 @akavlie committed Nov 10, 2011
Showing with 38 additions and 10 deletions.
  1. +31 −8 app.js
  2. +1 −1 index.html
  3. +6 −1 js/util.js
View
@@ -12,10 +12,18 @@ $(function() {
defaults: {
// expected properties:
// - sender
- // - text
+ // - raw
'type': 'message'
},
+ initialize: function() {
+ this.set({text: this.parse( irc.util.escapeHTML(this.get('raw')) )});
+ },
+
+ parse: function(text) {
+ return this._linkify(text);
+ },
+
// Set output text for status messages
setText: function() {
var text = '';
@@ -31,6 +39,21 @@ $(function() {
break;
}
this.set({text: text});
+ },
+
+ // Find and link URLs
+ _linkify: function(text) {
+ // see http://daringfireball.net/2010/07/improved_regex_for_matching_urls
+ var re = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
+ var parsed = text.replace(re, function(url) {
+ // turn into a link
+ var href = url;
+ if (url.indexOf('http') !== 0) {
+ href = 'http://' + url;
+ }
+ return '<a href="' + href + '" target="_blank">' + url + '</a>';
+ });
+ return parsed;
}
});
@@ -346,7 +369,7 @@ $(function() {
revised = command;
break;
}
- return irc.utils.swapCommand(command, revised, text);
+ return irc.util.swapCommand(command, revised, text);
},
sendInput: function(e) {
@@ -362,15 +385,15 @@ $(function() {
var msgParts = parsed.split(' ');
if (msgParts[0].toLowerCase() === 'privmsg') {
pm = frames.getByName(msgParts[1]) || new Frame({type: 'pm', name: msg.nick});
- pm.stream.add({sender: msg.nick, text: msg.text})
+ pm.stream.add({sender: msg.nick, raw: msg.text})
frames.add(pm);
}
} else {
socket.emit('say', {
target: frame.get('name'),
message: input
});
- frame.stream.add({sender: irc.me.get('nick'), text: input});
+ frame.stream.add({sender: irc.me.get('nick'), raw: input});
}
this.input.val('');
@@ -473,20 +496,20 @@ $(function() {
msg.to !== 'status') return;
frame = frames.getByName(msg.to);
if (frame) {
- frame.stream.add({sender: msg.from, text: msg.text});
+ frame.stream.add({sender: msg.from, raw: msg.text});
}
});
socket.on('pm', function(msg) {
pm = frames.getByName(msg.nick) || new Frame({type: 'pm', name: msg.nick});
- pm.stream.add({sender: msg.nick, text: msg.text})
+ pm.stream.add({sender: msg.nick, raw: msg.text})
frames.add(pm);
})
// Message of the Day event (on joining a server)
socket.on('motd', function(data) {
data.motd.split('\n').forEach(function(line) {
- frames.getByName('status').stream.add({sender: '', text: line});
+ frames.getByName('status').stream.add({sender: '', raw: line});
});
});
@@ -560,7 +583,7 @@ $(function() {
console.log(data.message);
frame = frames.getActive();
error = humanizeError(data.message);
- frame.stream.add({text: error, type: 'error'})
+ frame.stream.add({type: 'error', raw: error});
});
});
View
@@ -64,7 +64,7 @@
<!-- Mustache templates -->
<script id="message-tmpl" type="text/html">
{{#sender}}<span class="sender">{{sender}}</span>{{/sender}}
- <span class="text {{^sender}}no-sender{{/sender}}">{{text}}</span>
+ <span class="text {{^sender}}no-sender{{/sender}}">{{{text}}</span>
</script>
<script id="tab-tmpl" type="text/html">
View
@@ -2,6 +2,7 @@
(function(b){function c(){}for(var d="assert,clear,count,debug,dir,dirxml,error,exception,firebug,group,groupCollapsed,groupEnd,info,log,memoryProfile,memoryProfileEnd,profile,profileEnd,table,time,timeEnd,timeStamp,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
{console.log();return window.console;}catch(err){return window.console={};}})());
+
// POLYFILLS
// =========
@@ -74,14 +75,18 @@ if ( !Array.prototype.forEach ) {
// =================
window.irc = (function(module) {
- module.utils = {
+ module.util = {
// Replaces oldString with newString at beginning of text
swapCommand: function(oldString, newString, text) {
if (text.substring(0, oldString.length) === oldString)
return newString + text.substring(oldString.length, text.length);
else
throw 'String "' + oldString + '" not found at beginning of text';
+ },
+ escapeHTML: function(text) {
+ return text.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}
+
}
return module

0 comments on commit a72859e

Please sign in to comment.