Skip to content

Commit

Permalink
Fix MarkdownPipeline not being immutable by introducing a MarkdownPip…
Browse files Browse the repository at this point in the history
…elineBuilder (issue #5). Bump to version 0.2.0 (dev)
  • Loading branch information
xoofx committed May 30, 2016
1 parent dbaed0a commit 60350dc
Show file tree
Hide file tree
Showing 34 changed files with 341 additions and 167 deletions.
6 changes: 3 additions & 3 deletions src/Markdig.Tests/TestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ private static IEnumerable<MarkdownPipeline> GetPipeline(string extensionsGroupT
{
if (string.IsNullOrEmpty(extensionsGroupText))
{
yield return new MarkdownPipeline();
yield return new MarkdownPipelineBuilder().Build();
yield break;
}

var extensionGroups = extensionsGroupText.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var extensionsText in extensionGroups)
{
var pipeline = new MarkdownPipeline();
var pipeline = new MarkdownPipelineBuilder();
foreach (var extension in extensionsText.Split(new[] { '+' }, StringSplitOptions.RemoveEmptyEntries))
{
switch (extension.ToLowerInvariant())
Expand Down Expand Up @@ -113,7 +113,7 @@ private static IEnumerable<MarkdownPipeline> GetPipeline(string extensionsGroupT
}
}

yield return pipeline;
yield return pipeline.Build();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Markdig.Tests/TestPlayParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public void TestSimple()
//");

//var result = Markdown.ToHtml(text, new MarkdownPipeline().UseFootnotes().UseEmphasisExtra());
var result = Markdown.ToHtml(text, new MarkdownPipeline().UseAbbreviation());
var result = Markdown.ToHtml(text, new MarkdownPipelineBuilder().UseAbbreviation().Build());
//File.WriteAllText("test.html", result, Encoding.UTF8);
Console.WriteLine(result);
}

[Test]
public void TestSamePipelineAllExtensions()
{
var pipeline = new MarkdownPipeline().UseAllExtensions();
var pipeline = new MarkdownPipelineBuilder().UseAllExtensions().Build();

// Reuse the same pipeline
var result1 = Markdown.ToHtml("This is a \"\"citation\"\"", pipeline);
Expand Down
16 changes: 8 additions & 8 deletions src/Markdig/Extensions/Abbreviations/AbbreviationExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ namespace Markdig.Extensions.Abbreviations
/// <seealso cref="Markdig.IMarkdownExtension" />
public class AbbreviationExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
pipeline.BlockParsers.AddIfNotAlready<AbbreviationParser>();
}

var htmlRenderer = pipeline.Renderer as HtmlRenderer;
if (htmlRenderer != null)
public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null && !htmlRenderer.ObjectRenderers.Contains<HtmlAbbreviationRenderer>())
{
if (!htmlRenderer.ObjectRenderers.Contains<HtmlAbbreviationRenderer>())
{
// Must be inserted before CodeBlockRenderer
htmlRenderer.ObjectRenderers.Insert(0, new HtmlAbbreviationRenderer());
}
// Must be inserted before CodeBlockRenderer
htmlRenderer.ObjectRenderers.Insert(0, new HtmlAbbreviationRenderer());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public AutoIdentifierExtension(AutoIdentifierOptions options)
};
}

public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
var headingBlockParser = pipeline.BlockParsers.Find<HeadingBlockParser>();
if (headingBlockParser != null)
Expand All @@ -59,6 +59,10 @@ public void Setup(MarkdownPipeline pipeline)
}
}

public void Setup(IMarkdownRenderer renderer)
{
}

/// <summary>
/// Process on a new <see cref="HeadingBlock"/>
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
Expand All @@ -14,13 +15,17 @@ namespace Markdig.Extensions.Bootstrap
/// <seealso cref="Markdig.IMarkdownExtension" />
public class BootstrapExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
// Make sure we don't have a delegate twice
pipeline.DocumentProcessed -= PipelineOnDocumentProcessed;
pipeline.DocumentProcessed += PipelineOnDocumentProcessed;
}

public void Setup(IMarkdownRenderer renderer)
{
}

private static void PipelineOnDocumentProcessed(MarkdownDocument document)
{
foreach(var node in document.Descendants())
Expand Down
9 changes: 6 additions & 3 deletions src/Markdig/Extensions/Cites/CiteExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ namespace Markdig.Extensions.Cites
/// <seealso cref="Markdig.IMarkdownExtension" />
public class CiteExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
var parser = pipeline.InlineParsers.FindExact<EmphasisInlineParser>();
if (parser != null && !parser.HasEmphasisChar('"'))
{
parser.EmphasisDescriptors.Add(new EmphasisDescriptor('"', 2, 2, false));
}
}

var htmlRenderer = pipeline.Renderer as HtmlRenderer;
public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null)
{
// Extend the rendering here.
var emphasisRenderer = htmlRenderer.ObjectRenderers.FindExact<EmphasisInlineRenderer>();
var emphasisRenderer = renderer.ObjectRenderers.FindExact<EmphasisInlineRenderer>();
if (emphasisRenderer != null)
{
// TODO: Use an ordered list instead as we don't know if this specific GetTag has been already added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Markdig.Extensions.CustomContainers
/// <seealso cref="Markdig.IMarkdownExtension" />
public class CustomContainerExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
if (!pipeline.BlockParsers.Contains<CustomContainerParser>())
{
Expand All @@ -36,8 +36,11 @@ public void Setup(MarkdownPipeline pipeline)
return previousCreateEmphasisInline?.Invoke(emphasisChar, strong);
};
}
}

var htmlRenderer = pipeline.Renderer as HtmlRenderer;
public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null)
{
if (!htmlRenderer.ObjectRenderers.Contains<HtmlCustomContainerRenderer>())
Expand All @@ -51,6 +54,7 @@ public void Setup(MarkdownPipeline pipeline)
htmlRenderer.ObjectRenderers.Insert(0, new HtmlCustomContainerInlineRenderer());
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ namespace Markdig.Extensions.DefinitionLists
/// <seealso cref="Markdig.IMarkdownExtension" />
public class DefinitionListExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
if (!pipeline.BlockParsers.Contains<DefinitionListParser>())
{
// Insert the parser before any other parsers
pipeline.BlockParsers.Insert(0, new DefinitionListParser());
}
}

var htmlRenderer = pipeline.Renderer as HtmlRenderer;
public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null)
{
if (!htmlRenderer.ObjectRenderers.Contains<HtmlDefinitionListRenderer>())
Expand Down
8 changes: 7 additions & 1 deletion src/Markdig/Extensions/Emoji/EmojiExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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;

namespace Markdig.Extensions.Emoji
{
/// <summary>
Expand All @@ -10,13 +12,17 @@ namespace Markdig.Extensions.Emoji
/// <seealso cref="Markdig.IMarkdownExtension" />
public class EmojiExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
if (!pipeline.InlineParsers.Contains<EmojiParser>())
{
// Insert the parser before any other parsers
pipeline.InlineParsers.Insert(0, new EmojiParser());
}
}

public void Setup(IMarkdownRenderer renderer)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public EmphasisExtraExtension(EmphasisExtraOptions options = EmphasisExtraOption
/// </summary>
public EmphasisExtraOptions Options { get; }

public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
var parser = pipeline.InlineParsers.FindExact<EmphasisInlineParser>();
if (parser != null)
Expand Down Expand Up @@ -83,8 +83,11 @@ public void Setup(MarkdownPipeline pipeline)
parser.EmphasisDescriptors.Add(new EmphasisDescriptor('=', 2, 2, true));
}
}
}

var htmlRenderer = pipeline.Renderer as HtmlRenderer;
public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null)
{
// Extend the rendering here.
Expand Down
8 changes: 6 additions & 2 deletions src/Markdig/Extensions/Figures/FigureExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Markdig.Extensions.Figures
/// <seealso cref="Markdig.IMarkdownExtension" />
public class FigureExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
if (!pipeline.BlockParsers.Contains<FigureBlockParser>())
{
Expand All @@ -28,7 +28,11 @@ public void Setup(MarkdownPipeline pipeline)
pipeline.BlockParsers.Insert(0, new FigureBlockParser());
}
}
var htmlRenderer = pipeline.Renderer as HtmlRenderer;
}

public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null)
{
htmlRenderer.ObjectRenderers.AddIfNotAlready<HtmlFigureRenderer>();
Expand Down
7 changes: 5 additions & 2 deletions src/Markdig/Extensions/Footers/FooterExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Markdig.Extensions.Footers
/// <seealso cref="Markdig.IMarkdownExtension" />
public class FooterExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
if (!pipeline.BlockParsers.Contains<FooterBlockParser>())
{
Expand All @@ -27,8 +27,11 @@ public void Setup(MarkdownPipeline pipeline)
pipeline.BlockParsers.Insert(0, new FooterBlockParser());
}
}
}

var htmlRenderer = pipeline.Renderer as HtmlRenderer;
public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null)
{
htmlRenderer.ObjectRenderers.AddIfNotAlready(new HtmlFooterBlockRenderer());
Expand Down
7 changes: 5 additions & 2 deletions src/Markdig/Extensions/Footnotes/FootnoteExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ namespace Markdig.Extensions.Footnotes
/// <seealso cref="Markdig.IMarkdownExtension" />
public class FootnoteExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
if (!pipeline.BlockParsers.Contains<FootnoteParser>())
{
// Insert the parser before any other parsers
pipeline.BlockParsers.Insert(0, new FootnoteParser());
}
}

var htmlRenderer = pipeline.Renderer as HtmlRenderer;
public void Setup(IMarkdownRenderer renderer)
{
var htmlRenderer = renderer as HtmlRenderer;
if (htmlRenderer != null)
{
htmlRenderer.ObjectRenderers.AddIfNotAlready(new HtmlFootnoteGroupRenderer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using Markdig.Helpers;
using Markdig.Parsers;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
Expand All @@ -18,7 +19,7 @@ namespace Markdig.Extensions.GenericAttributes
/// <seealso cref="Markdig.IMarkdownExtension" />
public class GenericAttributesExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
if (!pipeline.InlineParsers.Contains<GenericAttributesParser>())
{
Expand All @@ -36,6 +37,10 @@ public void Setup(MarkdownPipeline pipeline)
}
}

public void Setup(IMarkdownRenderer renderer)
{
}

private bool TryProcessAttributesForHeading(BlockProcessor processor, ref StringSlice line, IBlock block)
{
// Try to find if there is any attributes { in the info string on the first line of a FencedCodeBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the license.txt file in the project root for more information.

using Markdig.Parsers.Inlines;
using Markdig.Renderers;

namespace Markdig.Extensions.Hardlines
{
Expand All @@ -12,7 +13,7 @@ namespace Markdig.Extensions.Hardlines
/// <seealso cref="Markdig.IMarkdownExtension" />
public class SoftlineBreakAsHardlineExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
// Simply modify the LineBreakInlineParser
// TODO: We might want more options (like pandoc)
Expand All @@ -22,5 +23,9 @@ public void Setup(MarkdownPipeline pipeline)
parser.EnableSoftAsHard = true;
}
}

public void Setup(IMarkdownRenderer renderer)
{
}
}
}
7 changes: 6 additions & 1 deletion src/Markdig/Extensions/ListExtra/ListExtraExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the license.txt file in the project root for more information.

using Markdig.Parsers;
using Markdig.Renderers;

namespace Markdig.Extensions.ListExtra
{
Expand All @@ -12,13 +13,17 @@ namespace Markdig.Extensions.ListExtra
/// <seealso cref="Markdig.IMarkdownExtension" />
public class ListExtraExtension : IMarkdownExtension
{
public void Setup(MarkdownPipeline pipeline)
public void Setup(MarkdownPipelineBuilder pipeline)
{
var parser = pipeline.BlockParsers.Find<ListBlockParser>();
if (parser != null)
{
parser.ItemParsers.AddIfNotAlready<ListExtraItemParser>();
}
}

public void Setup(IMarkdownRenderer renderer)
{
}
}
}
Loading

0 comments on commit 60350dc

Please sign in to comment.