Skip to content

Commit

Permalink
Merge pull request #29 from Sharp0802/dev
Browse files Browse the repository at this point in the history
[PR] Refactor management system for post
  • Loading branch information
Sharp0802 authored Oct 5, 2023
2 parents 29dacbd + ea5d08e commit 2835d67
Show file tree
Hide file tree
Showing 31 changed files with 701 additions and 328 deletions.
20 changes: 20 additions & 0 deletions src/BlogMan.Test/BlogMan.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Include="Resources/**/*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BlogMan\BlogMan.csproj"/>
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/BlogMan.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

await BlogMan.Program.Main(
"build",
"--project",
"Resources/test.json");
2 changes: 2 additions & 0 deletions src/BlogMan.Test/Resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
site/
19 changes: 19 additions & 0 deletions src/BlogMan.Test/Resources/post/test-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
LCID: ko-kr
Layout: default
Title: Welcome!
Timestamps:
- 2023-10-04
- 2023-10-05
- 2023-10-06
Topic: [
"test-topic-0",
"test-topic-1"
]
---

## Hello There!

[test-link-1](ref::test/test-post-1.md)

[test-link-2](ref::test/test/test-post-2.md)
19 changes: 19 additions & 0 deletions src/BlogMan.Test/Resources/post/test0/test-post-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
LCID: ko-kr
Layout: default
Title: Welcome!
Timestamps:
- 2023-10-04
- 2023-10-05
- 2023-10-06
Topic: [
"test-topic-0",
"test-topic-1"
]
---

## Hello There!

[test-link-1](ref::test/test-post-1.md)

[test-link-2](ref::test/test/test-post-2.md)
19 changes: 19 additions & 0 deletions src/BlogMan.Test/Resources/post/test0/test1/test-post-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
LCID: ko-kr
Layout: default
Title: Welcome!
Timestamps:
- 2023-10-04
- 2023-10-05
- 2023-10-06
Topic: [
"test-topic-0",
"test-topic-1"
]
---

## Hello There!

[test-link-1](ref::test0/test-post-1.md)

[test-link-2](ref::test0/test1/test-post-2.md)
20 changes: 20 additions & 0 deletions src/BlogMan.Test/Resources/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Info": {
"Name": "sharp0802-github-io",
"ApiVersion": "1.0.0.0",
"RootUri": "https://sharp0802.github.io/",
"PostDirectory": "post/",
"LayoutDirectory": "layout/",
"SiteDirectory": "docs/",
"BuildDirectory": "build/"
},
"Profile": {
"UserName": "Sharp0802",
"ProfileImage": "https://avatars.githubusercontent.com/u/64760536?v=4"
},
"Contacts": {
"GitHub": "https://github.com/Sharp0802",
"Email": "sharp0802.business@gmail.com",
"Misc": []
}
}
10 changes: 10 additions & 0 deletions src/BlogMan.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlogMan", "BlogMan\BlogMan.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlogMan.Generator", "BlogMan.Generator\BlogMan.Generator.csproj", "{815C3F5F-8B43-496B-BE37-B7680C4FB58E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlogMan.Test", "BlogMan.Test\BlogMan.Test.csproj", "{725CA205-AA94-4E78-BC75-AF3FE6710CC0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Expand Down Expand Up @@ -34,5 +36,13 @@ Global
{815C3F5F-8B43-496B-BE37-B7680C4FB58E}.Debug|x64.Build.0 = Debug|Any CPU
{815C3F5F-8B43-496B-BE37-B7680C4FB58E}.Release|x64.ActiveCfg = Release|Any CPU
{815C3F5F-8B43-496B-BE37-B7680C4FB58E}.Release|x64.Build.0 = Release|Any CPU
{725CA205-AA94-4E78-BC75-AF3FE6710CC0}.Debug|x86.ActiveCfg = Debug|Any CPU
{725CA205-AA94-4E78-BC75-AF3FE6710CC0}.Debug|x86.Build.0 = Debug|Any CPU
{725CA205-AA94-4E78-BC75-AF3FE6710CC0}.Release|x86.ActiveCfg = Release|Any CPU
{725CA205-AA94-4E78-BC75-AF3FE6710CC0}.Release|x86.Build.0 = Release|Any CPU
{725CA205-AA94-4E78-BC75-AF3FE6710CC0}.Debug|x64.ActiveCfg = Debug|Any CPU
{725CA205-AA94-4E78-BC75-AF3FE6710CC0}.Debug|x64.Build.0 = Debug|Any CPU
{725CA205-AA94-4E78-BC75-AF3FE6710CC0}.Release|x64.ActiveCfg = Release|Any CPU
{725CA205-AA94-4E78-BC75-AF3FE6710CC0}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
50 changes: 29 additions & 21 deletions src/BlogMan/Components/GoogleSitemapLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,61 @@ namespace BlogMan.Components;

public sealed class GoogleSitemapLinker : LinkerBase
{
private readonly object _sync = new();
private XElement? _root;

private static XNamespace Xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9";

private readonly object _sync = new();
private XElement? _root;

public GoogleSitemapLinker(Project project) : base(project)
{
}

protected override bool Initialize()
{
_root = new XElement(
"urlset",
new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
);
_root = new XElement(Xmlns.GetName("urlset"));
return true;
}

protected override bool Link(LinkerEventArgs args)
{
var uri = new Uri(
new Uri(Project.Info.RootUri),
Path.GetRelativePath(Project.Info.SiteDirectory, args.Destination.FullName));
var mod = args.PostNode.Metadata.Timestamps.Max();

var href = args.PostNode.GetHRef();
if (href.StartsWith('/'))
href = href[1..];

var uri = new Uri(new Uri(Project.Info.RootUri), href);

var mod = args.PostNode.Metadata?.Timestamps.Max();
if (mod is null)
return false;

lock (_sync)
{
_root!.Add(new XElement(
"url",
new XElement("loc", uri),
new XElement("lastmod", mod)
Xmlns.GetName("url"),
new XElement(Xmlns.GetName("loc"), uri),
new XElement(Xmlns.GetName("lastmod"), mod)
)
);
}

return true;
}

public override void Dispose()
protected override void CleanUp()
{
var xml = new XDocument(new XDeclaration("1.0", "UTF-8", null));
xml.Add(_root);

var dst = Path.Combine(Project.Info.SiteDirectory, "sitemap.xml");

using var writer = new XmlTextWriter(dst, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
using var stream = new FileStream(dst, FileMode.Create, FileAccess.Write);
using var writer = XmlWriter.Create(stream, new XmlWriterSettings
{
Encoding = Encoding.UTF8,
Indent = true,
IndentChars = "\t"
});

xml.Save(writer);
xml.WriteTo(writer);
}
}
90 changes: 65 additions & 25 deletions src/BlogMan/Components/Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,34 @@ public static void InitializeRoot(RootCommand root)

private static Project? ReadProject(FileInfo project)
{
return SEH.IO(project, static project =>
{
using var file = File.OpenRead(project.FullName);
var data = JsonSerializer.Deserialize(file, SourceGenerationContext.Default.Project);
if (data is null)
if (!SEH.IO(project, static project =>
{
Logger.Log(LogLevel.FAIL, "Cannot deserialize project file", project.FullName);
return null;
}
var errors = data.Validate().ToArray();
if (errors.Length != 0)
{
errors.PrintErrors(project.FullName);
return null;
}
return data;
}, out var data)
? data
: null;
using var file = File.OpenRead(project.FullName);
var data = JsonSerializer.Deserialize(file, SourceGenerationContext.Default.Project);
if (data is null)
{
Logger.Log(LogLevel.FAIL, "Cannot deserialize project file", project.FullName);
return null;
}
var errors = data.Validate().ToArray();
if (errors.Length != 0)
{
errors.PrintErrors(project.FullName);
return null;
}
return data;
}, out var proj))
return null;

proj.Info.BuildDirectory = Path.Combine(project.DirectoryName!, proj.Info.BuildDirectory);

Check warning on line 74 in src/BlogMan/Components/Initializer.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 74 in src/BlogMan/Components/Initializer.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
proj.Info.LayoutDirectory = Path.Combine(project.DirectoryName!, proj.Info.LayoutDirectory);
proj.Info.PostDirectory = Path.Combine(project.DirectoryName!, proj.Info.PostDirectory);
proj.Info.SiteDirectory = Path.Combine(project.DirectoryName!, proj.Info.SiteDirectory);

return proj;
}

private static void Build(FileInfo project)
Expand All @@ -93,10 +99,44 @@ private static void Build(FileInfo project)

Logger.Log(LogLevel.INFO, "Start building project");

if (new RazorTemplateLinker(data).Run())
Logger.Log(LogLevel.CMPL, "Complete building project");
else
Logger.Log(LogLevel.FAIL, "Failed to build project");

Logger.Log(LogLevel.INFO, "start global initializing");

if (!SEH.IO(data.Info.SiteDirectory, dir =>
{
var info = new DirectoryInfo(dir);
if (info.Exists)
info.Delete(true);
info.Create();
}))
{
Logger.Log(LogLevel.FAIL, "failed global initializing");
return;
}

Logger.Log(LogLevel.CMPL, "complete global initializing");

using (var razor = new RazorTemplateLinker(data))
{
if (!razor.Run())
{
Logger.Log(LogLevel.FAIL, "Failed to build project");
return;
}
}

Logger.Log(LogLevel.CMPL, "Complete building project");

using (var google = new GoogleSitemapLinker(data))
{
if (!google.Run())
{
Logger.Log(LogLevel.FAIL, "Failed to build sitemap");
return;
}
}

Logger.Log(LogLevel.CMPL, "Complete building sitemap");
}

private static void Init(string name, string root)
Expand Down
Loading

0 comments on commit 2835d67

Please sign in to comment.