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

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