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:
parent
34bf088c78
commit
af1e340816
|
|
@ -5,10 +5,11 @@ using System.Text.Json.Serialization;
|
||||||
public class ProjectSettings
|
public class ProjectSettings
|
||||||
{
|
{
|
||||||
// The Source and Destination need to be public for the json constructor to work properly.
|
// 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 Source {get; private set;}
|
||||||
public string Destination {get; private set;}
|
public string Destination {get; private set;}
|
||||||
private DirectoryInfo? _ProjectRoot;
|
|
||||||
public string? BaseUrl {get; private set;}
|
public string? BaseUrl {get; private set;}
|
||||||
|
public string? SiteName {get; private set;}
|
||||||
|
|
||||||
public DirectoryInfo InputDirectory {get {
|
public DirectoryInfo InputDirectory {get {
|
||||||
if (_ProjectRoot is null)
|
if (_ProjectRoot is null)
|
||||||
|
|
@ -26,10 +27,11 @@ public class ProjectSettings
|
||||||
}}
|
}}
|
||||||
|
|
||||||
[JsonConstructor]
|
[JsonConstructor]
|
||||||
public ProjectSettings(String source, String destination, string baseUrl) {
|
public ProjectSettings(string source, string destination, string baseUrl, string siteName) {
|
||||||
Source = source;
|
Source = source;
|
||||||
Destination = destination;
|
Destination = destination;
|
||||||
BaseUrl = baseUrl;
|
BaseUrl = baseUrl;
|
||||||
|
SiteName = siteName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProjectRoot(string projectRoot) {
|
public void setProjectRoot(string projectRoot) {
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ public static class Conversions{
|
||||||
{".md", Pandoc},
|
{".md", Pandoc},
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly string[] BaseUrlFiletypes = {
|
private static readonly string[] StringReplaceFiletypes = {
|
||||||
".md",
|
".md",
|
||||||
".html"
|
".html"
|
||||||
};
|
};
|
||||||
|
|
@ -46,9 +46,9 @@ public static class Conversions{
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -110,13 +110,13 @@ public static class Conversions{
|
||||||
|
|
||||||
// the empty string is used as it has a defined identity
|
// the empty string is used as it has a defined identity
|
||||||
string tmpFile = string.Empty;
|
string tmpFile = string.Empty;
|
||||||
if (BaseUrlFiletypes.Contains(file.Extension))
|
if (StringReplaceFiletypes.Contains(file.Extension))
|
||||||
{
|
{
|
||||||
Log.Information("Replacing baseurl for file {f}",file.FullName);
|
Log.Information("Replacing baseurl for file {f}",file.FullName);
|
||||||
tmpFile = Path.Join(Path.GetTempPath(),"pandoc",file.Name);
|
tmpFile = Path.Join(Path.GetTempPath(),"pandoc",file.Name);
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(tmpFile)!); // NOTE: It is practially impossible that this would actually return null
|
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.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)
|
if (template is not null)
|
||||||
{
|
{
|
||||||
|
|
@ -124,7 +124,7 @@ public static class Conversions{
|
||||||
string tmpTemplateFile = Path.Join(Path.GetTempPath(),"pandoc",template.Name);
|
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
|
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.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);
|
template = new(tmpTemplateFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -214,8 +214,8 @@ public static class Conversions{
|
||||||
.Replace(file.Extension,newExtension ?? file.Extension);
|
.Replace(file.Extension,newExtension ?? file.Extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string? BaseUrlReplace(FileInfo file, ProjectSettings settings){
|
private static string StringReplace(FileInfo file, ProjectSettings settings){
|
||||||
Log.Information("Doing BaseUrlReplace for {f}", file.FullName);
|
Log.Information("Doing StringReplace for {f}", file.FullName);
|
||||||
// Read the file
|
// Read the file
|
||||||
using (StreamReader FileReader = file.OpenText())
|
using (StreamReader FileReader = file.OpenText())
|
||||||
{
|
{
|
||||||
|
|
@ -223,12 +223,27 @@ public static class Conversions{
|
||||||
|
|
||||||
if (settings.BaseUrl is null)
|
if (settings.BaseUrl is null)
|
||||||
{
|
{
|
||||||
Log.Warning("BaseUrl is null, replacing templateString with nothing.");
|
Log.Warning("BaseUrl is null, replacing %BASEURL% with nothing.");
|
||||||
return filestring.Replace("%BASEURL%","");
|
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);
|
if (settings.SiteName is null)
|
||||||
return filestring.Replace("%BASEURL%",settings.BaseUrl);
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
{
|
{
|
||||||
"Source" : "./src",
|
"Source" : "./src",
|
||||||
"Destination" : "./dst",
|
"Destination" : "./dst",
|
||||||
"BaseUrl" : null
|
"BaseUrl" : null,
|
||||||
|
"SiteName" : "Example Town"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,11 @@
|
||||||
|
---
|
||||||
|
title: '%SITENAME%Page1'
|
||||||
|
---
|
||||||
# This is a markdown file
|
# This is a markdown file
|
||||||
|
|
||||||
This file is designed as a simple test to ensure that things are working
|
This file is designed as a simple test to ensure that things are working
|
||||||
the way I intend them to.
|
the way I intend them to.
|
||||||
|
%BASEURL%
|
||||||
|
|
||||||
|
HERE IS SOME MORE TEXT TO ENSURE I AM NOT GOING MAD
|
||||||
|
%SITENAME%
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user