Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit e0776fa

Browse files
committed
feat: add reference part
1 parent 399bbb8 commit e0776fa

File tree

2 files changed

+112
-21
lines changed

2 files changed

+112
-21
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
- [x] 标题、正文
1313
- [x] 摘要、关键词、结束语
1414
- [x] 页边距
15-
- [ ] 参考文献
15+
- [x] 参考文献
1616
- [ ] 封面
1717
- [ ] 目录
1818

1919
以上为我心中该项目demo的应有功能,在此之前除了更新进度外不会做进一步的文档更新。
2020

21+
同时以下功能虽然也可以开发,但是考虑其复杂性无法保证进度,并且手动调整相对方便。在我有空之后会继续开发以下功能。
22+
2123
- [ ] 页眉
2224
- [ ] 页脚
2325
- [ ] 图片

md2docx/md2docx.cs

Lines changed: 109 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using W14 = DocumentFormat.OpenXml.Office2010.Word;
1414
using W15 = DocumentFormat.OpenXml.Office2013.Word;
1515
using Microsoft.Toolkit.Parsers.Markdown.Inlines;
16-
using DocumentFormat.OpenXml.Drawing.Wordprocessing;
1716

1817
namespace md2docx
1918
{
@@ -72,7 +71,6 @@ private static void Main(string[] args)
7271
{
7372
endnote = yaml.Children["end"];
7473
}
75-
break;
7674
}
7775
}
7876

@@ -193,8 +191,7 @@ private static void GenerateMainDocumentPart1Content(MainDocumentPart mainDocume
193191
document1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
194192

195193
Body body1 = new Body();
196-
197-
// code below can be in one function
194+
198195
if (c_title != "")
199196
{
200197
Add_abstract(c_title, c_abs, c_kew, true, ref body1);
@@ -239,17 +236,36 @@ private static void GenerateMainDocumentPart1Content(MainDocumentPart mainDocume
239236
}
240237
body1.Append(docPara);
241238
}
239+
else if (element is QuoteBlock refer)
240+
{
241+
if (endnote != "")
242+
{
243+
Add_endnote(endnote, "结束语", ref body1);
244+
}
245+
246+
Paragraph para = new Paragraph
247+
{
248+
ParagraphProperties = new ParagraphProperties
249+
{
250+
ParagraphStyleId = new ParagraphStyleId { Val = "endtitle" }
251+
}
252+
};
253+
Run run = new Run { RunProperties = new RunProperties() };
254+
Text txt = new Text { Text = "参考文献", Space = SpaceProcessingModeValues.Preserve };
255+
run.Append(txt);
256+
para.Append(run);
257+
body1.Append(para);
258+
foreach (var e in refer.Blocks)
259+
{
260+
Deal_quote_refer(e, ref body1);
261+
}
262+
}
242263
else if(!(element is YamlHeaderBlock))
243264
{
244265
throw new Exception($"Rendering {element.GetType()} not implement yet");
245266
}
246267
}
247268

248-
if (endnote != "")
249-
{
250-
Add_endnote(endnote, "结束语", ref body1);
251-
}
252-
253269
SectionProperties sectionProperties1 = new SectionProperties() { RsidR = "00803857" };
254270
PageSize pageSize1 = new PageSize() { Width = (UInt32Value)11906U, Height = (UInt32Value)16838U };
255271
PageMargin pageMargin1 = new PageMargin() { Top = 1418, Right = (UInt32Value)1134U, Bottom = 1418, Left = (UInt32Value)1701U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U };
@@ -340,10 +356,38 @@ private static void Deal_md_inline(RunProperties rp, MarkdownInline inline, ref
340356
}
341357
break;
342358
default:
359+
Console.WriteLine(inline.ToString());
343360
throw new Exception($"Rendering {inline.GetType()} not implement yet");
344361
}
345362
}
346363

364+
private static void Deal_quote_refer(MarkdownBlock block, ref Body docBody)
365+
{
366+
if (!(block is ParagraphBlock))
367+
{
368+
throw new Exception($"Rendering {block.GetType()} in quote not support");
369+
}
370+
Paragraph docPara = new Paragraph
371+
{
372+
ParagraphProperties = new ParagraphProperties
373+
{
374+
ParagraphStyleId = new ParagraphStyleId { Val = "reference" }
375+
}
376+
};
377+
Run run = new Run
378+
{
379+
RunProperties = new RunProperties()
380+
};
381+
Text txt = new Text
382+
{
383+
Text = block.ToString(),
384+
Space = SpaceProcessingModeValues.Preserve
385+
};
386+
run.Append(txt);
387+
docPara.Append(run);
388+
docBody.Append(docPara);
389+
}
390+
347391
private static void Add_abstract(string title, string abs, string keyWords, bool isCN, ref Body docBody)
348392
{
349393
string subtitle = isCN ? "摘要" : "ABSTRACT";
@@ -440,8 +484,7 @@ private static void Add_endnote(string endnote, string title, ref Body docBody)
440484
{
441485
ParagraphProperties = new ParagraphProperties
442486
{
443-
ParagraphStyleId = new ParagraphStyleId { Val = "Abs" },
444-
PageBreakBefore = new PageBreakBefore()
487+
ParagraphStyleId = new ParagraphStyleId { Val = "endtitle" }
445488
}
446489
};
447490
Run run = new Run { RunProperties = new RunProperties() };
@@ -461,7 +504,7 @@ private static void Add_endnote(string endnote, string title, ref Body docBody)
461504
txt = new Text { Text = endnote, Space = SpaceProcessingModeValues.Preserve };
462505
run.Append(txt);
463506
para.Append(run);
464-
docBody.Append(para);
507+
docBody.Append(para);
465508
}
466509

467510
// Generates content of webSettingsPart1.
@@ -2038,7 +2081,6 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
20382081
SpacingBetweenLines spacingBetweenLines2 = new SpacingBetweenLines() { Before = "100", BeforeLines = 100, After = "100", AfterLines = 100 };
20392082
Justification justification1 = new Justification() { Val = JustificationValues.Center };
20402083
OutlineLevel outlineLevel1 = new OutlineLevel() { Val = 0 };
2041-
Indentation indentation1 = new Indentation() { FirstLine = "200", FirstLineChars = 200 };
20422084

20432085
styleParagraphProperties1.Append(keepNext1);
20442086
styleParagraphProperties1.Append(keepLines1);
@@ -2160,10 +2202,10 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
21602202

21612203
StyleParagraphProperties styleParagraphProperties10 = new StyleParagraphProperties();
21622204
SpacingBetweenLines spacingBetweenLines11 = new SpacingBetweenLines() { Before = "180", After = "180", Line = "360", LineRule = LineSpacingRuleValues.Auto };
2205+
Indentation indentation1 = new Indentation() { FirstLine = "200", FirstLineChars = 200 };
21632206

21642207
styleParagraphProperties10.Append(spacingBetweenLines11);
21652208
styleParagraphProperties10.Append(indentation1);
2166-
styleParagraphProperties10.Indentation = new Indentation() { FirstLine = "200", FirstLineChars = 200 };
21672209

21682210
StyleRunProperties styleRunProperties11 = new StyleRunProperties();
21692211
RunFonts runFonts12 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "宋体", ComplexScript = "Times New Roman" };
@@ -2253,6 +2295,51 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
22532295
style18.Append(styleParagraphProperties14);
22542296
style18.Append(styleRunProperties14);
22552297

2298+
Style style19 = new Style
2299+
{
2300+
Type = StyleValues.Paragraph,
2301+
StyleId = "endtitle",
2302+
StyleName = new StyleName { Val = "endtitle" },
2303+
NextParagraphStyle = new NextParagraphStyle { Val = "reference" },
2304+
PrimaryStyle = new PrimaryStyle(),
2305+
StyleParagraphProperties = new StyleParagraphProperties
2306+
{
2307+
KeepLines = new KeepLines(),
2308+
KeepNext = new KeepNext(),
2309+
PageBreakBefore = new PageBreakBefore(),
2310+
SpacingBetweenLines = new SpacingBetweenLines { Before = "100", BeforeLines = 100, After = "100", AfterLines = 100 },
2311+
Justification = new Justification { Val = JustificationValues.Center},
2312+
OutlineLevel = new OutlineLevel { Val = 0 },
2313+
},
2314+
StyleRunProperties = new StyleRunProperties
2315+
{
2316+
RunFonts = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "黑体", ComplexScriptTheme = ThemeFontValues.MajorBidi },
2317+
Color = new Color() { Val = "000000", ThemeColor = ThemeColorValues.Text1 },
2318+
FontSize = new FontSize() { Val = "36" },
2319+
FontSizeComplexScript = new FontSizeComplexScript() { Val = "36" },
2320+
}
2321+
};
2322+
2323+
Style style20 = new Style
2324+
{
2325+
Type = StyleValues.Paragraph,
2326+
StyleId = "reference",
2327+
StyleName = new StyleName { Val = "reference" },
2328+
NextParagraphStyle = new NextParagraphStyle { Val = "reference" },
2329+
PrimaryStyle = new PrimaryStyle(),
2330+
StyleParagraphProperties = new StyleParagraphProperties
2331+
{
2332+
SpacingBetweenLines = new SpacingBetweenLines { Before = "180", After = "180", Line = "360", LineRule = LineSpacingRuleValues.Auto }
2333+
},
2334+
StyleRunProperties = new StyleRunProperties
2335+
{
2336+
RunFonts = new RunFonts { EastAsia = "楷体", Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" },
2337+
Color = new Color() { Val = "000000", ThemeColor = ThemeColorValues.Text1 },
2338+
FontSize = new FontSize() { Val = "21" },
2339+
FontSizeComplexScript = new FontSizeComplexScript() { Val = "21" },
2340+
}
2341+
};
2342+
22562343
Style style34 = new Style() { Type = StyleValues.Character, StyleId = "VerbatimChar", CustomStyle = true };
22572344
StyleName styleName34 = new StyleName() { Val = "Verbatim Char" };
22582345
BasedOn basedOn23 = new BasedOn() { Val = "ad" };
@@ -2292,6 +2379,8 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
22922379
styles1.Append(style14);
22932380
styles1.Append(style17);
22942381
styles1.Append(style18);
2382+
styles1.Append(style19);
2383+
styles1.Append(style20);
22952384
styles1.Append(style34);
22962385
styles1.Append(style44);
22972386

@@ -2352,13 +2441,13 @@ private static void GenerateFontTablePart1Content(FontTablePart fontTablePart1)
23522441
font3.Append(pitch3);
23532442
font3.Append(fontSignature3);
23542443

2355-
Font font4 = new Font() { Name = "等线" };
2356-
AltName altName2 = new AltName() { Val = "DengXian" };
2357-
Panose1Number panose1Number4 = new Panose1Number() { Val = "02010600030101010101" };
2444+
Font font4 = new Font() { Name = "黑体" };
2445+
AltName altName2 = new AltName() { Val = "SimHei" };
2446+
Panose1Number panose1Number4 = new Panose1Number() { Val = "02010609060101010101" };
23582447
FontCharSet fontCharSet4 = new FontCharSet() { Val = "86" };
2359-
FontFamily fontFamily4 = new FontFamily() { Val = FontFamilyValues.Auto };
2360-
Pitch pitch4 = new Pitch() { Val = FontPitchValues.Variable };
2361-
FontSignature fontSignature4 = new FontSignature() { UnicodeSignature0 = "A00002BF", UnicodeSignature1 = "38CF7CFA", UnicodeSignature2 = "00000016", UnicodeSignature3 = "00000000", CodePageSignature0 = "0004000F", CodePageSignature1 = "00000000" };
2448+
FontFamily fontFamily4 = new FontFamily() { Val = FontFamilyValues.Modern };
2449+
Pitch pitch4 = new Pitch() { Val = FontPitchValues.Fixed };
2450+
FontSignature fontSignature4 = new FontSignature() { UnicodeSignature0 = "800002BF", UnicodeSignature1 = "38CF7CFA", UnicodeSignature2 = "00000016", UnicodeSignature3 = "00000000", CodePageSignature0 = "00040001", CodePageSignature1 = "00000000" };
23622451

23632452
font4.Append(altName2);
23642453
font4.Append(panose1Number4);

0 commit comments

Comments
 (0)