home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 July / INTERNET105.ISO / pc / software / windows / building / homesite / actionscripts / urlformat.js < prev   
Encoding:
Text File  |  1998-10-29  |  3.4 KB  |  117 lines

  1. function URLFormat(strText, strTarget) {
  2.     //
  3.     // THIS FUNCTION REQUIRES MICROSOFT JSCRIPT 3.1
  4.     //
  5.     // Available for download at:
  6.     //    http://www.microsoft.com/scripting/
  7.     //
  8.     // Pass this function a variable containting the text you want to
  9.     // process, and a string indicating the target of the anchor.  If
  10.     // you do not wish to specify a target, send an empty string ("")
  11.     // for the second parameter.
  12.     //
  13.     // Joel Mueller
  14.     // Creative Internet Solutions
  15.     // jmueller@creativeis.com
  16.     //
  17.     
  18.     /*@cc_on @*/
  19.     /*@if (@_jscript_version < 3) @*/
  20.         return "You must have JScript 3 or greater.";
  21.     /*@else  @*/
  22.     var app = Application;
  23.     if (strTarget.length > 0) {
  24.         strTarget = app.TagCase(' TARGET="') + strTarget + '"';
  25.     }
  26.     var strResults = new String();
  27.     if (strText.length > 0) {
  28.         var index = 0;
  29.         var newindex = 0;
  30.         var objSearch = /((((https?:|ftp:|gopher:)\/\/)|(www\.|ftp\.))[\-\w\?\%\.,\/&#!@:=\+~_]+[A-Za-z0-9\/])/ig;
  31.         if (objSearch.test(strText)) {
  32.             var resArray = strText.match(objSearch);
  33.             for (i=0; resArray.length > i; i++) {
  34.                 newindex = strText.indexOf(resArray[i], index);
  35.                 strResults = strResults + strText.substring(index, newindex);
  36.                 if (strResults.substring((strResults.length - 1),strResults.length) != "@") {
  37.                     strResults = strResults + app.TagCase('<A HREF="');
  38.                     if (resArray[i].substring(0,4) == "www.") {
  39.                         strResults = strResults + "http://";
  40.                     }
  41.                     else {
  42.                         if (resArray[i].substring(0,4) == "ftp.") {
  43.                             strResults = strResults + "ftp://";
  44.                         }
  45.                     }
  46.                     strResults = strResults + resArray[i] + '"' + strTarget + '>' + resArray[i] + app.TagCase('</A>');
  47.                 } else {
  48.                     strResults = strResults + resArray[i];
  49.                 }
  50.                 index = newindex + resArray[i].length;
  51.             }
  52.             if (index < strText.length) {
  53.                 strResults = strResults + strText.substring(index, strText.length);
  54.             }
  55.         } else {
  56.             strResults = strText;
  57.         }
  58.     }
  59.     return strResults;
  60.     /*@end @*/
  61. }
  62.  
  63. function MailToFormat(strText) {
  64.     //
  65.     // THIS FUNCTION REQUIRES MICROSOFT JSCRIPT 3.1
  66.     //
  67.     // Available for download at:
  68.     //    http://www.microsoft.com/scripting/
  69.     //
  70.     // Pass this function a string, and it will turn any e-mail addresses
  71.     // into mailto links.
  72.     //
  73.     // Joel Mueller
  74.     // Creative Internet Solutions
  75.     // jmueller@creativeis.com
  76.     //
  77.  
  78.     /*@cc_on @*/
  79.     /*@if (@_jscript_version < 3) @*/
  80.         return "You must have JScript 3 or greater.";
  81.     /*@else  @*/
  82.     var strResults = new String();
  83.     var app = Application;
  84.     if (strText.length > 0) {
  85.         var index = 0;
  86.         var newindex = 0;
  87.         var objSearch = /[\w\.\-]+@([\w\.]+\.)+[A-Za-z]{2,4}/ig;
  88.         if (objSearch.test(strText)) {
  89.             var resArray = strText.match(objSearch);
  90.             for (i=0; resArray.length > i; i++) {
  91.                 newindex = strText.indexOf(resArray[i], index);
  92.                 strResults = strResults + strText.substring(index, newindex);
  93.                 strResults = strResults + app.TagCase('<A HREF="mailto:' + resArray[i] + '">') + resArray[i] + app.TagCase('</A>');
  94.                 index = newindex + resArray[i].length;
  95.             }
  96.             if (index < strText.length) {
  97.                 strResults = strResults + strText.substring(index, strText.length);
  98.             }
  99.         } else {
  100.             strResults = strText;
  101.         }
  102.     }
  103.     return strResults;
  104.     /*@end @*/
  105. }
  106.  
  107. function Main() {
  108.     var app = Application;
  109.     var intSelection = app.ActiveDocument.SelLength;    
  110.  
  111.     var strText = eval("app.ActiveDocument." + ((intSelection > 0) ? "SelText" : "Text"));
  112.  
  113.     strText = MailToFormat(URLFormat(strText, ""));
  114.     
  115.     eval("app.ActiveDocument." + ((intSelection > 0) ? "SelText" : "Text") + " = strText");
  116. }
  117.