]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - js/ui/url.js
Add Q click whois thingies.
[irc/quakenet/qwebirc.git] / js / ui / url.js
index 8546f9a9cc02da6ff007a759ce09d89ad3af908f..81cbae738a6beb9a1f56d14ddcc2f2142941a6c1 100644 (file)
@@ -1,5 +1,5 @@
-function urlificate(element, text, execfn) {
-  var punct_re = /(\.*|\,|;)$/;
+qwebirc.ui.urlificate = function(element, text, execfn, cmdfn, window) {
+  var punct_re = /(\.*|\,|;|\])$/;
 
   var txtprocess = function(text, regex, appendfn, matchfn) {
     for(;;) {
@@ -15,7 +15,7 @@ function urlificate(element, text, execfn) {
       var after = text.substring(index + matched.length);
     
       appendfn(before);
-      var more = matchfn(matched);
+      var more = matchfn(matched, appendfn);
       if(!more)
         more = "";
       text = more + after;
@@ -23,7 +23,7 @@ function urlificate(element, text, execfn) {
   };
   
   var appendText = function(text) {
-    element.appendChild(document.createTextNode(text));  
+    qwebirc.util.NBSPCreate(text, element);
   };
   
   var appendChan = function(text) {
@@ -42,20 +42,53 @@ function urlificate(element, text, execfn) {
     return punct;
   };
 
-  var appendURL = function(text) {  
-    var newtext = text.replace(punct_re, "");
-    var punct = text.substring(newtext.length);
+  var appendURL = function(text, appendfn) {  
+    var url = text.replace(punct_re, "");
+    var punct = text.substring(url.length);
+    
+    var href = "";
+    var fn = null;
+    var target = "new";
+    var disptext = url;
+    
+    var ma = url.match(/^qwebirc:\/\/(.*)$/);
+    if(ma) {
+      var m = ma[1].match(/^([^\/]+)\/(.+)$/);
+      if(!m) {
+        appendfn(text);
+        return; 
+      }
+      
+      var cmd = cmdfn(m[1], window);
+      if(cmd) {
+        url = "#";
+        fn = cmd;
+        disptext = unescape(m[2]);
+        target = null;
+      } else {
+        appendfn(text);
+        return;
+      }
+    } else {
+      if(url.match(/^www\./))
+        url = "http://" + url;
+    }
     
     var a = new Element("a");
-    a.href = newtext;
-    a.target = "new";
-    a.appendChild(document.createTextNode(newtext));
+    a.href = url;
+    
+    if(target)
+      a.target = target;
+    a.appendChild(document.createTextNode(disptext));
+    
     element.appendChild(a);
+    if($defined(fn))
+      a.addEvent("click", function(e) { new Event(e).stop(); fn(disptext); });
     
     return punct;
   };
 
-  txtprocess(text, /\bhttp:\/\/[^ ]+/, function(text) {
+  txtprocess(text, /\b((https?|ftp|qwebirc):\/\/|www\.)[^ ]+/, function(text) {
     txtprocess(text, /\B#[^ ,]+/, appendText, appendChan);
   }, appendURL);
 }