From 09efd2938b511dda510ab3047b76353918b620e0 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Fri, 22 Nov 2024 19:50:06 +0100 Subject: [PATCH 1/2] update --- src/DotMarkdown/IMarkdownEscaperProvider.cs | 8 ++++++++ src/DotMarkdown/MarkdownBaseWriter.cs | 9 ++++++--- src/DotMarkdown/MarkdownCharEscaper.cs | 18 +++++++++--------- src/DotMarkdown/MarkdownEscaperProvider.cs | 10 ++++++++++ src/DotMarkdown/MarkdownWriterSettings.cs | 6 +++++- 5 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/DotMarkdown/IMarkdownEscaperProvider.cs create mode 100644 src/DotMarkdown/MarkdownEscaperProvider.cs diff --git a/src/DotMarkdown/IMarkdownEscaperProvider.cs b/src/DotMarkdown/IMarkdownEscaperProvider.cs new file mode 100644 index 00000000..1b8a64fd --- /dev/null +++ b/src/DotMarkdown/IMarkdownEscaperProvider.cs @@ -0,0 +1,8 @@ +// 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. + +namespace DotMarkdown; + +public interface IMarkdownEscaperProvider +{ + MarkdownCharEscaper GetDefaultEscaper(); +} \ No newline at end of file diff --git a/src/DotMarkdown/MarkdownBaseWriter.cs b/src/DotMarkdown/MarkdownBaseWriter.cs index 5904cfbe..5b51250f 100644 --- a/src/DotMarkdown/MarkdownBaseWriter.cs +++ b/src/DotMarkdown/MarkdownBaseWriter.cs @@ -31,7 +31,10 @@ internal abstract class MarkdownBaseWriter : MarkdownWriter protected MarkdownBaseWriter(MarkdownWriterSettings? settings = null) { - Settings = settings ?? MarkdownWriterSettings.Default; + settings ??= MarkdownWriterSettings.Default; + + Settings = settings; + Escaper = settings.EscaperProvider.GetDefaultEscaper(); _codeFenceRegex = Format.CodeFenceStyle switch { @@ -80,7 +83,7 @@ public override WriteState WriteState protected internal abstract int Length { get; set; } - protected MarkdownCharEscaper Escaper { get; set; } = MarkdownCharEscaper.Default; + protected MarkdownCharEscaper Escaper { get; set; } private TableColumnInfo CurrentColumn => _tableColumns![_tableColumnIndex]; @@ -704,7 +707,7 @@ public override void WriteEndLink(string url, string? title = null) ThrowIfCannotWriteEnd(State.Link); - Escaper = MarkdownCharEscaper.Default; + Escaper = Settings.EscaperProvider.GetDefaultEscaper(); WriteRaw("]"); WriteRaw("("); WriteString(url, MarkdownCharEscaper.LinkUrl); diff --git a/src/DotMarkdown/MarkdownCharEscaper.cs b/src/DotMarkdown/MarkdownCharEscaper.cs index 5e520adb..1e5083df 100644 --- a/src/DotMarkdown/MarkdownCharEscaper.cs +++ b/src/DotMarkdown/MarkdownCharEscaper.cs @@ -2,23 +2,23 @@ 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); diff --git a/src/DotMarkdown/MarkdownEscaperProvider.cs b/src/DotMarkdown/MarkdownEscaperProvider.cs new file mode 100644 index 00000000..6e4947f0 --- /dev/null +++ b/src/DotMarkdown/MarkdownEscaperProvider.cs @@ -0,0 +1,10 @@ +// 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. + +namespace DotMarkdown; + +internal sealed class MarkdownEscaperProvider : IMarkdownEscaperProvider +{ + public static IMarkdownEscaperProvider Default { get; } = new MarkdownEscaperProvider(); + + public MarkdownCharEscaper GetDefaultEscaper() => MarkdownCharEscaper.Default; +} \ No newline at end of file diff --git a/src/DotMarkdown/MarkdownWriterSettings.cs b/src/DotMarkdown/MarkdownWriterSettings.cs index 8da7fca1..8607c304 100644 --- a/src/DotMarkdown/MarkdownWriterSettings.cs +++ b/src/DotMarkdown/MarkdownWriterSettings.cs @@ -12,12 +12,14 @@ public MarkdownWriterSettings( MarkdownFormat? format = null, string? newLineChars = null, NewLineHandling newLineHandling = NewLineHandling.Replace, - bool closeOutput = false) + bool closeOutput = false, + IMarkdownEscaperProvider? escaper = null) { Format = format ?? MarkdownFormat.Default; NewLineChars = newLineChars ?? Environment.NewLine; NewLineHandling = newLineHandling; CloseOutput = closeOutput; + EscaperProvider = escaper ?? MarkdownEscaperProvider.Default; } public static MarkdownWriterSettings Default { get; } = new(); @@ -34,6 +36,8 @@ public MarkdownWriterSettings( public bool CloseOutput { get; } + public IMarkdownEscaperProvider EscaperProvider { get; } + public MarkdownWriterSettings WithFormat(MarkdownFormat format) { return new MarkdownWriterSettings(format, NewLineChars, NewLineHandling, CloseOutput); From 5b376feaed5f082fc8f6619080c21548d54e2669 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Fri, 29 Nov 2024 06:21:17 +0100 Subject: [PATCH 2/2] format --- src/DotMarkdown/IMarkdownEscaperProvider.cs | 2 +- src/DotMarkdown/MarkdownEscaperProvider.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DotMarkdown/IMarkdownEscaperProvider.cs b/src/DotMarkdown/IMarkdownEscaperProvider.cs index 1b8a64fd..dbf99239 100644 --- a/src/DotMarkdown/IMarkdownEscaperProvider.cs +++ b/src/DotMarkdown/IMarkdownEscaperProvider.cs @@ -5,4 +5,4 @@ namespace DotMarkdown; public interface IMarkdownEscaperProvider { MarkdownCharEscaper GetDefaultEscaper(); -} \ No newline at end of file +} diff --git a/src/DotMarkdown/MarkdownEscaperProvider.cs b/src/DotMarkdown/MarkdownEscaperProvider.cs index 6e4947f0..187cd635 100644 --- a/src/DotMarkdown/MarkdownEscaperProvider.cs +++ b/src/DotMarkdown/MarkdownEscaperProvider.cs @@ -7,4 +7,4 @@ internal sealed class MarkdownEscaperProvider : IMarkdownEscaperProvider public static IMarkdownEscaperProvider Default { get; } = new MarkdownEscaperProvider(); public MarkdownCharEscaper GetDefaultEscaper() => MarkdownCharEscaper.Default; -} \ No newline at end of file +}