-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CSharpToColouredHtml.Core (#135)
- Loading branch information
1 parent
41c4d38
commit b4bb8b6
Showing
17 changed files
with
256 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,9 @@ jobs: | |
with: | ||
PROJECT_FILE_PATH: src/Markdown.ColorCode/Markdown.ColorCode.csproj | ||
NUGET_KEY: ${{secrets.NUGET_API_KEY}} | ||
|
||
- name: Publish NuGet | ||
uses: alirezanet/[email protected] | ||
with: | ||
PROJECT_FILE_PATH: src/Markdown.ColorCode.CSharpToColoredHtml/Markdown.ColorCode.CSharpToColoredHtml.csproj | ||
NUGET_KEY: ${{secrets.NUGET_API_KEY}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ | |
"MD024": { | ||
"siblings_only": true | ||
}, | ||
"MD041": false | ||
"MD041": false, | ||
"MD034": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
global using ColorCode; | ||
global using ColorCode.Styling; | ||
global using Markdig; | ||
global using CsharpToColouredHTML.Core; | ||
global using Markdown.ColorCode.CSharpToColoredHtml.Internal; | ||
global using Markdown.ColorCode.Internal; | ||
global using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Markdown.ColorCode.CSharpToColoredHtml")] |
32 changes: 32 additions & 0 deletions
32
src/Markdown.ColorCode.CSharpToColoredHtml/Internal/CSharpToColoredHtmlFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using ColorCode.Common; | ||
|
||
namespace Markdown.ColorCode.CSharpToColoredHtml.Internal; | ||
|
||
/// <inheritdoc cref="IHtmlFormatter"/> | ||
[ExcludeFromCodeCoverage] | ||
internal sealed class CSharpToColoredHtmlFormatter : IHtmlFormatter | ||
{ | ||
private readonly IHtmlFormatter _internalFormatter; | ||
|
||
private readonly HTMLEmitterSettings _htmlEmitterSettings; | ||
|
||
private readonly CsharpColourer _csharpColorer; | ||
|
||
/// <summary> | ||
/// Create a new <see cref="CSharpToColoredHtmlFormatter"/>. | ||
/// </summary> | ||
/// <param name="internalFormatter">The internal formatter to use.</param> | ||
/// <param name="htmlEmitterSettings">The HTML emitter settings to use.</param> | ||
public CSharpToColoredHtmlFormatter(IHtmlFormatter internalFormatter, HTMLEmitterSettings htmlEmitterSettings) | ||
{ | ||
_internalFormatter = internalFormatter; | ||
_htmlEmitterSettings = htmlEmitterSettings; | ||
_csharpColorer = new CsharpColourer(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string? GetHtmlString(string sourceCode, ILanguage language) => language.Id == LanguageId.CSharp | ||
? _csharpColorer.ProcessSourceCode(sourceCode, new HTMLEmitter(_htmlEmitterSettings)) | ||
: _internalFormatter.GetHtmlString(sourceCode, language); | ||
} |
33 changes: 33 additions & 0 deletions
33
...own.ColorCode.CSharpToColoredHtml/Internal/HtmlFormatterFactoryWithCSharpToColoredHtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Markdown.ColorCode.CSharpToColoredHtml.Internal; | ||
|
||
/// <inheritdoc cref="IHtmlFormatterFactory"/> | ||
[ExcludeFromCodeCoverage] | ||
internal sealed class HtmlFormatterFactoryWithCSharpToColoredHtml : IHtmlFormatterFactory | ||
{ | ||
private readonly StyleDictionary _styleDictionary; | ||
|
||
private readonly HTMLEmitterSettings _htmlEmitterSettings; | ||
|
||
/// <summary> | ||
/// Create a new <see cref="HtmlFormatterFactoryWithCSharpToColoredHtml"/>. | ||
/// </summary> | ||
/// <param name="styleDictionary">The <see cref="StyleDictionary"/> to use with the returned <see cref="IHtmlFormatter"/>s.</param> | ||
/// <param name="htmlEmitterSettings">The <see cref="HTMLEmitterSettings"/> to use with the returned <see cref="IHtmlFormatter"/>s.</param> | ||
public HtmlFormatterFactoryWithCSharpToColoredHtml(StyleDictionary styleDictionary, HTMLEmitterSettings htmlEmitterSettings) | ||
{ | ||
_styleDictionary = styleDictionary; | ||
_htmlEmitterSettings = htmlEmitterSettings; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IHtmlFormatter Get(HtmlFormatterType htmlFormatterType) => htmlFormatterType switch | ||
{ | ||
HtmlFormatterType.Style => new HtmlStyleFormatter(_styleDictionary), | ||
HtmlFormatterType.Css => new HtmlCssFormatter(_styleDictionary), | ||
HtmlFormatterType.StyleWithCSharpToColoredHtml => new CSharpToColoredHtmlFormatter(new HtmlStyleFormatter(_styleDictionary), _htmlEmitterSettings), | ||
HtmlFormatterType.CssWithCSharpToColoredHtml => new CSharpToColoredHtmlFormatter(new HtmlCssFormatter(_styleDictionary), _htmlEmitterSettings), | ||
_ => throw new ArgumentOutOfRangeException(nameof(htmlFormatterType), htmlFormatterType, null) | ||
}; | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Markdown.ColorCode.CSharpToColoredHtml/Markdown.ColorCode.CSharpToColoredHtml.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net7.0;net8.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<GenerateDocumentationFile>True</GenerateDocumentationFile> | ||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> | ||
<Version>2.2.0</Version> | ||
<Authors>William Baldoumas</Authors> | ||
<Description>An extension for Markdig that adds syntax highlighting to code through the power of ColorCode, boosted with the CsharpToColouredHTML.Core package.</Description> | ||
<Copyright>Copyright ©2024 William Baldoumas</Copyright> | ||
<PackageProjectUrl>https://wbaldoumas.github.io/markdown-colorcode/index.html</PackageProjectUrl> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/wbaldoumas/markdown-colorcode</RepositoryUrl> | ||
<PackageTags>markdig;markdown;html;colorcode;colorize;highlight;renderer</PackageTags> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageIcon>icon.png</PackageIcon> | ||
<PackageId>Markdown.ColorCode.CSharpToColoredHtml</PackageId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<AdditionalFiles Include="..\..\stylecop.json" Link="stylecop.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\assets\images\icon.png"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<None Include="..\..\CHANGELOG.md"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<None Include="..\..\README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<None Include="..\..\LICENSE"> | ||
<Pack>True</Pack> | ||
<PackagePath></PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="ColorCode.Core" Version="2.0.15" /> | ||
<PackageReference Include="CsharpToColouredHTML.Core" Version="1.0.41" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Markdown.ColorCode\Markdown.ColorCode.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
40 changes: 40 additions & 0 deletions
40
src/Markdown.ColorCode.CSharpToColoredHtml/MarkdownPipelineBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace Markdown.ColorCode.CSharpToColoredHtml; | ||
|
||
/// <summary> | ||
/// Extensions for adding ColorCode code colorization to the Markdig <see cref="MarkdownPipelineBuilder"/>. | ||
/// </summary> | ||
public static class MarkdownPipelineBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Use ColorCode + CsharpToColouredHTML.Core to colorize HTML generated from Markdown. | ||
/// </summary> | ||
/// <param name="markdownPipelineBuilder">The <see cref="MarkdownPipelineBuilder"/> to configure.</param> | ||
/// <param name="htmlFormatterType">Optional. The type of HTML formatter to use when generating HTML from Markdown.</param> | ||
/// <param name="htmlEmitterSettings">Optional. The HTML emitter settings to use with CsharpToColouredHtml.Core.</param> | ||
/// <param name="styleDictionary">Optional. The styles to use when generating HTML from Markdown.</param> | ||
/// <param name="additionalLanguages">Optional. Additional languages used to augment the built-in languages provided by ColorCode-Universal.</param> | ||
/// <param name="defaultLanguageId">Optional. The default language to use if a given language can't be found.</param> | ||
/// <returns>The <see cref="MarkdownPipelineBuilder"/> configured with ColorCode.</returns> | ||
public static MarkdownPipelineBuilder UseColorCodeWithCSharpToColoredHtml( | ||
this MarkdownPipelineBuilder markdownPipelineBuilder, | ||
HtmlFormatterType htmlFormatterType = HtmlFormatterType.Style, | ||
HTMLEmitterSettings? htmlEmitterSettings = null, | ||
StyleDictionary? styleDictionary = null, | ||
IEnumerable<ILanguage>? additionalLanguages = null, | ||
string? defaultLanguageId = null) | ||
{ | ||
var languageExtractor = new LanguageExtractor( | ||
additionalLanguages ?? Enumerable.Empty<ILanguage>(), | ||
defaultLanguageId ?? string.Empty | ||
); | ||
|
||
var codeExtractor = new CodeExtractor(); | ||
var htmlFormatterFactory = new HtmlFormatterFactoryWithCSharpToColoredHtml(styleDictionary ?? StyleDictionary.DefaultDark, htmlEmitterSettings ?? new HTMLEmitterSettings()); | ||
var htmlFormatter = htmlFormatterFactory.Get(htmlFormatterType); | ||
var colorCodeExtension = new ColorCodeExtension(languageExtractor, codeExtractor, htmlFormatter); | ||
|
||
markdownPipelineBuilder.Extensions.Add(colorCodeExtension); | ||
|
||
return markdownPipelineBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters