feat: Add SiteName replacer

Now the string `%SITENAME%` can be replaced with a value stored in the
`cssitegen.json` file of a project.
This commit is contained in:
Robert Morrison 2024-06-03 03:22:41 +01:00
parent 34bf088c78
commit af1e340816
Signed by: robert
GPG Key ID: 73E012EB3F4EC696
4 changed files with 40 additions and 15 deletions

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -1,5 +1,6 @@
{
"Source" : "./src",
"Destination" : "./dst",
"BaseUrl" : null
"BaseUrl" : null,
"SiteName" : "Example Town"
}

View File

@ -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%