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

Commit 2341e42

Browse files
committed
feat: add add reference part
but set h/w to a constant value
1 parent 7a02585 commit 2341e42

File tree

4 files changed

+170
-39
lines changed

4 files changed

+170
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.user
77
*.userosscache
88
*.sln.docstates
9+
*.vscode
910

1011
# User-specific files (MonoDevelop/Xamarin Studio)
1112
*.userprefs

examples/config.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,31 @@
188188
"段前分页": false,
189189
"段前后空行": 1,
190190
"行距": 0
191+
},
192+
{
193+
"名称": "Image Title",
194+
"英文字体": "Times New Roman",
195+
"中文字体": "黑体",
196+
"字体大小": "五号",
197+
"对齐方式": "居中",
198+
"录入大纲": false,
199+
"大纲等级": 0,
200+
"加粗": false,
201+
"斜体": false,
202+
"下划线": false,
203+
"删除线": false,
204+
"首行缩进": 0,
205+
"段前分页": false,
206+
"段前后空行": 0,
207+
"行距": 0
191208
}
192209
],
193210
"对应关系": {
194211
"正文": "bodytext",
195212
"一级标题": "heading 1",
196213
"二级标题": "heading 2",
197214
"三级标题": "heading 3",
198-
"代码段": "Code",
215+
"代码段": "code",
199216
"引用": "reference"
200217
},
201218
"可选部分": {
@@ -204,7 +221,6 @@
204221
"目录": true,
205222
"页眉": true,
206223
"页脚": true,
207-
"结束语": true,
208224
"延迟样式": true
209225
}
210226
}

md2docx/GeneratedCode.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,5 +2068,92 @@ public static void GenerateFontTablePartContent(FontTablePart fontTablePart1)
20682068
fontTablePart1.Fonts = fonts1;
20692069
}
20702070

2071+
public static Run GenerateImageReference(int id)
2072+
{
2073+
Run run1 = new Run();
2074+
2075+
RunProperties runProperties1 = new RunProperties();
2076+
NoProof noProof1 = new NoProof();
2077+
2078+
runProperties1.Append(noProof1);
2079+
2080+
Drawing drawing1 = new Drawing();
2081+
2082+
Wp.Inline inline1 = new Wp.Inline() { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U, AnchorId = "06C249DF", EditId = "66989579" };
2083+
Wp.Extent extent1 = new Wp.Extent() { Cx = 1104900L, Cy = 1047750L };
2084+
Wp.EffectExtent effectExtent1 = new Wp.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L };
2085+
Wp.DocProperties docProperties1 = new Wp.DocProperties() { Id = (UInt32Value)1U, Name = $"图片 {id}" };
2086+
2087+
Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();
2088+
2089+
A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks() { NoChangeAspect = true };
2090+
graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
2091+
2092+
nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);
2093+
2094+
A.Graphic graphic1 = new A.Graphic();
2095+
graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
2096+
2097+
A.GraphicData graphicData1 = new A.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" };
2098+
2099+
Pic.Picture picture1 = new Pic.Picture();
2100+
picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");
2101+
2102+
Pic.NonVisualPictureProperties nonVisualPictureProperties1 = new Pic.NonVisualPictureProperties();
2103+
Pic.NonVisualDrawingProperties nonVisualDrawingProperties1 = new Pic.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" };
2104+
Pic.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new Pic.NonVisualPictureDrawingProperties();
2105+
2106+
nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
2107+
nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);
2108+
2109+
Pic.BlipFill blipFill1 = new Pic.BlipFill();
2110+
A.Blip blip1 = new A.Blip() { Embed = $"rId{id}" };
2111+
2112+
A.Stretch stretch1 = new A.Stretch();
2113+
A.FillRectangle fillRectangle1 = new A.FillRectangle();
2114+
2115+
stretch1.Append(fillRectangle1);
2116+
2117+
blipFill1.Append(blip1);
2118+
blipFill1.Append(stretch1);
2119+
2120+
Pic.ShapeProperties shapeProperties1 = new Pic.ShapeProperties();
2121+
2122+
A.Transform2D transform2D1 = new A.Transform2D();
2123+
A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L };
2124+
A.Extents extents1 = new A.Extents() { Cx = 1104900L, Cy = 1047750L };
2125+
2126+
transform2D1.Append(offset1);
2127+
transform2D1.Append(extents1);
2128+
2129+
A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
2130+
A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
2131+
2132+
presetGeometry1.Append(adjustValueList1);
2133+
2134+
shapeProperties1.Append(transform2D1);
2135+
shapeProperties1.Append(presetGeometry1);
2136+
2137+
picture1.Append(nonVisualPictureProperties1);
2138+
picture1.Append(blipFill1);
2139+
picture1.Append(shapeProperties1);
2140+
2141+
graphicData1.Append(picture1);
2142+
2143+
graphic1.Append(graphicData1);
2144+
2145+
inline1.Append(extent1);
2146+
inline1.Append(effectExtent1);
2147+
inline1.Append(docProperties1);
2148+
inline1.Append(nonVisualGraphicFrameDrawingProperties1);
2149+
inline1.Append(graphic1);
2150+
2151+
drawing1.Append(inline1);
2152+
2153+
run1.Append(runProperties1);
2154+
run1.Append(drawing1);
2155+
2156+
return run1;
2157+
}
20712158
}
20722159
}

md2docx/md2docx.cs

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,11 @@ private static void GenerateCoverImage(ImagePart imagePart1)
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>
190-
/// <param name="docPara">In which we append out text</param>
191-
private static void CovertMDInlines(RunProperties rp, IList<MarkdownInline> inlines, ref Paragraph docPara)
190+
/// <param name="paragraph">In which we append out text</param>
191+
private static void CovertMDInlines(RunProperties rp, IList<MarkdownInline> inlines, ref Paragraph paragraph,ref List<Paragraph> paragraphs)
192192
{
193193
foreach(MarkdownInline inline in inlines)
194194
{
195-
196195
switch (inline)
197196
{
198197
case TextRunInline mtxt:
@@ -201,7 +200,7 @@ private static void CovertMDInlines(RunProperties rp, IList<MarkdownInline> inli
201200
Text dtext = new Text { Text = mtxt.Text, Space = SpaceProcessingModeValues.Preserve };
202201
trun.Append(newtrp);
203202
trun.Append(dtext);
204-
docPara.Append(trun);
203+
paragraph.Append(trun);
205204
break;
206205
case CodeInline mcode:
207206
RunProperties newcrp = (RunProperties)rp.Clone();
@@ -210,34 +209,51 @@ private static void CovertMDInlines(RunProperties rp, IList<MarkdownInline> inli
210209
Text dcode = new Text { Text = mcode.Text };
211210
crun.Append(newcrp);
212211
crun.Append(dcode);
213-
docPara.Append(crun);
212+
paragraph.Append(crun);
214213
break;
215214
case BoldTextInline bd:
216215
RunProperties newbrp = (RunProperties)rp.Clone();
217216
newbrp.Bold = new Bold();
218217
newbrp.BoldComplexScript = new BoldComplexScript();
219-
CovertMDInlines(newbrp, bd.Inlines, ref docPara);
218+
CovertMDInlines(newbrp, bd.Inlines, ref paragraph, ref paragraphs);
220219
break;
221220
case ItalicTextInline it:
222221
RunProperties newirp = (RunProperties)rp.Clone();
223222
newirp.Italic = new Italic();
224223
newirp.ItalicComplexScript = new ItalicComplexScript();
225-
CovertMDInlines(newirp, it.Inlines, ref docPara);
224+
CovertMDInlines(newirp, it.Inlines, ref paragraph, ref paragraphs);
226225
break;
227226
case StrikethroughTextInline st:
228227
RunProperties newstrp = (RunProperties)rp.Clone();
229228
newstrp.Strike = new Strike();
230-
CovertMDInlines(newstrp, st.Inlines, ref docPara);
229+
CovertMDInlines(newstrp, st.Inlines, ref paragraph, ref paragraphs);
231230
break;
232231
case SubscriptTextInline sb:
233232
RunProperties newsbrp = (RunProperties)rp.Clone();
234233
newsbrp.VerticalTextAlignment = new VerticalTextAlignment() { Val = VerticalPositionValues.Subscript };
235-
CovertMDInlines(newsbrp, sb.Inlines, ref docPara);
234+
CovertMDInlines(newsbrp, sb.Inlines, ref paragraph, ref paragraphs);
236235
break;
237236
case SuperscriptTextInline sp:
238237
RunProperties newsprp = (RunProperties)rp.Clone();
239238
newsprp.VerticalTextAlignment = new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript };
240-
CovertMDInlines(newsprp, sp.Inlines, ref docPara);
239+
CovertMDInlines(newsprp, sp.Inlines, ref paragraph, ref paragraphs);
240+
break;
241+
case ImageInline img:
242+
ParagraphProperties newpp = (ParagraphProperties)paragraph.ParagraphProperties.Clone();
243+
paragraphs.Add(paragraph);
244+
paragraph = new Paragraph
245+
{
246+
ParagraphProperties = new ParagraphProperties
247+
{
248+
ParagraphStyleId = new ParagraphStyleId { Val = "Image Title" }
249+
}
250+
};
251+
Run run = new Run();
252+
Text txt = new Text { Text = img.Tooltip, Space = SpaceProcessingModeValues.Preserve };
253+
run.Append(txt);
254+
paragraph.Append(run);
255+
paragraphs.Add(paragraph);
256+
paragraph = new Paragraph { ParagraphProperties = newpp };
241257
break;
242258
default:
243259
Console.WriteLine(inline.ToString());
@@ -251,31 +267,32 @@ private static void CovertMDInlines(RunProperties rp, IList<MarkdownInline> inli
251267
/// </summary>
252268
/// <param name="block">Paragraph block, when block is not paragraph block, it throw a exception</param>
253269
/// <param name="docBody">In which we append our text</param>
254-
private static void CovertQuoteRefer(MarkdownBlock block, ref Body docBody)
270+
private static List<Paragraph> CovertQuoteRefer(QuoteBlock refer)
255271
{
256-
if (!(block is ParagraphBlock))
257-
{
258-
throw new Exception($"Rendering {block.GetType()} in reference in quote not support");
259-
}
260-
Paragraph docPara = new Paragraph
272+
List<Paragraph> paragraphs = new List<Paragraph>();
273+
274+
foreach (MarkdownBlock block in refer.Blocks)
261275
{
262-
ParagraphProperties = new ParagraphProperties
276+
277+
if (!(block is ParagraphBlock))
263278
{
264-
ParagraphStyleId = new ParagraphStyleId { Val = correspondecs["引用"] }
279+
throw new Exception($"Rendering {block.GetType()} in reference in quote not support");
265280
}
266-
};
267-
Run run = new Run
268-
{
269-
RunProperties = new RunProperties()
270-
};
271-
Text txt = new Text
272-
{
273-
Text = block.ToString(),
274-
Space = SpaceProcessingModeValues.Preserve
275-
};
276-
run.Append(txt);
277-
docPara.Append(run);
278-
docBody.Append(docPara);
281+
Paragraph paragraph = new Paragraph
282+
{
283+
ParagraphProperties = new ParagraphProperties
284+
{
285+
ParagraphStyleId = new ParagraphStyleId { Val = correspondecs["引用"] }
286+
}
287+
};
288+
Run run = new Run { RunProperties = new RunProperties() };
289+
Text txt = new Text { Text = block.ToString(), Space = SpaceProcessingModeValues.Preserve };
290+
run.Append(txt);
291+
paragraph.Append(run);
292+
paragraphs.Add(paragraph);
293+
}
294+
295+
return paragraphs;
279296
}
280297

281298
private static List<Paragraph> CovertCodeBlock(CodeBlock code)
@@ -314,8 +331,12 @@ private static void CovertMarkdownBlock(MarkdownBlock block, ref Body docBody)
314331
ParagraphStyleId = new ParagraphStyleId { Val = correspondecs["正文"] }
315332
}
316333
};
317-
CovertMDInlines(new RunProperties(), mpara.Inlines, ref docPara);
318-
docBody.Append(docPara);
334+
List<Paragraph> paragraphs = new List<Paragraph>();
335+
CovertMDInlines(new RunProperties(), mpara.Inlines, ref docPara, ref paragraphs);
336+
foreach (Paragraph paragraph in paragraphs)
337+
{
338+
docBody.Append(paragraph);
339+
}
319340
}
320341
else if (block is CodeBlock mcode)
321342
{
@@ -328,6 +349,7 @@ private static void CovertMarkdownBlock(MarkdownBlock block, ref Body docBody)
328349
else if (block is HeaderBlock mhead)
329350
{
330351
Paragraph docPara = new Paragraph { ParagraphProperties = new ParagraphProperties() };
352+
List<Paragraph> paragraphs = new List<Paragraph>();
331353
switch (mhead.HeaderLevel)
332354
{
333355
case int i when i < 10:
@@ -336,14 +358,19 @@ private static void CovertMarkdownBlock(MarkdownBlock block, ref Body docBody)
336358
default:
337359
throw new Exception($"Rendering {block.GetType()} not implement yet");
338360
}
339-
CovertMDInlines(new RunProperties(), mhead.Inlines, ref docPara);
340-
docBody.Append(docPara);
361+
CovertMDInlines(new RunProperties(), mhead.Inlines, ref docPara, ref paragraphs);
362+
paragraphs.Add(docPara);
363+
foreach (Paragraph paragraph in paragraphs)
364+
{
365+
docBody.Append(paragraph);
366+
}
341367
}
342368
else if (block is QuoteBlock refer)
343369
{
344-
foreach (var e in refer.Blocks)
370+
List<Paragraph> paragraphs = CovertQuoteRefer(refer);
371+
foreach (Paragraph paragraph in paragraphs)
345372
{
346-
CovertQuoteRefer(e, ref docBody);
373+
docBody.Append(paragraph);
347374
}
348375
}
349376
else if (!(block is YamlHeaderBlock))

0 commit comments

Comments
 (0)