Skip to content

Commit

Permalink
Merge pull request #416 from MihaZupan/new
Browse files Browse the repository at this point in the history
Random improvements
  • Loading branch information
xoofx authored Apr 18, 2020
2 parents 2dca211 + eb002db commit 5c52a72
Show file tree
Hide file tree
Showing 57 changed files with 571 additions and 776 deletions.
2 changes: 1 addition & 1 deletion src/Markdig.Tests/Markdig.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net451;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<OutputType>Library</OutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
26 changes: 13 additions & 13 deletions src/Markdig.Tests/TestDescendantsOrder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using NUnit.Framework;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using System;
using System.Linq;
using System.Collections.Generic;
using Markdig.Helpers;

namespace Markdig.Tests
{
Expand Down Expand Up @@ -33,9 +33,9 @@ public void TestSchemas()

foreach (LiteralInline literalInline in syntaxTree.Descendants<LiteralInline>())
{
Assert.AreSame(ArrayHelper<ListBlock>.Empty, literalInline.Descendants<ListBlock>());
Assert.AreSame(ArrayHelper<ParagraphBlock>.Empty, literalInline.Descendants<ParagraphBlock>());
Assert.AreSame(ArrayHelper<ContainerInline>.Empty, literalInline.Descendants<ContainerInline>());
Assert.AreSame(Array.Empty<ListBlock>(), literalInline.Descendants<ListBlock>());
Assert.AreSame(Array.Empty<ParagraphBlock>(), literalInline.Descendants<ParagraphBlock>());
Assert.AreSame(Array.Empty<ContainerInline>(), literalInline.Descendants<ContainerInline>());
}

foreach (ContainerInline containerInline in syntaxTree.Descendants<ContainerInline>())
Expand All @@ -50,13 +50,13 @@ public void TestSchemas()

if (containerInline.FirstChild is null)
{
Assert.AreSame(ArrayHelper<LiteralInline>.Empty, containerInline.Descendants<LiteralInline>());
Assert.AreSame(ArrayHelper<LiteralInline>.Empty, containerInline.FindDescendants<LiteralInline>());
Assert.AreSame(ArrayHelper<LiteralInline>.Empty, (containerInline as MarkdownObject).Descendants<LiteralInline>());
Assert.AreSame(Array.Empty<LiteralInline>(), containerInline.Descendants<LiteralInline>());
Assert.AreSame(Array.Empty<LiteralInline>(), containerInline.FindDescendants<LiteralInline>());
Assert.AreSame(Array.Empty<LiteralInline>(), (containerInline as MarkdownObject).Descendants<LiteralInline>());
}

Assert.AreSame(ArrayHelper<ListBlock>.Empty, containerInline.Descendants<ListBlock>());
Assert.AreSame(ArrayHelper<ParagraphBlock>.Empty, containerInline.Descendants<ParagraphBlock>());
Assert.AreSame(Array.Empty<ListBlock>(), containerInline.Descendants<ListBlock>());
Assert.AreSame(Array.Empty<ParagraphBlock>(), containerInline.Descendants<ParagraphBlock>());
}

foreach (ParagraphBlock paragraphBlock in syntaxTree.Descendants<ParagraphBlock>())
Expand All @@ -65,7 +65,7 @@ public void TestSchemas()
(paragraphBlock as MarkdownObject).Descendants<LiteralInline>(),
paragraphBlock.Descendants<LiteralInline>());

Assert.AreSame(ArrayHelper<ParagraphBlock>.Empty, paragraphBlock.Descendants<ParagraphBlock>());
Assert.AreSame(Array.Empty<ParagraphBlock>(), paragraphBlock.Descendants<ParagraphBlock>());
}

foreach (ContainerBlock containerBlock in syntaxTree.Descendants<ContainerBlock>())
Expand All @@ -80,9 +80,9 @@ public void TestSchemas()

if (containerBlock.Count == 0)
{
Assert.AreSame(ArrayHelper<LiteralInline>.Empty, containerBlock.Descendants<LiteralInline>());
Assert.AreSame(ArrayHelper<LiteralInline>.Empty, (containerBlock as Block).Descendants<LiteralInline>());
Assert.AreSame(ArrayHelper<LiteralInline>.Empty, (containerBlock as MarkdownObject).Descendants<LiteralInline>());
Assert.AreSame(Array.Empty<LiteralInline>(), containerBlock.Descendants<LiteralInline>());
Assert.AreSame(Array.Empty<LiteralInline>(), (containerBlock as Block).Descendants<LiteralInline>());
Assert.AreSame(Array.Empty<LiteralInline>(), (containerBlock as MarkdownObject).Descendants<LiteralInline>());
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Markdig/Extensions/Abbreviations/AbbreviationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System;
using System.Collections.Generic;
using Markdig.Helpers;
using Markdig.Syntax;

namespace Markdig.Extensions.Abbreviations
Expand All @@ -21,9 +22,9 @@ public static bool HasAbbreviations(this MarkdownDocument document)

public static void AddAbbreviation(this MarkdownDocument document, string label, Abbreviation abbr)
{
if (document == null) throw new ArgumentNullException(nameof(document));
if (label == null) throw new ArgumentNullException(nameof(label));
if (abbr == null) throw new ArgumentNullException(nameof(abbr));
if (document == null) ThrowHelper.ArgumentNullException(nameof(document));
if (label == null) ThrowHelper.ArgumentNullException_label();
if (abbr == null) ThrowHelper.ArgumentNullException(nameof(abbr));

var map = document.GetAbbreviations();
if (map == null)
Expand Down
3 changes: 2 additions & 1 deletion src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System;
using System.Collections.Generic;
using Markdig.Helpers;
using Markdig.Parsers;
Expand Down Expand Up @@ -115,7 +116,7 @@ private void DocumentOnProcessInlinesBegin(InlineProcessor inlineProcessor, Inli

ValidAbbreviationStart:;

if (prefixTree.TryMatchLongest(text, i, content.End - i + 1, out KeyValuePair<string, Abbreviation> abbreviationMatch))
if (prefixTree.TryMatchLongest(text.AsSpan(i, content.End - i + 1), out KeyValuePair<string, Abbreviation> abbreviationMatch))
{
var match = abbreviationMatch.Key;
if (!IsValidAbbreviationEnding(match, content, i))
Expand Down
12 changes: 6 additions & 6 deletions src/Markdig/Extensions/Emoji/EmojiMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,10 +1747,10 @@ public EmojiMapping(bool enableSmileys = true)
public EmojiMapping(IDictionary<string, string> shortcodeToUnicode, IDictionary<string, string> smileyToShortcode)
{
if (shortcodeToUnicode == null)
throw new ArgumentNullException(nameof(shortcodeToUnicode));
ThrowHelper.ArgumentNullException(nameof(shortcodeToUnicode));

if (smileyToShortcode == null)
throw new ArgumentNullException(nameof(smileyToShortcode));
ThrowHelper.ArgumentNullException(nameof(smileyToShortcode));

// Build emojis and smileys CompactPrefixTree

Expand All @@ -1768,7 +1768,7 @@ public EmojiMapping(IDictionary<string, string> shortcodeToUnicode, IDictionary<
foreach (var shortcode in shortcodeToUnicode)
{
if (string.IsNullOrEmpty(shortcode.Key) || string.IsNullOrEmpty(shortcode.Value))
throw new ArgumentException("The dictionaries cannot contain null or empty keys/values", nameof(shortcodeToUnicode));
ThrowHelper.ArgumentException("The dictionaries cannot contain null or empty keys/values", nameof(shortcodeToUnicode));

firstChars.Add(shortcode.Key[0]);
PrefixTree.Add(shortcode);
Expand All @@ -1777,15 +1777,15 @@ public EmojiMapping(IDictionary<string, string> shortcodeToUnicode, IDictionary<
foreach (var smiley in smileyToShortcode)
{
if (string.IsNullOrEmpty(smiley.Key) || string.IsNullOrEmpty(smiley.Value))
throw new ArgumentException("The dictionaries cannot contain null or empty keys/values", nameof(smileyToShortcode));
ThrowHelper.ArgumentException("The dictionaries cannot contain null or empty keys/values", nameof(smileyToShortcode));

if (!shortcodeToUnicode.TryGetValue(smiley.Value, out string unicode))
throw new ArgumentException(string.Format("Invalid smiley target: {0} is not present in the emoji shortcodes dictionary", smiley.Value));
ThrowHelper.ArgumentException(string.Format("Invalid smiley target: {0} is not present in the emoji shortcodes dictionary", smiley.Value));

firstChars.Add(smiley.Key[0]);

if (!PrefixTree.TryAdd(smiley.Key, unicode))
throw new ArgumentException(string.Format("Smiley {0} is already present in the emoji mapping", smiley.Key));
ThrowHelper.ArgumentException(string.Format("Smiley {0} is already present in the emoji mapping", smiley.Key));
}

OpeningCharacters = new List<char>(firstChars).ToArray();
Expand Down
3 changes: 2 additions & 1 deletion src/Markdig/Extensions/Emoji/EmojiParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System;
using System.Collections.Generic;
using Markdig.Helpers;
using Markdig.Parsers;
Expand Down Expand Up @@ -34,7 +35,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
}

// Try to match an emoji shortcode or smiley
if (!_emojiMapping.PrefixTree.TryMatchLongest(slice.Text, slice.Start, slice.Length, out KeyValuePair<string, string> match))
if (!_emojiMapping.PrefixTree.TryMatchLongest(slice.Text.AsSpan(slice.Start, slice.Length), out KeyValuePair<string, string> match))
{
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Markdig/Extensions/ListExtras/ListExtraItemParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

Expand Down Expand Up @@ -45,24 +45,24 @@ public override bool TryParse(BlockProcessor state, char pendingBulletType, out
if ((isRomanLow || isRomanUp) && (pendingBulletType == '\0' || pendingBulletType == 'i' || pendingBulletType == 'I'))
{
int startChar = state.Start;
int endChar = 0;
// With a roman, we can have multiple characters
// Note that we don't validate roman numbers
while (isRomanLow ? CharHelper.IsRomanLetterLowerPartial(c) : CharHelper.IsRomanLetterUpperPartial(c))
do
{
endChar = state.Start;
c = state.NextChar();
}
while (isRomanLow ? CharHelper.IsRomanLetterLowerPartial(c) : CharHelper.IsRomanLetterUpperPartial(c));

result.OrderedStart = CharHelper.RomanToArabic(state.Line.Text.Substring(startChar, endChar - startChar + 1)).ToString();
int orderValue = CharHelper.RomanToArabic(state.Line.Text.AsSpan(startChar, state.Start - startChar));
result.OrderedStart = CharHelper.SmallNumberToString(orderValue);
result.BulletType = isRomanLow ? 'i' : 'I';
result.DefaultOrderedStart = isRomanLow ? "i" : "I";
}
else
{
// otherwise we expect a regular alpha lettered list with a single character.
var isUpper = c.IsAlphaUpper();
result.OrderedStart = (Char.ToUpperInvariant(c) - 64).ToString();
result.OrderedStart = CharHelper.SmallNumberToString((c | 0x20) - 'a' + 1);
result.BulletType = isUpper ? 'A' : 'a';
result.DefaultOrderedStart = isUpper ? "A" : "a";
state.NextChar();
Expand Down
6 changes: 4 additions & 2 deletions src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Markdig.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -40,9 +41,10 @@ public bool TryHandle(Uri mediaUri, bool isSchemaRelative, out string iframeUrl)
public static IHostProvider Create(string hostPrefix, Func<Uri, string> handler, bool allowFullScreen = true, string iframeClass = null)
{
if (string.IsNullOrEmpty(hostPrefix))
throw new ArgumentException("hostPrefix is null or empty.", nameof(hostPrefix));
ThrowHelper.ArgumentException("hostPrefix is null or empty.", nameof(hostPrefix));
if (handler == null)
throw new ArgumentNullException(nameof(handler));
ThrowHelper.ArgumentNullException(nameof(handler));

return new DelegateProvider { HostPrefix = hostPrefix, Delegate = handler, AllowFullScreen = allowFullScreen, Class = iframeClass };
}

Expand Down
9 changes: 5 additions & 4 deletions src/Markdig/Extensions/SelfPipeline/SelfPipelineExtension.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System;
using Markdig.Helpers;
using Markdig.Renderers;

namespace Markdig.Extensions.SelfPipeline
Expand All @@ -29,7 +30,7 @@ public SelfPipelineExtension(string tag = null, string defaultExtensions = null)
tag = string.IsNullOrEmpty(tag) ? DefaultTag : tag;
if (tag.IndexOfAny(new []{'<', '>'}) >= 0)
{
throw new ArgumentException("Tag cannot contain `<` or `>` characters", nameof(tag));
ThrowHelper.ArgumentException("Tag cannot contain `<` or `>` characters", nameof(tag));
}

if (defaultExtensions != null)
Expand Down Expand Up @@ -57,7 +58,7 @@ public void Setup(MarkdownPipelineBuilder pipeline)
// Make sure that this pipeline has only one extension (itself)
if (pipeline.Extensions.Count > 1)
{
throw new InvalidOperationException(
ThrowHelper.InvalidOperationException(
"The SelfPipeline extension cannot be configured with other extensions");
}
}
Expand All @@ -74,7 +75,7 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
/// <exception cref="System.ArgumentNullException"></exception>
public MarkdownPipeline CreatePipelineFromInput(string inputText)
{
if (inputText == null) throw new ArgumentNullException(nameof(inputText));
if (inputText == null) ThrowHelper.ArgumentNullException(nameof(inputText));

var builder = new MarkdownPipelineBuilder();
string defaultConfig = DefaultExtensions;
Expand Down
3 changes: 1 addition & 2 deletions src/Markdig/Extensions/Tables/PipeTableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public class PipeTableParser : InlineParser, IPostInlineProcessor
/// <param name="options">The options.</param>
public PipeTableParser(LineBreakInlineParser lineBreakParser, PipeTableOptions options = null)
{
if (lineBreakParser == null) throw new ArgumentNullException(nameof(lineBreakParser));
this.lineBreakParser = lineBreakParser;
this.lineBreakParser = lineBreakParser ?? throw new ArgumentNullException(nameof(lineBreakParser));
OpeningCharacters = new[] { '|', '\n' };
Options = options ?? new PipeTableOptions();
}
Expand Down
17 changes: 0 additions & 17 deletions src/Markdig/Helpers/ArrayHelper.cs

This file was deleted.

Loading

0 comments on commit 5c52a72

Please sign in to comment.