2023-08-11 20:33:14 +00:00
|
|
|
using System.Buffers.Binary;
|
2023-08-15 16:04:43 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2023-08-11 14:51:20 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-08-13 17:02:19 +00:00
|
|
|
using OliverBooth.Data.Web;
|
2023-08-13 16:33:54 +00:00
|
|
|
using OliverBooth.Formatting;
|
|
|
|
using OliverBooth.Markdown.Template;
|
2023-08-08 20:03:41 +00:00
|
|
|
using SmartFormat;
|
|
|
|
using SmartFormat.Extensions;
|
|
|
|
|
|
|
|
namespace OliverBooth.Services;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a service that renders MediaWiki-style templates.
|
|
|
|
/// </summary>
|
2023-08-13 12:27:44 +00:00
|
|
|
internal sealed class TemplateService : ITemplateService
|
2023-08-08 20:03:41 +00:00
|
|
|
{
|
2023-08-11 20:33:14 +00:00
|
|
|
private static readonly Random Random = new();
|
2023-08-13 17:02:19 +00:00
|
|
|
private readonly IDbContextFactory<WebContext> _webContextFactory;
|
2023-08-08 20:03:41 +00:00
|
|
|
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>
|
2023-08-13 17:02:19 +00:00
|
|
|
/// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
|
|
|
|
public TemplateService(IServiceProvider serviceProvider,
|
|
|
|
IDbContextFactory<WebContext> webContextFactory)
|
2023-08-08 20:03:41 +00:00
|
|
|
{
|
|
|
|
_formatter = Smart.CreateDefaultSmartFormat();
|
|
|
|
_formatter.AddExtensions(new DefaultSource());
|
|
|
|
_formatter.AddExtensions(new ReflectionSource());
|
|
|
|
_formatter.AddExtensions(new DateFormatter());
|
2023-08-11 20:51:16 +00:00
|
|
|
_formatter.AddExtensions(new MarkdownFormatter(serviceProvider));
|
2023-08-11 20:33:14 +00:00
|
|
|
|
2023-08-08 20:03:41 +00:00
|
|
|
_webContextFactory = webContextFactory;
|
|
|
|
}
|
|
|
|
|
2023-08-13 12:27:44 +00:00
|
|
|
/// <inheritdoc />
|
2023-08-13 17:02:19 +00:00
|
|
|
public string RenderGlobalTemplate(TemplateInline templateInline)
|
2023-08-08 20:03:41 +00:00
|
|
|
{
|
|
|
|
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
|
2023-08-11 20:33:14 +00:00
|
|
|
|
2023-08-15 16:04:43 +00:00
|
|
|
return TryGetTemplate(templateInline.Name, templateInline.Variant, out ITemplate? template)
|
|
|
|
? RenderTemplate(templateInline, template)
|
|
|
|
: GetDefaultRender(templateInline);
|
2023-08-13 17:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-08-15 16:04:43 +00:00
|
|
|
public string RenderTemplate(TemplateInline templateInline, ITemplate? template)
|
2023-08-13 17:02:19 +00:00
|
|
|
{
|
2023-08-08 20:03:41 +00:00
|
|
|
if (template is null)
|
|
|
|
{
|
2023-08-15 16:04:43 +00:00
|
|
|
return GetDefaultRender(templateInline);
|
2023-08-08 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 20:33:14 +00:00
|
|
|
Span<byte> randomBytes = stackalloc byte[20];
|
|
|
|
Random.NextBytes(randomBytes);
|
|
|
|
|
2023-08-08 20:03:41 +00:00
|
|
|
var formatted = new
|
|
|
|
{
|
2023-08-15 16:04:43 +00:00
|
|
|
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"),
|
2023-08-08 20:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-08-11 20:51:16 +00:00
|
|
|
return _formatter.Format(template.FormatString, formatted);
|
2023-08-08 20:03:41 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2023-08-15 16:04:43 +00:00
|
|
|
return GetDefaultRender(templateInline);
|
2023-08-08 20:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-15 16:04:43 +00:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool TryGetTemplate(string name, [NotNullWhen(true)] out ITemplate? template)
|
|
|
|
{
|
|
|
|
return TryGetTemplate(name, string.Empty, out template);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool TryGetTemplate(string name, string variant, [NotNullWhen(true)] out ITemplate? template)
|
|
|
|
{
|
|
|
|
using WebContext context = _webContextFactory.CreateDbContext();
|
|
|
|
template = context.Templates.FirstOrDefault(t => t.Name == name && t.Variant == variant);
|
|
|
|
return template is not null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string GetDefaultRender(TemplateInline templateInline)
|
|
|
|
{
|
|
|
|
return string.IsNullOrWhiteSpace(templateInline.ArgumentString)
|
|
|
|
? $"{{{{{templateInline.Name}}}}}"
|
|
|
|
: $"{{{{{templateInline.Name}|{templateInline.ArgumentString}}}}}";
|
|
|
|
}
|
2023-08-08 20:03:41 +00:00
|
|
|
}
|