Renders themed pages as swappable sections with path-mirrored template scopes and a Razor fallback

This commit is contained in:
BinaryAlley committed 2026-08-20 06:50:17 -07:00
1 parent f17e685239
commit e37bb3ecc6
24 files changed
+351 -187

No files matched your search

@@ -0,0 +1,26 @@
#region ========================================================================= USING =====================================================================================
using Microsoft.Extensions.Localization;
using System.Collections.Generic;
#endregion
namespace Lumina.Presentation.Web.Core.Themes;
/// <summary>
/// Builds the page data entries that the themed endpoints expose to their templates.
/// </summary>
public static class ThemePageDataFactory
{
/// <summary>
/// Creates the localized strings of a page from the provided localizer, keyed by their resource names.
/// </summary>
/// <param name="localizer">The localizer of the page resources.</param>
/// <returns>The localized strings keyed by their resource names.</returns>
public static Dictionary<string, object?> CreateLocalizedStrings(IStringLocalizer localizer)
{
Dictionary<string, object?> strings = [];
foreach (LocalizedString localizedString in localizer.GetAllStrings(includeParentCultures: true))
strings[localizedString.Name] = localizedString.Value;
return strings;
}
}
@@ -1,7 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.DTO.Themes;
using Lumina.Presentation.Web.Common.Primitives;
using System;
using Lumina.Presentation.Web.Common.Services;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -11,7 +11,7 @@ namespace Lumina.Presentation.Web.Core.Themes;
/// <summary>
/// Renders a theme template against a page model, populating the theme metadata before rendering.
/// </summary>
public sealed class ThemePageRenderer
public class ThemePageRenderer
{
private readonly ThemeService _themeService;
private readonly ThemeTemplateEngine _templateEngine;
@@ -28,22 +28,20 @@ public sealed class ThemePageRenderer
}
/// <summary>
/// Renders the page model with the theme selected for the requested theme identifier.
/// Renders the page model with the theme selected for the requested theme identifier, producing the content and script sections of the page.
/// </summary>
/// <param name="model">The page model to render.</param>
/// <param name="requestedThemeId">The optional unique identifier of the theme to render with, falling back to the current theme when <see langword="null"/>.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the rendered HTML, or an error.</returns>
public async Task<Result<string>> RenderAsync(ThemePageDto model, string? requestedThemeId = null, CancellationToken cancellationToken = default)
/// <returns>An <see cref="Result{TValue}"/> containing either the rendered page, or an error.</returns>
public virtual async Task<Result<ThemePageRenderResultDto>> RenderAsync(ThemePageDto model, string? requestedThemeId = null, CancellationToken cancellationToken = default)
{
ThemeRenderDocumentDto document = await _themeService.GetRenderDocumentAsync(model.PageKey, requestedThemeId, cancellationToken);
model.ThemeId = document.Theme.Id;
model.ThemeName = document.Theme.Name;
model.ThemeVersion = document.Theme.Version;
model.AssetBase = $"/theme-assets/{document.Theme.Id}/assets";
model.CurrentYear = DateTime.UtcNow.Year;
model.ScriptId = ScriptIdentifierHelper.GenerateScriptId();
return _templateEngine.Render(document.Template, model);
return _templateEngine.RenderPage(document.Template, model);
}
}
@@ -110,7 +110,7 @@ public sealed class ThemeService
themeId = requestedThemeId;
ThemeTemplateResponseDto response = await _apiHttpClient.GetAsync<ThemeTemplateResponseDto>(
ApiRoutes.Themes.GET_THEME_TEMPLATE.Replace("{themeId}", themeId).Replace("{pageKey}", pageKey), cancellationToken).ConfigureAwait(false);
ApiRoutes.Themes.GET_THEME_TEMPLATE.Replace("{themeId}", themeId).Replace("{*pageKey}", pageKey), cancellationToken).ConfigureAwait(false);
return new ThemeRenderDocumentDto(ToThemeInfo(response.Theme), response.Template);
}
@@ -26,28 +26,12 @@ public sealed class ThemeTemplateEngine
RegexOptions.Compiled | RegexOptions.CultureInvariant);
/// <summary>
/// Validates the structure and expressions of a theme template.
/// </summary>
/// <param name="template">The template source to validate.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public Result<Success> Validate(string template)
{
ArgumentNullException.ThrowIfNull(template);
int cursor = 0;
Result<IReadOnlyList<ThemeTemplateNodeDto>> parseResult = ParseNodes(template, ref cursor, expectedClosingName: null, depth: 0);
if (parseResult.IsFailure)
return parseResult.Errors;
return Result.Success;
}
/// <summary>
/// Renders a theme template against the provided model.
/// Renders a theme template against the provided model, splitting the reserved top-level <c>scripts</c> section from the page content.
/// </summary>
/// <param name="template">The template source to render.</param>
/// <param name="model">The model the template expressions resolve against.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the rendered output, or an error.</returns>
public Result<string> Render(string template, object model)
/// <returns>An <see cref="Result{TValue}"/> containing either the rendered page, or an error.</returns>
public Result<ThemePageRenderResultDto> RenderPage(string template, object model)
{
ArgumentNullException.ThrowIfNull(template);
ArgumentNullException.ThrowIfNull(model);
@@ -57,12 +41,38 @@ public sealed class ThemeTemplateEngine
if (parseResult.IsFailure)
return parseResult.Errors;
StringBuilder output = new(Math.Min(template.Length * 2, 256 * 1024));
Result<Success> renderResult = RenderNodes(parseResult.Value, new ThemeRenderScopeDto(model, Parent: null), output);
if (renderResult.IsFailure)
return renderResult.Errors;
// the reserved top-level 'scripts' section holds the script of the page and is rendered separately, mirroring the
// 'Scripts' section of a Razor view, so that the layout can host it in the scripts container of the navigator;
// every other top-level node is the content of the page section
List<ThemeTemplateNodeDto> contentNodes = [];
ThemeSectionNodeDto? scriptsSection = null;
foreach (ThemeTemplateNodeDto node in parseResult.Value)
{
if (node is ThemeSectionNodeDto { Inverted: false } section
&& scriptsSection is null
&& string.Equals(section.Expression, "scripts", StringComparison.OrdinalIgnoreCase))
{
scriptsSection = section;
continue;
}
return output.ToString();
contentNodes.Add(node);
}
StringBuilder contentOutput = new(Math.Min(template.Length * 2, 256 * 1024));
Result<Success> contentResult = RenderNodes(contentNodes, new ThemeRenderScopeDto(model, Parent: null), contentOutput);
if (contentResult.IsFailure)
return contentResult.Errors;
StringBuilder scriptOutput = new(64 * 1024);
if (scriptsSection is not null)
{
Result<Success> scriptResult = RenderNodes(scriptsSection.Children, new ThemeRenderScopeDto(model, Parent: null), scriptOutput);
if (scriptResult.IsFailure)
return scriptResult.Errors;
}
return new ThemePageRenderResultDto(contentOutput.ToString(), scriptOutput.ToString());
}
/// <summary>