Add Markdown.Escape method

This commit is contained in:
Patrik Svensson 2020-10-19 20:48:23 +02:00 committed by Patrik Svensson
parent 70fc14e9cd
commit 1410cba6c5
5 changed files with 54 additions and 8 deletions

View File

@ -6,6 +6,24 @@ namespace Spectre.Console.Tests.Unit
{
public sealed class MarkupTests
{
public sealed class TheEscapeMethod
{
[Theory]
[InlineData("Hello World", "Hello World")]
[InlineData("Hello World [", "Hello World [[")]
[InlineData("Hello World ]", "Hello World ]]")]
[InlineData("Hello [World]", "Hello [[World]]")]
[InlineData("Hello [[World]]", "Hello [[[[World]]]]")]
public void Should_Escape_Markup_As_Expected(string input, string expected)
{
// Given, When
var result = Markup.Escape(input);
// Then
result.ShouldBe(expected);
}
}
[Theory]
[InlineData("Hello [[ World ]")]
[InlineData("Hello [[ World ] !")]

View File

@ -1,3 +1,5 @@
using System;
namespace Spectre.Console
{
/// <summary>
@ -6,12 +8,11 @@ namespace Spectre.Console
public static class StringExtensions
{
/// <summary>
/// Converts the string to something that is safe to
/// use in a markup string.
/// Escapes text so that it wont be interpreted as markup.
/// </summary>
/// <param name="text">The text to convert.</param>
/// <returns>A string that is safe to use in a markup string.</returns>
public static string SafeMarkup(this string text)
/// <param name="text">The text to escape.</param>
/// <returns>A string that is safe to use in markup.</returns>
public static string EscapeMarkup(this string text)
{
if (text == null)
{
@ -22,5 +23,16 @@ namespace Spectre.Console
.Replace("[", "[[")
.Replace("]", "]]");
}
/// <summary>
/// Escapes text so that it wont be interpreted as markup.
/// </summary>
/// <param name="text">The text to escape.</param>
/// <returns>A string that is safe to use in markup.</returns>
[Obsolete("Use EscapeMarkup extension instead.", false)]
public static string SafeMarkup(this string text)
{
return EscapeMarkup(text);
}
}
}

View File

@ -42,7 +42,7 @@ namespace Spectre.Console
{
var shortenTypes = (settings.Format & ExceptionFormats.ShortenTypes) != 0;
var type = Emphasize(ex.Type, new[] { '.' }, settings.Style.Exception, shortenTypes, settings);
var message = $"[{settings.Style.Message.ToMarkup()}]{ex.Message.SafeMarkup()}[/]";
var message = $"[{settings.Style.Message.ToMarkup()}]{ex.Message.EscapeMarkup()}[/]";
return new Markup(string.Concat(type, ": ", message));
}
@ -99,7 +99,7 @@ namespace Spectre.Console
{
var typeColor = settings.Style.ParameterType.ToMarkup();
var nameColor = settings.Style.ParameterName.ToMarkup();
var parameters = frame.Parameters.Select(x => $"[{typeColor}]{x.Type.SafeMarkup()}[/] [{nameColor}]{x.Name}[/]");
var parameters = frame.Parameters.Select(x => $"[{typeColor}]{x.Type.EscapeMarkup()}[/] [{nameColor}]{x.Name}[/]");
builder.Append(string.Join(", ", parameters));
}

View File

@ -199,7 +199,7 @@ namespace Spectre.Console
if (ShowHeader)
{
var heading = new DateTime(Year, Month, Day).ToString("Y", culture).SafeMarkup();
var heading = new DateTime(Year, Month, Day).ToString("Y", culture).EscapeMarkup();
table.Heading = new Title(heading, HeaderStyle);
}

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
@ -46,5 +47,20 @@ namespace Spectre.Console
{
return ((IRenderable)_paragraph).Render(context, maxWidth);
}
/// <summary>
/// Escapes text so that it wont be interpreted as markup.
/// </summary>
/// <param name="text">The text to escape.</param>
/// <returns>A string that is safe to use in markup.</returns>
public static string Escape(string text)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
return text.EscapeMarkup();
}
}
}