2023-08-11 21:33:14 +01:00
|
|
|
using System.Buffers.Binary;
|
|
|
|
using Markdig;
|
|
|
|
using Markdig.Syntax;
|
2023-08-11 15:51:20 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-08-08 21:03:41 +01:00
|
|
|
using OliverBooth.Data;
|
|
|
|
using OliverBooth.Data.Web;
|
2023-08-11 21:34:04 +01:00
|
|
|
using OliverBooth.Formatting;
|
2023-08-11 16:35:06 +01:00
|
|
|
using OliverBooth.Markdown.Template;
|
2023-08-08 21:03:41 +01:00
|
|
|
using SmartFormat;
|
|
|
|
using SmartFormat.Extensions;
|
|
|
|
|
|
|
|
namespace OliverBooth.Services;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a service that renders MediaWiki-style templates.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class TemplateService
|
|
|
|
{
|
2023-08-11 21:33:14 +01:00
|
|
|
private static readonly Random Random = new();
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2023-08-08 21:03:41 +01:00
|
|
|
private readonly IDbContextFactory<WebContext> _webContextFactory;
|
|
|
|
private readonly SmartFormatter _formatter;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="TemplateService" /> class.
|
|
|
|
/// </summary>
|
2023-08-11 21:33:14 +01:00
|
|
|
/// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param>
|
2023-08-08 21:03:41 +01:00
|
|
|
/// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
|
2023-08-11 21:33:14 +01:00
|
|
|
public TemplateService(IServiceProvider serviceProvider, IDbContextFactory<WebContext> webContextFactory)
|
2023-08-08 21:03:41 +01:00
|
|
|
{
|
|
|
|
_formatter = Smart.CreateDefaultSmartFormat();
|
|
|
|
_formatter.AddExtensions(new DefaultSource());
|
|
|
|
_formatter.AddExtensions(new ReflectionSource());
|
|
|
|
_formatter.AddExtensions(new DateFormatter());
|
2023-08-11 21:33:14 +01:00
|
|
|
|
|
|
|
_serviceProvider = serviceProvider;
|
2023-08-08 21:03:41 +01:00
|
|
|
_webContextFactory = webContextFactory;
|
|
|
|
Current = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TemplateService Current { get; private set; } = null!;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Renders the specified template with the specified arguments.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="templateInline">The template to render.</param>
|
|
|
|
/// <returns>The rendered template.</returns>
|
|
|
|
/// <exception cref="ArgumentNullException">
|
|
|
|
/// <paramref name="templateInline" /> is <see langword="null" />.
|
|
|
|
/// </exception>
|
|
|
|
public string RenderTemplate(TemplateInline templateInline)
|
|
|
|
{
|
|
|
|
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
|
2023-08-11 21:33:14 +01:00
|
|
|
MarkdownPipeline? markdownPipeline = _serviceProvider.GetService<MarkdownPipeline>();
|
|
|
|
|
2023-08-08 21:03:41 +01:00
|
|
|
using WebContext webContext = _webContextFactory.CreateDbContext();
|
|
|
|
ArticleTemplate? template = webContext.ArticleTemplates.Find(templateInline.Name);
|
|
|
|
if (template is null)
|
|
|
|
{
|
|
|
|
return $"{{{{{templateInline.Name}}}}}";
|
|
|
|
}
|
|
|
|
|
2023-08-11 21:33:14 +01:00
|
|
|
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);
|
|
|
|
|
2023-08-08 21:03:41 +01:00
|
|
|
var formatted = new
|
|
|
|
{
|
2023-08-11 21:33:14 +01:00
|
|
|
ArgumentList = arguments,
|
|
|
|
ArgumentString = string.Join("|", arguments),
|
2023-08-08 21:03:41 +01:00
|
|
|
templateInline.Params,
|
2023-08-11 21:33:14 +01:00
|
|
|
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
|
|
|
|
RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
|
2023-08-08 21:03:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Markdig.Markdown.ToHtml(_formatter.Format(template.FormatString, formatted));
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return $"{{{{{templateInline.Name}|{templateInline.ArgumentString}}}}}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|