@@ -130,14 +130,14 @@ private static void GenerateMainDocumentPart1Content(MainDocumentPart mainDocume
130130 if ( optionalParts [ "封面" ] )
131131 {
132132 GenerateCoverImage ( mainDocumentPart1 . AddNewPart < ImagePart > ( "image/jpeg" , "rId2" ) ) ;
133- GeneratedCode . GenerateCover ( ref docBody , Md2Docx . info ) ;
133+ GeneratedCode . GenerateCover ( ref docBody , info ) ;
134134 }
135135
136- if ( optionalParts [ "摘要" ] && Md2Docx . info . ContainsKey ( "c_title" ) )
136+ if ( optionalParts [ "摘要" ] && info . ContainsKey ( "c_title" ) )
137137 {
138138 AddAbstract ( info [ "c_title" ] , info [ "c_abs" ] , info [ "c_kew" ] , true , ref docBody ) ;
139139 }
140- if ( optionalParts [ "摘要" ] && Md2Docx . info . ContainsKey ( "e_title" ) )
140+ if ( optionalParts [ "摘要" ] && info . ContainsKey ( "e_title" ) )
141141 {
142142 AddAbstract ( info [ "e_title" ] , info [ "e_abs" ] , info [ "e_kew" ] , false , ref docBody ) ;
143143 }
@@ -150,7 +150,7 @@ private static void GenerateMainDocumentPart1Content(MainDocumentPart mainDocume
150150 // rendering body text(paragraph/heading, others are TBD)
151151 foreach ( var block in document . Blocks )
152152 {
153- DealMarkdownBlock ( block , ref docBody ) ;
153+ CovertMarkdownBlock ( block , ref docBody ) ;
154154 }
155155
156156 SectionProperties sectionProperties1 = new SectionProperties ( ) ;
@@ -183,12 +183,12 @@ private static void GenerateCoverImage(ImagePart imagePart1)
183183 }
184184
185185 /// <summary>
186- /// Deal MarkdonwInlines using dfs
186+ /// Covert MarkdonwInlines using dfs
187187 /// </summary>
188188 /// <param name="rp">Current Run Properties, because special style may be nesting so we need keep it</param>
189189 /// <param name="inline">Current Inline element</param>
190190 /// <param name="docPara">In which we append out text</param>
191- private static void DealMDInlines ( RunProperties rp , IList < MarkdownInline > inlines , ref Paragraph docPara )
191+ private static void CovertMDInlines ( RunProperties rp , IList < MarkdownInline > inlines , ref Paragraph docPara )
192192 {
193193 foreach ( MarkdownInline inline in inlines )
194194 {
@@ -216,28 +216,28 @@ private static void DealMDInlines(RunProperties rp, IList<MarkdownInline> inline
216216 RunProperties newbrp = ( RunProperties ) rp . Clone ( ) ;
217217 newbrp . Bold = new Bold ( ) ;
218218 newbrp . BoldComplexScript = new BoldComplexScript ( ) ;
219- DealMDInlines ( newbrp , bd . Inlines , ref docPara ) ;
219+ CovertMDInlines ( newbrp , bd . Inlines , ref docPara ) ;
220220 break ;
221221 case ItalicTextInline it :
222222 RunProperties newirp = ( RunProperties ) rp . Clone ( ) ;
223223 newirp . Italic = new Italic ( ) ;
224224 newirp . ItalicComplexScript = new ItalicComplexScript ( ) ;
225- DealMDInlines ( newirp , it . Inlines , ref docPara ) ;
225+ CovertMDInlines ( newirp , it . Inlines , ref docPara ) ;
226226 break ;
227227 case StrikethroughTextInline st :
228228 RunProperties newstrp = ( RunProperties ) rp . Clone ( ) ;
229229 newstrp . Strike = new Strike ( ) ;
230- DealMDInlines ( newstrp , st . Inlines , ref docPara ) ;
230+ CovertMDInlines ( newstrp , st . Inlines , ref docPara ) ;
231231 break ;
232232 case SubscriptTextInline sb :
233233 RunProperties newsbrp = ( RunProperties ) rp . Clone ( ) ;
234234 newsbrp . VerticalTextAlignment = new VerticalTextAlignment ( ) { Val = VerticalPositionValues . Subscript } ;
235- DealMDInlines ( newsbrp , sb . Inlines , ref docPara ) ;
235+ CovertMDInlines ( newsbrp , sb . Inlines , ref docPara ) ;
236236 break ;
237237 case SuperscriptTextInline sp :
238238 RunProperties newsprp = ( RunProperties ) rp . Clone ( ) ;
239239 newsprp . VerticalTextAlignment = new VerticalTextAlignment ( ) { Val = VerticalPositionValues . Superscript } ;
240- DealMDInlines ( newsprp , sp . Inlines , ref docPara ) ;
240+ CovertMDInlines ( newsprp , sp . Inlines , ref docPara ) ;
241241 break ;
242242 default :
243243 Console . WriteLine ( inline . ToString ( ) ) ;
@@ -251,11 +251,11 @@ private static void DealMDInlines(RunProperties rp, IList<MarkdownInline> inline
251251 /// </summary>
252252 /// <param name="block">Paragraph block, when block is not paragraph block, it throw a exception</param>
253253 /// <param name="docBody">In which we append our text</param>
254- private static void DealQuoteRefer ( MarkdownBlock block , ref Body docBody )
254+ private static void CovertQuoteRefer ( MarkdownBlock block , ref Body docBody )
255255 {
256256 if ( ! ( block is ParagraphBlock ) )
257257 {
258- throw new Exception ( $ "Rendering { block . GetType ( ) } in quote not support") ;
258+ throw new Exception ( $ "Rendering { block . GetType ( ) } in reference in quote not support") ;
259259 }
260260 Paragraph docPara = new Paragraph
261261 {
@@ -278,7 +278,31 @@ private static void DealQuoteRefer(MarkdownBlock block, ref Body docBody)
278278 docBody . Append ( docPara ) ;
279279 }
280280
281- private static void DealMarkdownBlock ( MarkdownBlock block , ref Body docBody )
281+ private static List < Paragraph > CovertCodeBlock ( CodeBlock code )
282+ {
283+ List < Paragraph > paragraphs = new List < Paragraph > ( ) ;
284+ string [ ] lines = code . Text . Split ( '\n ' ) ;
285+
286+ foreach ( string line in lines )
287+ {
288+ Paragraph paragraph = new Paragraph
289+ {
290+ ParagraphProperties = new ParagraphProperties
291+ {
292+ ParagraphStyleId = new ParagraphStyleId { Val = correspondecs [ "代码段" ] }
293+ }
294+ } ;
295+ Run run = new Run { RunProperties = new RunProperties ( ) } ;
296+ Text txt = new Text { Text = line , Space = SpaceProcessingModeValues . Preserve } ;
297+ run . Append ( txt ) ;
298+ paragraph . Append ( run ) ;
299+ paragraphs . Add ( paragraph ) ;
300+ }
301+
302+ return paragraphs ;
303+ }
304+
305+ private static void CovertMarkdownBlock ( MarkdownBlock block , ref Body docBody )
282306 {
283307
284308 if ( block is ParagraphBlock mpara )
@@ -290,9 +314,17 @@ private static void DealMarkdownBlock(MarkdownBlock block, ref Body docBody)
290314 ParagraphStyleId = new ParagraphStyleId { Val = correspondecs [ "正文" ] }
291315 }
292316 } ;
293- DealMDInlines ( new RunProperties ( ) , mpara . Inlines , ref docPara ) ;
317+ CovertMDInlines ( new RunProperties ( ) , mpara . Inlines , ref docPara ) ;
294318 docBody . Append ( docPara ) ;
295319 }
320+ else if ( block is CodeBlock mcode )
321+ {
322+ List < Paragraph > paragraphs = CovertCodeBlock ( mcode ) ;
323+ foreach ( Paragraph paragraph in paragraphs )
324+ {
325+ docBody . Append ( paragraph ) ;
326+ }
327+ }
296328 else if ( block is HeaderBlock mhead )
297329 {
298330 Paragraph docPara = new Paragraph { ParagraphProperties = new ParagraphProperties ( ) } ;
@@ -304,14 +336,14 @@ private static void DealMarkdownBlock(MarkdownBlock block, ref Body docBody)
304336 default :
305337 throw new Exception ( $ "Rendering { block . GetType ( ) } not implement yet") ;
306338 }
307- DealMDInlines ( new RunProperties ( ) , mhead . Inlines , ref docPara ) ;
339+ CovertMDInlines ( new RunProperties ( ) , mhead . Inlines , ref docPara ) ;
308340 docBody . Append ( docPara ) ;
309341 }
310342 else if ( block is QuoteBlock refer )
311343 {
312344 foreach ( var e in refer . Blocks )
313345 {
314- DealQuoteRefer ( e , ref docBody ) ;
346+ CovertQuoteRefer ( e , ref docBody ) ;
315347 }
316348 }
317349 else if ( ! ( block is YamlHeaderBlock ) )
0 commit comments