@@ -138,76 +138,28 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
138138 var tableCellIndex : number ;
139139 var columnAlignment : number [ ] = [ ] ;
140140
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+ }
201153 }
202154
203- function reformatReferences ( ) {
204- doc . fields . toggleShowCodes ( ) ;
155+ function findReplace ( findText : string , findProps : { } , replaceText : string , replaceProps : { } ) {
205156 var find = doc . range ( ) . find ;
206157 find . clearFormatting ( ) ;
158+ setProperties ( find , findProps ) ;
207159 var replace = find . replacement ;
208160 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 ) ;
211163 }
212164
213165 function write ( s : string ) {
@@ -230,7 +182,7 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
230182 write ( "|\n" ) ;
231183 }
232184
233- function stripFormattingMarks ( text : string ) {
185+ function trimEndFormattingMarks ( text : string ) {
234186 var i = text . length ;
235187 while ( i > 0 && text . charCodeAt ( i - 1 ) < 0x20 ) i -- ;
236188 return text . substr ( 0 , i ) ;
@@ -257,7 +209,7 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
257209 var level = 1 ;
258210 var sectionBreak = text . indexOf ( "\x0C" ) >= 0 ;
259211
260- text = stripFormattingMarks ( text ) ;
212+ text = trimEndFormattingMarks ( text ) ;
261213 if ( inTable ) {
262214 style = "Table" ;
263215 }
@@ -274,7 +226,7 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
274226 case "Heading" :
275227 case "Appendix" :
276228 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" ) ;
278230 break ;
279231
280232 case "Normal" :
@@ -337,21 +289,22 @@ function convertDocumentToMarkdown(doc: Word.Document): string {
337289 }
338290
339291 function writeDocument ( ) {
340- var p = doc . paragraphs . first ;
341- while ( p ) {
292+ for ( var p = doc . paragraphs . first ; p ; p = p . next ( ) ) {
342293 writeParagraph ( p ) ;
343- p = p . next ( ) ;
344294 }
345295 writeBlockEnd ( ) ;
346296 }
347297
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 ( ) ;
355308
356309 writeDocument ( ) ;
357310
0 commit comments