feat: format template arguments

This commit is contained in:
Oliver Booth 2023-08-11 21:33:14 +01:00
parent 9d0e16abc1
commit 034bd66b29
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61
2 changed files with 27 additions and 6 deletions

View File

@ -112,7 +112,7 @@ public sealed class TemplateInlineParser : InlineParser
out bool hasValue)
{
var isEscaped = false;
int startIndex = index;
for (; index < argumentSpan.Length; index++)
{

View File

@ -1,7 +1,9 @@
using System.Buffers.Binary;
using Markdig;
using Markdig.Syntax;
using Microsoft.EntityFrameworkCore;
using OliverBooth.Data;
using OliverBooth.Data.Web;
using OliverBooth.Markdown;
using OliverBooth.Markdown.Template;
using SmartFormat;
using SmartFormat.Extensions;
@ -13,20 +15,24 @@ namespace OliverBooth.Services;
/// </summary>
public sealed class TemplateService
{
private static readonly Random Random = new();
private readonly IServiceProvider _serviceProvider;
private readonly IDbContextFactory<WebContext> _webContextFactory;
private readonly SmartFormatter _formatter;
/// <summary>
/// Initializes a new instance of the <see cref="TemplateService" /> class.
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param>
/// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
public TemplateService(IDbContextFactory<WebContext> webContextFactory)
public TemplateService(IServiceProvider serviceProvider, IDbContextFactory<WebContext> webContextFactory)
{
_formatter = Smart.CreateDefaultSmartFormat();
_formatter.AddExtensions(new DefaultSource());
_formatter.AddExtensions(new ReflectionSource());
_formatter.AddExtensions(new DateFormatter());
_serviceProvider = serviceProvider;
_webContextFactory = webContextFactory;
Current = this;
}
@ -44,6 +50,8 @@ public sealed class TemplateService
public string RenderTemplate(TemplateInline templateInline)
{
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
MarkdownPipeline? markdownPipeline = _serviceProvider.GetService<MarkdownPipeline>();
using WebContext webContext = _webContextFactory.CreateDbContext();
ArticleTemplate? template = webContext.ArticleTemplates.Find(templateInline.Name);
if (template is null)
@ -51,11 +59,24 @@ public sealed class TemplateService
return $"{{{{{templateInline.Name}}}}}";
}
string[] arguments = templateInline.ArgumentList.ToArray();
for (var index = 0; index < arguments.Length; index++)
{
MarkdownDocument document = Markdig.Markdown.Parse(arguments[index], markdownPipeline);
string result = document.ToHtml(markdownPipeline);
arguments[index] = result;
}
Span<byte> randomBytes = stackalloc byte[20];
Random.NextBytes(randomBytes);
var formatted = new
{
templateInline.ArgumentList,
templateInline.ArgumentString,
ArgumentList = arguments,
ArgumentString = string.Join("|", arguments),
templateInline.Params,
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
};
try