-
Notifications
You must be signed in to change notification settings - Fork 826
/
toHtmlEnt.bat
88 lines (64 loc) · 2.35 KB
/
toHtmlEnt.bat
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
@if (@X)==(@Y) @end /* JScript comment
@echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */
if (WScript.Arguments.Length < 1 ) {
WScript.Echo(WScript.ScriptName + " input_file output_file");
WScript.Quit(0)
}
var fso= new ActiveXObject("Scripting.FileSystemObject");
var inputFile=WScript.Arguments.Item(0);
if (!fso.FileExists(inputFile)){
WScript.Echo(inputFile + " does not exist");
WScript.Echo("usage:");
WScript.Echo(WScript.ScriptName + " input_file [output_file]");
WScript.Quit(1);
}
function getFileContent(file) {
// :: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855&start=15&p=28898 ::
var ado = WScript.CreateObject("ADODB.Stream");
ado.Type = 2; // adTypeText = 2
ado.CharSet = "iso-8859-1"; // code page with minimum adjustments for input
ado.Open();
ado.LoadFromFile(file);
var adjustment = "\u20AC\u0081\u201A\u0192\u201E\u2026\u2020\u2021" +
"\u02C6\u2030\u0160\u2039\u0152\u008D\u017D\u008F" +
"\u0090\u2018\u2019\u201C\u201D\u2022\u2013\u2014" +
"\u02DC\u2122\u0161\u203A\u0153\u009D\u017E\u0178" ;
var fs = new ActiveXObject("Scripting.FileSystemObject");
var size = (fs.getFile(file)).size;
var content = ado.ReadText(size);
ado.Close();
return content;
}
function writeContent(file,content) {
// :: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855&start=15&p=28898 ::
var ado = WScript.CreateObject("ADODB.Stream");
ado.Type = 2; // adTypeText = 2
ado.CharSet = "iso-8859-1"; // right code page for output (no adjustments)
//ado.Mode=2;
ado.Open();
ado.WriteText(content);
ado.SaveToFile(file, 2);
ado.Close();
}
String.prototype.toHtmlEntities = function() {
// :: http://stackoverflow.com/questions/18749591/encode-html-entities-in-javascript ::
return this.replace(/[\u00A0-\u9999<>\&]/gim , function(s) {
return "&#" + s.charCodeAt(0) + ";";
});
};
function printToConsole(fileContent){
//WScript.Echo(fileContent);
WScript.Echo(fileContent.toHtmlEntities());
}
function printToFile(fileContent,file){
writeContent(fileContent.toHtmlEntities(),file);
}
if (WScript.Arguments.Length > 1) {
var outputFile=WScript.Arguments.Item(1);
printToFile(outputFile,getFileContent(inputFile));
} else {
printToConsole(getFileContent(inputFile));
}