FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TypeScript/scripts/word2md.js at asyncSimpleArrow · UmeshP53/TypeScript · GitHub
UmeshP53
/
TypeScript
Public
forked from
microsoft/TypeScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
TypeScript
/
scripts
/
word2md.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
235 lines (235 loc) · 8.86 KB
Breadcrumbs
TypeScript
/
scripts
/
word2md.js
Copy path
File metadata and controls
235 lines (235 loc) · 8.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// word2md - Word to Markdown conversion tool
//
// word2md converts a Microsoft Word document to Markdown formatted text. The tool uses the
// Word Automation APIs to start an instance of Word and access the contents of the document
// being converted. The tool must be run using the cscript.exe script host and requires Word
// to be installed on the target machine. The name of the document to convert must be specified
// as a command line argument and the resulting Markdown is written to standard output. The
// tool recognizes the specific Word styles used in the TypeScript Language Specification.
var
sys
=
(
function
(
)
{
var
fileStream
=
new
ActiveXObject
(
"ADODB.Stream"
)
;
fileStream
.
Type
=
2
/*text*/
;
var
binaryStream
=
new
ActiveXObject
(
"ADODB.Stream"
)
;
binaryStream
.
Type
=
1
/*binary*/
;
var
args
=
[
]
;
for
(
var
i
=
0
;
i
<
WScript
.
Arguments
.
length
;
i
++
)
{
args
[
i
]
=
WScript
.
Arguments
.
Item
(
i
)
;
}
return
{
args
:
args
,
createObject
:
function
(
typeName
)
{
return
new
ActiveXObject
(
typeName
)
;
}
,
write
:
function
(
s
)
{
WScript
.
StdOut
.
Write
(
s
)
;
}
,
writeFile
:
function
(
fileName
,
data
)
{
fileStream
.
Open
(
)
;
binaryStream
.
Open
(
)
;
try
{
// Write characters in UTF-8 encoding
fileStream
.
Charset
=
"utf-8"
;
fileStream
.
WriteText
(
data
)
;
// We don't want the BOM, skip it by setting the starting location to 3 (size of BOM).
fileStream
.
Position
=
3
;
fileStream
.
CopyTo
(
binaryStream
)
;
binaryStream
.
SaveToFile
(
fileName
,
2
/*overwrite*/
)
;
}
finally
{
binaryStream
.
Close
(
)
;
fileStream
.
Close
(
)
;
}
}
}
;
}
)
(
)
;
function
convertDocumentToMarkdown
(
doc
)
{
var
result
=
""
;
var
lastStyle
;
var
lastInTable
;
var
tableColumnCount
;
var
tableCellIndex
;
var
columnAlignment
=
[
]
;
function
setProperties
(
target
,
properties
)
{
for
(
var
name
in
properties
)
{
if
(
properties
.
hasOwnProperty
(
name
)
)
{
var
value
=
properties
[
name
]
;
if
(
typeof
value
===
"object"
)
{
setProperties
(
target
[
name
]
,
value
)
;
}
else
{
target
[
name
]
=
value
;
}
}
}
}
function
findReplace
(
findText
,
findOptions
,
replaceText
,
replaceOptions
)
{
var
find
=
doc
.
range
(
)
.
find
;
find
.
clearFormatting
(
)
;
setProperties
(
find
,
findOptions
)
;
var
replace
=
find
.
replacement
;
replace
.
clearFormatting
(
)
;
setProperties
(
replace
,
replaceOptions
)
;
find
.
execute
(
findText
,
false
,
false
,
false
,
false
,
false
,
true
,
0
,
true
,
replaceText
,
2
)
;
}
function
fixHyperlinks
(
)
{
var
count
=
doc
.
hyperlinks
.
count
;
for
(
var
i
=
0
;
i
<
count
;
i
++
)
{
var
hyperlink
=
doc
.
hyperlinks
.
item
(
i
+
1
)
;
var
address
=
hyperlink
.
address
;
if
(
address
&&
address
.
length
>
0
)
{
var
textToDisplay
=
hyperlink
.
textToDisplay
;
hyperlink
.
textToDisplay
=
"["
+
textToDisplay
+
"]("
+
address
+
")"
;
}
}
}
function
write
(
s
)
{
result
+=
s
;
}
function
writeTableHeader
(
)
{
for
(
var
i
=
0
;
i
<
tableColumnCount
-
1
;
i
++
)
{
switch
(
columnAlignment
[
i
]
)
{
case
1
:
write
(
"|:---:"
)
;
break
;
case
2
:
write
(
"|---:"
)
;
break
;
default
:
write
(
"|---"
)
;
}
}
write
(
"|\n"
)
;
}
function
trimEndFormattingMarks
(
text
)
{
var
i
=
text
.
length
;
while
(
i
>
0
&&
text
.
charCodeAt
(
i
-
1
)
<
0x20
)
i
--
;
return
text
.
substr
(
0
,
i
)
;
}
function
writeBlockEnd
(
)
{
switch
(
lastStyle
)
{
case
"Code"
:
write
(
"```\n\n"
)
;
break
;
case
"List Paragraph"
:
case
"Table"
:
case
"TOC"
:
write
(
"\n"
)
;
break
;
}
}
function
writeParagraph
(
p
)
{
var
text
=
p
.
range
.
text
;
var
style
=
p
.
style
.
nameLocal
;
var
inTable
=
p
.
range
.
tables
.
count
>
0
;
var
level
=
1
;
var
sectionBreak
=
text
.
indexOf
(
"\x0C"
)
>=
0
;
text
=
trimEndFormattingMarks
(
text
)
;
if
(
inTable
)
{
style
=
"Table"
;
}
else
if
(
style
.
match
(
/
\s
\d
$
/
)
)
{
level
=
+
style
.
substr
(
style
.
length
-
1
)
;
style
=
style
.
substr
(
0
,
style
.
length
-
2
)
;
}
if
(
lastStyle
&&
style
!==
lastStyle
)
{
writeBlockEnd
(
)
;
}
switch
(
style
)
{
case
"Heading"
:
case
"Appendix"
:
var
section
=
p
.
range
.
listFormat
.
listString
;
write
(
"####"
.
substr
(
0
,
level
)
+
' <a name="'
+
section
+
'"/>'
+
section
+
" "
+
text
+
"\n\n"
)
;
break
;
case
"Normal"
:
if
(
text
.
length
)
{
write
(
text
+
"\n\n"
)
;
}
break
;
case
"List Paragraph"
:
write
(
" "
.
substr
(
0
,
p
.
range
.
listFormat
.
listLevelNumber
*
2
-
2
)
+
"* "
+
text
+
"\n"
)
;
break
;
case
"Grammar"
:
write
(
"  "
+
text
.
replace
(
/
\s
\s
\s
/
g
,
" "
)
.
replace
(
/
\x0B
/
g
,
" \n   "
)
+
"\n\n"
)
;
break
;
case
"Code"
:
if
(
lastStyle
!==
"Code"
)
{
write
(
"```TypeScript\n"
)
;
}
else
{
write
(
"\n"
)
;
}
write
(
text
.
replace
(
/
\x0B
/
g
,
" \n"
)
+
"\n"
)
;
break
;
case
"Table"
:
if
(
!
lastInTable
)
{
tableColumnCount
=
p
.
range
.
tables
.
item
(
1
)
.
columns
.
count
+
1
;
tableCellIndex
=
0
;
}
if
(
tableCellIndex
<
tableColumnCount
)
{
columnAlignment
[
tableCellIndex
]
=
p
.
alignment
;
}
write
(
"|"
+
text
)
;
tableCellIndex
++
;
if
(
tableCellIndex
%
tableColumnCount
===
0
)
{
write
(
"\n"
)
;
if
(
tableCellIndex
===
tableColumnCount
)
{
writeTableHeader
(
)
;
}
}
break
;
case
"TOC Heading"
:
write
(
"## "
+
text
+
"\n\n"
)
;
break
;
case
"TOC"
:
var
strings
=
text
.
split
(
"\t"
)
;
write
(
" "
.
substr
(
0
,
level
*
2
-
2
)
+
"* ["
+
strings
[
0
]
+
" "
+
strings
[
1
]
+
"](#"
+
strings
[
0
]
+
")\n"
)
;
break
;
}
if
(
sectionBreak
)
{
write
(
"<br/>\n\n"
)
;
}
lastStyle
=
style
;
lastInTable
=
inTable
;
}
function
writeDocument
(
)
{
var
title
=
doc
.
builtInDocumentProperties
.
item
(
1
)
+
""
;
if
(
title
.
length
)
{
write
(
"# "
+
title
+
"\n\n"
)
;
}
for
(
var
p
=
doc
.
paragraphs
.
first
;
p
;
p
=
p
.
next
(
)
)
{
writeParagraph
(
p
)
;
}
writeBlockEnd
(
)
;
}
findReplace
(
"<"
,
{
}
,
"<"
,
{
}
)
;
findReplace
(
"<"
,
{
style
:
"Code"
}
,
"<"
,
{
}
)
;
findReplace
(
"<"
,
{
style
:
"Code Fragment"
}
,
"<"
,
{
}
)
;
findReplace
(
"<"
,
{
style
:
"Terminal"
}
,
"<"
,
{
}
)
;
findReplace
(
""
,
{
font
:
{
subscript
:
true
}
}
,
"<sub>^&</sub>"
,
{
font
:
{
subscript
:
false
}
}
)
;
findReplace
(
""
,
{
style
:
"Code Fragment"
}
,
"`^&`"
,
{
style
:
-
66
/* default font */
}
)
;
findReplace
(
""
,
{
style
:
"Production"
}
,
"*^&*"
,
{
style
:
-
66
/* default font */
}
)
;
findReplace
(
""
,
{
style
:
"Terminal"
}
,
"`^&`"
,
{
style
:
-
66
/* default font */
}
)
;
findReplace
(
""
,
{
font
:
{
bold
:
true
,
italic
:
true
}
}
,
"***^&***"
,
{
font
:
{
bold
:
false
,
italic
:
false
}
}
)
;
findReplace
(
""
,
{
font
:
{
italic
:
true
}
}
,
"*^&*"
,
{
font
:
{
italic
:
false
}
}
)
;
doc
.
fields
.
toggleShowCodes
(
)
;
findReplace
(
"^19 REF"
,
{
}
,
"[^&](#^&)"
,
{
}
)
;
doc
.
fields
.
toggleShowCodes
(
)
;
fixHyperlinks
(
)
;
writeDocument
(
)
;
result
=
result
.
replace
(
/
\x85
/
g
,
"\u2026"
)
;
result
=
result
.
replace
(
/
\x96
/
g
,
"\u2013"
)
;
result
=
result
.
replace
(
/
\x97
/
g
,
"\u2014"
)
;
return
result
;
}
function
main
(
args
)
{
if
(
args
.
length
!==
2
)
{
sys
.
write
(
"Syntax: word2md <inputfile> <outputfile>\n"
)
;
return
;
}
var
app
=
sys
.
createObject
(
"Word.Application"
)
;
var
doc
=
app
.
documents
.
open
(
args
[
0
]
)
;
sys
.
writeFile
(
args
[
1
]
,
convertDocumentToMarkdown
(
doc
)
)
;
doc
.
close
(
false
)
;
app
.
quit
(
)
;
}
main
(
sys
.
args
)
;
//# sourceMappingURL=file:///c:/ts/scripts/word2md.js.map
Back
|
FazBrowse Home
|
New Git URL