]> jfr.im git - irc/quakenet/qwebirc.git/commitdiff
Better URL/channel parsing.
authorChris Porter <redacted>
Fri, 17 Oct 2008 12:25:38 +0000 (13:25 +0100)
committerChris Porter <redacted>
Fri, 17 Oct 2008 12:25:38 +0000 (13:25 +0100)
js/ui/url.js

index 16ee8c9a9dbe1d421ffd81540892791811d20e07..5de24ce8ba28554101323a66acbc31ff759cc802 100644 (file)
@@ -1,54 +1,61 @@
-var url_re = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
-var chan_re = /\B#[^ ,]+/;
+function urlificate(element, text, execfn) {
+  var punct_re = /(\.*|\,|;)$/;
 
-function txtprocess(text, regex, appendfn, matchfn) {
-  for(;;) {
-    var index = text.search(regex);
-    if(index == -1) {
-      appendfn(text);
-      break;
-    }
-    var match = text.match(regex);
+  var txtprocess = function(text, regex, appendfn, matchfn) {
+    for(;;) {
+      var index = text.search(regex);
+      if(index == -1) {
+       appendfn(text);
+       break;
+      }
+      var match = text.match(regex);
       
-    var before = text.substring(0, index);
-    var matched = match[0];
-    var after = text.substring(index + matched.length);
-    
-    text = after;
-    appendfn(before);
-    matchfn(matched);
-  }
-}
-
-function urlificate(element, text, execfn) {
-  function appendText(text) {
-    chanficate(element, text, execfn);
-  }
-  function appendA(text) {
-    var a = document.createElement("a");
-    a.href = text;
-    a.target = "new";
-    a.appendChild(document.createTextNode(text));
+      var before = text.substring(0, index);
+      var matched = match[0];
+      var after = text.substring(index + matched.length);
     
-    element.appendChild(a);
-  }
-  txtprocess(text, url_re, appendText, appendA);
-}
-
-function chanficate(element, text, execfn) {
-  function appendText(text) {
+      appendfn(before);
+      var more = matchfn(matched);
+      if(!more)
+        more = "";
+      text = more + after;
+    }
+  };
+  
+  var appendText = function(text) {
     element.appendChild(document.createTextNode(text));  
-  }
-  function appendA(text) {
+  };
+  
+  var appendChan = function(text) {
+    var newtext = text.replace(punct_re, "");
+    var punct = text.substring(newtext.length);
+
     var a = document.createElement("a");
     a.href = "#";
     a.addEvent("click", function(e) {
       new Event(e).stop();
-      execfn("/JOIN " + text);
+      execfn("/JOIN " + newtext);
     });
-    a.appendChild(document.createTextNode(text));
+    a.appendChild(document.createTextNode(newtext));
+    element.appendChild(a);
+    
+    return punct;
+  };
+
+  var appendURL = function(text) {  
+    var newtext = text.replace(punct_re, "");
+    var punct = text.substring(newtext.length);
     
+    var a = document.createElement("a");
+    a.href = newtext;
+    a.target = "new";
+    a.appendChild(document.createTextNode(newtext));
     element.appendChild(a);
-  }
-  txtprocess(text, chan_re, appendText, appendA);
+    
+    return punct;
+  };
+
+  txtprocess(text, /\bhttp:\/\/[^ ]+/, function(text) {
+    txtprocess(text, /\B#[^ ,]+/, appendText, appendChan);
+  }, appendURL);
 }