Skip to content

Commit

Permalink
Add a facade for markup conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Nov 3, 2024
1 parent 9a93972 commit 9149d90
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
24 changes: 6 additions & 18 deletions GW2SDK.Tests/Features/Markup/MarkupHtmlConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ public class MarkupHtmlConverterTest
public void Converts_color_tags_to_span_elements()
{
var input = "This is <c=@flavor>flavor</c> text and this is <c=#FF0000>inline styled</c> text.";
var lexer = new MarkupLexer();
var parser = new MarkupParser();
var converter = new MarkupHtmlConverter();
var tokens = lexer.Tokenize(input);
var syntax = parser.Parse(tokens);
var actual = converter.Convert(syntax);

var actual = MarkupConverter.ToHtml(input);

Assert.Equal(
"This is <span style=\"color: #9BE8E4\">flavor</span> text and this is <span style=\"color: #FF0000\">inline styled</span> text.",
Expand All @@ -25,12 +21,8 @@ public void Converts_color_tags_to_span_elements()
public void Converts_br()
{
var input = "Line 1.<br>Line 2.<br/>Line 3.<br />Line 4.";
var lexer = new MarkupLexer();
var parser = new MarkupParser();
var converter = new MarkupHtmlConverter();
var tokens = lexer.Tokenize(input);
var syntax = parser.Parse(tokens);
var actual = converter.Convert(syntax);

var actual = MarkupConverter.ToHtml(input);

Assert.Equal(
"Line 1.<br>Line 2.<br>Line 3.<br>Line 4.",
Expand All @@ -42,12 +34,8 @@ public void Converts_br()
public void Converts_line_feeds()
{
var input = "Line 1.\nLine 2.\nLine 3.\nLine 4.";
var lexer = new MarkupLexer();
var parser = new MarkupParser();
var converter = new MarkupHtmlConverter();
var tokens = lexer.Tokenize(input);
var syntax = parser.Parse(tokens);
var actual = converter.Convert(syntax);

var actual = MarkupConverter.ToHtml(input);

Assert.Equal(
"Line 1.<br>Line 2.<br>Line 3.<br>Line 4.",
Expand Down
8 changes: 2 additions & 6 deletions GW2SDK.Tests/Features/Markup/MarkupTextConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ public void Describe_End_of_Dragons_Launch_Supply_Drop_Requisition()
"Dye Canisters—Red, and 2 Fine Black Lion Dye Canisters—Yellow.<br>Fourth " +
"Drop:<br>A Gold Essence Weapon Choice, 2 Guaranteed Wardrobe Unlocks, and " +
"2 Black Lion Miniature Claim Tickets.";
var lexer = new MarkupLexer();
var parser = new MarkupParser();
var converter = new MarkupTextConverter();
var tokens = lexer.Tokenize(input);
var syntax = parser.Parse(tokens);
var actual = converter.Convert(syntax);

var actual = MarkupConverter.ToPlainText(input);

Assert.Equal(
"""
Expand Down
45 changes: 45 additions & 0 deletions GW2SDK/Features/Markup/MarkupConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace GuildWars2.Markup;

/// <summary>Provides functionality to convert markup strings to other formats.</summary>
[PublicAPI]
public static class MarkupConverter
{
private static readonly MarkupLexer Lexer = new();

private static readonly MarkupParser Parser = new();

private static readonly MarkupTextConverter TextConverter = new();

private static readonly MarkupHtmlConverter HtmlConverter = new();

/// <summary>Converts a markup string to a string with all markup formatting removed.</summary>
/// <param name="markup">The markup string to convert.</param>
/// <returns>The text with all markup formatting removed.</returns>
public static string ToPlainText(string markup)
{
if (string.IsNullOrWhiteSpace(markup))
{
return markup;
}

var tokens = Lexer.Tokenize(markup);
var rootNode = Parser.Parse(tokens);
return TextConverter.Convert(rootNode);
}

/// <summary>Converts a markup string to a string with HTML formatting.</summary>
/// <param name="markup">The markup string to convert.</param>
/// <returns>The HTML string.</returns>
public static string ToHtml(string markup)
{
if (string.IsNullOrWhiteSpace(markup))
{
return markup;
}

var tokens = Lexer.Tokenize(markup);
var rootNode = Parser.Parse(tokens);
return HtmlConverter.Convert(rootNode);
}

}

0 comments on commit 9149d90

Please sign in to comment.