15 | {
16 | protected override void Write(HtmlRenderer renderer, MathBlock obj)
17 | {
18 | renderer.EnsureLine();
19 | if (renderer.EnableHtmlForBlock)
20 | {
21 | renderer.Write("");
22 | renderer.WriteLine("\\[");
23 | }
24 |
25 | renderer.WriteLeafRawLines(obj, true, renderer.EnableHtmlEscape);
26 |
27 | if (renderer.EnableHtmlForBlock)
28 | {
29 | renderer.Write("\\]");
30 | renderer.WriteLine("
");
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Mathematics/HtmlMathInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 | using Markdig.Renderers.Html;
7 |
8 | namespace Markdig.Extensions.Mathematics;
9 |
10 | ///
11 | /// A HTML renderer for a .
12 | ///
13 | ///
14 | public class HtmlMathInlineRenderer : HtmlObjectRenderer
15 | {
16 | protected override void Write(HtmlRenderer renderer, MathInline obj)
17 | {
18 | if (renderer.EnableHtmlForInline)
19 | {
20 | renderer.Write("\\(");
21 | }
22 |
23 | if (renderer.EnableHtmlEscape)
24 | {
25 | renderer.WriteEscape(ref obj.Content);
26 | }
27 | else
28 | {
29 | renderer.Write(ref obj.Content);
30 | }
31 |
32 | if (renderer.EnableHtmlForInline)
33 | {
34 | renderer.Write("\\) ");
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Mathematics/MathBlock.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Parsers;
6 | using Markdig.Syntax;
7 |
8 | namespace Markdig.Extensions.Mathematics;
9 |
10 | ///
11 | /// A math block.
12 | ///
13 | ///
14 | public class MathBlock : FencedCodeBlock
15 | {
16 | ///
17 | /// Initializes a new instance of the class.
18 | ///
19 | /// The parser.
20 | public MathBlock(BlockParser parser) : base(parser)
21 | {
22 | }
23 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Mathematics/MathInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 | using Markdig.Syntax.Inlines;
7 |
8 | namespace Markdig.Extensions.Mathematics;
9 |
10 | ///
11 | /// A math inline element.
12 | ///
13 | ///
14 | public class MathInline : LeafInline
15 | {
16 | ///
17 | /// Gets or sets the delimiter character used by this code inline.
18 | ///
19 | public char Delimiter { get; set; }
20 |
21 | ///
22 | /// Gets or sets the delimiter count.
23 | ///
24 | public int DelimiterCount { get; set; }
25 |
26 | ///
27 | /// The content as a .
28 | ///
29 | public StringSlice Content;
30 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/MediaLinks/IHostProvider.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Diagnostics.CodeAnalysis;
6 |
7 | namespace Markdig.Extensions.MediaLinks;
8 |
9 | ///
10 | /// Provides url for media links.
11 | ///
12 | public interface IHostProvider
13 | {
14 | ///
15 | /// "class" attribute of generated iframe.
16 | ///
17 | string? Class { get; }
18 |
19 | ///
20 | /// Generate url for iframe.
21 | ///
22 | /// Input media uri.
23 | /// if is a schema relative uri, i.e. uri starts with "//".
24 | /// Generated url for iframe.
25 | ///
26 | bool TryHandle(Uri mediaUri, bool isSchemaRelative, [NotNullWhen(true)] out string? iframeUrl);
27 |
28 | ///
29 | /// Should the generated iframe has allowfullscreen attribute.
30 | ///
31 | ///
32 | /// Should be false for audio embedding.
33 | ///
34 | bool AllowFullScreen { get; }
35 | }
36 |
--------------------------------------------------------------------------------
/src/Markdig/Extensions/NoRefLinks/NoFollowLinksExtension.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Extensions.ReferralLinks;
6 | using Markdig.Renderers;
7 |
8 | namespace Markdig.Extensions.NoRefLinks;
9 |
10 | ///
11 | /// Extension to automatically render rel=nofollow to all links in an HTML output.
12 | ///
13 | [Obsolete("Use ReferralLinksExtension class instead")]
14 | public class NoFollowLinksExtension : IMarkdownExtension
15 | {
16 | private ReferralLinksExtension _referralLinksExtension;
17 |
18 | public NoFollowLinksExtension()
19 | {
20 | _referralLinksExtension = new ReferralLinksExtension(["nofollow"]);
21 | }
22 |
23 | public void Setup(MarkdownPipelineBuilder pipeline)
24 | {
25 | _referralLinksExtension.Setup(pipeline);
26 | }
27 |
28 | public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
29 | {
30 | _referralLinksExtension.Setup(pipeline, renderer);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Markdig/Extensions/NonAsciiNoEscape/NonAsciiNoEscapeExtension.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 |
7 | namespace Markdig.Extensions.NonAsciiNoEscape;
8 |
9 | ///
10 | /// Extension that will disable URI escape with % characters for non-US-ASCII characters in order to workaround a bug under IE/Edge with local file links containing non US-ASCII chars. DO NOT USE OTHERWISE.
11 | ///
12 | public class NonAsciiNoEscapeExtension : IMarkdownExtension
13 | {
14 | public void Setup(MarkdownPipelineBuilder pipeline)
15 | {
16 | }
17 |
18 | public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
19 | {
20 | if (renderer is HtmlRenderer htmlRenderer)
21 | {
22 | htmlRenderer.UseNonAsciiNoEscape = true;
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/ReferralLinks/ReferralLinksExtension.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Linq;
6 |
7 | using Markdig.Renderers;
8 | using Markdig.Renderers.Html.Inlines;
9 |
10 | namespace Markdig.Extensions.ReferralLinks;
11 |
12 | public class ReferralLinksExtension : IMarkdownExtension
13 | {
14 | public ReferralLinksExtension(string[] rels)
15 | {
16 | Rels = rels?.ToList() ?? throw new ArgumentNullException(nameof(rels));
17 | }
18 |
19 | public List Rels { get; }
20 |
21 | public void Setup(MarkdownPipelineBuilder pipeline)
22 | {
23 | }
24 |
25 | public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
26 | {
27 | string relString = string.Join(" ", Rels.Where(r => !string.IsNullOrEmpty(r)));
28 |
29 | var linkRenderer = renderer.ObjectRenderers.Find();
30 | if (linkRenderer != null)
31 | {
32 | linkRenderer.Rel = relString;
33 | }
34 |
35 | var autolinkRenderer = renderer.ObjectRenderers.Find();
36 | if (autolinkRenderer != null)
37 | {
38 | autolinkRenderer.Rel = relString;
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/Markdig/Extensions/SmartyPants/HtmlSmartyPantRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 | using Markdig.Renderers.Html;
7 |
8 | namespace Markdig.Extensions.SmartyPants;
9 |
10 | ///
11 | /// A HTML renderer for a .
12 | ///
13 | ///
14 | public class HtmlSmartyPantRenderer : HtmlObjectRenderer
15 | {
16 | private static readonly SmartyPantOptions DefaultOptions = new SmartyPantOptions();
17 |
18 | private readonly SmartyPantOptions options;
19 |
20 | ///
21 | /// Initializes a new instance of the class.
22 | ///
23 | /// The options.
24 | ///
25 | public HtmlSmartyPantRenderer(SmartyPantOptions? options)
26 | {
27 | this.options = options ?? throw new ArgumentNullException(nameof(options));
28 | }
29 |
30 | protected override void Write(HtmlRenderer renderer, SmartyPant obj)
31 | {
32 | if (!options.Mapping.TryGetValue(obj.Type, out string? text))
33 | {
34 | DefaultOptions.Mapping.TryGetValue(obj.Type, out text);
35 | }
36 | renderer.Write(text);
37 | }
38 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/SmartyPants/SmartyPantOptions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Extensions.SmartyPants;
6 |
7 | ///
8 | /// The options used for .
9 | ///
10 | public class SmartyPantOptions
11 | {
12 | ///
13 | /// Initializes a new instance of the class.
14 | ///
15 | public SmartyPantOptions()
16 | {
17 | Mapping = new Dictionary()
18 | {
19 | {SmartyPantType.Quote, "'"},
20 | {SmartyPantType.DoubleQuote, "\""},
21 | {SmartyPantType.LeftQuote, "‘"},
22 | {SmartyPantType.RightQuote, "’"},
23 | {SmartyPantType.LeftDoubleQuote, "“"},
24 | {SmartyPantType.RightDoubleQuote, "”"},
25 | {SmartyPantType.LeftAngleQuote, "«"},
26 | {SmartyPantType.RightAngleQuote, "»"},
27 | {SmartyPantType.Ellipsis, "…"},
28 | {SmartyPantType.Dash2, "–"},
29 | {SmartyPantType.Dash3, "—"},
30 | };
31 | }
32 |
33 | ///
34 | /// Gets the mapping between a and its textual representation
35 | /// (usually an HTML entity).
36 | ///
37 | public Dictionary Mapping { get; }
38 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Tables/GridTableExtension.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 |
7 | namespace Markdig.Extensions.Tables;
8 |
9 | ///
10 | /// Extension that allows to use grid tables.
11 | ///
12 | ///
13 | public class GridTableExtension : IMarkdownExtension
14 | {
15 | public void Setup(MarkdownPipelineBuilder pipeline)
16 | {
17 | if (!pipeline.BlockParsers.Contains())
18 | {
19 | pipeline.BlockParsers.Insert(0, new GridTableParser());
20 | }
21 | }
22 |
23 | public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
24 | {
25 | if (renderer is HtmlRenderer htmlRenderer && !htmlRenderer.ObjectRenderers.Contains())
26 | {
27 | htmlRenderer.ObjectRenderers.Add(new HtmlTableRenderer());
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Tables/PipeTableDelimiterInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Parsers;
6 | using Markdig.Syntax.Inlines;
7 |
8 | namespace Markdig.Extensions.Tables;
9 |
10 | ///
11 | /// The delimiter used to separate the columns of a pipe table.
12 | ///
13 | ///
14 | public class PipeTableDelimiterInline : DelimiterInline
15 | {
16 | public PipeTableDelimiterInline(InlineParser parser) : base(parser)
17 | {
18 | }
19 |
20 | ///
21 | /// Gets or sets the index of line where this delimiter was found relative to the current block.
22 | ///
23 | public int LocalLineIndex { get; set; }
24 |
25 | public override string ToLiteral()
26 | {
27 | return "|";
28 | }
29 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Tables/TableColumnAlign.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Extensions.Tables;
6 |
7 | ///
8 | /// Defines the alignment of a column
9 | ///
10 | public enum TableColumnAlign
11 | {
12 | ///
13 | /// Align the column to the left
14 | ///
15 | Left,
16 |
17 | ///
18 | /// Align the column to the center
19 | ///
20 | Center,
21 |
22 | ///
23 | /// Align the column to the right
24 | ///
25 | Right,
26 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Tables/TableColumnDefinition.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Extensions.Tables;
6 |
7 | ///
8 | /// Defines a column.
9 | ///
10 | public class TableColumnDefinition
11 | {
12 | ///
13 | /// Gets or sets the width (in percentage) of this column. A value of 0 is unspecified.
14 | ///
15 | public float Width { get; set; }
16 |
17 | ///
18 | /// Gets or sets the column alignment.
19 | ///
20 | public TableColumnAlign? Alignment { get; set; }
21 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Tables/TableRow.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Extensions.Tables;
8 |
9 | ///
10 | /// Defines a row in a , contains , parent is .
11 | ///
12 | ///
13 | public class TableRow : ContainerBlock
14 | {
15 | ///
16 | /// Initializes a new instance of the class.
17 | ///
18 | public TableRow() : base(null)
19 | {
20 | }
21 |
22 | ///
23 | /// Gets or sets a value indicating whether this instance is header row.
24 | ///
25 | public bool IsHeader { get; set; }
26 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/TaskLists/HtmlTaskListRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 | using Markdig.Renderers.Html;
7 |
8 | namespace Markdig.Extensions.TaskLists;
9 |
10 | ///
11 | /// A HTML renderer for a .
12 | ///
13 | ///
14 | public class HtmlTaskListRenderer : HtmlObjectRenderer
15 | {
16 | protected override void Write(HtmlRenderer renderer, TaskList obj)
17 | {
18 | if (renderer.EnableHtmlForInline)
19 | {
20 | renderer.Write(" ");
26 | }
27 | else
28 | {
29 | renderer.Write('[');
30 | renderer.Write(obj.Checked ? "x" : " ");
31 | renderer.Write(']');
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/TaskLists/NormalizeTaskListRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers.Normalize;
6 |
7 | namespace Markdig.Extensions.TaskLists;
8 |
9 | public class NormalizeTaskListRenderer : NormalizeObjectRenderer
10 | {
11 | protected override void Write(NormalizeRenderer renderer, TaskList obj)
12 | {
13 | renderer.Write("[");
14 | renderer.Write(obj.Checked ? "X" : " ");
15 | renderer.Write("]");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/Markdig/Extensions/TaskLists/TaskList.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Diagnostics;
6 | using Markdig.Syntax.Inlines;
7 |
8 | namespace Markdig.Extensions.TaskLists;
9 |
10 | ///
11 | /// An inline for TaskList.
12 | ///
13 | [DebuggerDisplay("TaskList {Checked}")]
14 | public class TaskList : LeafInline
15 | {
16 | public bool Checked { get; set; }
17 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/TaskLists/TaskListExtension.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Parsers.Inlines;
6 | using Markdig.Renderers;
7 | using Markdig.Renderers.Normalize;
8 |
9 | namespace Markdig.Extensions.TaskLists;
10 |
11 | ///
12 | /// Extension to enable TaskList.
13 | ///
14 | public class TaskListExtension : IMarkdownExtension
15 | {
16 | public void Setup(MarkdownPipelineBuilder pipeline)
17 | {
18 | if (!pipeline.InlineParsers.Contains())
19 | {
20 | // Insert the parser after the code span parser
21 | pipeline.InlineParsers.InsertBefore(new TaskListInlineParser());
22 | }
23 | }
24 |
25 | public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
26 | {
27 | if (renderer is HtmlRenderer htmlRenderer)
28 | {
29 | htmlRenderer.ObjectRenderers.AddIfNotAlready();
30 | }
31 |
32 | if (renderer is NormalizeRenderer normalizeRenderer)
33 | {
34 | normalizeRenderer.ObjectRenderers.AddIfNotAlready();
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/TextRenderer/ConfigureNewLineExtension.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 |
7 | namespace Markdig.Extensions.TextRenderer;
8 |
9 | ///
10 | /// Extension that allows setting line-endings for any IMarkdownRenderer
11 | /// that inherits from
12 | ///
13 | ///
14 | public class ConfigureNewLineExtension : IMarkdownExtension
15 | {
16 | private readonly string newLine;
17 |
18 | public ConfigureNewLineExtension(string newLine)
19 | {
20 | this.newLine = newLine;
21 | }
22 |
23 | public void Setup(MarkdownPipelineBuilder pipeline)
24 | {
25 | }
26 |
27 | public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
28 | {
29 | if (renderer is TextRendererBase textRenderer)
30 | {
31 | textRenderer.Writer.NewLine = newLine;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Yaml/YamlFrontMatterBlock.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Parsers;
6 | using Markdig.Syntax;
7 |
8 | namespace Markdig.Extensions.Yaml;
9 |
10 | ///
11 | /// A YAML frontmatter block.
12 | ///
13 | ///
14 | public class YamlFrontMatterBlock : CodeBlock
15 | {
16 | ///
17 | /// Initializes a new instance of the class.
18 | ///
19 | /// The parser.
20 | public YamlFrontMatterBlock(BlockParser parser) : base(parser)
21 | {
22 | }
23 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Yaml/YamlFrontMatterHtmlRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 | using Markdig.Renderers.Html;
7 |
8 | namespace Markdig.Extensions.Yaml;
9 |
10 | ///
11 | /// Empty renderer for a
12 | ///
13 | ///
14 | public class YamlFrontMatterHtmlRenderer : HtmlObjectRenderer
15 | {
16 | protected override void Write(HtmlRenderer renderer, YamlFrontMatterBlock obj)
17 | {
18 | }
19 | }
--------------------------------------------------------------------------------
/src/Markdig/Extensions/Yaml/YamlFrontMatterRoundtripRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 | using Markdig.Renderers.Roundtrip;
7 |
8 | namespace Markdig.Extensions.Yaml;
9 |
10 | public class YamlFrontMatterRoundtripRenderer : MarkdownObjectRenderer
11 | {
12 | private readonly CodeBlockRenderer _codeBlockRenderer;
13 |
14 | public YamlFrontMatterRoundtripRenderer()
15 | {
16 | _codeBlockRenderer = new CodeBlockRenderer();
17 | }
18 |
19 | protected override void Write(RoundtripRenderer renderer, YamlFrontMatterBlock obj)
20 | {
21 | renderer.Writer.WriteLine("---");
22 | _codeBlockRenderer.Write(renderer, obj);
23 | renderer.Writer.WriteLine("---");
24 | }
25 | }
--------------------------------------------------------------------------------
/src/Markdig/Globals.cs:
--------------------------------------------------------------------------------
1 | global using System;
2 | global using System.Collections.Frozen;
3 | global using System.Collections.Generic;
--------------------------------------------------------------------------------
/src/Markdig/Helpers/BlockWrapper.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Helpers;
8 |
9 | // Used to avoid the overhead of type covariance checks
10 | internal readonly struct BlockWrapper(Block block) : IEquatable
11 | {
12 | public readonly Block Block = block;
13 |
14 | public static implicit operator Block(BlockWrapper wrapper) => wrapper.Block;
15 |
16 | public static implicit operator BlockWrapper(Block block) => new BlockWrapper(block);
17 |
18 | public bool Equals(BlockWrapper other) => ReferenceEquals(Block, other.Block);
19 |
20 | public override bool Equals(object? obj) => Block.Equals(obj);
21 |
22 | public override int GetHashCode() => Block.GetHashCode();
23 | }
24 |
--------------------------------------------------------------------------------
/src/Markdig/Helpers/DefaultObjectCache.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Helpers;
6 |
7 | ///
8 | /// A default object cache that expect the type {T} to provide a parameter less constructor
9 | ///
10 | /// The type of item to cache
11 | ///
12 | public abstract class DefaultObjectCache : ObjectCache where T : class, new()
13 | {
14 | protected override T NewInstance()
15 | {
16 | return new T();
17 | }
18 | }
--------------------------------------------------------------------------------
/src/Markdig/Helpers/HexConverter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Runtime.CompilerServices;
6 |
7 | namespace Markdig.Helpers;
8 |
9 | // Based on https://github.com/dotnet/runtime/blob/main/src/libraries/Common/src/System/HexConverter.cs
10 | internal static class HexConverter
11 | {
12 | public enum Casing : uint
13 | {
14 | Upper = 0,
15 | Lower = 0x2020U,
16 | }
17 |
18 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
19 | public static void ToCharsBuffer(byte value, Span buffer, int startingIndex = 0, Casing casing = Casing.Upper)
20 | {
21 | uint difference = (((uint)value & 0xF0U) << 4) + ((uint)value & 0x0FU) - 0x8989U;
22 | uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing;
23 |
24 | buffer[startingIndex + 1] = (char)(packedResult & 0xFF);
25 | buffer[startingIndex] = (char)(packedResult >> 8);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Markdig/Helpers/LazySubstring.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Diagnostics;
6 |
7 | namespace Markdig.Helpers;
8 |
9 | internal struct LazySubstring
10 | {
11 | private string _text;
12 | public int Offset;
13 | public int Length;
14 |
15 | public LazySubstring(string text)
16 | {
17 | _text = text;
18 | Offset = 0;
19 | Length = text.Length;
20 | }
21 |
22 | public LazySubstring(string text, int offset, int length)
23 | {
24 | Debug.Assert((ulong)offset + (ulong)length <= (ulong)text.Length, $"{offset}-{length} in {text}");
25 | _text = text;
26 | Offset = offset;
27 | Length = length;
28 | }
29 |
30 | public ReadOnlySpan AsSpan() => _text.AsSpan(Offset, Length);
31 |
32 | public override string ToString()
33 | {
34 | if (Offset != 0 || Length != _text.Length)
35 | {
36 | _text = _text.Substring(Offset, Length);
37 | Offset = 0;
38 | }
39 |
40 | return _text;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/Markdig/Helpers/Newline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Helpers;
6 |
7 | ///
8 | /// Represents a character or set of characters that represent a separation
9 | /// between two lines of text
10 | ///
11 | public enum NewLine : byte
12 | {
13 | // Values have the length encoded in last 2 bits
14 | None = 0,
15 | CarriageReturn = 4 | 1,
16 | LineFeed = 8 | 1,
17 | CarriageReturnLineFeed = 16 | 2
18 | }
19 |
20 | public static class NewLineExtensions
21 | {
22 | public static string AsString(this NewLine newLine) => newLine switch
23 | {
24 | NewLine.CarriageReturnLineFeed => "\r\n",
25 | NewLine.LineFeed => "\n",
26 | NewLine.CarriageReturn => "\r",
27 | _ => string.Empty,
28 | };
29 |
30 | public static int Length(this NewLine newLine) => (int)newLine & 3;
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/src/Markdig/Helpers/StringBuilderCache.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Text;
6 |
7 | namespace Markdig.Helpers;
8 |
9 | public static class StringBuilderCache
10 | {
11 | ///
12 | /// A StringBuilder that can be used locally in a method body only.
13 | ///
14 | [ThreadStatic]
15 | private static StringBuilder? local;
16 |
17 | ///
18 | /// Provides a string builder that can only be used locally in a method. This StringBuilder MUST not be stored.
19 | ///
20 | ///
21 | public static StringBuilder Local()
22 | {
23 | var sb = local ??= new StringBuilder();
24 | if (sb.Length != 0)
25 | {
26 | sb.Length = 0;
27 | }
28 | return sb;
29 | }
30 | }
--------------------------------------------------------------------------------
/src/Markdig/Helpers/StringBuilderExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Text;
6 |
7 | namespace Markdig.Helpers;
8 |
9 | ///
10 | /// Extensions for StringBuilder
11 | ///
12 | public static class StringBuilderExtensions
13 | {
14 | ///
15 | /// Appends the specified slice to this instance.
16 | ///
17 | /// The builder.
18 | /// The slice.
19 | public static StringBuilder Append(this StringBuilder builder, StringSlice slice)
20 | {
21 | return builder.Append(slice.Text, slice.Start, slice.Length);
22 | }
23 | }
--------------------------------------------------------------------------------
/src/Markdig/Helpers/UnicodeUtility.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Diagnostics;
6 | using System.Runtime.CompilerServices;
7 |
8 | namespace System.Text;
9 |
10 | // Based on https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeUtility.cs
11 | internal static class UnicodeUtility
12 | {
13 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
14 | public static bool IsBmpCodePoint(uint value) => value <= 0xFFFFu;
15 |
16 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
17 | public static bool IsValidUnicodeScalar(uint value)
18 | {
19 | return ((value - 0x110000u) ^ 0xD800u) >= 0xFFEF0800u;
20 | }
21 |
22 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
23 | public static void GetUtf16SurrogatesFromSupplementaryPlaneScalar(uint value, out char highSurrogateCodePoint, out char lowSurrogateCodePoint)
24 | {
25 | Debug.Assert(IsValidUnicodeScalar(value) && IsBmpCodePoint(value));
26 |
27 | highSurrogateCodePoint = (char)((value + ((0xD800u - 0x40u) << 10)) >> 10);
28 | lowSurrogateCodePoint = (char)((value & 0x3FFu) + 0xDC00u);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Markdig/IMarkdownExtension.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Renderers;
6 |
7 | namespace Markdig;
8 |
9 | ///
10 | /// Base interface for an extension.
11 | ///
12 | public interface IMarkdownExtension
13 | {
14 | ///
15 | /// Setups this extension for the specified pipeline.
16 | ///
17 | /// The pipeline.
18 | void Setup(MarkdownPipelineBuilder pipeline);
19 |
20 | ///
21 | /// Setups this extension for the specified renderer.
22 | ///
23 | /// The pipeline used to parse the document.
24 | /// The renderer.
25 | void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer);
26 | }
--------------------------------------------------------------------------------
/src/Markdig/Markdig.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Markdig
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/Markdig/MarkdownParserContext.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig;
6 |
7 | ///
8 | /// Provides a context that can be used as part of parsing Markdown documents.
9 | ///
10 | public class MarkdownParserContext
11 | {
12 | ///
13 | /// Gets or sets the context property collection.
14 | ///
15 | public Dictionary Properties { get; }
16 |
17 | ///
18 | /// Initializes a new instance of the class.
19 | ///
20 | public MarkdownParserContext()
21 | {
22 | Properties = new Dictionary();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Markdig/Parsers/BlockParserList.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Parsers;
6 |
7 | ///
8 | /// A List of .
9 | ///
10 | ///
11 | public class BlockParserList : ParserList
12 | {
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | /// The parsers.
17 | public BlockParserList(IEnumerable parsers) : base(parsers)
18 | {
19 | }
20 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/BlockState.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Parsers;
6 |
7 | ///
8 | /// Defines the result of parsing a line for a .
9 | ///
10 | public enum BlockState
11 | {
12 | ///
13 | /// A line is not accepted by this parser.
14 | ///
15 | None,
16 |
17 | ///
18 | /// The parser is skipped.
19 | ///
20 | Skip,
21 |
22 | ///
23 | /// The parser accepts a line and instruct to continue.
24 | ///
25 | Continue,
26 |
27 | ///
28 | /// The parser accepts a line, instruct to continue but discard the line (not stored on the block)
29 | ///
30 | ContinueDiscard,
31 |
32 | ///
33 | /// The parser is ending a block, instruct to stop and keep the line being processed.
34 | ///
35 | Break,
36 |
37 | ///
38 | /// The parser is ending a block, instruct to stop and discard the line being processed.
39 | ///
40 | BreakDiscard
41 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/IAttributesParseable.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 | using Markdig.Syntax;
7 |
8 | namespace Markdig.Parsers;
9 |
10 | ///
11 | /// A delegates that allows to process attached attributes at time.
12 | ///
13 | /// The processor.
14 | /// The slice to look for attached attributes.
15 | /// The block.
16 | /// true if attributes were found; otherwise false
17 | public delegate bool TryParseAttributesDelegate(
18 | BlockProcessor processor, ref StringSlice slice, IBlock block);
19 |
20 | ///
21 | /// An interface used to tag that supports parsing
22 | ///
23 | public interface IAttributesParseable
24 | {
25 | ///
26 | /// A delegates that allows to process attached attributes
27 | ///
28 | TryParseAttributesDelegate? TryParseAttributes { get; set; }
29 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/IInlineParser.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 |
7 | namespace Markdig.Parsers;
8 |
9 | ///
10 | /// Base interface for parsing an .
11 | ///
12 | ///
13 | ///
14 | public interface IInlineParser : IMarkdownParser
15 | {
16 | ///
17 | /// Tries to match the specified slice.
18 | ///
19 | /// The parser processor.
20 | /// The text slice.
21 | /// true if this parser found a match; false otherwise
22 | bool Match(InlineProcessor processor, ref StringSlice slice);
23 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/IMarkdownParser.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Parsers;
6 |
7 | ///
8 | /// Base interface for a block or inline parser.
9 | ///
10 | /// The type of processor.
11 | public interface IMarkdownParser
12 | {
13 | ///
14 | /// Gets the opening characters this parser will be triggered if the character is found.
15 | ///
16 | char[]? OpeningCharacters { get; }
17 |
18 | ///
19 | /// Initializes this parser with the specified parser processor.
20 | ///
21 | void Initialize();
22 |
23 | ///
24 | /// Gets the index of this parser in or .
25 | ///
26 | int Index { get; }
27 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/IPostInlineProcessor.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Parsers;
8 |
9 | ///
10 | /// A processor called at the end of processing all inlines.
11 | ///
12 | public interface IPostInlineProcessor
13 | {
14 | ///
15 | /// Processes the delimiters.
16 | ///
17 | /// The parser state.
18 | /// The root inline.
19 | /// The last child.
20 | /// Index of this delimiter processor.
21 | ///
22 | /// true to continue to the next delimiter processor;
23 | /// false to stop the process (in case a processor is performing sub-sequent processor itself)
24 | bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild, int postInlineProcessorIndex, bool isFinalProcessing);
25 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/InlineParser.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 |
7 | namespace Markdig.Parsers;
8 |
9 | ///
10 | /// Base class for parsing an .
11 | ///
12 | ///
13 | public abstract class InlineParser : ParserBase, IInlineParser
14 | {
15 | ///
16 | /// Tries to match the specified slice.
17 | ///
18 | /// The parser processor.
19 | /// The text slice.
20 | /// true if this parser found a match; false otherwise
21 | public abstract bool Match(InlineProcessor processor, ref StringSlice slice);
22 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/InlineParserList.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Parsers;
6 |
7 | ///
8 | /// A list of .
9 | ///
10 | ///
11 | public class InlineParserList : ParserList
12 | {
13 | public InlineParserList(IEnumerable parsers) : base(parsers)
14 | {
15 | // Prepare the list of post inline processors
16 | var postInlineProcessors = new List();
17 | foreach (var parser in this)
18 | {
19 | if (parser is IPostInlineProcessor delimProcessor)
20 | {
21 | postInlineProcessors.Add(delimProcessor);
22 | }
23 | }
24 | PostInlineProcessors = postInlineProcessors.ToArray();
25 | }
26 |
27 | ///
28 | /// Gets the registered post inline processors.
29 | ///
30 | public IPostInlineProcessor[] PostInlineProcessors { get; private set; }
31 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/ListItemParser.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Parsers;
6 |
7 | ///
8 | /// A parser base class for a list item.
9 | ///
10 | public abstract class ListItemParser
11 | {
12 | ///
13 | /// Defines the characters that are used for detecting this list item.
14 | ///
15 | public char[]? OpeningCharacters { get; protected set; }
16 |
17 | ///
18 | /// Tries to parse the current input as a list item for this particular instance.
19 | ///
20 | /// The block processor
21 | /// The type of the current bullet type
22 | /// The result of parsing
23 | /// true if parsing was successful; false otherwise
24 | public abstract bool TryParse(BlockProcessor state, char pendingBulletType, out ListInfo result);
25 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/ParserBase.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Parsers;
6 |
7 | ///
8 | /// Base class for a or .
9 | ///
10 | /// Type of the parser processor
11 | ///
12 | public abstract class ParserBase : IMarkdownParser
13 | {
14 | ///
15 | /// Gets the opening characters this parser will be triggered if the character is found.
16 | ///
17 | public char[]? OpeningCharacters { get; set; }
18 |
19 | ///
20 | /// Initializes this parser with the specified parser processor.
21 | ///
22 | public virtual void Initialize()
23 | {
24 | }
25 |
26 | ///
27 | /// Gets the index of this parser in or .
28 | ///
29 | public int Index { get; internal set; }
30 | }
--------------------------------------------------------------------------------
/src/Markdig/Parsers/UnorderedListItemParser.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Parsers;
6 |
7 | ///
8 | /// The default parser used to parse unordered list item (-, +, *)
9 | ///
10 | ///
11 | public class UnorderedListItemParser : ListItemParser
12 | {
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | public UnorderedListItemParser()
17 | {
18 | OpeningCharacters = ['-', '+', '*'];
19 | }
20 |
21 | public override bool TryParse(BlockProcessor state, char pendingBulletType, out ListInfo result)
22 | {
23 | result = new ListInfo(state.CurrentChar);
24 | state.NextChar();
25 | return true;
26 | }
27 | }
--------------------------------------------------------------------------------
/src/Markdig/Polyfills/Ascii.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | #if !NET8_0_OR_GREATER
6 |
7 | namespace System.Text;
8 |
9 | internal static class Ascii
10 | {
11 | public static bool IsValid(this string value)
12 | {
13 | return IsValid(value.AsSpan());
14 | }
15 |
16 | public static bool IsValid(this ReadOnlySpan value)
17 | {
18 | for (int i = 0; i < value.Length; i++)
19 | {
20 | if (value[i] > 127)
21 | {
22 | return false;
23 | }
24 | }
25 |
26 | return true;
27 | }
28 | }
29 |
30 | #endif
--------------------------------------------------------------------------------
/src/Markdig/Polyfills/ConcurrentQueue.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | #if NETFRAMEWORK || NETSTANDARD2_0
6 | namespace System.Collections.Concurrent;
7 |
8 | internal static class ConcurrentQueueExtensions
9 | {
10 | public static void Clear(this ConcurrentQueue queue)
11 | {
12 | while (queue.TryDequeue(out _)) { }
13 | }
14 | }
15 | #endif
--------------------------------------------------------------------------------
/src/Markdig/Polyfills/EncodingExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | #if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
6 |
7 | using System.Runtime.InteropServices;
8 |
9 | namespace System.Text;
10 |
11 | internal static class EncodingExtensions
12 | {
13 | public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan chars, Span bytes)
14 | {
15 | fixed (char* charsPtr = &MemoryMarshal.GetReference(chars))
16 | {
17 | fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes))
18 | {
19 | return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
20 | }
21 | }
22 | }
23 | }
24 | #endif
25 |
--------------------------------------------------------------------------------
/src/Markdig/Polyfills/FrozenCollections.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | #if !NET8_0_OR_GREATER
6 |
7 | namespace System.Collections.Frozen;
8 |
9 | // We're using a polyfill instead of conditionally referencing the package as the package is untested on older TFMs, and
10 | // brings in a reference to System.Runtime.CompilerServices.Unsafe, which conflicts with our polyfills of that type.
11 |
12 | internal sealed class FrozenDictionary : Dictionary
13 | {
14 | public FrozenDictionary(Dictionary dictionary) : base(dictionary) { }
15 | }
16 |
17 | internal static class FrozenDictionaryExtensions
18 | {
19 | public static FrozenDictionary ToFrozenDictionary(this Dictionary dictionary)
20 | {
21 | return new FrozenDictionary(dictionary);
22 | }
23 | }
24 |
25 | internal sealed class FrozenSet : HashSet
26 | {
27 | public FrozenSet(HashSet set, IEqualityComparer comparer) : base(set, comparer) { }
28 | }
29 |
30 | internal static class FrozenSetExtensions
31 | {
32 | public static FrozenSet ToFrozenSet(this HashSet set, IEqualityComparer comparer)
33 | {
34 | return new FrozenSet(set, comparer);
35 | }
36 | }
37 |
38 | #endif
--------------------------------------------------------------------------------
/src/Markdig/Polyfills/IndexOfHelpers.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | #if !NET8_0_OR_GREATER
6 |
7 | namespace System;
8 |
9 | internal static class IndexOfHelpers
10 | {
11 | public static bool ContainsAnyExcept(this ReadOnlySpan span, char value0, char value1, char value2)
12 | {
13 | for (int i = 0; i < span.Length; i++)
14 | {
15 | char c = span[i];
16 | if (c != value0 && c != value1 && c != value2)
17 | {
18 | return true;
19 | }
20 | }
21 |
22 | return false;
23 | }
24 |
25 | #if !NETSTANDARD2_1_OR_GREATER
26 | public static int IndexOfAny(this ReadOnlySpan span, string values)
27 | {
28 | for (int i = 0; i < span.Length; i++)
29 | {
30 | char c = span[i];
31 |
32 | foreach (char v in values)
33 | {
34 | if (c == v)
35 | {
36 | return i;
37 | }
38 | }
39 | }
40 |
41 | return -1;
42 | }
43 | #endif
44 |
45 | #if !NET6_0_OR_GREATER
46 | public static bool Contains(this ReadOnlySpan span, T value) where T : IEquatable
47 | {
48 | return span.IndexOf(value) >= 0;
49 | }
50 | #endif
51 | }
52 |
53 | #endif
--------------------------------------------------------------------------------
/src/Markdig/Polyfills/NullableAttributes.cs:
--------------------------------------------------------------------------------
1 | // Based on code at located at https://github.com/dotnet/runtime/blob/79ae74f5ca5c8a6fe3a48935e85bd7374959c570/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
2 |
3 | // Licensed to the .NET Foundation under one or more agreements.
4 | // The .NET Foundation licenses this file to you under the MIT license.
5 |
6 | namespace System.Diagnostics.CodeAnalysis;
7 |
8 | #if !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP3_1_OR_GREATER
9 | internal sealed class DoesNotReturnAttribute : Attribute { }
10 |
11 | [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
12 | internal sealed class NotNullWhenAttribute : Attribute
13 | {
14 | public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
15 |
16 | public bool ReturnValue { get; }
17 | }
18 |
19 | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, Inherited = false)]
20 | internal sealed class AllowNullAttribute : Attribute { }
21 |
22 | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
23 | internal sealed class MaybeNullAttribute : Attribute { }
24 | #endif
25 |
26 | #if !NET5_0_OR_GREATER
27 | internal sealed class MemberNotNullAttribute : Attribute
28 | {
29 | public MemberNotNullAttribute(string member) => Members = [member];
30 |
31 | public MemberNotNullAttribute(params string[] members) => Members = members;
32 |
33 | public string[] Members { get; }
34 | }
35 | #endif
36 |
--------------------------------------------------------------------------------
/src/Markdig/Polyfills/SpanExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | #if NET462 || NETSTANDARD2_0
6 |
7 | using System.Diagnostics;
8 |
9 | namespace System;
10 |
11 | internal static class SpanExtensions
12 | {
13 | public static bool StartsWith(this ReadOnlySpan span, string prefix, StringComparison comparisonType)
14 | {
15 | Debug.Assert(comparisonType is StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase);
16 |
17 | return
18 | span.Length >= prefix.Length &&
19 | span.Slice(0, prefix.Length).Equals(prefix.AsSpan(), comparisonType);
20 | }
21 | }
22 |
23 | #endif
--------------------------------------------------------------------------------
/src/Markdig/Polyfills/Unsafe.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | #if NETSTANDARD2_1
6 |
7 | using System.Diagnostics.CodeAnalysis;
8 |
9 | namespace System.Runtime.CompilerServices;
10 |
11 | internal static class Unsafe
12 | {
13 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
14 | [return: NotNullIfNotNull(nameof(o))]
15 | public static T? As(object? o) where T : class
16 | {
17 | return (T?)o;
18 | }
19 | }
20 | #endif
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/HeadingRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Html;
8 |
9 | ///
10 | /// An HTML renderer for a .
11 | ///
12 | ///
13 | public class HeadingRenderer : HtmlObjectRenderer
14 | {
15 | private static readonly string[] HeadingTexts = [
16 | "h1",
17 | "h2",
18 | "h3",
19 | "h4",
20 | "h5",
21 | "h6",
22 | ];
23 |
24 | protected override void Write(HtmlRenderer renderer, HeadingBlock obj)
25 | {
26 | int index = obj.Level - 1;
27 | string[] headings = HeadingTexts;
28 | string headingText = ((uint)index < (uint)headings.Length)
29 | ? headings[index]
30 | : $"h{obj.Level}";
31 |
32 | if (renderer.EnableHtmlForBlock)
33 | {
34 | renderer.Write('<');
35 | renderer.WriteRaw(headingText);
36 | renderer.WriteAttributes(obj);
37 | renderer.WriteRaw('>');
38 | }
39 |
40 | renderer.WriteLeafInline(obj);
41 |
42 | if (renderer.EnableHtmlForBlock)
43 | {
44 | renderer.Write("");
45 | renderer.WriteRaw(headingText);
46 | renderer.WriteLine('>');
47 | }
48 |
49 | renderer.EnsureLine();
50 | }
51 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/HtmlBlockRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Html;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class HtmlBlockRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, HtmlBlock obj)
16 | {
17 | renderer.WriteLeafRawLines(obj, true, false);
18 | }
19 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/HtmlObjectRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Html;
8 |
9 | ///
10 | /// A base class for HTML rendering and Markdown objects.
11 | ///
12 | /// The type of the object.
13 | ///
14 | public abstract class HtmlObjectRenderer : MarkdownObjectRenderer where TObject : MarkdownObject
15 | {
16 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/Inlines/CodeInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Html.Inlines;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class CodeInlineRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, CodeInline obj)
16 | {
17 | if (renderer.EnableHtmlForInline)
18 | {
19 | renderer.Write("');
22 | }
23 | if (renderer.EnableHtmlEscape)
24 | {
25 | renderer.WriteEscape(obj.ContentSpan);
26 | }
27 | else
28 | {
29 | renderer.Write(obj.ContentSpan);
30 | }
31 | if (renderer.EnableHtmlForInline)
32 | {
33 | renderer.WriteRaw("
");
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/Inlines/DelimiterInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Html.Inlines;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class DelimiterInlineRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, DelimiterInline obj)
16 | {
17 | renderer.WriteEscape(obj.ToLiteral());
18 | renderer.WriteChildren(obj);
19 | }
20 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/Inlines/HtmlEntityInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Html.Inlines;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class HtmlEntityInlineRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, HtmlEntityInline obj)
16 | {
17 | if (renderer.EnableHtmlEscape)
18 | {
19 | renderer.WriteEscape(obj.Transcoded);
20 | }
21 | else
22 | {
23 | renderer.Write(obj.Transcoded);
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/Inlines/HtmlInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Html.Inlines;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class HtmlInlineRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, HtmlInline obj)
16 | {
17 | if (renderer.EnableHtmlForInline)
18 | {
19 | renderer.Write(obj.Tag);
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/Inlines/LineBreakInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Html.Inlines;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class LineBreakInlineRenderer : HtmlObjectRenderer
14 | {
15 | ///
16 | /// Gets or sets a value indicating whether to render this softline break as a HTML hardline break tag (<br />)
17 | ///
18 | public bool RenderAsHardlineBreak { get; set; }
19 |
20 | protected override void Write(HtmlRenderer renderer, LineBreakInline obj)
21 | {
22 | if (renderer.IsLastInContainer) return;
23 |
24 | if (renderer.EnableHtmlForInline)
25 | {
26 | if (obj.IsHard || RenderAsHardlineBreak)
27 | {
28 | renderer.WriteLine(" ");
29 | }
30 | }
31 |
32 | renderer.EnsureLine();
33 | }
34 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/Inlines/LiteralInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Html.Inlines;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class LiteralInlineRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, LiteralInline obj)
16 | {
17 | if (renderer.EnableHtmlEscape)
18 | {
19 | renderer.WriteEscape(ref obj.Content);
20 | }
21 | else
22 | {
23 | renderer.Write(ref obj.Content);
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/ParagraphRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Html;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class ParagraphRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, ParagraphBlock obj)
16 | {
17 | if (!renderer.ImplicitParagraph && renderer.EnableHtmlForBlock)
18 | {
19 | if (!renderer.IsFirstInContainer)
20 | {
21 | renderer.EnsureLine();
22 | }
23 |
24 | renderer.Write("');
27 | }
28 | renderer.WriteLeafInline(obj);
29 | if (!renderer.ImplicitParagraph)
30 | {
31 | if (renderer.EnableHtmlForBlock)
32 | {
33 | renderer.WriteLine("
");
34 | }
35 |
36 | renderer.EnsureLine();
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/QuoteBlockRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Html;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class QuoteBlockRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, QuoteBlock obj)
16 | {
17 | renderer.EnsureLine();
18 | if (renderer.EnableHtmlForBlock)
19 | {
20 | renderer.Write("');
23 | }
24 | var savedImplicitParagraph = renderer.ImplicitParagraph;
25 | renderer.ImplicitParagraph = false;
26 | renderer.WriteChildren(obj);
27 | renderer.ImplicitParagraph = savedImplicitParagraph;
28 | if (renderer.EnableHtmlForBlock)
29 | {
30 | renderer.WriteLine(" ");
31 | }
32 | renderer.EnsureLine();
33 | }
34 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Html/ThematicBreakRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Html;
8 |
9 | ///
10 | /// A HTML renderer for a .
11 | ///
12 | ///
13 | public class ThematicBreakRenderer : HtmlObjectRenderer
14 | {
15 | protected override void Write(HtmlRenderer renderer, ThematicBreakBlock obj)
16 | {
17 | if (renderer.EnableHtmlForBlock)
18 | {
19 | renderer.Write(" ");
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/IMarkdownObjectRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers;
8 |
9 | ///
10 | /// Base interface for the renderer of a .
11 | ///
12 | public interface IMarkdownObjectRenderer
13 | {
14 | ///
15 | /// Accepts the specified .
16 | ///
17 | /// The renderer.
18 | /// The of the Markdown object.
19 | /// true If this renderer is accepting to render the specified Markdown object
20 | bool Accept(RendererBase renderer, Type objectType);
21 |
22 | ///
23 | /// Writes the specified to the .
24 | ///
25 | /// The renderer.
26 | /// The object to render.
27 | void Write(RendererBase renderer, MarkdownObject objectToRender);
28 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/IMarkdownRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 | using Markdig.Syntax.Inlines;
7 |
8 | namespace Markdig.Renderers;
9 |
10 | ///
11 | /// Base interface for a renderer for a Markdown .
12 | ///
13 | public interface IMarkdownRenderer
14 | {
15 | ///
16 | /// Occurs when before writing an object.
17 | ///
18 | event Action ObjectWriteBefore;
19 |
20 | ///
21 | /// Occurs when after writing an object.
22 | ///
23 | event Action ObjectWriteAfter;
24 |
25 | ///
26 | /// Gets the object renderers that will render and elements.
27 | ///
28 | ObjectRendererCollection ObjectRenderers { get; }
29 |
30 | ///
31 | /// Renders the specified markdown object.
32 | ///
33 | /// The markdown object.
34 | /// The result of the rendering.
35 | object Render(MarkdownObject markdownObject);
36 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/HeadingRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Normalize;
8 |
9 | ///
10 | /// An Normalize renderer for a .
11 | ///
12 | ///
13 | public class HeadingRenderer : NormalizeObjectRenderer
14 | {
15 | private static readonly string[] HeadingTexts = [
16 | "#",
17 | "##",
18 | "###",
19 | "####",
20 | "#####",
21 | "######",
22 | ];
23 |
24 | protected override void Write(NormalizeRenderer renderer, HeadingBlock obj)
25 | {
26 | if (obj.Level is > 0 and <= 6)
27 | {
28 | renderer.Write(HeadingTexts[obj.Level - 1]);
29 | }
30 | else
31 | {
32 | renderer.Write('#', obj.Level);
33 | }
34 |
35 | renderer.Write(' ');
36 | renderer.WriteLeafInline(obj);
37 |
38 | renderer.FinishBlock(renderer.Options.EmptyLineAfterHeading);
39 | }
40 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/HtmlBlockRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Normalize;
8 |
9 | public class HtmlBlockRenderer : NormalizeObjectRenderer
10 | {
11 | protected override void Write(NormalizeRenderer renderer, HtmlBlock obj)
12 | {
13 | renderer.WriteLeafRawLines(obj, true, false);
14 | }
15 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/Inlines/AutolinkInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Normalize.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for an .
11 | ///
12 | ///
13 | public class AutolinkInlineRenderer : NormalizeObjectRenderer
14 | {
15 | protected override void Write(NormalizeRenderer renderer, AutolinkInline obj)
16 | {
17 | renderer.Write('<').Write(obj.Url).Write('>');
18 | }
19 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/Inlines/DelimiterInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Normalize.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | ///
13 | public class DelimiterInlineRenderer : NormalizeObjectRenderer
14 | {
15 | protected override void Write(NormalizeRenderer renderer, DelimiterInline obj)
16 | {
17 | renderer.Write(obj.ToLiteral());
18 | renderer.WriteChildren(obj);
19 | }
20 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/Inlines/EmphasisInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Normalize.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for an .
11 | ///
12 | ///
13 | public class EmphasisInlineRenderer : NormalizeObjectRenderer
14 | {
15 | protected override void Write(NormalizeRenderer renderer, EmphasisInline obj)
16 | {
17 | renderer.Write(obj.DelimiterChar, obj.DelimiterCount);
18 | renderer.WriteChildren(obj);
19 | renderer.Write(obj.DelimiterChar, obj.DelimiterCount);
20 | }
21 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/Inlines/LineBreakInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Normalize.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | ///
13 | public class LineBreakInlineRenderer : NormalizeObjectRenderer
14 | {
15 | ///
16 | /// Gets or sets a value indicating whether to render this softline break as a Normalize hardline break tag (<br />)
17 | ///
18 | public bool RenderAsHardlineBreak { get; set; }
19 |
20 | protected override void Write(NormalizeRenderer renderer, LineBreakInline obj)
21 | {
22 | if (obj.IsHard)
23 | {
24 | renderer.Write(obj.IsBackslash ? "\\" : " ");
25 | }
26 | renderer.WriteLine();
27 | }
28 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/Inlines/LiteralInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 | using Markdig.Syntax.Inlines;
7 |
8 | namespace Markdig.Renderers.Normalize.Inlines;
9 |
10 | ///
11 | /// A Normalize renderer for a .
12 | ///
13 | ///
14 | public class LiteralInlineRenderer : NormalizeObjectRenderer
15 | {
16 | protected override void Write(NormalizeRenderer renderer, LiteralInline obj)
17 | {
18 | if (obj.IsFirstCharacterEscaped && obj.Content.Length > 0 && obj.Content[obj.Content.Start].IsAsciiPunctuation())
19 | {
20 | renderer.Write('\\');
21 | }
22 | renderer.Write(ref obj.Content);
23 | }
24 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/Inlines/NormalizeHtmlEntityInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Normalize.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | public class NormalizeHtmlEntityInlineRenderer : NormalizeObjectRenderer
13 | {
14 | protected override void Write(NormalizeRenderer renderer, HtmlEntityInline obj)
15 | {
16 | renderer.Write(obj.Original);
17 | }
18 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/Inlines/NormalizeHtmlInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Normalize.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | public class NormalizeHtmlInlineRenderer : NormalizeObjectRenderer
13 | {
14 | protected override void Write(NormalizeRenderer renderer, HtmlInline obj)
15 | {
16 | renderer.Write(obj.Tag);
17 | }
18 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/LinkReferenceDefinitionGroupRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Normalize;
8 |
9 | public class LinkReferenceDefinitionGroupRenderer : NormalizeObjectRenderer
10 | {
11 | protected override void Write(NormalizeRenderer renderer, LinkReferenceDefinitionGroup obj)
12 | {
13 | renderer.EnsureLine();
14 | renderer.WriteChildren(obj);
15 | renderer.FinishBlock(false);
16 | }
17 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/LinkReferenceDefinitionRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Normalize;
8 |
9 | public class LinkReferenceDefinitionRenderer : NormalizeObjectRenderer
10 | {
11 | protected override void Write(NormalizeRenderer renderer, LinkReferenceDefinition linkDef)
12 | {
13 | renderer.EnsureLine();
14 | renderer.Write('[');
15 | renderer.Write(linkDef.Label);
16 | renderer.Write("]: ");
17 |
18 | renderer.Write(linkDef.Url);
19 |
20 | if (linkDef.Title != null)
21 | {
22 | renderer.Write(" \"");
23 | renderer.Write(linkDef.Title.Replace("\"", "\\\""));
24 | renderer.Write('"');
25 | }
26 | renderer.FinishBlock(false);
27 | }
28 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/NormalizeObjectRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Normalize;
8 |
9 | ///
10 | /// A base class for Normalize rendering and Markdown objects.
11 | ///
12 | /// The type of the object.
13 | ///
14 | public abstract class NormalizeObjectRenderer : MarkdownObjectRenderer where TObject : MarkdownObject
15 | {
16 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/ParagraphRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Normalize;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | ///
13 | public class ParagraphRenderer : NormalizeObjectRenderer
14 | {
15 | protected override void Write(NormalizeRenderer renderer, ParagraphBlock obj)
16 | {
17 | renderer.WriteLeafInline(obj);
18 | renderer.FinishBlock(!renderer.CompactParagraph);
19 | }
20 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/QuoteBlockRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Normalize;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | ///
13 | public class QuoteBlockRenderer : NormalizeObjectRenderer
14 | {
15 | protected override void Write(NormalizeRenderer renderer, QuoteBlock obj)
16 | {
17 | var quoteIndent = renderer.Options.SpaceAfterQuoteBlock ? obj.QuoteChar + " " : obj.QuoteChar.ToString();
18 | renderer.PushIndent(quoteIndent);
19 | renderer.WriteChildren(obj);
20 | renderer.PopIndent();
21 |
22 | renderer.FinishBlock(true);
23 | }
24 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Normalize/ThematicBreakRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Normalize;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | ///
13 | public class ThematicBreakRenderer : NormalizeObjectRenderer
14 | {
15 | protected override void Write(NormalizeRenderer renderer, ThematicBreakBlock obj)
16 | {
17 | renderer.WriteLine(new string(obj.ThematicChar, obj.ThematicCharCount));
18 |
19 | renderer.FinishBlock(renderer.Options.EmptyLineAfterThematicBreak);
20 | }
21 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/ObjectRendererCollection.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 |
7 | namespace Markdig.Renderers;
8 |
9 | ///
10 | /// A collection of .
11 | ///
12 | ///
13 | public class ObjectRendererCollection : OrderedList
14 | {
15 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/EmptyBlockRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Roundtrip;
8 |
9 | public class EmptyBlockRenderer : RoundtripObjectRenderer
10 | {
11 | protected override void Write(RoundtripRenderer renderer, EmptyBlock noBlocksFoundBlock)
12 | {
13 | renderer.RenderLinesAfter(noBlocksFoundBlock);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/HtmlBlockRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Roundtrip;
8 |
9 | public class HtmlBlockRenderer : RoundtripObjectRenderer
10 | {
11 | protected override void Write(RoundtripRenderer renderer, HtmlBlock obj)
12 | {
13 | renderer.RenderLinesBefore(obj);
14 | //renderer.Write(obj.BeforeWhitespace); // Lines content is written, including whitespace
15 | renderer.WriteLeafRawLines(obj);
16 | renderer.RenderLinesAfter(obj);
17 | }
18 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/Inlines/AutolinkInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Roundtrip.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for an .
11 | ///
12 | ///
13 | public class AutolinkInlineRenderer : RoundtripObjectRenderer
14 | {
15 | protected override void Write(RoundtripRenderer renderer, AutolinkInline obj)
16 | {
17 | renderer.Write('<').Write(obj.Url).Write('>');
18 | }
19 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/Inlines/CodeInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Roundtrip.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | ///
13 | public class CodeInlineRenderer : RoundtripObjectRenderer
14 | {
15 | protected override void Write(RoundtripRenderer renderer, CodeInline obj)
16 | {
17 | renderer.Write(obj.Delimiter, obj.DelimiterCount);
18 | if (!obj.ContentSpan.IsEmpty)
19 | {
20 | renderer.Write(obj.ContentWithTrivia);
21 | }
22 | renderer.Write(obj.Delimiter, obj.DelimiterCount);
23 | }
24 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/Inlines/DelimiterInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Roundtrip.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | ///
13 | public class DelimiterInlineRenderer : RoundtripObjectRenderer
14 | {
15 | protected override void Write(RoundtripRenderer renderer, DelimiterInline obj)
16 | {
17 | renderer.Write(obj.ToLiteral());
18 | renderer.WriteChildren(obj);
19 | }
20 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/Inlines/EmphasisInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Roundtrip.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for an .
11 | ///
12 | ///
13 | public class EmphasisInlineRenderer : RoundtripObjectRenderer
14 | {
15 | protected override void Write(RoundtripRenderer renderer, EmphasisInline obj)
16 | {
17 | renderer.Write(obj.DelimiterChar, obj.DelimiterCount);
18 | renderer.WriteChildren(obj);
19 | renderer.Write(obj.DelimiterChar, obj.DelimiterCount);
20 | }
21 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/Inlines/LineBreakInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Roundtrip.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | ///
13 | public class LineBreakInlineRenderer : RoundtripObjectRenderer
14 | {
15 | protected override void Write(RoundtripRenderer renderer, LineBreakInline obj)
16 | {
17 | if (obj.IsHard && obj.IsBackslash)
18 | {
19 | renderer.Write("\\");
20 | }
21 | renderer.WriteLine(obj.NewLine);
22 | }
23 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/Inlines/LiteralInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 | using Markdig.Syntax.Inlines;
7 |
8 | namespace Markdig.Renderers.Roundtrip.Inlines;
9 |
10 | ///
11 | /// A Normalize renderer for a .
12 | ///
13 | ///
14 | public class LiteralInlineRenderer : RoundtripObjectRenderer
15 | {
16 | protected override void Write(RoundtripRenderer renderer, LiteralInline obj)
17 | {
18 | if (obj.IsFirstCharacterEscaped && obj.Content.Length > 0 && obj.Content[obj.Content.Start].IsAsciiPunctuation())
19 | {
20 | renderer.Write('\\');
21 | }
22 | renderer.Write(ref obj.Content);
23 | }
24 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/Inlines/RoundtripHtmlEntityInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Roundtrip.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | public class RoundtripHtmlEntityInlineRenderer : RoundtripObjectRenderer
13 | {
14 | protected override void Write(RoundtripRenderer renderer, HtmlEntityInline obj)
15 | {
16 | renderer.Write(obj.Original);
17 | }
18 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/Inlines/RoundtripHtmlInlineRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax.Inlines;
6 |
7 | namespace Markdig.Renderers.Roundtrip.Inlines;
8 |
9 | ///
10 | /// A Normalize renderer for a .
11 | ///
12 | public class RoundtripHtmlInlineRenderer : RoundtripObjectRenderer
13 | {
14 | protected override void Write(RoundtripRenderer renderer, HtmlInline obj)
15 | {
16 | renderer.Write(obj.Tag);
17 | }
18 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/LinkReferenceDefinitionGroupRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Roundtrip;
8 |
9 | public class LinkReferenceDefinitionGroupRenderer : RoundtripObjectRenderer
10 | {
11 | protected override void Write(RoundtripRenderer renderer, LinkReferenceDefinitionGroup obj)
12 | {
13 | renderer.WriteChildren(obj);
14 | }
15 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/ParagraphRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 | using System.Diagnostics;
7 |
8 | namespace Markdig.Renderers.Roundtrip;
9 |
10 | ///
11 | /// A Roundtrip renderer for a .
12 | ///
13 | ///
14 | [DebuggerDisplay("renderer.Writer.ToString()")]
15 | public class ParagraphRenderer : RoundtripObjectRenderer
16 | {
17 | protected override void Write(RoundtripRenderer renderer, ParagraphBlock paragraph)
18 | {
19 | renderer.RenderLinesBefore(paragraph);
20 | renderer.Write(paragraph.TriviaBefore);
21 | renderer.WriteLeafInline(paragraph);
22 | //renderer.Write(paragraph.Newline); // paragraph typically has LineBreakInlines as closing inline nodes
23 | renderer.RenderLinesAfter(paragraph);
24 | }
25 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/RoundtripObjectRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Roundtrip;
8 |
9 | ///
10 | /// A base class for Normalize rendering and Markdown objects.
11 | ///
12 | /// The type of the object.
13 | ///
14 | public abstract class RoundtripObjectRenderer : MarkdownObjectRenderer where TObject : MarkdownObject
15 | {
16 | }
--------------------------------------------------------------------------------
/src/Markdig/Renderers/Roundtrip/ThematicBreakRenderer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Syntax;
6 |
7 | namespace Markdig.Renderers.Roundtrip;
8 |
9 | ///
10 | /// A Roundtrip renderer for a .
11 | ///
12 | ///
13 | public class ThematicBreakRenderer : RoundtripObjectRenderer
14 | {
15 | protected override void Write(RoundtripRenderer renderer, ThematicBreakBlock obj)
16 | {
17 | renderer.RenderLinesBefore(obj);
18 |
19 | renderer.Write(obj.Content);
20 | renderer.WriteLine(obj.NewLine);
21 | renderer.RenderLinesAfter(obj);
22 | }
23 | }
--------------------------------------------------------------------------------
/src/Markdig/SkipLocalsInit.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | #if NET5_0_OR_GREATER
6 | [module: System.Runtime.CompilerServices.SkipLocalsInit]
7 | #endif
8 |
--------------------------------------------------------------------------------
/src/Markdig/Syntax/BlankLineBlock.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Syntax;
6 |
7 | ///
8 | /// A blank line, used internally by some parsers to store blank lines in a container. They are removed before the end of the document.
9 | ///
10 | ///
11 | public sealed class BlankLineBlock : Block
12 | {
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | public BlankLineBlock() : base(null)
17 | {
18 | // A blankline is never opened
19 | IsOpen = false;
20 | }
21 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/CodeBlock.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 | using Markdig.Parsers;
7 |
8 | namespace Markdig.Syntax;
9 |
10 | ///
11 | /// Represents an indented code block.
12 | ///
13 | ///
14 | /// Related to CommonMark spec: 4.4 Indented code blocks
15 | ///
16 | public class CodeBlock : LeafBlock
17 | {
18 | public class CodeBlockLine
19 | {
20 | public StringSlice TriviaBefore { get; set; }
21 | }
22 |
23 | private List? _codeBlockLines;
24 | public List CodeBlockLines => _codeBlockLines ??= [];
25 |
26 | ///
27 | /// Initializes a new instance of the class.
28 | ///
29 | /// The parser.
30 | public CodeBlock(BlockParser parser) : base(parser)
31 | {
32 | }
33 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/HtmlBlock.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Parsers;
6 |
7 | namespace Markdig.Syntax;
8 |
9 | ///
10 | /// Represents a group of lines that is treated as raw HTML (and will not be escaped in HTML output).
11 | ///
12 | ///
13 | public class HtmlBlock : LeafBlock
14 | {
15 | ///
16 | /// Initializes a new instance of the class.
17 | ///
18 | /// The parser.
19 | public HtmlBlock(BlockParser? parser) : base(parser)
20 | {
21 | }
22 |
23 | ///
24 | /// Gets or sets the type of block.
25 | ///
26 | public HtmlBlockType Type { get; set; }
27 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/HtmlBlockType.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Syntax;
6 |
7 | ///
8 | /// Defines the type of
9 | ///
10 | public enum HtmlBlockType
11 | {
12 | ///
13 | /// A SGML document type starting by <!LETTER.
14 | ///
15 | DocumentType,
16 |
17 | ///
18 | /// A raw CDATA sequence.
19 | ///
20 | CData,
21 |
22 | ///
23 | /// A HTML comment.
24 | ///
25 | Comment,
26 |
27 | ///
28 | /// A SGM processing instruction tag <?
29 | ///
30 | ProcessingInstruction,
31 |
32 | ///
33 | /// A script pre or style tag.
34 | ///
35 | ScriptPreOrStyle,
36 |
37 | ///
38 | /// An HTML interrupting block
39 | ///
40 | InterruptingBlock,
41 |
42 | ///
43 | /// An HTML non-interrupting block
44 | ///
45 | NonInterruptingBlock
46 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/Inlines/AutolinkInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Diagnostics;
6 |
7 | namespace Markdig.Syntax.Inlines;
8 |
9 | ///
10 | /// An autolink (Section 6.7 CommonMark specs)
11 | ///
12 | ///
13 | [DebuggerDisplay("<{Url}>")]
14 | public sealed class AutolinkInline : LeafInline
15 | {
16 | public AutolinkInline(string url)
17 | {
18 | Url = url;
19 | }
20 |
21 | ///
22 | /// Gets or sets a value indicating whether this instance is an email link.
23 | ///
24 | public bool IsEmail { get; set; }
25 |
26 | ///
27 | /// Gets or sets the URL of this link.
28 | ///
29 | public string Url { get; set; }
30 |
31 | public override string ToString()
32 | {
33 | return Url;
34 | }
35 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/Inlines/DelimiterType.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Syntax.Inlines;
6 |
7 | ///
8 | /// Gets the type of a .
9 | ///
10 | [Flags]
11 | public enum DelimiterType : byte
12 | {
13 | ///
14 | /// An undefined open or close delimiter.
15 | ///
16 | Undefined,
17 |
18 | ///
19 | /// An open delimiter.
20 | ///
21 | Open,
22 |
23 | ///
24 | /// A close delimiter.
25 | ///
26 | Close,
27 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/Inlines/EmphasisInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Diagnostics;
6 |
7 | namespace Markdig.Syntax.Inlines;
8 |
9 | ///
10 | /// An emphasis and strong emphasis (Section 6.4 CommonMark specs).
11 | ///
12 | ///
13 | [DebuggerDisplay("{DelimiterChar} Count: {DelimiterCount}")]
14 | public class EmphasisInline : ContainerInline
15 | {
16 | ///
17 | /// Gets or sets the delimiter character of this emphasis.
18 | ///
19 | public char DelimiterChar { get; set; }
20 |
21 | ///
22 | /// Gets or sets a value indicating whether this is strong.
23 | /// Marked obsolete as EmphasisInline can now be represented by more than two delimiter characters
24 | ///
25 | [Obsolete("Use `DelimiterCount == 2` instead", error: false)]
26 | public bool IsDouble
27 | {
28 | get => DelimiterCount == 2;
29 | set => DelimiterCount = value ? 2 : 1;
30 | }
31 |
32 | ///
33 | /// Gets or sets the number of delimiter characters for this emphasis.
34 | ///
35 | public int DelimiterCount { get; set; }
36 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/Inlines/HtmlEntityInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Diagnostics;
6 | using Markdig.Helpers;
7 |
8 | namespace Markdig.Syntax.Inlines;
9 |
10 | ///
11 | /// An entity HTML.
12 | ///
13 | ///
14 | [DebuggerDisplay("{Original} -> {Transcoded}")]
15 | public class HtmlEntityInline : LeafInline
16 | {
17 | ///
18 | /// Gets or sets the original HTML entity name
19 | ///
20 | public StringSlice Original { get; set; }
21 |
22 | ///
23 | /// Gets or sets the transcoded literal that will be used for output
24 | ///
25 | public StringSlice Transcoded { get; set; }
26 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/Inlines/HtmlInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using System.Diagnostics;
6 |
7 | namespace Markdig.Syntax.Inlines;
8 |
9 | ///
10 | /// A Raw HTML (Section 6.8 CommonMark specs).
11 | ///
12 | ///
13 | [DebuggerDisplay("{Tag}")]
14 | public class HtmlInline : LeafInline
15 | {
16 | public HtmlInline(string tag)
17 | {
18 | Tag = tag;
19 | }
20 |
21 | ///
22 | /// Gets or sets the full declaration of this tag.
23 | ///
24 | public string Tag { get; set; }
25 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/Inlines/IInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Syntax.Inlines;
6 |
7 | ///
8 | /// Base interface for all syntax tree inlines.
9 | ///
10 | ///
11 | public interface IInline : IMarkdownObject
12 | {
13 | ///
14 | /// Gets the parent container of this inline.
15 | ///
16 | ContainerInline? Parent { get; }
17 |
18 | ///
19 | /// Gets the previous inline.
20 | ///
21 | Inline? PreviousSibling { get; }
22 |
23 | ///
24 | /// Gets the next sibling inline.
25 | ///
26 | Inline? NextSibling { get; }
27 |
28 | ///
29 | /// Gets or sets a value indicating whether this instance is closed.
30 | ///
31 | bool IsClosed { get; set; }
32 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/Inlines/LeafInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Syntax.Inlines;
6 |
7 | ///
8 | /// A base class for a leaf inline.
9 | ///
10 | ///
11 | public abstract class LeafInline : Inline
12 | {
13 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/Inlines/LineBreakInline.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 |
7 | namespace Markdig.Syntax.Inlines;
8 |
9 | ///
10 | /// A base class for a line break.
11 | ///
12 | ///
13 | public class LineBreakInline : LeafInline
14 | {
15 | public bool IsHard { get; set; }
16 |
17 | public bool IsBackslash { get; set; }
18 |
19 | public NewLine NewLine { get; set; }
20 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/MarkdownDocument.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | namespace Markdig.Syntax;
6 |
7 | ///
8 | /// The root Markdown document.
9 | ///
10 | ///
11 | public class MarkdownDocument : ContainerBlock
12 | {
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | public MarkdownDocument() : base(null)
17 | {
18 | }
19 |
20 | ///
21 | /// Gets the number of lines in this
22 | ///
23 | public int LineCount;
24 |
25 | ///
26 | /// Gets a list of zero-based indexes of line beginnings in the source span
27 | /// Available if is used, otherwise null
28 | ///
29 | public List? LineStartIndexes { get; set; }
30 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/NoBlocksFoundBlock.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Parsers;
6 |
7 | namespace Markdig.Syntax;
8 |
9 | ///
10 | /// Block representing a document with characters but no blocks. This can
11 | /// happen when an input document consists solely of trivia.
12 | ///
13 | public sealed class EmptyBlock : LeafBlock
14 | {
15 | public EmptyBlock (BlockParser? parser) : base(parser)
16 | {
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/Markdig/Syntax/ParagraphBlock.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Parsers;
6 |
7 | namespace Markdig.Syntax;
8 |
9 | ///
10 | /// Represents a paragraph.
11 | ///
12 | ///
13 | /// Related to CommonMark spec: 4.8 Paragraphs
14 | ///
15 | public class ParagraphBlock : LeafBlock
16 | {
17 | ///
18 | /// Initializes a new instance of the class.
19 | ///
20 | public ParagraphBlock() : this(null)
21 | {
22 | }
23 |
24 | ///
25 | /// Initializes a new instance of the class.
26 | ///
27 | /// The parser used to create this block.
28 | public ParagraphBlock(BlockParser? parser) : base(parser)
29 | {
30 | // Inlines are processed for a paragraph
31 | ProcessInlines = true;
32 | IsParagraphBlock = true;
33 | }
34 |
35 | public int LastLine => Line + Lines.Count - 1;
36 | }
--------------------------------------------------------------------------------
/src/Markdig/Syntax/ThematicBreakBlock.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Alexandre Mutel. All rights reserved.
2 | // This file is licensed under the BSD-Clause 2 license.
3 | // See the license.txt file in the project root for more information.
4 |
5 | using Markdig.Helpers;
6 | using Markdig.Parsers;
7 |
8 | namespace Markdig.Syntax;
9 |
10 | ///
11 | /// Represents a thematic break (Section 4.1 CommonMark specs).
12 | ///
13 | public class ThematicBreakBlock : LeafBlock
14 | {
15 | ///
16 | /// Initializes a new instance of the class.
17 | ///
18 | /// The parser used to create this block.
19 | public ThematicBreakBlock(BlockParser parser) : base(parser)
20 | {
21 | }
22 |
23 | public char ThematicChar { get; set; }
24 |
25 | public int ThematicCharCount { get; set; }
26 |
27 | public StringSlice Content;
28 | }
--------------------------------------------------------------------------------
/src/Markdig/readme.md:
--------------------------------------------------------------------------------
1 | ---
2 | uid: Markdig
3 | ---
4 |
5 | # Summary
6 |
7 | Contains the top level classes for using Markdig. The entry user class is .
--------------------------------------------------------------------------------
/src/SpecFileGen/SpecFileGen.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0;net8.0;net9.0
6 | enable
7 | false
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/UnicodeNormDApp/UnicodeNormDApp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | net8.0
5 | false
6 | enable
7 |
8 |
--------------------------------------------------------------------------------
/src/dotnet-releaser.toml:
--------------------------------------------------------------------------------
1 | [msbuild]
2 | project = ["markdig.sln", "./Markdig.Signed/Markdig.Signed.csproj"]
3 | build_debug = true
4 | [github]
5 | user = "xoofx"
6 | repo = "markdig"
7 | [test]
8 | run_tests_for_debug = true
9 | [coverage]
10 | exclude = "[SpecFileGen*]*"
--------------------------------------------------------------------------------
/src/global.json:
--------------------------------------------------------------------------------
1 | {
2 | "sdk": {
3 | "version": "9.0.100",
4 | "rollForward": "latestMinor",
5 | "allowPrerelease": false
6 | }
7 | }
--------------------------------------------------------------------------------
/src/mdtoc/mdtoc.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Exe
4 | net8.0
5 | false
6 | enable
7 | enable
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------