feat: share template system among all projects

This commit is contained in:
Oliver Booth 2023-08-13 13:27:44 +01:00
parent bd5fd6114a
commit 287af40501
Signed by: oliverbooth
GPG Key ID: B89D139977693FED
17 changed files with 195 additions and 69 deletions

View File

@ -25,6 +25,12 @@ internal sealed class BlogContext : DbContext
/// <value>The collection of blog posts.</value> /// <value>The collection of blog posts.</value>
public DbSet<BlogPost> BlogPosts { get; private set; } = null!; public DbSet<BlogPost> BlogPosts { get; private set; } = null!;
/// <summary>
/// Gets the collection of templates in the database.
/// </summary>
/// <value>The collection of templates.</value>
public DbSet<Template> Templates { get; private set; } = null!;
/// <summary> /// <summary>
/// Gets the collection of users in the database. /// Gets the collection of users in the database.
/// </summary> /// </summary>
@ -43,6 +49,7 @@ internal sealed class BlogContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.ApplyConfiguration(new BlogPostConfiguration()); modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration()); modelBuilder.ApplyConfiguration(new UserConfiguration());
} }
} }

View File

@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace OliverBooth.Blog.Data.Configuration;
/// <summary>
/// Represents the configuration for the <see cref="Template" /> entity.
/// </summary>
internal sealed class TemplateConfiguration : IEntityTypeConfiguration<Template>
{
public void Configure(EntityTypeBuilder<Template> builder)
{
builder.ToTable("Template");
builder.HasKey(e => e.Name);
builder.Property(e => e.Name).IsRequired();
builder.Property(e => e.FormatString).IsRequired();
}
}

View File

@ -0,0 +1,76 @@
using OliverBooth.Common.Data;
namespace OliverBooth.Blog.Data;
/// <summary>
/// Represents a MediaWiki-style template.
/// </summary>
public sealed class Template : ITemplate, IEquatable<Template>
{
/// <inheritdoc />
public string FormatString { get; internal set; } = string.Empty;
/// <inheritdoc />
public string Name { get; private set; } = string.Empty;
/// <summary>
/// Returns a value indicating whether two instances of <see cref="Template" /> are equal.
/// </summary>
/// <param name="left">The first instance of <see cref="Template" /> to compare.</param>
/// <param name="right">The second instance of <see cref="Template" /> to compare.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator ==(Template? left, Template? right) => Equals(left, right);
/// <summary>
/// Returns a value indicating whether two instances of <see cref="Template" /> are not equal.
/// </summary>
/// <param name="left">The first instance of <see cref="Template" /> to compare.</param>
/// <param name="right">The second instance of <see cref="Template" /> to compare.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are not equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator !=(Template? left, Template? right) => !(left == right);
/// <summary>
/// Returns a value indicating whether this instance of <see cref="Template" /> is equal to another
/// instance.
/// </summary>
/// <param name="other">An instance to compare with this instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="other" /> is equal to this instance; otherwise,
/// <see langword="false" />.
/// </returns>
public bool Equals(Template? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name;
}
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="obj" /> is an instance of <see cref="Template" /> and
/// equals the value of this instance; otherwise, <see langword="false" />.
/// </returns>
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || obj is Template other && Equals(other);
}
/// <summary>
/// Gets the hash code for this instance.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
// ReSharper disable once NonReadonlyMemberInGetHashCode
return Name.GetHashCode();
}
}

View File

@ -3,6 +3,7 @@ using OliverBooth.Blog.Middleware;
using OliverBooth.Blog.Services; using OliverBooth.Blog.Services;
using OliverBooth.Common; using OliverBooth.Common;
using OliverBooth.Common.Extensions; using OliverBooth.Common.Extensions;
using OliverBooth.Common.Services;
using Serilog; using Serilog;
using X10D.Hosting.DependencyInjection; using X10D.Hosting.DependencyInjection;
@ -20,6 +21,7 @@ builder.Services.ConfigureOptions<OliverBoothConfigureOptions>();
builder.Services.AddDbContextFactory<BlogContext>(); builder.Services.AddDbContextFactory<BlogContext>();
builder.Services.AddSingleton<IBlogPostService, BlogPostService>(); builder.Services.AddSingleton<IBlogPostService, BlogPostService>();
builder.Services.AddSingleton<IUserService, UserService>(); builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddSingleton<ITemplateService, TemplateService>();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation(); builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();

View File

@ -0,0 +1,18 @@
namespace OliverBooth.Common.Data;
/// <summary>
/// Represents a template.
/// </summary>
public interface ITemplate
{
/// <summary>
/// Gets or sets the format string.
/// </summary>
/// <value>The format string.</value>
string FormatString { get; }
/// <summary>
/// Gets the name of the template.
/// </summary>
string Name { get; }
}

View File

@ -1,12 +1,12 @@
using System.Globalization; using System.Globalization;
using SmartFormat.Core.Extensions; using SmartFormat.Core.Extensions;
namespace OliverBooth.Formatting; namespace OliverBooth.Common.Formatting;
/// <summary> /// <summary>
/// Represents a SmartFormat formatter that formats a date. /// Represents a SmartFormat formatter that formats a date.
/// </summary> /// </summary>
internal sealed class DateFormatter : IFormatter public sealed class DateFormatter : IFormatter
{ {
/// <inheritdoc /> /// <inheritdoc />
public bool CanAutoDetect { get; set; } = true; public bool CanAutoDetect { get; set; } = true;

View File

@ -1,12 +1,13 @@
using Markdig; using Markdig;
using Microsoft.Extensions.DependencyInjection;
using SmartFormat.Core.Extensions; using SmartFormat.Core.Extensions;
namespace OliverBooth.Formatting; namespace OliverBooth.Common.Formatting;
/// <summary> /// <summary>
/// Represents a SmartFormat formatter that formats markdown. /// Represents a SmartFormat formatter that formats markdown.
/// </summary> /// </summary>
internal sealed class MarkdownFormatter : IFormatter public sealed class MarkdownFormatter : IFormatter
{ {
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;

View File

@ -1,21 +1,21 @@
using Markdig; using Markdig;
using Markdig.Renderers; using Markdig.Renderers;
using OliverBooth.Services; using OliverBooth.Common.Services;
namespace OliverBooth.Markdown.Template; namespace OliverBooth.Common.Markdown;
/// <summary> /// <summary>
/// Represents a Markdown extension that adds support for MediaWiki-style templates. /// Represents a Markdown extension that adds support for MediaWiki-style templates.
/// </summary> /// </summary>
internal sealed class TemplateExtension : IMarkdownExtension public sealed class TemplateExtension : IMarkdownExtension
{ {
private readonly TemplateService _templateService; private readonly ITemplateService _templateService;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TemplateExtension" /> class. /// Initializes a new instance of the <see cref="TemplateExtension" /> class.
/// </summary> /// </summary>
/// <param name="templateService">The template service.</param> /// <param name="templateService">The template service.</param>
public TemplateExtension(TemplateService templateService) public TemplateExtension(ITemplateService templateService)
{ {
_templateService = templateService; _templateService = templateService;
} }

View File

@ -1,6 +1,6 @@
using Markdig.Syntax.Inlines; using Markdig.Syntax.Inlines;
namespace OliverBooth.Markdown.Template; namespace OliverBooth.Common.Markdown;
/// <summary> /// <summary>
/// Represents a Markdown inline element that represents a MediaWiki-style template. /// Represents a Markdown inline element that represents a MediaWiki-style template.

View File

@ -2,7 +2,7 @@ using Cysharp.Text;
using Markdig.Helpers; using Markdig.Helpers;
using Markdig.Parsers; using Markdig.Parsers;
namespace OliverBooth.Markdown.Template; namespace OliverBooth.Common.Markdown;
/// <summary> /// <summary>
/// Represents a Markdown inline parser that handles MediaWiki-style templates. /// Represents a Markdown inline parser that handles MediaWiki-style templates.

View File

@ -1,21 +1,21 @@
using Markdig.Renderers; using Markdig.Renderers;
using Markdig.Renderers.Html; using Markdig.Renderers.Html;
using OliverBooth.Services; using OliverBooth.Common.Services;
namespace OliverBooth.Markdown.Template; namespace OliverBooth.Common.Markdown;
/// <summary> /// <summary>
/// Represents a Markdown object renderer that handles <see cref="TemplateInline" /> elements. /// Represents a Markdown object renderer that handles <see cref="TemplateInline" /> elements.
/// </summary> /// </summary>
internal sealed class TemplateRenderer : HtmlObjectRenderer<TemplateInline> internal sealed class TemplateRenderer : HtmlObjectRenderer<TemplateInline>
{ {
private readonly TemplateService _templateService; private readonly ITemplateService _templateService;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TemplateRenderer" /> class. /// Initializes a new instance of the <see cref="TemplateRenderer" /> class.
/// </summary> /// </summary>
/// <param name="templateService">The <see cref="TemplateService" />.</param> /// <param name="templateService">The <see cref="TemplateService" />.</param>
public TemplateRenderer(TemplateService templateService) public TemplateRenderer(ITemplateService templateService)
{ {
_templateService = templateService; _templateService = templateService;
} }

View File

@ -0,0 +1,19 @@
using OliverBooth.Common.Markdown;
namespace OliverBooth.Common.Services;
/// <summary>
/// Represents a service that renders MediaWiki-style templates.
/// </summary>
public interface ITemplateService
{
/// <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>
string RenderTemplate(TemplateInline templateInline);
}

View File

@ -4,13 +4,13 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace OliverBooth.Data.Web.Configuration; namespace OliverBooth.Data.Web.Configuration;
/// <summary> /// <summary>
/// Represents the configuration for the <see cref="ArticleTemplate" /> entity. /// Represents the configuration for the <see cref="Template" /> entity.
/// </summary> /// </summary>
internal sealed class ArticleTemplateConfiguration : IEntityTypeConfiguration<ArticleTemplate> internal sealed class TemplateConfiguration : IEntityTypeConfiguration<Template>
{ {
public void Configure(EntityTypeBuilder<ArticleTemplate> builder) public void Configure(EntityTypeBuilder<Template> builder)
{ {
builder.ToTable("ArticleTemplate"); builder.ToTable("Template");
builder.HasKey(e => e.Name); builder.HasKey(e => e.Name);
builder.Property(e => e.Name).IsRequired(); builder.Property(e => e.Name).IsRequired();

View File

@ -1,45 +1,42 @@
using OliverBooth.Common.Data;
namespace OliverBooth.Data.Web; namespace OliverBooth.Data.Web;
/// <summary> /// <summary>
/// Represents a MediaWiki-style template. /// Represents a MediaWiki-style template.
/// </summary> /// </summary>
public sealed class ArticleTemplate : IEquatable<ArticleTemplate> public sealed class Template : ITemplate, IEquatable<Template>
{ {
/// <summary> /// <inheritdoc />
/// Gets or sets the format string. public string FormatString { get; internal set; } = string.Empty;
/// </summary>
/// <value>The format string.</value>
public string FormatString { get; set; } = string.Empty;
/// <summary> /// <inheritdoc />
/// Gets the name of the template.
/// </summary>
public string Name { get; private set; } = string.Empty; public string Name { get; private set; } = string.Empty;
/// <summary> /// <summary>
/// Returns a value indicating whether two instances of <see cref="ArticleTemplate" /> are equal. /// Returns a value indicating whether two instances of <see cref="Template" /> are equal.
/// </summary> /// </summary>
/// <param name="left">The first instance of <see cref="ArticleTemplate" /> to compare.</param> /// <param name="left">The first instance of <see cref="Template" /> to compare.</param>
/// <param name="right">The second instance of <see cref="ArticleTemplate" /> to compare.</param> /// <param name="right">The second instance of <see cref="Template" /> to compare.</param>
/// <returns> /// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are equal; otherwise, /// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are equal; otherwise,
/// <see langword="false" />. /// <see langword="false" />.
/// </returns> /// </returns>
public static bool operator ==(ArticleTemplate? left, ArticleTemplate? right) => Equals(left, right); public static bool operator ==(Template? left, Template? right) => Equals(left, right);
/// <summary> /// <summary>
/// Returns a value indicating whether two instances of <see cref="ArticleTemplate" /> are not equal. /// Returns a value indicating whether two instances of <see cref="Template" /> are not equal.
/// </summary> /// </summary>
/// <param name="left">The first instance of <see cref="ArticleTemplate" /> to compare.</param> /// <param name="left">The first instance of <see cref="Template" /> to compare.</param>
/// <param name="right">The second instance of <see cref="ArticleTemplate" /> to compare.</param> /// <param name="right">The second instance of <see cref="Template" /> to compare.</param>
/// <returns> /// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are not equal; otherwise, /// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are not equal; otherwise,
/// <see langword="false" />. /// <see langword="false" />.
/// </returns> /// </returns>
public static bool operator !=(ArticleTemplate? left, ArticleTemplate? right) => !(left == right); public static bool operator !=(Template? left, Template? right) => !(left == right);
/// <summary> /// <summary>
/// Returns a value indicating whether this instance of <see cref="ArticleTemplate" /> is equal to another /// Returns a value indicating whether this instance of <see cref="Template" /> is equal to another
/// instance. /// instance.
/// </summary> /// </summary>
/// <param name="other">An instance to compare with this instance.</param> /// <param name="other">An instance to compare with this instance.</param>
@ -47,7 +44,7 @@ public sealed class ArticleTemplate : IEquatable<ArticleTemplate>
/// <see langword="true" /> if <paramref name="other" /> is equal to this instance; otherwise, /// <see langword="true" /> if <paramref name="other" /> is equal to this instance; otherwise,
/// <see langword="false" />. /// <see langword="false" />.
/// </returns> /// </returns>
public bool Equals(ArticleTemplate? other) public bool Equals(Template? other)
{ {
if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true; if (ReferenceEquals(this, other)) return true;
@ -59,12 +56,12 @@ public sealed class ArticleTemplate : IEquatable<ArticleTemplate>
/// </summary> /// </summary>
/// <param name="obj">An object to compare with this instance.</param> /// <param name="obj">An object to compare with this instance.</param>
/// <returns> /// <returns>
/// <see langword="true" /> if <paramref name="obj" /> is an instance of <see cref="ArticleTemplate" /> and /// <see langword="true" /> if <paramref name="obj" /> is an instance of <see cref="Template" /> and
/// equals the value of this instance; otherwise, <see langword="false" />. /// equals the value of this instance; otherwise, <see langword="false" />.
/// </returns> /// </returns>
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
return ReferenceEquals(this, obj) || obj is ArticleTemplate other && Equals(other); return ReferenceEquals(this, obj) || obj is Template other && Equals(other);
} }
/// <summary> /// <summary>

View File

@ -20,18 +20,18 @@ public sealed class WebContext : DbContext
_configuration = configuration; _configuration = configuration;
} }
/// <summary>
/// Gets the set of article templates.
/// </summary>
/// <value>The set of article templates.</value>
public DbSet<ArticleTemplate> ArticleTemplates { get; private set; } = null!;
/// <summary> /// <summary>
/// Gets the set of site configuration items. /// Gets the set of site configuration items.
/// </summary> /// </summary>
/// <value>The set of site configuration items.</value> /// <value>The set of site configuration items.</value>
public DbSet<SiteConfiguration> SiteConfiguration { get; private set; } = null!; public DbSet<SiteConfiguration> SiteConfiguration { get; private set; } = null!;
/// <summary>
/// Gets the collection of templates in the database.
/// </summary>
/// <value>The collection of templates.</value>
public DbSet<Template> Templates { get; private set; } = null!;
/// <inheritdoc /> /// <inheritdoc />
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
@ -42,7 +42,7 @@ public sealed class WebContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.ApplyConfiguration(new ArticleTemplateConfiguration()); modelBuilder.ApplyConfiguration(new TemplateConfiguration());
modelBuilder.ApplyConfiguration(new SiteConfigurationConfiguration()); modelBuilder.ApplyConfiguration(new SiteConfigurationConfiguration());
} }
} }

View File

@ -1,8 +1,8 @@
using Markdig; using Markdig;
using OliverBooth.Common; using OliverBooth.Common;
using OliverBooth.Common.Extensions; using OliverBooth.Common.Extensions;
using OliverBooth.Common.Services;
using OliverBooth.Data; using OliverBooth.Data;
using OliverBooth.Markdown.Template;
using OliverBooth.Markdown.Timestamp; using OliverBooth.Markdown.Timestamp;
using OliverBooth.Services; using OliverBooth.Services;
using Serilog; using Serilog;
@ -18,11 +18,11 @@ builder.Logging.ClearProviders();
builder.Logging.AddSerilog(); builder.Logging.AddSerilog();
builder.Services.ConfigureOptions<OliverBoothConfigureOptions>(); builder.Services.ConfigureOptions<OliverBoothConfigureOptions>();
builder.Services.AddSingleton<TemplateService>(); builder.Services.AddSingleton<ITemplateService, TemplateService>();
builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder() builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
.Use<TimestampExtension>() .Use<TimestampExtension>()
.Use(new TemplateExtension(provider.GetRequiredService<TemplateService>())) .Use(new TemplateExtension(provider.GetRequiredService<ITemplateService>()))
.UseAdvancedExtensions() .UseAdvancedExtensions()
.UseBootstrap() .UseBootstrap()
.UseEmojiAndSmiley() .UseEmojiAndSmiley()

View File

@ -1,11 +1,10 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using Markdig;
using Markdig.Syntax;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OliverBooth.Common.Formatting;
using OliverBooth.Common.Markdown;
using OliverBooth.Common.Services;
using OliverBooth.Data; using OliverBooth.Data;
using OliverBooth.Data.Web; using OliverBooth.Data.Web;
using OliverBooth.Formatting;
using OliverBooth.Markdown.Template;
using SmartFormat; using SmartFormat;
using SmartFormat.Extensions; using SmartFormat.Extensions;
@ -14,10 +13,9 @@ namespace OliverBooth.Services;
/// <summary> /// <summary>
/// Represents a service that renders MediaWiki-style templates. /// Represents a service that renders MediaWiki-style templates.
/// </summary> /// </summary>
public sealed class TemplateService internal sealed class TemplateService : ITemplateService
{ {
private static readonly Random Random = new(); 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;
@ -34,27 +32,16 @@ public sealed class TemplateService
_formatter.AddExtensions(new DateFormatter()); _formatter.AddExtensions(new DateFormatter());
_formatter.AddExtensions(new MarkdownFormatter(serviceProvider)); _formatter.AddExtensions(new MarkdownFormatter(serviceProvider));
_serviceProvider = serviceProvider;
_webContextFactory = webContextFactory; _webContextFactory = webContextFactory;
Current = this;
} }
public static TemplateService Current { get; private set; } = null!; /// <inheritdoc />
/// <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) public string RenderTemplate(TemplateInline templateInline)
{ {
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline)); if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
using WebContext webContext = _webContextFactory.CreateDbContext(); using WebContext webContext = _webContextFactory.CreateDbContext();
ArticleTemplate? template = webContext.ArticleTemplates.Find(templateInline.Name); Template? template = webContext.Templates.Find(templateInline.Name);
if (template is null) if (template is null)
{ {
return $"{{{{{templateInline.Name}}}}}"; return $"{{{{{templateInline.Name}}}}}";