Add support for diagrams extension. wip with mermaid (issue #34)

This commit is contained in:
Alexandre Mutel
2016-07-05 23:24:37 +09:00
parent 4a146f00f6
commit c6c2f58ec0
9 changed files with 3397 additions and 3262 deletions

View File

@@ -86,6 +86,7 @@
<None Include="Specs\GridTableSpecs.md" />
<None Include="Specs\HardlineBreakSpecs.md" />
<None Include="Specs\BootstrapSpecs.md" />
<None Include="Specs\DiagramsSpecs.md" />
<None Include="Specs\TaskListSpecs.md" />
<None Include="Specs\SmartyPantsSpecs.md" />
<None Include="Specs\MediaSpecs.md" />

View File

@@ -0,0 +1,26 @@
# Extensions
Adds support for diagrams extension:
## Mermaid diagrams
Using a fenced code block with the `mermaid` language info will output a `<div class='mermaid'>` instead of a `pre/code` block:
```````````````````````````````` example
```mermaid
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
```
.
<div class="mermaid">graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
</div>
````````````````````````````````
TODO: Add other text diagram languages

File diff suppressed because it is too large Load Diff

View File

@@ -57,6 +57,7 @@ SOFTWARE.
new KeyValuePair<string, string>(Host.ResolvePath("SmartyPantsSpecs.md"), "pipetables+smartypants|advanced+smartypants"), // Check with smartypants to make sure that it doesn't break pipetables
new KeyValuePair<string, string>(Host.ResolvePath("AutoIdentifierSpecs.md"), "autoidentifiers|advanced"),
new KeyValuePair<string, string>(Host.ResolvePath("TaskListSpecs.md"), "tasklists|advanced"),
new KeyValuePair<string, string>(Host.ResolvePath("DiagramsSpecs.md"), "diagrams|advanced"),
};
var emptyLines = false;
var displayEmptyLines = false;

View File

@@ -0,0 +1,31 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using Markdig.Renderers;
using Markdig.Renderers.Html;
namespace Markdig.Extensions.Diagrams
{
/// <summary>
/// Extension to allow diagrams.
/// </summary>
/// <seealso cref="Markdig.IMarkdownExtension" />
public class DiagramExtension : IMarkdownExtension
{
public void Setup(MarkdownPipelineBuilder pipeline)
{
}
public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null)
{
var codeRenderer = htmlRenderer.ObjectRenderers.FindExact<CodeBlockRenderer>();
// TODO: Add other well known diagram languages
codeRenderer.BlocksAsDiv.Add("mermaid");
}
}
}
}

View File

@@ -9,6 +9,7 @@ using Markdig.Extensions.Bootstrap;
using Markdig.Extensions.Citations;
using Markdig.Extensions.CustomContainers;
using Markdig.Extensions.DefinitionLists;
using Markdig.Extensions.Diagrams;
using Markdig.Extensions.Emoji;
using Markdig.Extensions.EmphasisExtras;
using Markdig.Extensions.Figures;
@@ -57,6 +58,7 @@ namespace Markdig
.UsePipeTables()
.UseListExtras()
.UseTaskLists()
.UseDiagrams()
.UseGenericAttributes(); // Must be last as it is one parser that is modifying other parsers
}
@@ -89,6 +91,17 @@ namespace Markdig
return pipeline;
}
/// <summary>
/// Uses the diagrams extension
/// </summary>
/// <param name="pipeline">The pipeline.</param>
/// <returns>The modified pipeline</returns>
public static MarkdownPipelineBuilder UseDiagrams(this MarkdownPipelineBuilder pipeline)
{
pipeline.Extensions.AddIfNotAlready<DiagramExtension>();
return pipeline;
}
/// <summary>
/// Uses precise source code location (useful for syntax highlighting).
/// </summary>
@@ -456,6 +469,9 @@ namespace Markdig
case "tasklists":
pipeline.UseTaskLists();
break;
case "diagrams":
pipeline.UseDiagrams();
break;
default:
throw new ArgumentException($"Invalid extension `{extension}` from `{extensions}`", nameof(extensions));
}

View File

@@ -12,13 +12,15 @@ namespace Markdig.Parsers
/// <seealso cref="Markdig.Parsers.BlockParser" />
public class FencedCodeBlockParser : FencedBlockParserBase<FencedCodeBlock>
{
public const string DefaultInfoPrefix = "language-";
/// <summary>
/// Initializes a new instance of the <see cref="FencedCodeBlockParser"/> class.
/// </summary>
public FencedCodeBlockParser()
{
OpeningCharacters = new[] {'`', '~'};
InfoPrefix = "language-";
InfoPrefix = DefaultInfoPrefix;
}
protected override FencedCodeBlock CreateFencedBlock(BlockProcessor processor)

View File

@@ -1,6 +1,10 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System;
using System.Collections.Generic;
using Markdig.Parsers;
using Markdig.Syntax;
namespace Markdig.Renderers.Html
@@ -11,24 +15,57 @@ namespace Markdig.Renderers.Html
/// <seealso cref="Markdig.Renderers.Html.HtmlObjectRenderer{Markdig.Syntax.CodeBlock}" />
public class CodeBlockRenderer : HtmlObjectRenderer<CodeBlock>
{
/// <summary>
/// Initializes a new instance of the <see cref="CodeBlockRenderer"/> class.
/// </summary>
public CodeBlockRenderer()
{
BlocksAsDiv = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}
public bool OutputAttributesOnPre { get; set; }
/// <summary>
/// Gets a map of fenced code block infos that should be rendered as div blocks instead of pre/code blocks.
/// </summary>
public HashSet<string> BlocksAsDiv { get; }
protected override void Write(HtmlRenderer renderer, CodeBlock obj)
{
renderer.EnsureLine();
renderer.Write("<pre");
if (OutputAttributesOnPre)
var fencedCodeBlock = obj as FencedCodeBlock;
if (fencedCodeBlock?.Info != null && BlocksAsDiv.Contains(fencedCodeBlock.Info))
{
renderer.WriteAttributes(obj);
var infoPrefix = (obj.Parser as FencedCodeBlockParser)?.InfoPrefix ??
FencedCodeBlockParser.DefaultInfoPrefix;
// We are replacing the HTML attribute `language-mylang` by `mylang` only for a div block
// NOTE that we are allocating a closure here
renderer.Write("<div")
.WriteAttributes(obj.TryGetAttributes(),
cls => cls.StartsWith(infoPrefix) ? cls.Substring(infoPrefix.Length) : cls)
.Write(">");
renderer.WriteLeafRawLines(obj, true, true, true);
renderer.WriteLine("</div>");
}
renderer.Write("><code");
if (!OutputAttributesOnPre)
else
{
renderer.WriteAttributes(obj);
renderer.Write("<pre");
if (OutputAttributesOnPre)
{
renderer.WriteAttributes(obj);
}
renderer.Write("><code");
if (!OutputAttributesOnPre)
{
renderer.WriteAttributes(obj);
}
renderer.Write(">");
renderer.WriteLeafRawLines(obj, true, true);
renderer.WriteLine("</code></pre>");
}
renderer.Write(">");
renderer.WriteLeafRawLines(obj, true, true);
renderer.WriteLine("</code></pre>");
}
}
}

View File

@@ -83,26 +83,28 @@ namespace Markdig.Renderers
/// Writes the content escaped for HTML.
/// </summary>
/// <param name="slice">The slice.</param>
/// <param name="softEscape">Only escape &lt; and &amp;</param>
/// <returns>This instance</returns>
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public HtmlRenderer WriteEscape(ref StringSlice slice)
public HtmlRenderer WriteEscape(ref StringSlice slice, bool softEscape = false)
{
if (slice.Start > slice.End)
{
return this;
}
return WriteEscape(slice.Text, slice.Start, slice.Length);
return WriteEscape(slice.Text, slice.Start, slice.Length, softEscape);
}
/// <summary>
/// Writes the content escaped for HTML.
/// </summary>
/// <param name="slice">The slice.</param>
/// <param name="softEscape">Only escape &lt; and &amp;</param>
/// <returns>This instance</returns>
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public HtmlRenderer WriteEscape(StringSlice slice)
public HtmlRenderer WriteEscape(StringSlice slice, bool softEscape = false)
{
return WriteEscape(ref slice);
return WriteEscape(ref slice, softEscape);
}
/// <summary>
@@ -111,8 +113,9 @@ namespace Markdig.Renderers
/// <param name="content">The content.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <param name="softEscape">Only escape &lt; and &amp;</param>
/// <returns>This instance</returns>
public HtmlRenderer WriteEscape(string content, int offset, int length)
public HtmlRenderer WriteEscape(string content, int offset, int length, bool softEscape = false)
{
if (string.IsNullOrEmpty(content) || length == 0)
return this;
@@ -132,12 +135,15 @@ namespace Markdig.Renderers
previousOffset = offset + 1;
break;
case '>':
Write(content, previousOffset, offset - previousOffset);
if (EnableHtmlEscape)
if (!softEscape)
{
Write("&gt;");
Write(content, previousOffset, offset - previousOffset);
if (EnableHtmlEscape)
{
Write("&gt;");
}
previousOffset = offset + 1;
}
previousOffset = offset + 1;
break;
case '&':
Write(content, previousOffset, offset - previousOffset);
@@ -148,12 +154,15 @@ namespace Markdig.Renderers
previousOffset = offset + 1;
break;
case '"':
Write(content, previousOffset, offset - previousOffset);
if (EnableHtmlEscape)
if (!softEscape)
{
Write("&quot;");
Write(content, previousOffset, offset - previousOffset);
if (EnableHtmlEscape)
{
Write("&quot;");
}
previousOffset = offset + 1;
}
previousOffset = offset + 1;
break;
}
}
@@ -233,8 +242,9 @@ namespace Markdig.Renderers
/// Writes the specified <see cref="HtmlAttributes"/>.
/// </summary>
/// <param name="attributes">The attributes to render.</param>
/// <param name="classFilter">A class filter used to transform a class into another class at writing time</param>
/// <returns>This instance</returns>
public HtmlRenderer WriteAttributes(HtmlAttributes attributes)
public HtmlRenderer WriteAttributes(HtmlAttributes attributes, Func<string, string> classFilter = null)
{
if (attributes == null)
{
@@ -256,7 +266,7 @@ namespace Markdig.Renderers
{
Write(" ");
}
WriteEscape(cssClass);
WriteEscape(classFilter != null ? classFilter(cssClass) : cssClass);
}
Write("\"");
}
@@ -284,8 +294,9 @@ namespace Markdig.Renderers
/// <param name="leafBlock">The leaf block.</param>
/// <param name="writeEndOfLines">if set to <c>true</c> write end of lines.</param>
/// <param name="escape">if set to <c>true</c> escape the content for HTML</param>
/// <param name="softEscape">Only escape &lt; and &amp;</param>
/// <returns>This instance</returns>
public HtmlRenderer WriteLeafRawLines(LeafBlock leafBlock, bool writeEndOfLines, bool escape)
public HtmlRenderer WriteLeafRawLines(LeafBlock leafBlock, bool writeEndOfLines, bool escape, bool softEscape = false)
{
if (leafBlock == null) throw new ArgumentNullException(nameof(leafBlock));
if (leafBlock.Lines.Lines != null)
@@ -300,7 +311,7 @@ namespace Markdig.Renderers
}
if (escape)
{
WriteEscape(ref slices[i].Slice);
WriteEscape(ref slices[i].Slice, softEscape);
}
else
{