-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoc2html.js
312 lines (266 loc) · 7.49 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// doc2html and more
//
// Copyright (c) 2004, 2009, 2010, 2012 Ildar Shaimordanov
//
///////////////////////////////////////////////////////////////////////////
//[requires[ js/win32/FileSystem.js ]]
//[requires[ js/String.js ]]
//[requires[ js/Array.js ]]
//[requires[ js/Object.js ]]
//[requires[ js/win32/Enumerator.js ]]
///////////////////////////////////////////////////////////////////////////
function alert(msg)
{
WScript.Echo(msg);
};
function quit(code)
{
WScript.Quit(code);
};
function help()
{
var msg = [
'doc2html and more',
'Copyright (C) 2004, 2009, 2010, 2012 Ildar Shaimordanov',
'',
'This tool allows to convert any DOC or DOCX file to several different ',
'formats such as plain text TXT (both DOS, Win, etc), or reach text RTF, or ',
'hyper-text HTML (default), MHT (web archive), XML, or PDF, or XPS. ',
'',
'Using doc2fb.xsl file it is possible to convert to FictionBook format (FB2). ',
'',
'There are options:',
'',
'/H',
' Outputs this help page.',
'',
'/F:format',
' Specifies output format as TXT, RTF, HTML, MHT, XML, PDF or XPS. ',
' Additionally FB2 stands for transformations to FictionBook format. ',
'',
'/E:encoding',
' A numeric value of the encoding to be used when saving as a plain text ',
' file. This option is significant with /F:TXT only. Refer to your ',
' system locales to learn which encodings ar available there. ',
'',
' The Russian or Ukrainian users can refer to the list below: ',
' 866 - DOS',
' 28595 - ISO',
' 20866 - KOI8-R',
' 21866 - KOI8-U',
' 10007 - Mac',
' 1251 - Win',
'',
'/L:lineending',
' The option specifies characters to be used as line ending. There are ',
' four available values - CRLF, CR, LF, or LFCR. The default value is ',
' CRLF. This option is significant with /F:TXT only. ',
'',
'/XSL:filename',
' The option specifies a name of a XSLT file for transformations to FictionBook format. ',
'',
'/V',
' Turn on verbosity.',
'',
'/FG',
' Specify this if you want to launch WINWORD in foreground.',
].join('\n');
alert(msg);
};
if ( WScript.Arguments.length < 1 || WScript.Arguments.Named.Exists('H') ) {
help();
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 = 0;
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");
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.SaveAs(
// 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
true,
// AllowSubstitutions
false,
// LineEnding
fileLineEnding);
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');
}
}