From af1e3408167bfceb4c88032509c438b557ac5546 Mon Sep 17 00:00:00 2001 From: Robert Morrison Date: Mon, 3 Jun 2024 03:22:41 +0100 Subject: [PATCH] feat: Add SiteName replacer Now the string `%SITENAME%` can be replaced with a value stored in the `cssitegen.json` file of a project. --- ProjectSettings/ProjectSettings.cs | 6 ++-- SiteFile/SiteFile.ConverterFunctions.cs | 39 +++++++++++++++++-------- Testing/cssitegen.json | 3 +- Testing/src/index.md | 7 +++++ 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/ProjectSettings/ProjectSettings.cs b/ProjectSettings/ProjectSettings.cs index 9247dd2..2929220 100644 --- a/ProjectSettings/ProjectSettings.cs +++ b/ProjectSettings/ProjectSettings.cs @@ -5,10 +5,11 @@ using System.Text.Json.Serialization; public class ProjectSettings { // The Source and Destination need to be public for the json constructor to work properly. + private DirectoryInfo? _ProjectRoot; public string Source {get; private set;} public string Destination {get; private set;} - private DirectoryInfo? _ProjectRoot; public string? BaseUrl {get; private set;} + public string? SiteName {get; private set;} public DirectoryInfo InputDirectory {get { if (_ProjectRoot is null) @@ -26,10 +27,11 @@ public class ProjectSettings }} [JsonConstructor] - public ProjectSettings(String source, String destination, string baseUrl) { + public ProjectSettings(string source, string destination, string baseUrl, string siteName) { Source = source; Destination = destination; BaseUrl = baseUrl; + SiteName = siteName; } public void setProjectRoot(string projectRoot) { diff --git a/SiteFile/SiteFile.ConverterFunctions.cs b/SiteFile/SiteFile.ConverterFunctions.cs index 969019d..fc53443 100644 --- a/SiteFile/SiteFile.ConverterFunctions.cs +++ b/SiteFile/SiteFile.ConverterFunctions.cs @@ -15,7 +15,7 @@ public static class Conversions{ {".md", Pandoc}, }; - private static readonly string[] BaseUrlFiletypes = { + private static readonly string[] StringReplaceFiletypes = { ".md", ".html" }; @@ -46,9 +46,9 @@ public static class Conversions{ } try { - if (BaseUrlFiletypes.Contains(file.Extension)) + if (StringReplaceFiletypes.Contains(file.Extension)) { - File.WriteAllText(newPath.FullName, BaseUrlReplace(file, settings)); + File.WriteAllText(newPath.FullName, StringReplace(file, settings)); } else { @@ -110,13 +110,13 @@ public static class Conversions{ // the empty string is used as it has a defined identity string tmpFile = string.Empty; - if (BaseUrlFiletypes.Contains(file.Extension)) + if (StringReplaceFiletypes.Contains(file.Extension)) { Log.Information("Replacing baseurl for file {f}",file.FullName); tmpFile = Path.Join(Path.GetTempPath(),"pandoc",file.Name); Directory.CreateDirectory(Path.GetDirectoryName(tmpFile)!); // NOTE: It is practially impossible that this would actually return null File.Create(tmpFile).Close(); // TODO: Use the filestream provided by File.Create within a using block to write the text - File.WriteAllText(tmpFile,BaseUrlReplace(file,settings)); + File.WriteAllText(tmpFile,StringReplace(file,settings)); if (template is not null) { @@ -124,7 +124,7 @@ public static class Conversions{ string tmpTemplateFile = Path.Join(Path.GetTempPath(),"pandoc",template.Name); Directory.CreateDirectory(Path.GetDirectoryName(tmpTemplateFile)!); // NOTE: It is practially impossible that this would actually return null File.Create(tmpTemplateFile).Close(); // TODO: Use the filestream provided by File.Create within a using block to write the text - File.WriteAllText(tmpTemplateFile,BaseUrlReplace(template,settings)); + File.WriteAllText(tmpTemplateFile,StringReplace(template,settings)); template = new(tmpTemplateFile); } } @@ -214,8 +214,8 @@ public static class Conversions{ .Replace(file.Extension,newExtension ?? file.Extension); } - private static string? BaseUrlReplace(FileInfo file, ProjectSettings settings){ - Log.Information("Doing BaseUrlReplace for {f}", file.FullName); + private static string StringReplace(FileInfo file, ProjectSettings settings){ + Log.Information("Doing StringReplace for {f}", file.FullName); // Read the file using (StreamReader FileReader = file.OpenText()) { @@ -223,12 +223,27 @@ public static class Conversions{ if (settings.BaseUrl is null) { - Log.Warning("BaseUrl is null, replacing templateString with nothing."); - return filestring.Replace("%BASEURL%",""); + Log.Warning("BaseUrl is null, replacing %BASEURL% with nothing."); + filestring = filestring.Replace("%BASEURL%",""); + } + else + { + Log.Information("Replacing %BASEURL% with {BaseUrl}",settings.BaseUrl); + filestring = filestring.Replace("%BASEURL%",settings.BaseUrl); } - Log.Information("Replacing templateString with {BaseUrl}",settings.BaseUrl); - return filestring.Replace("%BASEURL%",settings.BaseUrl); + if (settings.SiteName is null) + { + Log.Warning("SiteName is null, replacing %SITENAME% with nothing."); + filestring = filestring.Replace("%SITENAME%",""); + } + else + { + Log.Information("Replacing %SITENAME% with {SiteName}",settings.SiteName); + filestring = filestring.Replace("%SITENAME%",settings.SiteName); + } + + return filestring; } } diff --git a/Testing/cssitegen.json b/Testing/cssitegen.json index fd0fa32..3bf289c 100644 --- a/Testing/cssitegen.json +++ b/Testing/cssitegen.json @@ -1,5 +1,6 @@ { "Source" : "./src", "Destination" : "./dst", - "BaseUrl" : null + "BaseUrl" : null, + "SiteName" : "Example Town" } diff --git a/Testing/src/index.md b/Testing/src/index.md index 4afc140..b0a19e8 100644 --- a/Testing/src/index.md +++ b/Testing/src/index.md @@ -1,4 +1,11 @@ +--- +title: '%SITENAME%Page1' +--- # This is a markdown file This file is designed as a simple test to ensure that things are working the way I intend them to. +%BASEURL% + +HERE IS SOME MORE TEXT TO ENSURE I AM NOT GOING MAD +%SITENAME%