refactor!: renamed GetText to GetTextContentString

This commit is contained in:
D. Moonfire 2023-01-15 12:43:42 -06:00
parent a8c6d0e582
commit c5c9b8bf9c
11 changed files with 37 additions and 21 deletions

View file

@ -21,7 +21,7 @@ public class IdentifyHandlebarsFromContent : IOperation
Entity entity, Entity entity,
ITextContent content) ITextContent content)
{ {
string text = content.GetText(); string text = content.GetTextContentString();
if (text.Contains("{{") && text.Contains("}}")) if (text.Contains("{{") && text.Contains("}}"))
{ {

View file

@ -51,7 +51,7 @@ public partial class RenderContentTemplate : OperationBase
HasHandlebarsTemplate _, HasHandlebarsTemplate _,
ITextContent content) ITextContent content)
{ {
string text = content.GetText(); string text = content.GetTextContentString();
HandlebarsTemplate<object, object> template = HandlebarsTemplate<object, object> template =
this.cache.GetLiteralTemplate(text); this.cache.GetLiteralTemplate(text);
object model = this.CreateModelCallback!(entity); object model = this.CreateModelCallback!(entity);

View file

@ -22,7 +22,7 @@ public class ConvertHtmlEntitiesToUnicode : OperationBase
Entity entity, Entity entity,
ITextContent content) ITextContent content)
{ {
string text = content.GetText(); string text = content.GetTextContentString();
string resolved = WebUtility.HtmlDecode(text); string resolved = WebUtility.HtmlDecode(text);
return entity.SetTextContent(resolved); return entity.SetTextContent(resolved);

View file

@ -44,7 +44,7 @@ public class ConvertMarkdownToGemtext : ConvertMarkdownToBase
ITextContent markdownContent, ITextContent markdownContent,
MarkdownPipeline options) MarkdownPipeline options)
{ {
string markdown = markdownContent.GetText(); string markdown = markdownContent.GetTextContentString();
string gemtext = MarkdownGemtext.ToGemtext(markdown, options); string gemtext = MarkdownGemtext.ToGemtext(markdown, options);
var content = new StringTextContent(gemtext); var content = new StringTextContent(gemtext);

View file

@ -43,7 +43,7 @@ public class ConvertMarkdownToHtml : ConvertMarkdownToBase
MarkdownPipeline options) MarkdownPipeline options)
{ {
// Convert the entity to Html. // Convert the entity to Html.
string markdown = markdownContent.GetText(); string markdown = markdownContent.GetTextContentString();
string html = Markdig.Markdown.ToHtml(markdown, options); string html = Markdig.Markdown.ToHtml(markdown, options);
var htmlContent = new StringTextContent(html); var htmlContent = new StringTextContent(html);

View file

@ -38,7 +38,7 @@ public class MakeSingleLinkListItems : IOperation
/// </summary> /// </summary>
private Entity MakeSingleLinkLists(Entity entity) private Entity MakeSingleLinkLists(Entity entity)
{ {
string content = entity.GetText()!; string content = entity.GetTextContentString()!;
string output = Regex.Replace( string output = Regex.Replace(
content, content,

View file

@ -32,7 +32,7 @@ public static class NitrideYamlEntityExtensions
this Entity entity, this Entity entity,
IDeserializer deserializer) IDeserializer deserializer)
{ {
string? text = entity.GetText(); string? text = entity.GetTextContentString();
return text != null return text != null
? deserializer.Deserialize<TType>(text) ? deserializer.Deserialize<TType>(text)

View file

@ -1,3 +1,5 @@
using System.Text;
using MfGames.Gallium; using MfGames.Gallium;
namespace MfGames.Nitride.Contents; namespace MfGames.Nitride.Contents;
@ -41,7 +43,6 @@ public static class EntityTextContentExtensions
/// </summary> /// </summary>
/// <param name="entity">The entity to query and modify.</param> /// <param name="entity">The entity to query and modify.</param>
/// <param name="content">The content to add to the entity.</param> /// <param name="content">The content to add to the entity.</param>
/// <typeparam name="TType">The base type of the content.</typeparam>
/// <returns>The entity or a copy with the new component.</returns> /// <returns>The entity or a copy with the new component.</returns>
public static Entity SetTextContent( public static Entity SetTextContent(
this Entity entity, this Entity entity,
@ -49,4 +50,19 @@ public static class EntityTextContentExtensions
{ {
return entity.SetTextContent(new StringTextContent(content)); return entity.SetTextContent(new StringTextContent(content));
} }
/// <summary>
/// Remove all existing content components from the entity and then adds
/// the new text content as a component into the entity before
/// returning it.
/// </summary>
/// <param name="entity">The entity to query and modify.</param>
/// <param name="content">The content to add to the entity.</param>
/// <returns>The entity or a copy with the new component.</returns>
public static Entity SetTextContent(
this Entity entity,
StringBuilder content)
{
return entity.SetTextContent(content.ToString());
}
} }

View file

@ -6,7 +6,7 @@ namespace MfGames.Nitride.Contents;
public static class TextContentExtensions public static class TextContentExtensions
{ {
public static string GetText(this ITextContent content) public static string GetTextContentString(this ITextContent content)
{ {
var writer = new StringWriter(); var writer = new StringWriter();
using TextReader reader = content.GetReader(); using TextReader reader = content.GetReader();
@ -21,28 +21,28 @@ public static class TextContentExtensions
return writer.ToString(); return writer.ToString();
} }
public static string? GetText(this IBinaryContent content) public static string? GetTextContentString(this IBinaryContent content)
{ {
if (content is ITextContentConvertable convertableContent) if (content is ITextContentConvertable convertableContent)
{ {
return convertableContent return convertableContent
.ToTextContent() .ToTextContent()
.GetText(); .GetTextContentString();
} }
return null; return null;
} }
public static string? GetText(this Entity entity) public static string? GetTextContentString(this Entity entity)
{ {
if (entity.TryGet(out ITextContent textContent)) if (entity.TryGet(out ITextContent textContent))
{ {
return textContent.GetText(); return textContent.GetTextContentString();
} }
if (entity.TryGet(out IBinaryContent binaryContent)) if (entity.TryGet(out IBinaryContent binaryContent))
{ {
return binaryContent.GetText(); return binaryContent.GetTextContentString();
} }
return null; return null;

View file

@ -34,7 +34,7 @@ public class MakeSingleLinkListItemsTests : TestBase<MarkdownTestContext>
MakeSingleLinkListItems? MakeSingleLinkListItems?
op = context.Resolve<MakeSingleLinkListItems>(); op = context.Resolve<MakeSingleLinkListItems>();
IEnumerable<Entity> output = op.Run(input); IEnumerable<Entity> output = op.Run(input);
string content = output.First().GetText()!.Trim(); string content = output.First().GetTextContentString()!.Trim();
Assert.Equal("- [Empty](link)", content); Assert.Equal("- [Empty](link)", content);
} }
@ -53,7 +53,7 @@ public class MakeSingleLinkListItemsTests : TestBase<MarkdownTestContext>
MakeSingleLinkListItems? MakeSingleLinkListItems?
op = context.Resolve<MakeSingleLinkListItems>(); op = context.Resolve<MakeSingleLinkListItems>();
IEnumerable<Entity> output = op.Run(input); IEnumerable<Entity> output = op.Run(input);
string content = output.First().GetText()!.Trim(); string content = output.First().GetTextContentString()!.Trim();
Assert.Equal("- [Empty space](link)", content); Assert.Equal("- [Empty space](link)", content);
} }
@ -72,7 +72,7 @@ public class MakeSingleLinkListItemsTests : TestBase<MarkdownTestContext>
MakeSingleLinkListItems? MakeSingleLinkListItems?
op = context.Resolve<MakeSingleLinkListItems>(); op = context.Resolve<MakeSingleLinkListItems>();
IEnumerable<Entity> output = op.Run(input); IEnumerable<Entity> output = op.Run(input);
string content = output.First().GetText()!.Trim(); string content = output.First().GetTextContentString()!.Trim();
Assert.Equal("- Empty", content); Assert.Equal("- Empty", content);
} }
@ -91,7 +91,7 @@ public class MakeSingleLinkListItemsTests : TestBase<MarkdownTestContext>
MakeSingleLinkListItems? MakeSingleLinkListItems?
op = context.Resolve<MakeSingleLinkListItems>(); op = context.Resolve<MakeSingleLinkListItems>();
IEnumerable<Entity> output = op.Run(input); IEnumerable<Entity> output = op.Run(input);
string content = output.First().GetText()!.Trim(); string content = output.First().GetTextContentString()!.Trim();
Assert.Equal("- [Empty space](link)", content); Assert.Equal("- [Empty space](link)", content);
} }

View file

@ -39,7 +39,7 @@ public class ParseYamlHeaderTest : NitrideTestBase
string.Empty, string.Empty,
}, },
output.Get<ITextContent>() output.Get<ITextContent>()
.GetText() .GetTextContentString()
.Split("\n")); .Split("\n"));
Assert.True(output.Has<TestHeader>()); Assert.True(output.Has<TestHeader>());
@ -72,7 +72,7 @@ public class ParseYamlHeaderTest : NitrideTestBase
string.Empty, string.Empty,
}, },
output.Get<ITextContent>() output.Get<ITextContent>()
.GetText() .GetTextContentString()
.Split("\n")); .Split("\n"));
Assert.True(output.Has<TestHeader>()); Assert.True(output.Has<TestHeader>());
@ -109,7 +109,7 @@ public class ParseYamlHeaderTest : NitrideTestBase
string.Empty, string.Empty,
}, },
output.Get<ITextContent>() output.Get<ITextContent>()
.GetText() .GetTextContentString()
.Split("\n")); .Split("\n"));
Assert.True(output.Has<TestHeader>()); Assert.True(output.Has<TestHeader>());