home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / intface / pca / pcaFormat.js < prev    next >
Encoding:
JavaScript  |  2003-04-10  |  5.4 KB  |  197 lines

  1. // class mag niet met kleine letters worden geschreven,
  2. // anders wordt deze als reserved woord behandeld.
  3.  
  4.  
  5. var htmlTagOpen = "<";                var htmlOpen = '<span cLaSs="html">' + htmlTagOpen + '</SPAN><span cLaSs="html">' ;
  6. var htmlTagClose = ">";                var htmlClose = '</SPAN><span cLaSs="html">' + htmlTagClose + '</SPAN>' ;
  7.  
  8. // this part is language dependent. Different values needed here for PHP, ASP...
  9. var scriptDelimOpen = '<%';
  10. var scriptDelimClose = '%>'
  11. var scriptCommentOpen = "'";
  12. var scriptCommentClose = "\n";
  13.  
  14. reserved = new stringArray(23)
  15. reserved[1] = "end";
  16. reserved[2] = "sub";
  17. reserved[3] = "set";
  18. reserved[4] = "do";
  19. reserved[5] = "loop";
  20. reserved[6] = "until";
  21. reserved[7] = "while";
  22. reserved[8] = "wend";
  23. reserved[9] = "for";
  24. reserved[10] = "each";
  25. reserved[11] = "next";
  26. reserved[12] = "function";
  27. reserved[13] = "in";
  28. reserved[14] = "true";
  29. reserved[15] = "false";
  30. reserved[16] = "if";
  31. reserved[17] = "then";
  32. reserved[18] = "else";
  33. reserved[19] = "class";
  34. reserved[20] = "private";
  35. reserved[21] = "public";
  36. reserved[22] = "new";
  37. reserved[23] = "dim";
  38.  
  39. // end language dependent part
  40.  
  41. function formatScript(scr) {
  42.     return _formatScript(scr);
  43. }
  44.  
  45. function formatHtml(html) {    
  46.     return _formatHtml(html);
  47. }
  48.  
  49. function formatSyntax(text) {
  50.     return _formatSyntax(text);
  51. }
  52.  
  53.  
  54. function stringArray(howMany) {
  55.     this.length = howMany;
  56.     for (var i = 1; i <= howMany; i++) {
  57.         this[i] = '';
  58.     }
  59. }
  60.  
  61. function replace(s, src, trg) {
  62.     var sp = s.split(src);
  63.     return sp.join(trg);
  64. }
  65. function isRegChar(ch) {
  66.     var res = false;
  67.     if ( (ch >= 'a') && (ch <= 'z') ) res = true;
  68.     if ( (ch >= 'A') && (ch <= 'Z') ) res = true;
  69.     if ( (ch >= '0') && (ch <= '9') ) res = true;
  70.     if ( (ch == '_') ) res = true;
  71.     return res;
  72. }
  73.  
  74. function replaceWord(s, src, trg) {
  75.     var sp = s.split(src);
  76.     var res = sp[0];
  77.     for (var q = 1; q < sp.length; q++) {
  78.         // check if character before and character after are seperation chars, or if src is just part of another word
  79.         if ( (!isRegChar(sp[q].charAt(0) ) )  && (!isRegChar(res.charAt(res.length - 1) ) ) ) {
  80.             res = res + "" + trg + "" + sp[q];
  81.         } else {
  82.             // alert("Kan [" + src + "] niet omzetten in [" + trg + "]");
  83.             res = res + "" + src + "" + sp[q];
  84.         }
  85.     }
  86.     return res;
  87. }
  88.  
  89. function markReservedWords(snippet) {
  90.         for (var i = 1; i <= reserved.length; i++) {
  91.             snippet = replaceWord(snippet, reserved[i], '<span cLaSs="res">' + reserved[i] + '</span>');
  92.         }
  93.         return snippet;
  94. }
  95.  
  96. function markScriptComment(snippet) {
  97.         comment = snippet.split(scriptCommentOpen);
  98.         if (comment.length > 1) {
  99.             for (var i = 1; i < comment.length; i ++) {
  100.                 commentEnd = comment[i].split(scriptCommentClose);
  101.                 comment[i] = commentEnd.join(scriptCommentClose + "</SPAN>");
  102.             }
  103.             snippet = comment.join('<span cLaSs="comment">' + scriptCommentOpen);
  104.         }
  105.         return snippet;
  106. }
  107.  
  108. function _formatScript(scr) {
  109.  
  110.     scr = replace(replace(scr, '<', '<'), '>', '>');
  111.  
  112.     var newScr = "";
  113.     
  114.     var idx1, idx2;
  115.     var preQuote;
  116.     var quoteChar = '"';
  117.     
  118.     while ( (idx1 = scr.indexOf(quoteChar)) > -1) {
  119.         preQuote = scr.substring(0, idx1); 
  120.         preQuote = markReservedWords(preQuote);
  121.         
  122.         preQuote = markScriptComment(preQuote);
  123.         
  124.         preQuote = replace(preQuote, "  ", "  "); 
  125.         newScr += preQuote + '<span cLaSs="string">' + quoteChar;
  126.         scr = scr.substring(idx1 + 1, scr.length);
  127.         idx2 = scr.indexOf(quoteChar);
  128.         if (idx2 == -1) idx2 = scr.length;
  129.         newScr += scr.substring(0, idx2 + 1) + "</SPAN>";
  130.         scr = scr.substring(idx2 + 1, scr.length);
  131.     }
  132.     newScr += replace(markScriptComment(markReservedWords(scr)), "  ", "  ");
  133.  
  134.     scr = newScr;
  135.     scr = replace(scr, scriptDelimOpen, '<span cLaSs="script">' + scriptDelimOpen + '</SPAN>');
  136.     scr = replace(scr, scriptDelimClose, '<span cLaSs="script">' + scriptDelimClose + '</SPAN>');
  137.     
  138.     return replace(scr, "\n", "<br>\n");
  139. }
  140.  
  141. function _formatHtml(html) {
  142.     
  143.     html = replace(replace(html, '<', '<'), '>', '>');
  144.  
  145.     var newHtml = "", nonHtml = "";
  146.     
  147.     var oldHtml = html;
  148.     
  149.     var idx1, idx2;
  150.     
  151.     while ( (idx1 = html.indexOf(htmlTagOpen)) > -1) {
  152.         idx2 = html.indexOf(htmlTagClose);
  153.         
  154.         if (idx1 > 0) nonHtml = html.substring(0, idx1); else nonHtml = "";
  155.  
  156.         idxScript = html.indexOf(scriptDelimOpen);
  157.         if (idxScript > idx1 && idxScript < idx2) {
  158.         }
  159.  
  160.         if (html.substring(idx1 + htmlTagOpen.length, idx1 + 3 + htmlTagOpen.length) == "!--") {
  161.             // we have comment!
  162.             newHtml = newHtml + nonHtml + '<span cLaSs="comment">' + html.substring(idx1, idx2 + htmlTagClose.length) + "</SPAN>";
  163.         } else {
  164.             // regular html tag
  165.             newHtml = newHtml + nonHtml + htmlOpen + html.substring(idx1 + htmlTagOpen.length, idx2) + htmlClose;
  166.         }
  167.         html = html.substring(idx2 + htmlTagClose.length, html.length);
  168.     }
  169.         
  170.     return replace(newHtml + "" + html, "\n", "<br>\n");
  171.  
  172. }
  173.  
  174. function _formatSyntax(text) {
  175.  
  176.     text = replace(replace(text, '<', '<'), '>', '>');
  177.  
  178.     var newText = "";
  179.  
  180.     var idx1, idx2;
  181.     
  182.     while ( (idx1 = text.indexOf(scriptDelimOpen)) > -1) {
  183.         idx2 =text.indexOf(scriptDelimClose);
  184.         if (-1 == idx2) idx2 = text.length;
  185.  
  186.         html = formatHtml(text.substring(0, idx1));
  187.         script = formatScript(text.substring(idx1, idx2 + scriptDelimClose.length));
  188.  
  189.         newText = newText + html + script;
  190.  
  191.         text = text.substring(idx2 + scriptDelimClose.length, text.length);
  192.     }
  193.     text = newText + formatHtml(text);
  194.  
  195.     return text;
  196. }
  197.