Skip to content

Commit

Permalink
提供 Markdown 解析和渲染能力。
Browse files Browse the repository at this point in the history
  • Loading branch information
CYJB committed Jan 17, 2023
1 parent 6753703 commit 43c460a
Show file tree
Hide file tree
Showing 126 changed files with 34,899 additions and 21 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[*] # 对于所有文件
# 缩进风格
indent_style = tab
# 缩进宽度
tab_width = 4
# 文件编码格式
charset = utf-8
# 行尾格式
end_of_line = crlf
# 文件结尾添加换行符
insert_final_newline = true

[*.cs]
# IDE0180: 使用元组交换值
csharp_style_prefer_tuple_swap = false
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Build Folders
bin
obj
Properties
*.pdb
.vs

# mstest test results
TestResults

# test project
ConsoleTest

# files
*.suo
*.snk
*.vspx
*.user
37 changes: 37 additions & 0 deletions Cyjb.Markdown.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32602.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cyjb.Markdown", "Cyjb.Markdown\Cyjb.Markdown.csproj", "{72856772-0E03-4DB2-97A3-F6500FDA3B09}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTest", "ConsoleTest\ConsoleTest.csproj", "{CD75D1F5-B6A7-41BD-BEFC-667073127287}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestMarkdown", "TestMarkdown\TestMarkdown.csproj", "{18330B97-4F8B-478A-9363-F6B65C8896DA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{72856772-0E03-4DB2-97A3-F6500FDA3B09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72856772-0E03-4DB2-97A3-F6500FDA3B09}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72856772-0E03-4DB2-97A3-F6500FDA3B09}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72856772-0E03-4DB2-97A3-F6500FDA3B09}.Release|Any CPU.Build.0 = Release|Any CPU
{CD75D1F5-B6A7-41BD-BEFC-667073127287}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD75D1F5-B6A7-41BD-BEFC-667073127287}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD75D1F5-B6A7-41BD-BEFC-667073127287}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD75D1F5-B6A7-41BD-BEFC-667073127287}.Release|Any CPU.Build.0 = Release|Any CPU
{18330B97-4F8B-478A-9363-F6B65C8896DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18330B97-4F8B-478A-9363-F6B65C8896DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18330B97-4F8B-478A-9363-F6B65C8896DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18330B97-4F8B-478A-9363-F6B65C8896DA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EA27249C-0A99-4DE4-A2BC-739E1E3CCE31}
EndGlobalSection
EndGlobal
78 changes: 78 additions & 0 deletions Cyjb.Markdown/Cyjb.Markdown.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>../CYJB_Code_Key.snk</AssemblyOriginatorKeyFile>
<PackageProjectUrl>https://github.com/CYJB/Cyjb.Markdown</PackageProjectUrl>
<Description>提供 Markdown 解析和渲染能力</Description>
<Copyright>Copyright (c) 2022, CYJB</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<NeutralLanguage>en</NeutralLanguage>
<Version>$(VersionPrefix)</Version>
<VersionPrefix>1.0.0</VersionPrefix>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<TransformOnBuild>true</TransformOnBuild>
<Authors>CYJB</Authors>
</PropertyGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
<None Include="..\README.md" Pack="True" PackagePath="" />
<None Include="LICENSE.txt" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cyjb" Version="1.0.11">
<GeneratePathProperty>True</GeneratePathProperty>
</PackageReference>
<PackageReference Include="Cyjb.Compilers.Design" Version="1.0.8">
<GeneratePathProperty>True</GeneratePathProperty>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Cyjb.Compilers.Runtime" Version="1.0.8" />
</ItemGroup>

<ItemGroup>
<None Update="Parse\Blocks\BlockLexer.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>BlockLexer.designed.cs</LastGenOutput>
</None>
<None Update="Parse\Inlines\InlineLexer.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>InlineLexer.designed.cs</LastGenOutput>
</None>
<None Update="Resources.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Resources.cs</LastGenOutput>
</None>
</ItemGroup>

<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>

<ItemGroup>
<Compile Update="Parse\Blocks\BlockLexer.designed.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>BlockLexer.tt</DependentUpon>
</Compile>
<Compile Update="Parse\Inlines\InlineLexer.designed.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>InlineLexer.tt</DependentUpon>
</Compile>
<Compile Update="Resources.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.tt</DependentUpon>
</Compile>
</ItemGroup>

</Project>
92 changes: 92 additions & 0 deletions Cyjb.Markdown/Document.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Cyjb.Markdown.Parse.Blocks;
using Cyjb.Markdown.Syntax;
using Cyjb.Text;

namespace Cyjb.Markdown;

/// <summary>
/// 表示一个 Markdown 文档。
/// </summary>
public sealed class Document : Node, INodeContainer<BlockNode>
{
/// <summary>
/// 解析指定 Markdown 字符串,并返回解析后的语法树。
/// </summary>
/// <param name="text">要解析的字符串。</param>
/// <param name="options">解析的选项。</param>
/// <returns>解析得到的 Markdown 语法树。</returns>
public static Document Parse(string text, ParseOptions? options = null)
{
return new BlockParser(text, options).Parse();
}

/// <summary>
/// 子节点列表。
/// </summary>
private readonly NodeList<BlockNode> children;

/// <summary>
/// 使用指定的文本范围初始化 <see cref="Document"/> 类的新实例。
/// </summary>
/// <param name="span">文本范围。</param>
public Document(TextSpan span = default) : base(MarkdownKind.Document)
{
children = new NodeList<BlockNode>(this);
Span = span;
}

/// <summary>
/// 获取子节点列表。
/// </summary>
public NodeList<BlockNode> Children => children;

/// <summary>
/// 获取前驱兄弟节点。
/// </summary>
/// <value>总是为 <c>null</c>。</value>
public override Node? Prev => null;
/// <summary>
/// 获取后继兄弟节点。
/// </summary>
/// <value>总是为 <c>null</c>。</value>
public override Node? Next => null;
/// <summary>
/// 获取第一个子节点,如果不存在则返回 <c>null</c>。
/// </summary>
public override BlockNode? FirstChild => children.FirstOrDefault();
/// <summary>
/// 获取最后一个子节点,如果不存在则返回 <c>null</c>。
/// </summary>
public override BlockNode? LastChild => children.LastOrDefault();

/// <summary>
/// 设置前驱兄弟节点。
/// </summary>
/// <param name="node">要设置的节点。</param>
internal override void SetPrev(Node? node) { }
/// <summary>
/// 设置后继兄弟节点。
/// </summary>
/// <param name="node">要设置的节点。</param>
internal override void SetNext(Node? node) { }

/// <summary>
/// 应用指定的访问器。
/// </summary>
/// <param name="visitor">节点访问器。</param>
public override void Accept(SyntaxVisitor visitor)
{
visitor.VisitDocument(this);
}

/// <summary>
/// 应用指定的访问器。
/// </summary>
/// <param name="visitor">节点访问器。</param>
/// <returns>返回的结果。</returns>
/// <typeparam name="TResult">返回结果的类型。</typeparam>
public override TResult Accept<TResult>(SyntaxVisitor<TResult> visitor)
{
return visitor.VisitDocument(this)!;
}
}
23 changes: 23 additions & 0 deletions Cyjb.Markdown/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2022, CYJB
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110 changes: 110 additions & 0 deletions Cyjb.Markdown/MarkdownKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
namespace Cyjb.Markdown;

/// <summary>
/// 表示 Markdown 的节点类型。
/// </summary>
public enum MarkdownKind
{
/// <summary>
/// Markdown 文档。
/// </summary>
Document,

/// <summary>
/// 分隔线。
/// </summary>
ThematicBreak,
/// <summary>
/// 标题。
/// </summary>
Heading,
/// <summary>
/// 代码块
/// </summary>
CodeBlock,
/// <summary>
/// HTML 块。
/// </summary>
HtmlBlock,
/// <summary>
/// 链接定义。
/// </summary>
LinkDefinition,
/// <summary>
/// 段落。
/// </summary>
Paragraph,
/// <summary>
/// 引用。
/// </summary>
Quote,
/// <summary>
/// 列表。
/// </summary>
List,
/// <summary>
/// 列表项。
/// </summary>
ListItem,

/// <summary>
/// 行内代码段。
/// </summary>
CodeSpan,
/// <summary>
/// 强调。
/// </summary>
Emphasis,
/// <summary>
/// 粗体。
/// </summary>
Strong,
/// <summary>
/// 删除线。
/// </summary>
Strikethrough,
/// <summary>
/// 链接。
/// </summary>
Link,
/// <summary>
/// 图片。
/// </summary>
Image,
/// <summary>
/// 行内 HTML 起始标签。
/// </summary>
HtmlStartTag,
/// <summary>
/// 行内 HTML 结束标签。
/// </summary>
HtmlEndTag,
/// <summary>
/// 行内 HTML 注释。
/// </summary>
HtmlComment,
/// <summary>
/// 行内 HTML 处理结构。
/// </summary>
HtmlProcessing,
/// <summary>
/// 行内 HTML 声明。
/// </summary>
HtmlDeclaration,
/// <summary>
/// 行内 HTML CDATA 段。
/// </summary>
HtmlCData,
/// <summary>
/// 硬换行。
/// </summary>
HardBreak,
/// <summary>
/// 软换行。
/// </summary>
SoftBreak,
/// <summary>
/// 文本。
/// </summary>
Literal,
}
Loading

0 comments on commit 43c460a

Please sign in to comment.