]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - js/ui/url.js
Don't show multiple 'maximum retries exceeded' dialogs.
[irc/quakenet/qwebirc.git] / js / ui / url.js
index 8546f9a9cc02da6ff007a759ce09d89ad3af908f..64507eb31e3af90f5eb48ed73f608dc7b880cbe5 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,15 +23,16 @@ function urlificate(element, text, execfn) {
   };
   
   var appendText = function(text) {
-    element.appendChild(document.createTextNode(text));  
+    qwebirc.util.NBSPCreate(text, element);
   };
   
   var appendChan = function(text) {
     var newtext = text.replace(punct_re, "");
     var punct = text.substring(newtext.length);
 
-    var a = new Element("a");
+    var a = new Element("span");
     a.href = "#";
+    a.addClass("hyperlink-channel");
     a.addEvent("click", function(e) {
       new Event(e).stop();
       execfn("/JOIN " + newtext);
@@ -42,20 +43,68 @@ 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 elementType = "a";
+    var addClass;
+    
+    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) {
+        addClass = m[1];
+        elementType = cmd[0];
+        if(cmd[0] != "a") {
+          url = null;
+        } else {
+          url = "#";
+        }
+        fn = cmd[1];
+        disptext = unescape(m[2]);
+        target = null;
+      } else {
+        appendfn(text);
+        return;
+      }
+      if(m[3])
+        punct = m[3] + punct;
+    } else {
+      if(url.match(/^www\./))
+        url = "http://" + url;
+    }
+    
+    var a = new Element(elementType);
+    if(addClass)
+      a.addClass("hyperlink-" + addClass);
+      
+    if(url) {
+      a.href = url;
+    
+      if(target)
+        a.target = target;
+    }
+    a.appendChild(document.createTextNode(disptext));
     
-    var a = new Element("a");
-    a.href = newtext;
-    a.target = "new";
-    a.appendChild(document.createTextNode(newtext));
     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);
 }