@@ -138,76 +138,28 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
138
138
var tableCellIndex : number ;
139
139
var columnAlignment : number [ ] = [ ] ;
140
140
141
- function reformatSubscripts ( ) {
142
- var find = doc . range ( ) . find ;
143
- find . clearFormatting ( ) ;
144
- find . font . subscript = true ;
145
- var replace = find . replacement ;
146
- replace . clearFormatting ( ) ;
147
- replace . font . subscript = false ;
148
- find . execute ( "" , false , false , false , false , false , true , 0 , true , "<sub>^&</sub>" , 2 ) ;
149
- }
150
-
151
- function reformatCodeFragments ( ) {
152
- var find = doc . range ( ) . find ;
153
- find . clearFormatting ( ) ;
154
- find . style = "Code Fragment" ;
155
- var replace = find . replacement ;
156
- replace . clearFormatting ( ) ;
157
- replace . style = - 66 ; // Default Paragraph Font
158
- find . execute ( "" , false , false , false , false , false , true , 0 , true , "`^&`" , 2 ) ;
159
- }
160
-
161
- function reformatProductions ( ) {
162
- var find = doc . range ( ) . find ;
163
- find . clearFormatting ( ) ;
164
- find . style = "Production" ;
165
- var replace = find . replacement ;
166
- replace . clearFormatting ( ) ;
167
- replace . style = - 66 ; // Default Paragraph Font
168
- find . execute ( "" , false , false , false , false , false , true , 0 , true , "*^&*" , 2 ) ;
169
- }
170
-
171
- function reformatTerminals ( ) {
172
- var find = doc . range ( ) . find ;
173
- find . clearFormatting ( ) ;
174
- find . style = "Terminal" ;
175
- var replace = find . replacement ;
176
- replace . clearFormatting ( ) ;
177
- replace . style = - 66 ; // Default Paragraph Font
178
- find . execute ( "" , false , false , false , false , false , true , 0 , true , "`^&`" , 2 ) ;
179
- }
180
-
181
- function reformatBoldItalic ( ) {
182
- var find = doc . range ( ) . find ;
183
- find . clearFormatting ( ) ;
184
- find . font . bold = true ;
185
- find . font . italic = true ;
186
- var replace = find . replacement ;
187
- replace . clearFormatting ( ) ;
188
- replace . font . bold = false ;
189
- replace . font . italic = false ;
190
- find . execute ( "" , false , false , false , false , false , true , 0 , true , "***^&***" , 2 ) ;
191
- }
192
-
193
- function reformatItalic ( ) {
194
- var find = doc . range ( ) . find ;
195
- find . clearFormatting ( ) ;
196
- find . font . italic = true ;
197
- var replace = find . replacement ;
198
- replace . clearFormatting ( ) ;
199
- replace . font . italic = false ;
200
- find . execute ( "" , false , false , false , false , false , true , 0 , true , "*^&*" , 2 ) ;
141
+ function setProperties ( target : { } , properties : { } ) {
142
+ for ( var name in properties ) {
143
+ if ( properties . hasOwnProperty ( name ) ) {
144
+ var value = properties [ name ] ;
145
+ if ( typeof value === "object" ) {
146
+ setProperties ( target [ name ] , value ) ;
147
+ }
148
+ else {
149
+ target [ name ] = value ;
150
+ }
151
+ }
152
+ }
201
153
}
202
154
203
- function reformatReferences ( ) {
204
- doc . fields . toggleShowCodes ( ) ;
155
+ function findReplace ( findText : string , findProps : { } , replaceText : string , replaceProps : { } ) {
205
156
var find = doc . range ( ) . find ;
206
157
find . clearFormatting ( ) ;
158
+ setProperties ( find , findProps ) ;
207
159
var replace = find . replacement ;
208
160
replace . clearFormatting ( ) ;
209
- find . execute ( "^19 REF" , false , false , false , false , false , true , 0 , true , "[^&](#^&)" , 2 ) ;
210
- doc . fields . toggleShowCodes ( ) ;
161
+ setProperties ( replace , replaceProps ) ;
162
+ find . execute ( findText , false , false , false , false , false , true , 0 , true , replaceText , 2 ) ;
211
163
}
212
164
213
165
function write ( s : string ) {
@@ -230,7 +182,7 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
230
182
write ( "|\n" ) ;
231
183
}
232
184
233
- function stripFormattingMarks ( text : string ) {
185
+ function trimEndFormattingMarks ( text : string ) {
234
186
var i = text . length ;
235
187
while ( i > 0 && text . charCodeAt ( i - 1 ) < 0x20 ) i -- ;
236
188
return text . substr ( 0 , i ) ;
@@ -257,7 +209,7 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
257
209
var level = 1 ;
258
210
var sectionBreak = text . indexOf ( "\x0C" ) >= 0 ;
259
211
260
- text = stripFormattingMarks ( text ) ;
212
+ text = trimEndFormattingMarks ( text ) ;
261
213
if ( inTable ) {
262
214
style = "Table" ;
263
215
}
@@ -274,7 +226,7 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
274
226
case "Heading" :
275
227
case "Appendix" :
276
228
var section = p . range . listFormat . listString ;
277
- write ( "####" . substr ( 0 , level ) + " <a name=\"" + section + "\"/>" + section + " " + text + "\n\n" ) ;
229
+ write ( "####" . substr ( 0 , level ) + ' <a name="' + section + '"/>' + section + " " + text + "\n\n" ) ;
278
230
break ;
279
231
280
232
case "Normal" :
@@ -337,21 +289,22 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
337
289
}
338
290
339
291
function writeDocument ( ) {
340
- var p = doc . paragraphs . first ;
341
- while ( p ) {
292
+ for ( var p = doc . paragraphs . first ; p ; p = p . next ( ) ) {
342
293
writeParagraph ( p ) ;
343
- p = p . next ( ) ;
344
294
}
345
295
writeBlockEnd ( ) ;
346
296
}
347
297
348
- reformatSubscripts ( ) ;
349
- reformatCodeFragments ( ) ;
350
- reformatProductions ( ) ;
351
- reformatTerminals ( ) ;
352
- reformatBoldItalic ( ) ;
353
- reformatItalic ( ) ;
354
- reformatReferences ( ) ;
298
+ findReplace ( "" , { font : { subscript : true } } , "<sub>^&</sub>" , { font : { subscript : false } } ) ;
299
+ findReplace ( "" , { style : "Code Fragment" } , "`^&`" , { style : - 66 /* default font */ } ) ;
300
+ findReplace ( "" , { style : "Production" } , "*^&*" , { style : - 66 /* default font */ } ) ;
301
+ findReplace ( "" , { style : "Terminal" } , "`^&`" , { style : - 66 /* default font */ } ) ;
302
+ findReplace ( "" , { font : { bold : true , italic : true } } , "***^&***" , { font : { bold : false , italic : false } } ) ;
303
+ findReplace ( "" , { font : { italic : true } } , "*^&*" , { font : { italic : false } } ) ;
304
+
305
+ doc . fields . toggleShowCodes ( ) ;
306
+ findReplace ( "^19 REF" , { } , "[^&](#^&)" , { } ) ;
307
+ doc . fields . toggleShowCodes ( ) ;
355
308
356
309
writeDocument ( ) ;
357
310
0 commit comments