-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added extensions configuration point and PlantUmlExtension
- Loading branch information
Showing
20 changed files
with
406 additions
and
22 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
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
41 changes: 41 additions & 0 deletions
41
src/Docfx.MarkdigEngine.Extensions/PlantUml/FormatterFactory.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,41 @@ | ||
using PlantUml.Net; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
internal class FormatterFactory | ||
{ | ||
private readonly DocfxPlantUmlSettings settings; | ||
|
||
private OutputFormat OutputFormat => settings.OutputFormat; | ||
|
||
public FormatterFactory(DocfxPlantUmlSettings settings) | ||
{ | ||
this.settings = settings; | ||
} | ||
|
||
internal IOutputFormatter CreateOutputFormatter() | ||
{ | ||
switch (OutputFormat) | ||
{ | ||
case OutputFormat.Svg: | ||
return new SvgOutputFormatter(); | ||
|
||
case OutputFormat.Ascii: | ||
return new AsciiOutputFormatter(); | ||
|
||
case OutputFormat.Ascii_Unicode: | ||
return new AsciiUnicodeOutputFormatter(); | ||
|
||
case OutputFormat.Png: | ||
case OutputFormat.Eps: | ||
case OutputFormat.Pdf: | ||
case OutputFormat.Vdx: | ||
case OutputFormat.Xmi: | ||
case OutputFormat.Scxml: | ||
case OutputFormat.Html: | ||
case OutputFormat.LaTeX: | ||
default: | ||
throw new NotSupportedException($"output format {OutputFormat} is not currently supported"); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Docfx.MarkdigEngine.Extensions/PlantUml/IOutputFormatter.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,6 @@ | ||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
internal interface IOutputFormatter | ||
{ | ||
string FormatOutput(byte[] output); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Docfx.MarkdigEngine.Extensions/PlantUml/OutputFormatters/AsciiOutputFormatter.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,18 @@ | ||
|
||
using static System.Text.Encoding; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
internal class AsciiOutputFormatter : IOutputFormatter | ||
{ | ||
public AsciiOutputFormatter() | ||
{ | ||
} | ||
|
||
public string FormatOutput(byte[] output) | ||
{ | ||
string ascii = ASCII.GetString(output); | ||
return $"<div class=\"lang-plantUml\"><pre>{ascii}</pre></div>"; | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
src/Docfx.MarkdigEngine.Extensions/PlantUml/OutputFormatters/AsciiUnicodeOutputFormatter.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,17 @@ | ||
|
||
using static System.Text.Encoding; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
internal class AsciiUnicodeOutputFormatter : IOutputFormatter | ||
{ | ||
public AsciiUnicodeOutputFormatter() | ||
{ | ||
} | ||
|
||
public string FormatOutput(byte[] output) | ||
{ | ||
string ascii = UTF8.GetString(output); | ||
return $"<div class=\"lang-plantUml\"><pre>{ascii}</pre></div>"; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Docfx.MarkdigEngine.Extensions/PlantUml/OutputFormatters/SvgOutputFormatter.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,16 @@ | ||
using static System.Text.Encoding; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
internal class SvgOutputFormatter : IOutputFormatter | ||
{ | ||
public SvgOutputFormatter() | ||
{ | ||
} | ||
|
||
public string FormatOutput(byte[] output) | ||
{ | ||
string svg = UTF8.GetString(output); | ||
return $"<div class=\"lang-plantUml\">{svg}</div>"; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Docfx.MarkdigEngine.Extensions/PlantUml/PlantUmlCodeBlockRenderer.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,57 @@ | ||
using Markdig.Renderers; | ||
using Markdig.Syntax; | ||
using Markdig.Renderers.Html; | ||
using PlantUml.Net; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
/// <summary> | ||
/// An HTML renderer for a <see cref="CodeBlock"/> and <see cref="FencedCodeBlock"/>. | ||
/// </summary> | ||
/// <seealso cref="HtmlObjectRenderer{CodeBlock}" /> | ||
public class CustomCodeBlockRenderer : CodeBlockRenderer | ||
{ | ||
private readonly MarkdownContext _context; | ||
private readonly DocfxPlantUmlSettings _settings; | ||
private readonly RendererFactory rendererFactory; | ||
private readonly FormatterFactory formatterFactory; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CodeBlockRenderer"/> class. | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="settings"></param> | ||
public CustomCodeBlockRenderer(MarkdownContext context, DocfxPlantUmlSettings settings) | ||
{ | ||
_context = context; | ||
_settings = settings; | ||
|
||
rendererFactory = new RendererFactory(); | ||
formatterFactory = new FormatterFactory(settings); | ||
} | ||
|
||
protected override void Write(HtmlRenderer renderer, CodeBlock obj) | ||
{ | ||
if (obj is FencedCodeBlock fencedCodeBlock | ||
&& fencedCodeBlock.Info is string info | ||
&& info.Equals("plantuml", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
IPlantUmlRenderer plantUmlRenderer = rendererFactory.CreateRenderer(_settings); | ||
IOutputFormatter outputFormatter = formatterFactory.CreateOutputFormatter(); | ||
|
||
// Get PlantUML code. | ||
var plantUmlCode = fencedCodeBlock.Lines.ToString(); | ||
|
||
byte[] output = plantUmlRenderer.Render(plantUmlCode, _settings.OutputFormat); | ||
|
||
renderer.EnsureLine(); | ||
renderer.Write(outputFormatter.FormatOutput(output)); | ||
renderer.EnsureLine(); | ||
|
||
return; | ||
} | ||
|
||
// Fallback to default CodeBlockRenderer | ||
base.Write(renderer, obj); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/Docfx.MarkdigEngine.Extensions/PlantUml/PlantUmlExtension.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,69 @@ | ||
using Markdig; | ||
using Markdig.Renderers; | ||
using Markdig.Renderers.Html; | ||
using PlantUml.Net; | ||
|
||
namespace Docfx.MarkdigEngine.Extensions; | ||
|
||
public class DocfxPlantUmlSettings : PlantUmlSettings | ||
{ | ||
public DocfxPlantUmlSettings() : base() | ||
{ | ||
} | ||
|
||
public DocfxPlantUmlSettings(IReadOnlyDictionary<string, string> config) : this() | ||
{ | ||
if (config.TryGetValue("remoteUrl", out var url)) | ||
RemoteUrl = url; | ||
if (config.TryGetValue("outputFormat", out var format)) | ||
OutputFormat = Enum.Parse<OutputFormat>(format, true); | ||
if (config.TryGetValue("javaPath", out var path)) | ||
JavaPath = path; | ||
if (config.TryGetValue("localPlantUmlPath", out path)) | ||
LocalPlantUmlPath = path; | ||
if (config.TryGetValue("localGraphvizDotPath", out path)) | ||
LocalGraphvizDotPath = path; | ||
if (config.TryGetValue("renderingMode", out var renderMode)) | ||
RenderingMode = Enum.Parse<RenderingMode>(renderMode, true); | ||
} | ||
|
||
public OutputFormat OutputFormat { get; set; } = OutputFormat.Svg; | ||
} | ||
|
||
internal class PlantUmlExtension : IMarkdownExtension | ||
{ | ||
private readonly MarkdownContext _context; | ||
private readonly DocfxPlantUmlSettings _settings; | ||
|
||
public PlantUmlExtension(MarkdownContext context) | ||
{ | ||
_context = context; | ||
_settings = new(); | ||
|
||
var config = _context.GetExtensionConfiguration("PlantUml"); | ||
if (config != null) | ||
_settings = new DocfxPlantUmlSettings(config); | ||
} | ||
|
||
public void Setup(MarkdownPipelineBuilder pipeline) | ||
{ | ||
} | ||
|
||
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) | ||
{ | ||
if (renderer is HtmlRenderer { ObjectRenderers: not null } htmlRenderer) | ||
{ | ||
var customRenderer = new CustomCodeBlockRenderer(_context, _settings); | ||
var renderers = htmlRenderer.ObjectRenderers; | ||
|
||
if (renderers.Contains<CodeBlockRenderer>()) | ||
{ | ||
renderers.InsertBefore<CodeBlockRenderer>(customRenderer); | ||
} | ||
else | ||
{ | ||
renderers.AddIfNotAlready(customRenderer); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.