Compare commits

...

2 Commits

17 changed files with 195 additions and 75 deletions

View File

@ -25,6 +25,12 @@ internal sealed class BlogContext : DbContext
/// <value>The collection of blog posts.</value>
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>
/// Gets the collection of users in the database.
/// </summary>
@ -43,6 +49,7 @@ internal sealed class BlogContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
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.Common;
using OliverBooth.Common.Extensions;
using OliverBooth.Common.Services;
using Serilog;
using X10D.Hosting.DependencyInjection;
@ -20,6 +21,7 @@ builder.Services.ConfigureOptions<OliverBoothConfigureOptions>();
builder.Services.AddDbContextFactory<BlogContext>();
builder.Services.AddSingleton<IBlogPostService, BlogPostService>();
builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddSingleton<ITemplateService, TemplateService>();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
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 SmartFormat.Core.Extensions;
namespace OliverBooth.Formatting;
namespace OliverBooth.Common.Formatting;
/// <summary>
/// Represents a SmartFormat formatter that formats a date.
/// </summary>
internal sealed class DateFormatter : IFormatter
public sealed class DateFormatter : IFormatter
{
/// <inheritdoc />
public bool CanAutoDetect { get; set; } = true;

View File

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

View File

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

View File

@ -1,6 +1,6 @@
using Markdig.Syntax.Inlines;
namespace OliverBooth.Markdown.Template;
namespace OliverBooth.Common.Markdown;
/// <summary>
/// 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.Parsers;
namespace OliverBooth.Markdown.Template;
namespace OliverBooth.Common.Markdown;
/// <summary>
/// Represents a Markdown inline parser that handles MediaWiki-style templates.

View File

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

View File

@ -1,45 +1,42 @@
using OliverBooth.Common.Data;
namespace OliverBooth.Data.Web;
/// <summary>
/// Represents a MediaWiki-style template.
/// </summary>
public sealed class ArticleTemplate : IEquatable<ArticleTemplate>
public sealed class Template : ITemplate, IEquatable<Template>
{
/// <summary>
/// Gets or sets the format string.
/// </summary>
/// <value>The format string.</value>
public string FormatString { get; set; } = string.Empty;
/// <inheritdoc />
public string FormatString { get; internal set; } = string.Empty;
/// <summary>
/// Gets the name of the template.
/// </summary>
/// <inheritdoc />
public string Name { get; private set; } = string.Empty;
/// <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>
/// <param name="left">The first instance of <see cref="ArticleTemplate" /> to compare.</param>
/// <param name="right">The second 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="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 ==(ArticleTemplate? left, ArticleTemplate? right) => Equals(left, right);
public static bool operator ==(Template? left, Template? right) => Equals(left, right);
/// <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>
/// <param name="left">The first instance of <see cref="ArticleTemplate" /> to compare.</param>
/// <param name="right">The second 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="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 !=(ArticleTemplate? left, ArticleTemplate? right) => !(left == right);
public static bool operator !=(Template? left, Template? right) => !(left == right);
/// <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.
/// </summary>
/// <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="false" />.
/// </returns>
public bool Equals(ArticleTemplate? other)
public bool Equals(Template? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
@ -59,12 +56,12 @@ public sealed class ArticleTemplate : IEquatable<ArticleTemplate>
/// </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="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" />.
/// </returns>
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>

View File

@ -20,18 +20,18 @@ public sealed class WebContext : DbContext
_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>
/// Gets the set of site configuration items.
/// </summary>
/// <value>The set of site configuration items.</value>
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 />
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
@ -42,7 +42,7 @@ public sealed class WebContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ArticleTemplateConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
modelBuilder.ApplyConfiguration(new SiteConfigurationConfiguration());
}
}

View File

@ -1,8 +1,8 @@
using Markdig;
using OliverBooth.Common;
using OliverBooth.Common.Extensions;
using OliverBooth.Common.Services;
using OliverBooth.Data;
using OliverBooth.Markdown.Template;
using OliverBooth.Markdown.Timestamp;
using OliverBooth.Services;
using Serilog;
@ -18,11 +18,11 @@ builder.Logging.ClearProviders();
builder.Logging.AddSerilog();
builder.Services.ConfigureOptions<OliverBoothConfigureOptions>();
builder.Services.AddSingleton<TemplateService>();
builder.Services.AddSingleton<ITemplateService, TemplateService>();
builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
.Use<TimestampExtension>()
.Use(new TemplateExtension(provider.GetRequiredService<TemplateService>()))
.Use(new TemplateExtension(provider.GetRequiredService<ITemplateService>()))
.UseAdvancedExtensions()
.UseBootstrap()
.UseEmojiAndSmiley()
@ -32,11 +32,6 @@ builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
builder.Services.AddDbContextFactory<WebContext>();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddControllersWithViews();
builder.Services.AddCors(options => options.AddPolicy("BlogApi", policy => (builder.Environment.IsDevelopment()
? policy.AllowAnyOrigin()
: policy.WithOrigins("https://oliverbooth.dev"))
.AllowAnyMethod()
.AllowAnyHeader()));
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.WebHost.AddCertificateFromEnvironment(2845, 5049);
@ -54,7 +49,6 @@ app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseCors("BlogApi");
app.MapControllers();
app.MapRazorPages();

View File

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