diff --git a/src/Markdig/Markdown.cs b/src/Markdig/Markdown.cs index 9e11388c4..d09ac8011 100644 --- a/src/Markdig/Markdown.cs +++ b/src/Markdig/Markdown.cs @@ -29,11 +29,12 @@ public static partial class Markdown /// The markdown. /// The normalize options /// The pipeline. + /// A parser context used for the parsing. /// A normalized markdown text. - public static string Normalize(string markdown, NormalizeOptions options = null, MarkdownPipeline pipeline = null) + public static string Normalize(string markdown, NormalizeOptions options = null, MarkdownPipeline pipeline = null, MarkdownParserContext context = null) { var writer = new StringWriter(); - Normalize(markdown, writer, options, pipeline); + Normalize(markdown, writer, options, pipeline, context); return writer.ToString(); } @@ -44,8 +45,9 @@ public static string Normalize(string markdown, NormalizeOptions options = null, /// The destination that will receive the result of the conversion. /// The normalize options /// The pipeline. + /// A parser context used for the parsing. /// A normalized markdown text. - public static MarkdownDocument Normalize(string markdown, TextWriter writer, NormalizeOptions options = null, MarkdownPipeline pipeline = null) + public static MarkdownDocument Normalize(string markdown, TextWriter writer, NormalizeOptions options = null, MarkdownPipeline pipeline = null, MarkdownParserContext context = null) { pipeline = pipeline ?? new MarkdownPipelineBuilder().Build(); pipeline = CheckForSelfPipeline(pipeline, markdown); @@ -54,7 +56,7 @@ public static MarkdownDocument Normalize(string markdown, TextWriter writer, Nor var renderer = new NormalizeRenderer(writer, options); pipeline.Setup(renderer); - var document = Parse(markdown, pipeline); + var document = Parse(markdown, pipeline, context); renderer.Render(document); writer.Flush(); @@ -82,9 +84,10 @@ public static string ToHtml(string markdown, MarkdownPipeline pipeline = null) /// A Markdown text. /// The destination that will receive the result of the conversion. /// The pipeline used for the conversion. + /// A parser context used for the parsing. /// The Markdown document that has been parsed /// if reader or writer variable are null - public static MarkdownDocument ToHtml(string markdown, TextWriter writer, MarkdownPipeline pipeline = null) + public static MarkdownDocument ToHtml(string markdown, TextWriter writer, MarkdownPipeline pipeline = null, MarkdownParserContext context = null) { if (markdown == null) throw new ArgumentNullException(nameof(markdown)); if (writer == null) throw new ArgumentNullException(nameof(writer)); @@ -95,7 +98,7 @@ public static MarkdownDocument ToHtml(string markdown, TextWriter writer, Markdo var renderer = new HtmlRenderer(writer); pipeline.Setup(renderer); - var document = Parse(markdown, pipeline); + var document = Parse(markdown, pipeline, context); renderer.Render(document); writer.Flush(); @@ -108,15 +111,16 @@ public static MarkdownDocument ToHtml(string markdown, TextWriter writer, Markdo /// A Markdown text. /// The renderer to convert Markdown to. /// The pipeline used for the conversion. + /// A parser context used for the parsing. /// if markdown or writer variable are null - public static object Convert(string markdown, IMarkdownRenderer renderer, MarkdownPipeline pipeline = null) + public static object Convert(string markdown, IMarkdownRenderer renderer, MarkdownPipeline pipeline = null, MarkdownParserContext context = null) { if (markdown == null) throw new ArgumentNullException(nameof(markdown)); if (renderer == null) throw new ArgumentNullException(nameof(renderer)); pipeline = pipeline ?? new MarkdownPipelineBuilder().Build(); pipeline = CheckForSelfPipeline(pipeline, markdown); - var document = Parse(markdown, pipeline); + var document = Parse(markdown, pipeline, context); pipeline.Setup(renderer); return renderer.Render(document); } @@ -138,15 +142,16 @@ public static MarkdownDocument Parse(string markdown) /// /// The markdown text. /// The pipeline used for the parsing. + /// A parser context used for the parsing. /// An AST Markdown document /// if markdown variable is null - public static MarkdownDocument Parse(string markdown, MarkdownPipeline pipeline) + public static MarkdownDocument Parse(string markdown, MarkdownPipeline pipeline, MarkdownParserContext context = null) { if (markdown == null) throw new ArgumentNullException(nameof(markdown)); pipeline = pipeline ?? new MarkdownPipelineBuilder().Build(); pipeline = CheckForSelfPipeline(pipeline, markdown); - return MarkdownParser.Parse(markdown, pipeline); + return MarkdownParser.Parse(markdown, pipeline, context); } private static MarkdownPipeline CheckForSelfPipeline(MarkdownPipeline pipeline, string markdown) @@ -165,9 +170,10 @@ private static MarkdownPipeline CheckForSelfPipeline(MarkdownPipeline pipeline, /// A Markdown text. /// The destination that will receive the result of the conversion. /// The pipeline used for the conversion. + /// A parser context used for the parsing. /// The Markdown document that has been parsed /// if reader or writer variable are null - public static MarkdownDocument ToPlainText(string markdown, TextWriter writer, MarkdownPipeline pipeline = null) + public static MarkdownDocument ToPlainText(string markdown, TextWriter writer, MarkdownPipeline pipeline = null, MarkdownParserContext context = null) { if (markdown == null) throw new ArgumentNullException(nameof(markdown)); if (writer == null) throw new ArgumentNullException(nameof(writer)); @@ -182,7 +188,7 @@ public static MarkdownDocument ToPlainText(string markdown, TextWriter writer, M }; pipeline.Setup(renderer); - var document = Parse(markdown, pipeline); + var document = Parse(markdown, pipeline, context); renderer.Render(document); writer.Flush(); @@ -194,13 +200,14 @@ public static MarkdownDocument ToPlainText(string markdown, TextWriter writer, M /// /// A Markdown text. /// The pipeline used for the conversion. + /// A parser context used for the parsing. /// The result of the conversion /// if markdown variable is null - public static string ToPlainText(string markdown, MarkdownPipeline pipeline = null) + public static string ToPlainText(string markdown, MarkdownPipeline pipeline = null, MarkdownParserContext context = null) { if (markdown == null) throw new ArgumentNullException(nameof(markdown)); var writer = new StringWriter(); - ToPlainText(markdown, writer, pipeline); + ToPlainText(markdown, writer, pipeline, context); return writer.ToString(); } } diff --git a/src/Markdig/MarkdownParserContext.cs b/src/Markdig/MarkdownParserContext.cs new file mode 100644 index 000000000..aa326ee17 --- /dev/null +++ b/src/Markdig/MarkdownParserContext.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; + +namespace Markdig +{ + /// + /// Provides a context that can be used as part of parsing Markdown documents. + /// + public sealed class MarkdownParserContext + { + /// + /// Gets or sets the context property collection. + /// + public Dictionary Properties { get; } + + /// + /// Initializes a new instance of the class. + /// + public MarkdownParserContext() + { + Properties = new Dictionary(); + } + } +} diff --git a/src/Markdig/Parsers/BlockProcessor.cs b/src/Markdig/Parsers/BlockProcessor.cs index efea3f430..d8d94ab7f 100644 --- a/src/Markdig/Parsers/BlockProcessor.cs +++ b/src/Markdig/Parsers/BlockProcessor.cs @@ -41,7 +41,7 @@ private BlockProcessor(BlockProcessor root) /// The list of parsers. /// /// - public BlockProcessor(StringBuilderCache stringBuilders, MarkdownDocument document, BlockParserList parsers) + public BlockProcessor(StringBuilderCache stringBuilders, MarkdownDocument document, BlockParserList parsers, MarkdownParserContext context) { if (stringBuilders == null) throw new ArgumentNullException(nameof(stringBuilders)); if (document == null) throw new ArgumentNullException(nameof(document)); @@ -51,6 +51,7 @@ public BlockProcessor(StringBuilderCache stringBuilders, MarkdownDocument docume Document = document; document.IsOpen = true; Parsers = parsers; + Context = context; OpenedBlocks = new List(); NewBlocks = new Stack(); root = this; @@ -67,6 +68,11 @@ public BlockProcessor(StringBuilderCache stringBuilders, MarkdownDocument docume /// public BlockParserList Parsers { get; } + /// + /// Gets the parser context or null if none is available. + /// + public MarkdownParserContext Context { get; } + /// /// Gets the current active container. /// diff --git a/src/Markdig/Parsers/InlineProcessor.cs b/src/Markdig/Parsers/InlineProcessor.cs index 945ebdaf9..d61f69396 100644 --- a/src/Markdig/Parsers/InlineProcessor.cs +++ b/src/Markdig/Parsers/InlineProcessor.cs @@ -36,7 +36,7 @@ public class InlineProcessor /// The inline created event. /// /// - public InlineProcessor(StringBuilderCache stringBuilders, MarkdownDocument document, InlineParserList parsers, bool preciseSourcelocation) + public InlineProcessor(StringBuilderCache stringBuilders, MarkdownDocument document, InlineParserList parsers, bool preciseSourcelocation, MarkdownParserContext context) { if (stringBuilders == null) throw new ArgumentNullException(nameof(stringBuilders)); if (document == null) throw new ArgumentNullException(nameof(document)); @@ -44,6 +44,7 @@ public InlineProcessor(StringBuilderCache stringBuilders, MarkdownDocument docum StringBuilders = stringBuilders; Document = document; Parsers = parsers; + Context = context; PreciseSourceLocation = preciseSourcelocation; lineOffsets = new List(); ParserStates = new object[Parsers.Count]; @@ -80,6 +81,11 @@ public InlineProcessor(StringBuilderCache stringBuilders, MarkdownDocument docum /// public InlineParserList Parsers { get; } + /// + /// Gets the parser context or null if none is available. + /// + public MarkdownParserContext Context { get; } + /// /// Gets the root document. /// diff --git a/src/Markdig/Parsers/MarkdownParser.cs b/src/Markdig/Parsers/MarkdownParser.cs index f3f16f9d5..7ad8c4120 100644 --- a/src/Markdig/Parsers/MarkdownParser.cs +++ b/src/Markdig/Parsers/MarkdownParser.cs @@ -35,9 +35,10 @@ public sealed class MarkdownParser /// /// The reader. /// The pipeline. + /// A parser context used for the parsing. /// /// - private MarkdownParser(string text, MarkdownPipeline pipeline) + private MarkdownParser(string text, MarkdownPipeline pipeline, MarkdownParserContext context) { if (text == null) throw new ArgumentNullException(nameof(text)); if (pipeline == null) throw new ArgumentNullException(nameof(pipeline)); @@ -51,10 +52,10 @@ private MarkdownParser(string text, MarkdownPipeline pipeline) document = new MarkdownDocument(); // Initialize the block parsers - blockProcessor = new BlockProcessor(stringBuilderCache, document, pipeline.BlockParsers); + blockProcessor = new BlockProcessor(stringBuilderCache, document, pipeline.BlockParsers, context); // Initialize the inline parsers - inlineProcessor = new InlineProcessor(stringBuilderCache, document, pipeline.InlineParsers, pipeline.PreciseSourceLocation) + inlineProcessor = new InlineProcessor(stringBuilderCache, document, pipeline.InlineParsers, pipeline.PreciseSourceLocation, context) { DebugLog = pipeline.DebugLog }; @@ -67,15 +68,16 @@ private MarkdownParser(string text, MarkdownPipeline pipeline) /// /// A Markdown text /// The pipeline used for the parsing. + /// A parser context used for the parsing. /// An AST Markdown document /// if reader variable is null - public static MarkdownDocument Parse(string text, MarkdownPipeline pipeline = null) + public static MarkdownDocument Parse(string text, MarkdownPipeline pipeline = null, MarkdownParserContext context = null) { if (text == null) throw new ArgumentNullException(nameof(text)); pipeline = pipeline ?? new MarkdownPipelineBuilder().Build(); // Perform the parsing - var markdownParser = new MarkdownParser(text, pipeline); + var markdownParser = new MarkdownParser(text, pipeline, context); return markdownParser.Parse(); }