Add editable portal page content
This commit is contained in:
+28
-9
@@ -10,7 +10,10 @@ internal static class WebPortal
|
||||
private const string TemplateFileName = "index.template.html";
|
||||
private const string OutputFileName = "index.html";
|
||||
private const string ServiceCardsToken = "{{NGINXMAN_SERVICE_CARDS}}";
|
||||
private const string TemplateVersionMarker = "<!-- NGINXMAN_TEMPLATE_VERSION:2 -->";
|
||||
private const string SiteNameToken = "{{NGINXMAN_PORTAL_SITE_NAME}}";
|
||||
private const string HeadlineToken = "{{NGINXMAN_PORTAL_HEADLINE}}";
|
||||
private const string IntroductionToken = "{{NGINXMAN_PORTAL_INTRODUCTION}}";
|
||||
private const string TemplateVersionMarker = "<!-- NGINXMAN_TEMPLATE_VERSION:3 -->";
|
||||
private static readonly UTF8Encoding Utf8WithoutBom = new(false);
|
||||
|
||||
public static void ExtractTo(string dataDirectory)
|
||||
@@ -58,26 +61,42 @@ internal static class WebPortal
|
||||
}
|
||||
}
|
||||
|
||||
public static string RenderTo(string dataDirectory, IReadOnlyList<RouteEntry> routes)
|
||||
public static string RenderTo(
|
||||
string dataDirectory,
|
||||
PortalContentSettings portalContent,
|
||||
IReadOnlyList<RouteEntry> routes)
|
||||
{
|
||||
var targetDirectory = Path.Combine(dataDirectory, "www");
|
||||
var templatePath = Path.Combine(targetDirectory, TemplateFileName);
|
||||
if (!File.Exists(templatePath))
|
||||
if (!File.Exists(templatePath) ||
|
||||
!File.ReadAllText(templatePath, Encoding.UTF8).Contains(
|
||||
TemplateVersionMarker,
|
||||
StringComparison.Ordinal))
|
||||
{
|
||||
ExtractTo(dataDirectory);
|
||||
}
|
||||
|
||||
var template = File.ReadAllText(templatePath, Encoding.UTF8);
|
||||
if (!template.Contains(ServiceCardsToken, StringComparison.Ordinal))
|
||||
var requiredTokens = new[]
|
||||
{
|
||||
ServiceCardsToken,
|
||||
SiteNameToken,
|
||||
HeadlineToken,
|
||||
IntroductionToken
|
||||
};
|
||||
var missingToken = requiredTokens.FirstOrDefault(
|
||||
token => !template.Contains(token, StringComparison.Ordinal));
|
||||
if (missingToken is not null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"网页模板缺少入口占位符 {ServiceCardsToken}: {templatePath}");
|
||||
$"网页模板缺少占位符 {missingToken}: {templatePath}");
|
||||
}
|
||||
|
||||
var content = template.Replace(
|
||||
ServiceCardsToken,
|
||||
BuildServiceCards(routes),
|
||||
StringComparison.Ordinal);
|
||||
var content = template
|
||||
.Replace(SiteNameToken, Encode(portalContent.SiteName), StringComparison.Ordinal)
|
||||
.Replace(HeadlineToken, Encode(portalContent.Headline), StringComparison.Ordinal)
|
||||
.Replace(IntroductionToken, Encode(portalContent.Introduction), StringComparison.Ordinal)
|
||||
.Replace(ServiceCardsToken, BuildServiceCards(routes), StringComparison.Ordinal);
|
||||
var outputPath = Path.Combine(targetDirectory, OutputFileName);
|
||||
File.WriteAllText(outputPath, content, Utf8WithoutBom);
|
||||
return outputPath;
|
||||
|
||||
Reference in New Issue
Block a user