Skip to content

Commit

Permalink
impl
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Nov 22, 2024
1 parent 87c8998 commit 9f1bb92
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/DotMarkdown/IMarkdownContextEscaper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DotMarkdown;

public interface IMarkdownContextEscaper
{
MarkdownCharEscaper GetEscaper(MarkdownEscapeContext context);
}
22 changes: 12 additions & 10 deletions src/DotMarkdown/MarkdownBaseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public override WriteState WriteState

private bool IsLastColumn => _tableColumnIndex == _tableColumnCount - 1;

private MarkdownCharEscaper GetEscaper(MarkdownEscapeContext escapeContext) => Settings.Escaper.GetEscaper(escapeContext);

protected string EscapeChar(int ch)
{
switch (ch)
Expand Down Expand Up @@ -373,7 +375,7 @@ public override void WriteInlineCode(string text)
if (text[0] == '`')
WriteRaw(" ");

WriteString(text, (_tableColumnCount > 0) ? MarkdownCharEscaper.InlineCodeInsideTable : MarkdownCharEscaper.NoEscape);
WriteString(text, (_tableColumnCount > 0) ? GetEscaper(MarkdownEscapeContext.InlineCodeInsideTable) : GetEscaper(MarkdownEscapeContext.NoEscape));

if (text[length - 1] == '`')
WriteRaw(" ");
Expand Down Expand Up @@ -667,7 +669,7 @@ public override void WriteImage(string text, string url, string? title = null)
WriteRaw("!");
WriteSquareBrackets(text);
WriteRaw("(");
WriteString(url, MarkdownCharEscaper.LinkUrl);
WriteString(url, GetEscaper(MarkdownEscapeContext.LinkUrl));
WriteLinkTitle(title);
WriteRaw(")");

Expand All @@ -687,7 +689,7 @@ public override void WriteStartLink()
Push(State.Link);

WriteRaw("[");
Escaper = MarkdownCharEscaper.LinkText;
Escaper = GetEscaper(MarkdownEscapeContext.LinkText);
}
catch
{
Expand All @@ -704,10 +706,10 @@ public override void WriteEndLink(string url, string? title = null)

ThrowIfCannotWriteEnd(State.Link);

Escaper = MarkdownCharEscaper.Default;
Escaper = GetEscaper(MarkdownEscapeContext.Default);
WriteRaw("]");
WriteRaw("(");
WriteString(url, MarkdownCharEscaper.LinkUrl);
WriteString(url, GetEscaper(MarkdownEscapeContext.LinkUrl));
WriteLinkTitle(title);
WriteRaw(")");

Expand Down Expand Up @@ -817,21 +819,21 @@ private void WriteLinkTitle(string? title)

WriteRaw(" ");
WriteRaw("\"");
WriteString(title!, MarkdownCharEscaper.LinkTitle);
WriteString(title!, GetEscaper(MarkdownEscapeContext.LinkTitle));
WriteRaw("\"");
}

private void WriteSquareBrackets(string text)
{
WriteRaw("[");
WriteString(text, MarkdownCharEscaper.LinkText);
WriteString(text, GetEscaper(MarkdownEscapeContext.LinkText));
WriteRaw("]");
}

private void WriteAngleBrackets(string text)
{
WriteRaw("<");
WriteString(text, MarkdownCharEscaper.AngleBrackets);
WriteString(text, GetEscaper(MarkdownEscapeContext.AngleBrackets));
WriteRaw(">");
}

Expand All @@ -841,7 +843,7 @@ public override void WriteIndentedCodeBlock(string text)
{
Push(State.IndentedCodeBlock);
WriteLine(Format.EmptyLineBeforeCodeBlock);
WriteString(text, MarkdownCharEscaper.NoEscape);
WriteString(text, GetEscaper(MarkdownEscapeContext.NoEscape));
WriteLine();
WriteEmptyLineIf(Format.EmptyLineAfterCodeBlock);
Pop(State.IndentedCodeBlock);
Expand Down Expand Up @@ -923,7 +925,7 @@ public override void WriteFencedCodeBlock(string text, string? info = null)
WriteRaw(info!);

WriteLine();
WriteString(text, MarkdownCharEscaper.NoEscape);
WriteString(text, GetEscaper(MarkdownEscapeContext.NoEscape));
WriteLineIfNecessary();
WriteFence(length);
WriteLine();
Expand Down
20 changes: 10 additions & 10 deletions src/DotMarkdown/MarkdownCharEscaper.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version .0. See License.txt in the project root for license information.

namespace DotMarkdown;

internal abstract class MarkdownCharEscaper
public abstract class MarkdownCharEscaper
{
public static char DefaultEscapingChar { get; } = '\\';
internal static char DefaultEscapingChar { get; } = '\\';

public static MarkdownCharEscaper Default { get; } = new DefaultMarkdownEscaper();
internal static MarkdownCharEscaper Default { get; } = new DefaultMarkdownEscaper();

public static MarkdownCharEscaper LinkText { get; } = new LinkTextMarkdownEscaper();
internal static MarkdownCharEscaper LinkText { get; } = new LinkTextMarkdownEscaper();

public static MarkdownCharEscaper LinkUrl { get; } = new LinkUrlMarkdownEscaper();
internal static MarkdownCharEscaper LinkUrl { get; } = new LinkUrlMarkdownEscaper();

public static MarkdownCharEscaper LinkTitle { get; } = new LinkTitleMarkdownEscaper();
internal static MarkdownCharEscaper LinkTitle { get; } = new LinkTitleMarkdownEscaper();

public static MarkdownCharEscaper AngleBrackets { get; } = new AngleBracketsMarkdownEscaper();
internal static MarkdownCharEscaper AngleBrackets { get; } = new AngleBracketsMarkdownEscaper();

public static MarkdownCharEscaper InlineCodeInsideTable { get; } = new InlineCodeInsideTableMarkdownEscaper();
internal static MarkdownCharEscaper InlineCodeInsideTable { get; } = new InlineCodeInsideTableMarkdownEscaper();

public static MarkdownCharEscaper NoEscape { get; } = new NoEscapeMarkdownEscaper();
internal static MarkdownCharEscaper NoEscape { get; } = new NoEscapeMarkdownEscaper();

public abstract bool ShouldBeEscaped(char value);

Expand Down
23 changes: 23 additions & 0 deletions src/DotMarkdown/MarkdownContextEscaper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace DotMarkdown;

internal sealed class MarkdownContextEscaper : IMarkdownContextEscaper
{
public static IMarkdownContextEscaper Default { get; } = new MarkdownContextEscaper();

public MarkdownCharEscaper GetEscaper(MarkdownEscapeContext context)
{
return context switch
{
MarkdownEscapeContext.Default => MarkdownCharEscaper.Default,
MarkdownEscapeContext.LinkText => MarkdownCharEscaper.LinkText,
MarkdownEscapeContext.LinkUrl => MarkdownCharEscaper.LinkUrl,
MarkdownEscapeContext.LinkTitle => MarkdownCharEscaper.LinkTitle,
MarkdownEscapeContext.AngleBrackets => MarkdownCharEscaper.AngleBrackets,
MarkdownEscapeContext.InlineCodeInsideTable => MarkdownCharEscaper.InlineCodeInsideTable,
MarkdownEscapeContext.NoEscape => MarkdownCharEscaper.NoEscape,
_ => throw new ArgumentOutOfRangeException(nameof(context)),
};
}
}
12 changes: 12 additions & 0 deletions src/DotMarkdown/MarkdownEscapeContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace DotMarkdown;

public enum MarkdownEscapeContext
{
Default = 0,
LinkText = 1,
LinkUrl = 2,
LinkTitle = 3,
AngleBrackets = 4,
InlineCodeInsideTable = 5,
NoEscape = 6,
}
6 changes: 5 additions & 1 deletion src/DotMarkdown/MarkdownWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public MarkdownWriterSettings(
MarkdownFormat? format = null,
string? newLineChars = null,
NewLineHandling newLineHandling = NewLineHandling.Replace,
bool closeOutput = false)
bool closeOutput = false,
IMarkdownContextEscaper? escaper = null)
{
Format = format ?? MarkdownFormat.Default;
NewLineChars = newLineChars ?? Environment.NewLine;
NewLineHandling = newLineHandling;
CloseOutput = closeOutput;
Escaper = escaper ?? MarkdownContextEscaper.Default;
}

public static MarkdownWriterSettings Default { get; } = new();
Expand All @@ -34,6 +36,8 @@ public MarkdownWriterSettings(

public bool CloseOutput { get; }

public IMarkdownContextEscaper Escaper { get; }

public MarkdownWriterSettings WithFormat(MarkdownFormat format)
{
return new MarkdownWriterSettings(format, NewLineChars, NewLineHandling, CloseOutput);
Expand Down

0 comments on commit 9f1bb92

Please sign in to comment.