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

Commit a91e4af

Browse files
committed
feat: add style factory
to generate style from JsonObject, with unit test, but not complete
1 parent 7f71324 commit a91e4af

File tree

6 files changed

+111
-1
lines changed

6 files changed

+111
-1
lines changed

md2docx.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.28307.757
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "md2docx", "md2docx\md2docx.csproj", "{57F2D503-1BD2-452E-8B5B-D513E92F204E}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "md2docxTests", "md2docxTests\md2docxTests.csproj", "{F8387CFA-7F83-4688-B121-C5768456B48B}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{57F2D503-1BD2-452E-8B5B-D513E92F204E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{57F2D503-1BD2-452E-8B5B-D513E92F204E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{57F2D503-1BD2-452E-8B5B-D513E92F204E}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{F8387CFA-7F83-4688-B121-C5768456B48B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{F8387CFA-7F83-4688-B121-C5768456B48B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{F8387CFA-7F83-4688-B121-C5768456B48B}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{F8387CFA-7F83-4688-B121-C5768456B48B}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

md2docx/StyleFactory.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json.Linq;
7+
using DocumentFormat.OpenXml.Wordprocessing;
8+
9+
namespace md2docx
10+
{
11+
public class StyleFactory
12+
{
13+
public Style GenerateStyle(JObject jObject)
14+
{
15+
return new Style
16+
{
17+
Type = StyleValues.Paragraph,
18+
StyleId = (string)jObject["名称"],
19+
StyleName = new StyleName { Val = (string)jObject["名称"] },
20+
StyleRunProperties = new StyleRunProperties
21+
{
22+
RunFonts = new RunFonts { Ascii = (string)jObject["英文字体"], HighAnsi = (string)jObject["英文字体"], ComplexScript = (string)jObject["英文字体"], EastAsia = (string)jObject["中文字体"] },
23+
FontSize = new FontSize { Val = (string)jObject["字体大小"] },
24+
FontSizeComplexScript = new FontSizeComplexScript { Val = (string)jObject["字体大小"] }
25+
}
26+
};
27+
}
28+
}
29+
}

md2docx/md2docx.csproj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,35 @@
5757
<None Include="packages.config" />
5858
</ItemGroup>
5959
<ItemGroup>
60-
<Reference Include="DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
60+
<Reference Include="DocumentFormat.OpenXml, Version=2.9.1.0, Culture=neutral, PublicKeyToken=8fb06cb64d019a17, processorArchitecture=MSIL">
61+
<HintPath>..\packages\DocumentFormat.OpenXml.2.9.1\lib\net46\DocumentFormat.OpenXml.dll</HintPath>
62+
</Reference>
6163
<Reference Include="Microsoft.Toolkit, Version=5.1.0.0, Culture=neutral, PublicKeyToken=4aff67a105548ee2, processorArchitecture=MSIL">
6264
<HintPath>..\packages\Microsoft.Toolkit.5.1.1\lib\netstandard2.0\Microsoft.Toolkit.dll</HintPath>
6365
</Reference>
6466
<Reference Include="Microsoft.Toolkit.Parsers, Version=5.1.0.0, Culture=neutral, PublicKeyToken=4aff67a105548ee2, processorArchitecture=MSIL">
6567
<HintPath>..\packages\Microsoft.Toolkit.Parsers.5.1.1\lib\netstandard2.0\Microsoft.Toolkit.Parsers.dll</HintPath>
6668
</Reference>
69+
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
70+
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
71+
</Reference>
72+
<Reference Include="System" />
73+
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
74+
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
75+
<Private>True</Private>
76+
<Private>True</Private>
77+
</Reference>
78+
<Reference Include="System.IO.Packaging, Version=4.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
79+
<HintPath>..\packages\System.IO.Packaging.4.5.0\lib\net46\System.IO.Packaging.dll</HintPath>
80+
</Reference>
81+
<Reference Include="System.Runtime.Serialization" />
82+
<Reference Include="System.Xml" />
83+
<Reference Include="System.Xml.Linq" />
6784
<Reference Include="WindowsBase" />
6885
</ItemGroup>
6986
<ItemGroup>
7087
<Compile Include="md2docx.cs" />
88+
<Compile Include="StyleFactory.cs" />
7189
</ItemGroup>
7290
<ItemGroup>
7391
<BootstrapperPackage Include=".NETFramework,Version=v4.6.1">

md2docx/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="DocumentFormat.OpenXml" version="2.9.1" targetFramework="net461" />
34
<package id="Microsoft.Toolkit" version="5.1.1" targetFramework="net461" />
45
<package id="Microsoft.Toolkit.Parsers" version="5.1.1" targetFramework="net461" />
6+
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
7+
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
8+
<package id="System.IO.Packaging" version="4.5.0" targetFramework="net461" />
59
</packages>

md2docxTests/StyleFactoryTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using md2docx;
3+
using DocumentFormat.OpenXml.Wordprocessing;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace md2docxTests
7+
{
8+
[TestClass]
9+
public class StyleFactoryTests
10+
{
11+
[TestMethod]
12+
public void TestStyle()
13+
{
14+
Style style = new Style
15+
{
16+
Type = StyleValues.Paragraph,
17+
StyleId = "heading 1",
18+
StyleName = new StyleName { Val = "heading 1" },
19+
StyleRunProperties = new StyleRunProperties
20+
{
21+
RunFonts = new RunFonts { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "黑体", ComplexScript = "Times New Roman" },
22+
FontSize = new FontSize { Val = "32" },
23+
FontSizeComplexScript = new FontSizeComplexScript { Val = "32" }
24+
}
25+
};
26+
JObject jObject = JObject.Parse(@"{""名称"":""heading 1"",""英文字体"":""Times New Roman"",""中文字体"":""黑体"",""字体大小"":""32""}");
27+
StyleFactory factory = new StyleFactory();
28+
Assert.AreEqual(factory.GenerateStyle(jObject).OuterXml, style.OuterXml);
29+
}
30+
}
31+
}

md2docxTests/md2docxTests.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="DocumentFormat.OpenXml" Version="2.9.1" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
13+
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
14+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
15+
<PackageReference Include="WindowsBase" Version="4.6.1055" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\md2docx\md2docx.csproj" />
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)