Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds markdown parser context #285

Merged
merged 2 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/Markdig/Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public static partial class Markdown
/// <param name="markdown">The markdown.</param>
/// <param name="options">The normalize options</param>
/// <param name="pipeline">The pipeline.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <returns>A normalized markdown text.</returns>
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();
}

Expand All @@ -44,8 +45,9 @@ public static string Normalize(string markdown, NormalizeOptions options = null,
/// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param>
/// <param name="options">The normalize options</param>
/// <param name="pipeline">The pipeline.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <returns>A normalized markdown text.</returns>
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);
Expand All @@ -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();

Expand Down Expand Up @@ -82,9 +84,10 @@ public static string ToHtml(string markdown, MarkdownPipeline pipeline = null)
/// <param name="markdown">A Markdown text.</param>
/// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param>
/// <param name="pipeline">The pipeline used for the conversion.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <returns>The Markdown document that has been parsed</returns>
/// <exception cref="System.ArgumentNullException">if reader or writer variable are null</exception>
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));
Expand All @@ -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();

Expand All @@ -108,15 +111,16 @@ public static MarkdownDocument ToHtml(string markdown, TextWriter writer, Markdo
/// <param name="markdown">A Markdown text.</param>
/// <param name="renderer">The renderer to convert Markdown to.</param>
/// <param name="pipeline">The pipeline used for the conversion.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <exception cref="System.ArgumentNullException">if markdown or writer variable are null</exception>
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);
}
Expand All @@ -138,15 +142,16 @@ public static MarkdownDocument Parse(string markdown)
/// </summary>
/// <param name="markdown">The markdown text.</param>
/// <param name="pipeline">The pipeline used for the parsing.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <returns>An AST Markdown document</returns>
/// <exception cref="System.ArgumentNullException">if markdown variable is null</exception>
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)
Expand All @@ -165,9 +170,10 @@ private static MarkdownPipeline CheckForSelfPipeline(MarkdownPipeline pipeline,
/// <param name="markdown">A Markdown text.</param>
/// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param>
/// <param name="pipeline">The pipeline used for the conversion.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <returns>The Markdown document that has been parsed</returns>
/// <exception cref="System.ArgumentNullException">if reader or writer variable are null</exception>
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));
Expand All @@ -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();

Expand All @@ -194,13 +200,14 @@ public static MarkdownDocument ToPlainText(string markdown, TextWriter writer, M
/// </summary>
/// <param name="markdown">A Markdown text.</param>
/// <param name="pipeline">The pipeline used for the conversion.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <returns>The result of the conversion</returns>
/// <exception cref="System.ArgumentNullException">if markdown variable is null</exception>
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();
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/Markdig/MarkdownParserContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;

namespace Markdig
{
/// <summary>
/// Provides a context that can be used as part of parsing Markdown documents.
/// </summary>
public sealed class MarkdownParserContext
{
/// <summary>
/// Gets or sets the context property collection.
/// </summary>
public IDictionary<object, object> Properties { get; set; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can expose only a Dictionary<object, object>, not a IDictionary and remove also the setter.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, forgot that I can do that right away from github, so simple 😅


/// <summary>
/// Initializes a new instance of the <see cref="MarkdownParserContext" /> class.
/// </summary>
public MarkdownParserContext()
{
Properties = new Dictionary<object, object>();
}
}
}
8 changes: 7 additions & 1 deletion src/Markdig/Parsers/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private BlockProcessor(BlockProcessor root)
/// <param name="parsers">The list of parsers.</param>
/// <exception cref="System.ArgumentNullException">
/// </exception>
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));
Expand All @@ -51,6 +51,7 @@ public BlockProcessor(StringBuilderCache stringBuilders, MarkdownDocument docume
Document = document;
document.IsOpen = true;
Parsers = parsers;
Context = context;
OpenedBlocks = new List<Block>();
NewBlocks = new Stack<Block>();
root = this;
Expand All @@ -67,6 +68,11 @@ public BlockProcessor(StringBuilderCache stringBuilders, MarkdownDocument docume
/// </summary>
public BlockParserList Parsers { get; }

/// <summary>
/// Gets the parser context or <c>null</c> if none is available.
/// </summary>
public MarkdownParserContext Context { get; }

/// <summary>
/// Gets the current active container.
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion src/Markdig/Parsers/InlineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ public class InlineProcessor
/// <param name="inlineCreated">The inline created event.</param>
/// <exception cref="System.ArgumentNullException">
/// </exception>
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));
if (parsers == null) throw new ArgumentNullException(nameof(parsers));
StringBuilders = stringBuilders;
Document = document;
Parsers = parsers;
Context = context;
PreciseSourceLocation = preciseSourcelocation;
lineOffsets = new List<StringLineGroup.LineOffset>();
ParserStates = new object[Parsers.Count];
Expand Down Expand Up @@ -80,6 +81,11 @@ public InlineProcessor(StringBuilderCache stringBuilders, MarkdownDocument docum
/// </summary>
public InlineParserList Parsers { get; }

/// <summary>
/// Gets the parser context or <c>null</c> if none is available.
/// </summary>
public MarkdownParserContext Context { get; }

/// <summary>
/// Gets the root document.
/// </summary>
Expand Down
12 changes: 7 additions & 5 deletions src/Markdig/Parsers/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public sealed class MarkdownParser
/// </summary>
/// <param name="text">The reader.</param>
/// <param name="pipeline">The pipeline.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <exception cref="System.ArgumentNullException">
/// </exception>
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));
Expand All @@ -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
};
Expand All @@ -67,15 +68,16 @@ private MarkdownParser(string text, MarkdownPipeline pipeline)
/// </summary>
/// <param name="text">A Markdown text</param>
/// <param name="pipeline">The pipeline used for the parsing.</param>
/// <param name="context">A parser context used for the parsing.</param>
/// <returns>An AST Markdown document</returns>
/// <exception cref="System.ArgumentNullException">if reader variable is null</exception>
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();
}

Expand Down