-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow customization of escaping
Adds ITextFormatter abstraction for escaping text. The formatter that is used for escaping text during serialization of spans can be customized by providing a implementation of ITextFormatter and passing it to serialization via MdSerializationOptions. The formatter is specified using the newly introduced property MdSerializationOptions.TextFormatter. If no formatter is specified, DefaultTextFormatter is used which escapes exactly like the previous implementation in MdTextSpan.
- Loading branch information
Showing
8 changed files
with
146 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/MarkdownGenerator/_Model/_Options/DefaultTextFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Text; | ||
|
||
namespace Grynwald.MarkdownGenerator | ||
{ | ||
/// <summary> | ||
/// Default implementation of <see cref="ITextFormatter"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// <see cref="DefaultTextFormatter"/> escapes all characters with a function in the Markdown syntax | ||
/// using a backslash. | ||
/// </remarks> | ||
public sealed class DefaultTextFormatter : ITextFormatter | ||
{ | ||
/// <summary> | ||
/// Gets the singleton instance of <see cref="DefaultTextFormatter"/> | ||
/// </summary> | ||
public static readonly DefaultTextFormatter Instance = new DefaultTextFormatter(); | ||
|
||
|
||
private DefaultTextFormatter() | ||
{ } | ||
|
||
|
||
/// <inheritdoc /> | ||
public string EscapeText(string text) | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
for (var i = 0; i < text.Length; i++) | ||
{ | ||
switch (text[i]) | ||
{ | ||
case '\\': | ||
case '/': | ||
case '<': | ||
case '>': | ||
case '*': | ||
case '_': | ||
case '-': | ||
case '=': | ||
case '#': | ||
case '`': | ||
case '~': | ||
case '[': | ||
case ']': | ||
case '!': | ||
case '|': | ||
stringBuilder.Append('\\'); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
stringBuilder.Append(text[i]); | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Grynwald.MarkdownGenerator | ||
{ | ||
/// <summary> | ||
/// Provides an abstraction over converting text to be written into a Markdown file. | ||
/// </summary> | ||
/// <remarks> | ||
/// When text is written to a Markdown file, some characters with a syntactical meaning | ||
/// in Markdown need to be escaped. | ||
/// <para> | ||
/// As the way escaping works differs between some Markdown-to-HTML converters, | ||
/// the escaping can be customized. | ||
/// </para> | ||
/// </remarks> | ||
/// <seealso cref="MdSerializationOptions.TextFormatter"/> | ||
/// <seealso cref="DefaultTextFormatter"/> | ||
public interface ITextFormatter | ||
{ | ||
/// <summary> | ||
/// Escapes the specified text. | ||
/// </summary> | ||
/// <param name="text">The text to process.</param> | ||
string EscapeText(string text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters