@@ -17,6 +17,8 @@ namespace md2docx
1717 class Md2Docx
1818 {
1919 static Dictionary < string , string > info = new Dictionary < string , string > ( ) ;
20+ static Dictionary < string , string > correspondecs ;
21+ static Dictionary < string , bool > optionalParts ;
2022
2123 /// <summary>
2224 /// Print usage without exit
@@ -80,6 +82,8 @@ private static void Main(string[] args)
8082
8183 string md = System . IO . File . ReadAllText ( mdPath ) ;
8284 JObject config = JObject . Parse ( System . IO . File . ReadAllText ( configPath ) ) ;
85+ correspondecs = config [ "对应关系" ] . ToObject < Dictionary < string , string > > ( ) ;
86+ optionalParts = config [ "可选部分" ] . ToObject < Dictionary < string , bool > > ( ) ;
8387 MarkdownDocument mddoc = new MarkdownDocument ( ) ;
8488 mddoc . Parse ( md ) ;
8589
@@ -98,10 +102,10 @@ private static void Main(string[] args)
98102 using ( WordprocessingDocument document = WordprocessingDocument . Create ( docPath , WordprocessingDocumentType . Document ) )
99103 {
100104 MainDocumentPart mainDocumentPart1 = document . AddMainDocumentPart ( ) ;
101- GenerateMainDocumentPart1Content ( mainDocumentPart1 , mddoc , ( JObject ) config [ "对应关系" ] , ( JObject ) config [ "可选部分" ] ) ;
105+ GenerateMainDocumentPart1Content ( mainDocumentPart1 , mddoc ) ;
102106
103107 StyleDefinitionsPart styleDefinitionsPart1 = mainDocumentPart1 . AddNewPart < StyleDefinitionsPart > ( "rId1" ) ;
104- GenerateStyleDefinitionsPart1Content ( styleDefinitionsPart1 , ( JArray ) config [ "样式" ] , ( bool ) config [ "可选部分" ] [ "延迟样式" ] ) ;
108+ GenerateStyleDefinitionsPart1Content ( styleDefinitionsPart1 , ( JArray ) config [ "样式" ] ) ;
105109
106110 FontTablePart fontTablePart1 = mainDocumentPart1 . AddNewPart < FontTablePart > ( "rId6" ) ;
107111 GeneratedCode . GenerateFontTablePartContent ( fontTablePart1 ) ;
@@ -117,36 +121,36 @@ private static void Main(string[] args)
117121 /// <param name="document">word document</param>
118122 /// <param name="correspondecs">how markdown blocks are mapping to styles</param>
119123 /// <param name="optionalParts">double check if user need these parts</param>
120- private static void GenerateMainDocumentPart1Content ( MainDocumentPart mainDocumentPart1 , MarkdownDocument document , JObject correspondecs , JObject optionalParts )
124+ private static void GenerateMainDocumentPart1Content ( MainDocumentPart mainDocumentPart1 , MarkdownDocument document )
121125 {
122126 Document document1 = new Document ( ) { MCAttributes = new MarkupCompatibilityAttributes ( ) } ;
123127
124128 Body docBody = new Body ( ) ;
125129
126- if ( ( bool ) optionalParts [ "封面" ] )
130+ if ( optionalParts [ "封面" ] )
127131 {
128132 GenerateCoverImage ( mainDocumentPart1 . AddNewPart < ImagePart > ( "image/jpeg" , "rId2" ) ) ;
129133 GeneratedCode . GenerateCover ( ref docBody , Md2Docx . info ) ;
130134 }
131135
132- if ( ( bool ) optionalParts [ "摘要" ] && Md2Docx . info . ContainsKey ( "c_title" ) )
136+ if ( optionalParts [ "摘要" ] && Md2Docx . info . ContainsKey ( "c_title" ) )
133137 {
134- AddAbstract ( info [ "c_title" ] , info [ "c_abs" ] , info [ "c_kew" ] , true , ref docBody , ref correspondecs ) ;
138+ AddAbstract ( info [ "c_title" ] , info [ "c_abs" ] , info [ "c_kew" ] , true , ref docBody ) ;
135139 }
136- if ( ( bool ) optionalParts [ "摘要" ] && Md2Docx . info . ContainsKey ( "e_title" ) )
140+ if ( optionalParts [ "摘要" ] && Md2Docx . info . ContainsKey ( "e_title" ) )
137141 {
138- AddAbstract ( info [ "e_title" ] , info [ "e_abs" ] , info [ "e_kew" ] , false , ref docBody , ref correspondecs ) ;
142+ AddAbstract ( info [ "e_title" ] , info [ "e_abs" ] , info [ "e_kew" ] , false , ref docBody ) ;
139143 }
140144
141- if ( ( bool ) optionalParts [ "目录" ] )
145+ if ( optionalParts [ "目录" ] )
142146 {
143147 GeneratedCode . GenerateTOC ( ref docBody ) ;
144148 }
145149
146150 // rendering body text(paragraph/heading, others are TBD)
147151 foreach ( var block in document . Blocks )
148152 {
149- DealMarkdownBlock ( block , ref docBody , ref correspondecs ) ;
153+ DealMarkdownBlock ( block , ref docBody ) ;
150154 }
151155
152156 SectionProperties sectionProperties1 = new SectionProperties ( ) ;
@@ -247,7 +251,7 @@ private static void DealMDInlines(RunProperties rp, IList<MarkdownInline> inline
247251 /// </summary>
248252 /// <param name="block">Paragraph block, when block is not paragraph block, it throw a exception</param>
249253 /// <param name="docBody">In which we append our text</param>
250- private static void DealQuoteRefer ( MarkdownBlock block , ref Body docBody , ref JObject correspondecs )
254+ private static void DealQuoteRefer ( MarkdownBlock block , ref Body docBody )
251255 {
252256 if ( ! ( block is ParagraphBlock ) )
253257 {
@@ -257,7 +261,7 @@ private static void DealQuoteRefer(MarkdownBlock block, ref Body docBody, ref JO
257261 {
258262 ParagraphProperties = new ParagraphProperties
259263 {
260- ParagraphStyleId = new ParagraphStyleId { Val = ( string ) correspondecs [ "引用" ] }
264+ ParagraphStyleId = new ParagraphStyleId { Val = correspondecs [ "引用" ] }
261265 }
262266 } ;
263267 Run run = new Run
@@ -274,7 +278,7 @@ private static void DealQuoteRefer(MarkdownBlock block, ref Body docBody, ref JO
274278 docBody . Append ( docPara ) ;
275279 }
276280
277- private static void DealMarkdownBlock ( MarkdownBlock block , ref Body docBody , ref JObject correspondecs )
281+ private static void DealMarkdownBlock ( MarkdownBlock block , ref Body docBody )
278282 {
279283
280284 if ( block is ParagraphBlock mpara )
@@ -283,7 +287,7 @@ private static void DealMarkdownBlock(MarkdownBlock block, ref Body docBody, ref
283287 {
284288 ParagraphProperties = new ParagraphProperties
285289 {
286- ParagraphStyleId = new ParagraphStyleId { Val = ( string ) correspondecs [ "正文" ] }
290+ ParagraphStyleId = new ParagraphStyleId { Val = correspondecs [ "正文" ] }
287291 }
288292 } ;
289293 DealMDInlines ( new RunProperties ( ) , mpara . Inlines , ref docPara ) ;
@@ -295,7 +299,7 @@ private static void DealMarkdownBlock(MarkdownBlock block, ref Body docBody, ref
295299 switch ( mhead . HeaderLevel )
296300 {
297301 case int i when i < 10 :
298- docPara . ParagraphProperties . ParagraphStyleId = new ParagraphStyleId ( ) { Val = ( string ) correspondecs [ ChineseNumber [ i ] + "级标题" ] } ;
302+ docPara . ParagraphProperties . ParagraphStyleId = new ParagraphStyleId ( ) { Val = correspondecs [ chineseNumber [ i ] + "级标题" ] } ;
299303 break ;
300304 default :
301305 throw new Exception ( $ "Rendering { block . GetType ( ) } not implement yet") ;
@@ -307,7 +311,7 @@ private static void DealMarkdownBlock(MarkdownBlock block, ref Body docBody, ref
307311 {
308312 foreach ( var e in refer . Blocks )
309313 {
310- DealQuoteRefer ( e , ref docBody , ref correspondecs ) ;
314+ DealQuoteRefer ( e , ref docBody ) ;
311315 }
312316 }
313317 else if ( ! ( block is YamlHeaderBlock ) )
@@ -323,7 +327,7 @@ private static void DealMarkdownBlock(MarkdownBlock block, ref Body docBody, ref
323327 /// <param name="keyWords">Key words</param>
324328 /// <param name="isCN">If it's Chinese abstract</param>
325329 /// <param name="docBody">In which we append our text</param>
326- private static void AddAbstract ( string title , string abs , string keyWords , bool isCN , ref Body docBody , ref JObject correspondecs )
330+ private static void AddAbstract ( string title , string abs , string keyWords , bool isCN , ref Body docBody )
327331 {
328332 string subtitle = isCN ? "摘要" : "ABSTRACT" ;
329333 string keyWT = isCN ? "关键词:" : "Key words: " ;
@@ -366,7 +370,7 @@ private static void AddAbstract(string title, string abs, string keyWords, bool
366370 {
367371 ParagraphProperties = new ParagraphProperties
368372 {
369- ParagraphStyleId = new ParagraphStyleId { Val = ( string ) correspondecs [ "正文" ] }
373+ ParagraphStyleId = new ParagraphStyleId { Val = correspondecs [ "正文" ] }
370374 }
371375 } ;
372376 run = new Run { RunProperties = new RunProperties ( ) } ;
@@ -379,7 +383,7 @@ private static void AddAbstract(string title, string abs, string keyWords, bool
379383 {
380384 ParagraphProperties = new ParagraphProperties
381385 {
382- ParagraphStyleId = new ParagraphStyleId { Val = ( string ) correspondecs [ "正文" ] }
386+ ParagraphStyleId = new ParagraphStyleId { Val = correspondecs [ "正文" ] }
383387 }
384388 } ;
385389 run = new Run { RunProperties = new RunProperties ( ) } ;
@@ -392,7 +396,7 @@ private static void AddAbstract(string title, string abs, string keyWords, bool
392396 {
393397 ParagraphProperties = new ParagraphProperties
394398 {
395- ParagraphStyleId = new ParagraphStyleId { Val = ( string ) correspondecs [ "正文" ] }
399+ ParagraphStyleId = new ParagraphStyleId { Val = correspondecs [ "正文" ] }
396400 }
397401 } ;
398402 run = new Run
@@ -419,7 +423,7 @@ private static void AddAbstract(string title, string abs, string keyWords, bool
419423 /// <param name="styleDefinitionsPart1">Styles object</param>
420424 /// <param name="styleConfig">Config json object</param>
421425 /// <param name="latent">If user need latent style</param>
422- private static void GenerateStyleDefinitionsPart1Content ( StyleDefinitionsPart styleDefinitionsPart1 , JArray styleConfig , bool latent )
426+ private static void GenerateStyleDefinitionsPart1Content ( StyleDefinitionsPart styleDefinitionsPart1 , JArray styleConfig )
423427 {
424428 Styles styles = new Styles ( ) { MCAttributes = new MarkupCompatibilityAttributes ( ) } ;
425429
@@ -441,7 +445,7 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
441445
442446 styles . Append ( docDefaults ) ;
443447
444- if ( latent )
448+ if ( optionalParts [ "延迟样式" ] )
445449 {
446450 styles . Append ( GeneratedCode . GenerateLatentStyles ( ) ) ;
447451 }
0 commit comments