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