oliverbooth.dev/OliverBooth/Services/TemplateService.cs

70 lines
2.4 KiB
C#
Raw Normal View History

2023-08-11 20:33:14 +00:00
using System.Buffers.Binary;
2023-08-11 14:51:20 +00:00
using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog;
using OliverBooth.Formatting;
using OliverBooth.Markdown.Template;
using SmartFormat;
using SmartFormat.Extensions;
namespace OliverBooth.Services;
/// <summary>
/// Represents a service that renders MediaWiki-style templates.
/// </summary>
internal sealed class TemplateService : ITemplateService
{
2023-08-11 20:33:14 +00:00
private static readonly Random Random = new();
private readonly IDbContextFactory<BlogContext> _webContextFactory;
private readonly SmartFormatter _formatter;
/// <summary>
/// Initializes a new instance of the <see cref="TemplateService" /> class.
/// </summary>
2023-08-11 20:33:14 +00:00
/// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param>
/// <param name="webContextFactory">The <see cref="BlogContext" /> factory.</param>
public TemplateService(IServiceProvider serviceProvider, IDbContextFactory<BlogContext> webContextFactory)
{
_formatter = Smart.CreateDefaultSmartFormat();
_formatter.AddExtensions(new DefaultSource());
_formatter.AddExtensions(new ReflectionSource());
_formatter.AddExtensions(new DateFormatter());
_formatter.AddExtensions(new MarkdownFormatter(serviceProvider));
2023-08-11 20:33:14 +00:00
_webContextFactory = webContextFactory;
}
/// <inheritdoc />
public string RenderTemplate(TemplateInline templateInline)
{
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
2023-08-11 20:33:14 +00:00
using BlogContext webContext = _webContextFactory.CreateDbContext();
Template? template = webContext.Templates.Find(templateInline.Name);
if (template is null)
{
return $"{{{{{templateInline.Name}}}}}";
}
2023-08-11 20:33:14 +00:00
Span<byte> randomBytes = stackalloc byte[20];
Random.NextBytes(randomBytes);
var formatted = new
{
templateInline.ArgumentList,
templateInline.ArgumentString,
templateInline.Params,
2023-08-11 20:33:14 +00:00
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
};
try
{
return _formatter.Format(template.FormatString, formatted);
}
catch
{
return $"{{{{{templateInline.Name}|{templateInline.ArgumentString}}}}}";
}
}
}