-
Notifications
You must be signed in to change notification settings - Fork 10
/
doc2html.js
258 lines (215 loc) · 5.72 KB
/
doc2html.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*!
* doc2html and more
*
* Copyright (c) 2004-2020 Ildar Shaimordanov
*/
function alert(msg)
{
WScript.Echo(msg);
};
function quit(code)
{
WScript.Quit(code);
};
if ( WScript.Arguments.length < 1 || WScript.Arguments.Named.Exists('H') ) {
WScript.Arguments.ShowUsage();
quit();
}
var verbose = false;
function warn(text)
{
if ( ! arguments.callee.verbose ) {
return;
}
var now = new Date();
var date = "%04d/%02d/%02d %02d:%02d:%02d.%03d ".sprintf(
now.getFullYear(),
now.getMonth(),
now.getDay(),
now.getHours(),
now.getMinutes(),
now.getSeconds(),
now.getMilliseconds());
alert(date + text);
};
// Verbosely ?
warn.verbose = WScript.FullName.match(/cscript\.exe$/i) && WScript.Arguments.Named.Exists('V');
var formats = {
'txt': 2,
'dos': 4,
'rtf': 6,
'html': 8,
'mht': 9,
'xml': 11,
'fb2': 11,
'pdf': 17,
'xps': 18};
var lineEndings = {
'crlf': 0,
'cr': 1,
'lf': 2,
'lfcr': 3};
var xslName = WScript.ScriptFullName.replace(/[^\\]+$/, '') + 'doc2fb.xsl';
var xslFile;
var fileFormat = 'html';
var fileEncoding = 0;
var fileLineEnding = -1;
var word;
var e;
try {
// Creating of 'Send To' context menu
if ( WScript.Arguments.Named.Exists('help') && WScript.Arguments.Named.item('help').toLowerCase() == 'sendto' ) {
var wshShell = new ActiveXObject('WScript.Shell');
var sendTo = wshShell.SpecialFolders('SendTo');
var folder = sendTo + '\\doc2xxx';
warn('Creating of the folder "' + folder + '"');
try {
fso.CreateFolder(folder);
} catch (e) {
warn('The folder already exists');
}
Object.forIn(formats, function(value, key)
{
warn('Creating of the shortcut for "' + key + '" format');
var lnk;
lnk = wshShell.CreateShortcut(folder + '\\doc2' + key + '.lnk');
lnk.TargetPath = WScript.ScriptFullName;
lnk.Arguments = '/f:' + key;
lnk.Description = 'Convert .DOC to .' + key.toUpperCase();
lnk.Save();
}, true);
warn('Done');
quit();
}
warn('Validate formatting parameters');
var arg;
// /F:format
if ( WScript.Arguments.Named.Exists('F') ) {
arg = WScript.Arguments.Named.item('F');
fileFormat = (arg || '').toLowerCase();
if ( isNaN(formats[fileFormat]) ) {
throw new Error('Unknown output format: "' + arg + '"');
}
}
// /F:TXT /E:Encoding
if ( fileFormat == 'txt' && WScript.Arguments.Named.Exists('E') ) {
arg = WScript.Arguments.Named.item('E');
fileEncoding = Number(arg);
if ( isNaN(fileEncoding) || fileEncoding <= 0 ) {
throw new Error('Illegal encoding value: "' + arg + '"');
}
}
// /F:TXT /L:lineending
if ( fileFormat == 'txt' && WScript.Arguments.Named.Exists('L') ) {
arg = WScript.Arguments.Named.item('L');
fileLineEnding = Number(lineEndings[(arg || '').toLowerCase()]);
if ( isNaN(fileLineEnding) ) {
throw new Error('Illegal line ending: "' + arg + '"');
}
}
// /F:FB2 /XSL:filename
if ( fileFormat == 'fb2' ) {
if ( WScript.Arguments.Named.Exists('XSL') ) {
xslName = WScript.Arguments.Named.item('XSL');
}
var fso = new ActiveXObject("Scripting.FileSystemObject");
try {
var xslFile = fso.GetFile(xslName);
} catch (e) {
fileFormat = 'xml';
alert(xslName + ' not found.');
}
}
// Process file list
warn('Processing arguments');
var filelist = Enumerator.map(WScript.Arguments.Unnamed, function(i, fc)
{
return FileSystem.glob(i);
}).flatten();
if ( filelist.length < 1 ) {
throw new Error('Empty file list');
}
warn('Files to be processed:\n\t' + filelist.join('\n\t'));
// Launch the WINWORD application and start a file converting loop
warn('WINWORD is starting');
var word = new ActiveXObject("Word.Application");
word.DisplayAlerts = 0;
word.Options.WarnBeforeSavingPrintingSendingMarkup = false;
if ( WScript.Arguments.Named.Exists('fg') ) {
warn('Activating and displaying of the WINWORD in foreground');
word.Visible = true;
word.Activate();
}
filelist.forEach(function(filename)
{
var docfile = String(filename);
var newfile = docfile.replace(/\.[^\.]+$/, '.' + fileFormat);
warn('Open "' + docfile + '"');
var doc = word.Documents.Open(docfile);
if ( fileFormat == 'fb2' ) {
//? doc.XMLSaveDataOnly = false;
doc.XMLUseXSLTWhenSaving = true;
doc.XMLSaveThroughXSLT = '' + xslFile;
//? doc.XMLHideNamespaces = true;
doc.XMLShowAdvancedErrors = true;
doc.XMLSchemaReferences.HideValidationErrors = false;
//? doc.XMLSchemaReferences.AutomaticValidation = true;
doc.XMLSchemaReferences.IgnoreMixedContent = false;
//? doc.XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = true;
doc.XMLSchemaReferences.ShowPlaceholderText = false;
}
warn('Save as "' + newfile + '"');
doc.SaveAs2(
// FileName
newfile,
// FileFormat
formats[fileFormat],
// LockComments
false,
// Password
'',
// AddToRecentFiles
false,
// Write Password
'',
// ReadOnlyRecommended
false,
// EmbedTrueTypeFonts
false,
// SaveNativePictureFormat
false,
// SaveFormsData
false,
// SaveAsAOCELetter
false,
// Encoding
fileEncoding,
// InsertLineBreaks
fileLineEnding > -1,
// AllowSubstitutions
false,
// LineEnding
fileLineEnding,
// AddBiDiMarks
true,
// CompatibilityMode
0);
warn('Close this document');
doc.Close();
});
} catch (e) {
alert('Error encountered: '
// + '['
// + (e.number >> 0x10)
// + ':'
// + (e.number & 0xFFFF)
// + '] - '
+ e.description);
} finally {
if ( word ) {
warn('WINWORD is closing');
word.Quit();
WScript.Sleep(500);
warn('Done');
}
}