Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Use C# 12 syntax #761

Merged
merged 13 commits into from
Dec 18, 2023
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AbbreviationParser : BlockParser
/// </summary>
public AbbreviationParser()
{
OpeningCharacters = new[] { '*' };
OpeningCharacters = ['*'];
}

public override BlockState TryOpen(BlockProcessor processor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System.IO;

using Markdig.Helpers;
using Markdig.Parsers;
using Markdig.Renderers;
Expand Down
9 changes: 2 additions & 7 deletions src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ namespace Markdig.Extensions.AutoLinks;
/// Extension to automatically create <see cref="LinkInline"/> when a link url http: or mailto: is found.
/// </summary>
/// <seealso cref="IMarkdownExtension" />
public class AutoLinkExtension : IMarkdownExtension
public class AutoLinkExtension(AutoLinkOptions? options) : IMarkdownExtension
{
public readonly AutoLinkOptions Options;

public AutoLinkExtension(AutoLinkOptions? options)
{
Options = options ?? new AutoLinkOptions();
}
public readonly AutoLinkOptions Options = options ?? new AutoLinkOptions();

public void Setup(MarkdownPipelineBuilder pipeline)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public AutoLinkParser(AutoLinkOptions options)
{
Options = options ?? throw new ArgumentNullException(nameof(options));

OpeningCharacters = new char[]
{
OpeningCharacters =
[
'h', // for http:// and https://
'f', // for ftp://
'm', // for mailto:
't', // for tel:
'w', // for www.
};
];

_listOfCharCache = new ListOfCharCache();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CustomContainerParser : FencedBlockParserBase<CustomContainer>
/// </summary>
public CustomContainerParser()
{
OpeningCharacters = new [] {':'};
OpeningCharacters = [':'];

// We don't need a prefix
InfoPrefix = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DefinitionListParser : BlockParser
/// </summary>
public DefinitionListParser()
{
OpeningCharacters = new [] {':', '~'};
OpeningCharacters = [':', '~'];
}

public override BlockState TryOpen(BlockProcessor processor)
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Emoji/EmojiMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,6 @@ public EmojiMapping(IDictionary<string, string> shortcodeToUnicode, IDictionary<
ThrowHelper.ArgumentException(string.Format("Smiley {0} is already present in the emoji mapping", smiley.Key));
}

OpeningCharacters = new List<char>(firstChars).ToArray();
OpeningCharacters = [.. firstChars];
}
}
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Figures/FigureBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FigureBlockParser : BlockParser
/// </summary>
public FigureBlockParser()
{
OpeningCharacters = new[] { '^' };
OpeningCharacters = ['^'];
}

public override BlockState TryOpen(BlockProcessor processor)
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Footers/FooterBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class FooterBlockParser : BlockParser
/// </summary>
public FooterBlockParser()
{
OpeningCharacters = new[] {'^'};
OpeningCharacters = ['^'];
}

public override BlockState TryOpen(BlockProcessor processor)
Expand Down
10 changes: 4 additions & 6 deletions src/Markdig/Extensions/Footnotes/FootnoteParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class FootnoteParser : BlockParser

public FootnoteParser()
{
OpeningCharacters = new [] {'['};
OpeningCharacters = ['['];
}

public override BlockState TryOpen(BlockProcessor processor)
Expand All @@ -49,7 +49,7 @@ private BlockState TryOpen(BlockProcessor processor, bool isContinue)

// Advance the column
int deltaColumn = processor.Start - start;
processor.Column = processor.Column + deltaColumn;
processor.Column += deltaColumn;

processor.NextChar(); // Skip ':'

Expand Down Expand Up @@ -170,10 +170,8 @@ private void Document_ProcessInlinesEnd(InlineProcessor state, Inline? inline)
paragraphBlock = new ParagraphBlock();
footnote.Add(paragraphBlock);
}
if (paragraphBlock.Inline == null)
{
paragraphBlock.Inline = new ContainerInline();
}

paragraphBlock.Inline ??= new ContainerInline();

foreach (var link in footnote.Links)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class GenericAttributesParser : InlineParser
/// </summary>
public GenericAttributesParser()
{
OpeningCharacters = new[] { '{' };
OpeningCharacters = ['{'];
}

public override bool Match(InlineProcessor processor, ref StringSlice slice)
Expand Down Expand Up @@ -136,10 +136,7 @@ public static bool TryParse(ref StringSlice slice, [NotNullWhen(true)] out HtmlA
var text = slice.Text.Substring(start, end - start + 1);
if (isClass)
{
if (classes is null)
{
classes = new List<string>();
}
classes ??= new List<string>();
classes.Add(text);
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Mathematics/MathBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MathBlockParser : FencedBlockParserBase<MathBlock>
/// </summary>
public MathBlockParser()
{
OpeningCharacters = new [] {'$'};
OpeningCharacters = ['$'];
// We expect to match only a $$, no less, no more
MinimumMatchCount = 2;
MaximumMatchCount = 2;
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Mathematics/MathInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MathInlineParser : InlineParser
/// </summary>
public MathInlineParser()
{
OpeningCharacters = new[] { '$' };
OpeningCharacters = ['$'];
DefaultClass = "math";
}

Expand Down
24 changes: 10 additions & 14 deletions src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,19 @@ namespace Markdig.Extensions.MediaLinks;

public class HostProviderBuilder
{
private sealed class DelegateProvider : IHostProvider
private sealed class DelegateProvider(
string hostPrefix,
Func<Uri, string?> handler,
bool allowFullscreen = true,
string? className = null) : IHostProvider
{
public DelegateProvider(string hostPrefix, Func<Uri, string?> handler, bool allowFullscreen = true, string? className = null)
{
HostPrefix = hostPrefix;
Delegate = handler;
AllowFullScreen = allowFullscreen;
Class = className;
}

public string HostPrefix { get; }
public string HostPrefix { get; } = hostPrefix;

public Func<Uri, string?> Delegate { get; }
public Func<Uri, string?> Delegate { get; } = handler;

public bool AllowFullScreen { get; }
public bool AllowFullScreen { get; } = allowFullscreen;

public string? Class { get; }
public string? Class { get; } = className;

public bool TryHandle(Uri mediaUri, bool isSchemaRelative, [NotNullWhen(true)] out string? iframeUrl)
{
Expand Down Expand Up @@ -71,7 +67,7 @@ public static IHostProvider Create(string hostPrefix, Func<Uri, string?> handler

#region Known providers

private static readonly string[] SplitAnd = { "&" };
private static readonly string[] SplitAnd = ["&"];
private static string[] SplitQuery(Uri uri)
{
var query = uri.Query.Substring(uri.Query.IndexOf('?') + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NoFollowLinksExtension : IMarkdownExtension

public NoFollowLinksExtension()
{
_referralLinksExtension = new ReferralLinksExtension(new[] { "nofollow" });
_referralLinksExtension = new ReferralLinksExtension(["nofollow"]);
}

public void Setup(MarkdownPipelineBuilder pipeline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SmartyPantsInlineParser : InlineParser, IPostInlineProcessor
/// </summary>
public SmartyPantsInlineParser()
{
OpeningCharacters = new[] {'\'', '"', '<', '>', '.', '-'};
OpeningCharacters = ['\'', '"', '<', '>', '.', '-'];
}

public override bool Match(InlineProcessor processor, ref StringSlice slice)
Expand Down
13 changes: 5 additions & 8 deletions src/Markdig/Extensions/Tables/GridTableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class GridTableParser : BlockParser
{
public GridTableParser()
{
OpeningCharacters = new[] { '+' };
OpeningCharacters = ['+'];
}

public override BlockState TryOpen(BlockProcessor processor)
Expand Down Expand Up @@ -144,7 +144,7 @@ private static void SetRowSpanState(List<GridTableState.ColumnSlice> columns, St
line.Start = lineStart + columnSlice.Start + 1;
line.End = lineStart + columnSlice.End - 1;
line.Trim();
if (line.IsEmptyOrWhitespace() || !IsRowSeperator(line))
if (line.IsEmptyOrWhitespace() || !IsRowSeparator(line))
{
hasRowSpan = true;
columnSlice.CurrentCell.RowSpan++;
Expand All @@ -158,7 +158,7 @@ private static void SetRowSpanState(List<GridTableState.ColumnSlice> columns, St
}
}

private static bool IsRowSeperator(StringSlice slice)
private static bool IsRowSeparator(StringSlice slice)
{
char c = slice.CurrentChar;
do
Expand Down Expand Up @@ -263,7 +263,7 @@ private BlockState HandleContents(BlockProcessor processor, GridTableState table
}
sliceForCell.TrimEnd();

if (!isRowLine || !IsRowSeperator(sliceForCell))
if (!isRowLine || !IsRowSeparator(sliceForCell))
{
if (columnSlice.CurrentCell is null)
{
Expand All @@ -273,10 +273,7 @@ private BlockState HandleContents(BlockProcessor processor, GridTableState table
ColumnIndex = i
};

if (columnSlice.BlockProcessor is null)
{
columnSlice.BlockProcessor = processor.CreateChild();
}
columnSlice.BlockProcessor ??= processor.CreateChild();

// Ensure that the BlockParser is aware that the TableCell is the top-level container
columnSlice.BlockProcessor.Open(columnSlice.CurrentCell);
Expand Down
30 changes: 8 additions & 22 deletions src/Markdig/Extensions/Tables/GridTableState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ namespace Markdig.Extensions.Tables;
/// <summary>
/// Internal state used by the <see cref="GridTableParser"/>
/// </summary>
internal sealed class GridTableState
internal sealed class GridTableState(int start, bool expectRow)
{
public GridTableState(int start, bool expectRow)
{
Start = start;
ExpectRow = expectRow;
}

public int Start { get; }
public int Start { get; } = start;

public StringLineGroup Lines;

public List<ColumnSlice>? ColumnSlices { get; private set; }

public bool ExpectRow { get; }
public bool ExpectRow { get; } = expectRow;

public int StartRowGroup { get; set; }

Expand All @@ -45,26 +39,18 @@ public void AddColumn(int start, int end, TableColumnAlign? align)
ColumnSlices.Add(new ColumnSlice(start, end, align));
}

public sealed class ColumnSlice
public sealed class ColumnSlice(int start, int end, TableColumnAlign? align)
{
public ColumnSlice(int start, int end, TableColumnAlign? align)
{
Start = start;
End = end;
Align = align;
CurrentColumnSpan = -1;
}

/// <summary>
/// Gets or sets the index position of this column (after the |)
/// </summary>
public int Start { get; }
public int Start { get; } = start;

public int End { get; }
public int End { get; } = end;

public TableColumnAlign? Align { get; }
public TableColumnAlign? Align { get; } = align;

public int CurrentColumnSpan { get; set; }
public int CurrentColumnSpan { get; set; } = -1;

public int PreviousColumnSpan { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Tables/PipeTableBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PipeTableBlockParser : BlockParser
/// </summary>
public PipeTableBlockParser()
{
OpeningCharacters = new[] {'-'};
OpeningCharacters = ['-'];
}

public override BlockState TryOpen(BlockProcessor processor)
Expand Down
10 changes: 5 additions & 5 deletions src/Markdig/Extensions/Tables/PipeTableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class PipeTableParser : InlineParser, IPostInlineProcessor
/// <summary>
/// Initializes a new instance of the <see cref="PipeTableParser" /> class.
/// </summary>
/// <param name="lineBreakParser">The linebreak parser to use</param>
/// <param name="lineBreakParser">The line break parser to use</param>
/// <param name="options">The options.</param>
public PipeTableParser(LineBreakInlineParser lineBreakParser, PipeTableOptions? options = null)
{
this.lineBreakParser = lineBreakParser ?? throw new ArgumentNullException(nameof(lineBreakParser));
OpeningCharacters = new[] { '|', '\n', '\r' };
OpeningCharacters = ['|', '\n', '\r'];
Options = options ?? new PipeTableOptions();
}

Expand Down Expand Up @@ -637,10 +637,10 @@ private sealed class TableState

public int LineIndex { get; set; }

public List<Inline> ColumnAndLineDelimiters { get; } = new();
public List<Inline> ColumnAndLineDelimiters { get; } = [];

public List<TableCell> Cells { get; } = new();
public List<TableCell> Cells { get; } = [];

public List<Inline> EndOfLines { get; } = new();
public List<Inline> EndOfLines { get; } = [];
}
}
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/TaskLists/TaskListInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TaskListInlineParser : InlineParser
/// </summary>
public TaskListInlineParser()
{
OpeningCharacters = new[] {'['};
OpeningCharacters = ['['];
ListClass = "contains-task-list";
ListItemClass = "task-list-item";
}
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class YamlFrontMatterParser : BlockParser
/// </summary>
public YamlFrontMatterParser()
{
this.OpeningCharacters = new[] { '-' };
OpeningCharacters = ['-'];
}

/// <summary>
Expand Down
Loading
Loading