Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. <html>
  2. <head>
  3. <title>DynAPI Tex2Var Converter - Converts text/html to JavaScript variable</title>
  4. <script>
  5.  
  6. function convert(){
  7.         var f=document.forms['frm'];
  8.         var cbo=f.cbo;
  9.         if(cbo.options[0].selected) text2string();
  10.         else if(cbo.options[1].selected) text2array();
  11. };
  12.  
  13. function text2string(){
  14.         var f=document.forms['frm'];
  15.         var vn=(f.txtname.value||'h'); // variable name
  16.         var i=(f['in'].value.length=0)? '':f['in'].value;
  17.         var t="var "+vn+"='"+Var2TextEncode(i)+"';\n";
  18.         f['out'].value=t;
  19. };
  20.  
  21. function text2array(){
  22.         var f=document.forms['frm'];
  23.         var vn=(f.txtname.value||'h'); // variable name
  24.         var t=["var "+vn+"=[''],s=0;\n"];
  25.         var i=f['in'].value
  26.         i=i.replace(/\r/g,'');
  27.  
  28.         var arr=i.split("\n");
  29.         for (var c=0;c<arr.length;c++){
  30.                 x=arr[c];
  31.                 if(x) {
  32.                         x=x.replace(/^\t/g,""); // remove leading tabs
  33.                         x=x.replace(/\'/g,"\\'"); // '
  34.                         t[t.length]=vn+"[s++]='"+x+"';\n";
  35.                 }
  36.         }
  37.         f['out'].value=t.join('');
  38. };
  39.  
  40. // Var2Text Encode - converts multiline text into single line
  41. Var2TextEncode=function (text){
  42.         if (!text) return '';
  43.         text=text.replace(/\\/g,"\\\\");        // replace \ with \\
  44.         text=text.replace(/\'/g,"\\'");         // replace ' with \'
  45.         text=text.replace(/\r\n/g,"\\n");       // replace CrLf with \n
  46.         text=text.replace(/\n/g,"\\n");         // replace single Lf with \n
  47.         text=text.replace(/\r/g,"\\r");         // replace single Cr with \n
  48.         return text;
  49. };
  50.  
  51. </script>
  52. </head>
  53. <body bgcolor="#FFFFFF">
  54. <form name="frm">
  55. <div align="center">
  56.  <table border="0" bgcolor="#E0E0E0" style="border: 2px solid #000080" cellpadding="3">
  57.    <tr>
  58.      <td bgcolor="#000080" style="border-bottom: 1px solid #C0C0C0">
  59.        <p><b><font face="Arial" color="#FFFFFF" size="2">Text2Var
  60.        Converter</font></b>
  61.      </td>
  62.    </tr>
  63.  <center>
  64.    <tr>
  65.      <td bgcolor="#F5F5DC" style="border-bottom: 1px solid #C0C0C0">
  66.        <table border="0" cellspacing="1" cellpadding="2">
  67.          <tr>
  68.            <td valign="bottom"><font face="Arial" size="2">
  69. Variable Name:</font><br>
  70. <input type="text" size="40" name="txtname"> &nbsp; </td>
  71.            <td valign="bottom"><font face="Arial" size="2">Format:</font><br>
  72. <select size="1" name="cbo">
  73.          <option selected value="single">Text/HTML-to-JS String</option>
  74.          <option value="multi">Text/HTML-to-JS Array</option>
  75.        </select></td>
  76.            <td valign="bottom">&nbsp;<input type="button" onclick="convert()" value="Convert" name="cmdconvert">
  77.            </td>
  78.          </tr>
  79.        </table>
  80.      </td>
  81.    </tr>
  82.    <tr>
  83.      <td valign="top"><font face="Arial">
  84. Text/HTML to be converted (Input):</font><br>
  85. <textarea cols=76 rows=10 name="in"></textarea></td>
  86.    </tr>
  87.    <tr>
  88.      <td>
  89.        <table border="0" cellspacing="1" cellpadding="0">
  90.          <tr>
  91.            <td><font face="Arial">
  92. JavaScript (Output): </font></td>
  93.            <td align="right"><font face="Arial">
  94. <input type="checkbox" name="chkwrap" value="ON" onclick="this.form.out.wrap=(this.checked)? 'soft':'off';" checked>Wrap
  95.              </font></td>
  96.          </tr>
  97.          <tr>
  98.            <td colspan="2">
  99. <textarea cols=76 rows=7 name="out"></textarea>
  100. <br>
  101.            </td>
  102.          </tr>
  103.        </table>
  104.      </td>
  105.    </tr>
  106.  </table>
  107.  </center>
  108. </div>
  109. </form>
  110. </body>
  111. </html>