Skip to content

Commit

Permalink
Merge pull request #530 from carbon/cq
Browse files Browse the repository at this point in the history
Improve Code Quality
  • Loading branch information
xoofx authored Mar 15, 2021
2 parents 7ad7b55 + 4526d88 commit 5fefcbb
Show file tree
Hide file tree
Showing 36 changed files with 114 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HtmlCustomContainerInlineRenderer : HtmlObjectRenderer<CustomContai
{
protected override void Write(HtmlRenderer renderer, CustomContainerInline obj)
{
renderer.Write("<span").WriteAttributes(obj).Write(">");
renderer.Write("<span").WriteAttributes(obj).Write('>');
renderer.WriteChildren(obj);
renderer.Write("</span>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected override void Write(HtmlRenderer renderer, CustomContainer obj)
renderer.EnsureLine();
if (renderer.EnableHtmlForBlock)
{
renderer.Write("<div").WriteAttributes(obj).Write(">");
renderer.Write("<div").WriteAttributes(obj).Write('>');
}
// We don't escape a CustomContainer
renderer.WriteChildren(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class HtmlDefinitionListRenderer : HtmlObjectRenderer<DefinitionList>
protected override void Write(HtmlRenderer renderer, DefinitionList list)
{
renderer.EnsureLine();
renderer.Write("<dl").WriteAttributes(list).WriteLine(">");
renderer.Write("<dl").WriteAttributes(list).WriteLine('>');
foreach (var item in list)
{
bool hasOpendd = false;
Expand All @@ -41,15 +41,15 @@ protected override void Write(HtmlRenderer renderer, DefinitionList list)
hasOpendd = false;
countdd = 0;
}
renderer.Write("<dt").WriteAttributes(definitionTerm).Write(">");
renderer.Write("<dt").WriteAttributes(definitionTerm).Write('>');
renderer.WriteLeafInline(definitionTerm);
renderer.WriteLine("</dt>");
}
else
{
if (!hasOpendd)
{
renderer.Write("<dd").WriteAttributes(definitionItem).Write(">");
renderer.Write("<dd").WriteAttributes(definitionItem).Write('>');
countdd = 0;
hasOpendd = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class HtmlFigureCaptionRenderer : HtmlObjectRenderer<FigureCaption>
protected override void Write(HtmlRenderer renderer, FigureCaption obj)
{
renderer.EnsureLine();
renderer.Write("<figcaption").WriteAttributes(obj).Write(">");
renderer.Write("<figcaption").WriteAttributes(obj).Write('>');
renderer.WriteLeafInline(obj);
renderer.WriteLine("</figcaption>");
}
Expand Down
3 changes: 1 addition & 2 deletions src/Markdig/Extensions/Footnotes/Footnote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class Footnote : ContainerBlock
{
public Footnote(BlockParser parser) : base(parser)
{
Links = new List<FootnoteLink>();
Order = -1;
}

Expand All @@ -33,7 +32,7 @@ public Footnote(BlockParser parser) : base(parser)
/// <summary>
/// Gets the links referencing this footnote.
/// </summary>
public List<FootnoteLink> Links { get; private set; }
public List<FootnoteLink> Links { get; } = new ();

/// <summary>
/// The label span
Expand Down
6 changes: 3 additions & 3 deletions src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ private static string[] SplitQuery(Uri uri)
}
var queryParams = SplitQuery(uri);
return BuildYouTubeIframeUrl(
queryParams.FirstOrDefault(p => p.StartsWith("v="))?.Substring(2),
queryParams.FirstOrDefault(p => p.StartsWith("t="))?.Substring(2)
queryParams.FirstOrDefault(p => p.StartsWith("v=", StringComparison.Ordinal))?.Substring(2),
queryParams.FirstOrDefault(p => p.StartsWith("t=", StringComparison.Ordinal))?.Substring(2)
);
}

private static string? YouTubeShortened(Uri uri)
{
return BuildYouTubeIframeUrl(
uri.AbsolutePath.Substring(1),
SplitQuery(uri).FirstOrDefault(p => p.StartsWith("t="))?.Substring(2)
SplitQuery(uri).FirstOrDefault(p => p.StartsWith("t=", StringComparison.Ordinal))?.Substring(2)
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private bool TryLinkInlineRenderer(HtmlRenderer renderer, LinkInline linkInline)
{
// see https://tools.ietf.org/html/rfc3986#section-4.2
// since relative uri doesn't support many properties, "http" is used as a placeholder here.
if (linkInline.Url.StartsWith("//") && Uri.TryCreate("http:" + linkInline.Url, UriKind.Absolute, out uri))
if (linkInline.Url.StartsWith("//", StringComparison.Ordinal) && Uri.TryCreate("http:" + linkInline.Url, UriKind.Absolute, out uri))
{
isSchemaRelative = true;
}
Expand Down Expand Up @@ -101,7 +101,7 @@ private bool TryGuessAudioVideoFile(Uri uri, bool isSchemaRelative, HtmlRenderer
Options.ExtensionToMimeType.TryGetValue(path.Substring(lastDot), out string? mimeType))
{
var htmlAttributes = GetHtmlAttributes(linkInline);
var isAudio = mimeType.StartsWith("audio");
var isAudio = mimeType.StartsWith("audio", StringComparison.Ordinal);
var tagType = isAudio ? "audio" : "video";

renderer.Write($"<{tagType}");
Expand Down Expand Up @@ -144,7 +144,7 @@ private bool TryRenderIframeFromKnownProviders(Uri uri, bool isSchemaRelative, H
var htmlAttributes = GetHtmlAttributes(linkInline);
renderer.Write("<iframe src=\"");
renderer.WriteEscapeUrl(iframeUrl);
renderer.Write("\"");
renderer.Write('"');

if (!string.IsNullOrEmpty(Options.Width))
htmlAttributes.AddPropertyIfNotExist("width", Options.Width);
Expand Down
6 changes: 3 additions & 3 deletions src/Markdig/Extensions/Tables/HtmlTableRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HtmlTableRenderer : HtmlObjectRenderer<Table>
protected override void Write(HtmlRenderer renderer, Table table)
{
renderer.EnsureLine();
renderer.Write("<table").WriteAttributes(table).WriteLine(">");
renderer.Write("<table").WriteAttributes(table).WriteLine('>');

bool hasBody = false;
bool hasAlreadyHeader = false;
Expand Down Expand Up @@ -69,7 +69,7 @@ protected override void Write(HtmlRenderer renderer, Table table)
renderer.WriteLine("<tbody>");
hasBody = true;
}
renderer.Write("<tr").WriteAttributes(row).WriteLine(">");
renderer.Write("<tr").WriteAttributes(row).WriteLine('>');
for (int i = 0; i < row.Count; i++)
{
var cellObj = row[i];
Expand Down Expand Up @@ -109,7 +109,7 @@ protected override void Write(HtmlRenderer renderer, Table table)
}
}
renderer.WriteAttributes(cell);
renderer.Write(">");
renderer.Write('>');

var previousImplicitParagraph = renderer.ImplicitParagraph;
if (cell.Count == 1)
Expand Down
13 changes: 3 additions & 10 deletions src/Markdig/Extensions/Tables/PipeTableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -625,24 +625,17 @@ private static bool IsNullOrSpace(Inline? inline)

private sealed class TableState
{
public TableState()
{
ColumnAndLineDelimiters = new List<Inline>();
Cells = new List<TableCell>();
EndOfLines = new List<Inline>();
}

public bool IsInvalidTable { get; set; }

public bool LineHasPipe { get; set; }

public int LineIndex { get; set; }

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

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

public List<Inline> EndOfLines { get; }
public List<Inline> EndOfLines { get; } = new();
}
}
}
5 changes: 2 additions & 3 deletions src/Markdig/Extensions/Tables/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Table : ContainerBlock
/// <summary>
/// Initializes a new instance of the <see cref="Table"/> class.
/// </summary>
public Table() : this(null)
public Table() : base(null)
{
}

Expand All @@ -27,13 +27,12 @@ public Table() : this(null)
/// <param name="parser">The parser used to create this block.</param>
public Table(BlockParser? parser) : base(parser)
{
ColumnDefinitions = new List<TableColumnDefinition>();
}

/// <summary>
/// Gets or sets the column alignments. May be null.
/// </summary>
public List<TableColumnDefinition> ColumnDefinitions { get; }
public List<TableColumnDefinition> ColumnDefinitions { get; } = new();

/// <summary>
/// Checks if the table structure is valid.
Expand Down
12 changes: 5 additions & 7 deletions src/Markdig/Helpers/CharacterMap.cs
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.

#nullable disable

using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -16,10 +14,10 @@ namespace Markdig.Helpers
/// Allows to associate characters to a data structures and query efficiently for them.
/// </summary>
/// <typeparam name="T"></typeparam>
public class CharacterMap<T> where T : class
public sealed class CharacterMap<T> where T : class
{
private readonly T[] asciiMap;
private readonly Dictionary<uint, T> nonAsciiMap;
private readonly Dictionary<uint, T>? nonAsciiMap;
private readonly BoolVector128 isOpeningCharacter;

/// <summary>
Expand Down Expand Up @@ -78,7 +76,7 @@ public CharacterMap(IEnumerable<KeyValuePair<char, T>> maps)
/// </summary>
/// <param name="openingChar">The opening character.</param>
/// <returns>A list of parsers valid for the specified opening character or null if no parsers registered.</returns>
public T this[uint openingChar]
public T? this[uint openingChar]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
Expand All @@ -90,7 +88,7 @@ public T this[uint openingChar]
}
else
{
T map = null;
T? map = null;
nonAsciiMap?.TryGetValue(openingChar, out map);
return map;
}
Expand Down Expand Up @@ -174,7 +172,7 @@ private int IndexOfOpeningCharacterNonAscii(string text, int start, int end)
for (int i = start; i <= end; i++)
{
char c = pText[i];
if (c < 128 ? isOpeningCharacter[c] : nonAsciiMap.ContainsKey(c))
if (c < 128 ? isOpeningCharacter[c] : nonAsciiMap!.ContainsKey(c))
{
return i;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Markdig/Helpers/CompactPrefixTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ private bool TryInsert(in KeyValuePair<string, TValue> pair, InsertionBehavior b
Debug.Assert(key.Length != previousKey.Length);
if (previousKey.Length < key.Length) // If the input was sorted, this should be hit
{
Debug.Assert(key.StartsWith(previousKey));
Debug.Assert(key.StartsWith(previousKey, StringComparison.Ordinal));
node.ChildChar = key[i];
node.MatchIndex = previousMatchIndex;
EnsureTreeCapacity(TreeSize + 1);
Expand All @@ -533,7 +533,7 @@ private bool TryInsert(in KeyValuePair<string, TValue> pair, InsertionBehavior b
else // if key.Length < previousKey.Length
{
Debug.Assert(key.Length < previousKey.Length);
Debug.Assert(previousKey.StartsWith(key));
Debug.Assert(previousKey.StartsWith(key, StringComparison.Ordinal));
node.ChildChar = previousKey[i];
node.MatchIndex = Count;
EnsureTreeCapacity(TreeSize + 1);
Expand Down Expand Up @@ -583,7 +583,7 @@ private bool TryInsert(in KeyValuePair<string, TValue> pair, InsertionBehavior b
else
{
// This node has a child char, therefore we either don't have a match attached or that match is simply a prefix of the current key
Debug.Assert(node.MatchIndex == -1 || key.StartsWith(_matches[node.MatchIndex].Key));
Debug.Assert(node.MatchIndex == -1 || key.StartsWith(_matches[node.MatchIndex].Key, StringComparison.Ordinal));

// Set this pair as the current node's first element in the Children list
node.Children = _childrenIndex;
Expand Down Expand Up @@ -641,7 +641,7 @@ private bool TryInsert(in KeyValuePair<string, TValue> pair, InsertionBehavior b
// It's not a duplicate but shares key.Length characters, therefore it's longer
// This will never occur if the input was sorted
Debug.Assert(previousMatch.Key.Length > key.Length);
Debug.Assert(previousMatch.Key.StartsWith(key));
Debug.Assert(previousMatch.Key.StartsWith(key, StringComparison.Ordinal));
Debug.Assert(node.ChildChar == 0 && node.Children == -1);

// It is a leaf node
Expand Down
2 changes: 2 additions & 0 deletions src/Markdig/Markdig.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>0.23.0</VersionPrefix>
<Authors>Alexandre Mutel</Authors>
<!-- Markdig.Wpf still supports net452, a target still supported by by Microsoft until January 10, 2023 -->
<!-- see: https://github.com/xoofx/markdig/pull/466 -->
<TargetFrameworks>net452;netstandard2.0;netstandard2.1;netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<PackageTags>Markdown CommonMark md html md2html</PackageTags>
<PackageReleaseNotes>https://github.com/lunet-io/markdig/blob/master/changelog.md</PackageReleaseNotes>
Expand Down
1 change: 0 additions & 1 deletion src/Markdig/Parsers/IndentedCodeBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public override BlockState TryContinue(BlockProcessor processor, Block? block)
{
TriviaBefore = processor.UseTrivia(processor.Start - 1)
};
cb.CodeBlockLines ??= new List<CodeBlockLine>();
cb.CodeBlockLines.Add(codeBlockLine);
cb.NewLine = processor.Line.NewLine; // ensure block newline is last newline
}
Expand Down
3 changes: 1 addition & 2 deletions src/Markdig/Parsers/Inlines/AutolinkInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
int column;
if (LinkHelper.TryParseAutolink(ref slice, out string? link, out bool isEmail))
{
processor.Inline = new AutolinkInline()
processor.Inline = new AutolinkInline(link)
{
IsEmail = isEmail,
Url = link,
Span = new SourceSpan(processor.GetSourcePosition(saved.Start, out line, out column), processor.GetSourcePosition(slice.Start - 1)),
Line = line,
Column = column
Expand Down
3 changes: 1 addition & 2 deletions src/Markdig/Parsers/Inlines/CodeInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
int delimiterCount = Math.Min(openSticks, closeSticks);
var spanStart = processor.GetSourcePosition(startPosition, out int line, out int column);
var spanEnd = processor.GetSourcePosition(slice.Start - 1);
processor.Inline = new CodeInline()
processor.Inline = new CodeInline(content)
{
Delimiter = match,
Content = content,
ContentWithTrivia = new StringSlice(slice.Text, contentStart, contentEnd - 1),
Span = new SourceSpan(spanStart, spanEnd),
Line = line,
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Parsers/ListBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private BlockState TryParseListItem(BlockProcessor state, Block? block)
if ((block ?? state.LastBlock) is ParagraphBlock previousParagraph)
{
if (state.IsBlankLine ||
state.IsOpen(previousParagraph) && listInfo.BulletType == '1' && listInfo.OrderedStart != "1")
state.IsOpen(previousParagraph) && listInfo.BulletType == '1' && listInfo.OrderedStart is not "1")
{
state.GoToColumn(initColumn);
state.TriviaStart = savedTriviaStart; // restore changed TriviaStart state
Expand Down
6 changes: 3 additions & 3 deletions src/Markdig/Renderers/Html/CodeBlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ protected override void Write(HtmlRenderer renderer, CodeBlock obj)
{
renderer.Write("<div")
.WriteAttributes(obj.TryGetAttributes(),
cls => cls.StartsWith(infoPrefix) ? cls.Substring(infoPrefix.Length) : cls)
.Write(">");
cls => cls.StartsWith(infoPrefix, StringComparison.Ordinal) ? cls.Substring(infoPrefix.Length) : cls)
.Write('>');
}

renderer.WriteLeafRawLines(obj, true, true, true);
Expand Down Expand Up @@ -77,7 +77,7 @@ protected override void Write(HtmlRenderer renderer, CodeBlock obj)
renderer.WriteAttributes(obj);
}

renderer.Write(">");
renderer.Write('>');
}

renderer.WriteLeafRawLines(obj, true, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Renderers/Html/HeadingRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override void Write(HtmlRenderer renderer, HeadingBlock obj)

if (renderer.EnableHtmlForBlock)
{
renderer.Write("<").Write(headingText).WriteAttributes(obj).Write(">");
renderer.Write("<").Write(headingText).WriteAttributes(obj).Write('>');
}

renderer.WriteLeafInline(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected override void Write(HtmlRenderer renderer, AutolinkInline obj)
renderer.Write($" rel=\"{Rel}\"");
}

renderer.Write(">");
renderer.Write('>');
}

renderer.WriteEscape(obj.Url);
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Renderers/Html/Inlines/CodeInlineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override void Write(HtmlRenderer renderer, CodeInline obj)
{
if (renderer.EnableHtmlForInline)
{
renderer.Write("<code").WriteAttributes(obj).Write(">");
renderer.Write("<code").WriteAttributes(obj).Write('>');
}
if (renderer.EnableHtmlEscape)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Markdig/Renderers/Html/Inlines/EmphasisInlineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ protected override void Write(HtmlRenderer renderer, EmphasisInline obj)
if (renderer.EnableHtmlForInline)
{
tag = GetTag(obj);
renderer.Write("<").Write(tag).WriteAttributes(obj).Write(">");
renderer.Write("<").Write(tag).WriteAttributes(obj).Write('>');
}
renderer.WriteChildren(obj);
if (renderer.EnableHtmlForInline)
{
renderer.Write("</").Write(tag).Write(">");
renderer.Write("</").Write(tag).Write('>');
}
}

Expand Down
Loading

0 comments on commit 5fefcbb

Please sign in to comment.