From 7bcaec39d8ddfb66ce4453d0175fb0daed071823 Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Tue, 2 Jul 2019 18:39:41 +1000 Subject: [PATCH] ssg: StaticSiteClient: tidy post front matter class and generation logic --- .../Clients/StaticSiteClient.cs | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSiteClient.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSiteClient.cs index 7f788134..a667cb00 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSiteClient.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSiteClient.cs @@ -193,24 +193,43 @@ namespace OpenLiveWriter.BlogClient.Clients /// /// Post to generate front matter for /// - private PostFrontMatter GetFrontMatterForPost(BlogPost post) => - new PostFrontMatter() + private PostFrontMatter GetFrontMatterForPost(BlogPost post) + { + var frontMatter = new PostFrontMatter() { - title = post.Title, - author = post.Author.Name, - date = post.DatePublished.ToString("yyyy-MM-dd HH:mm:ss"), - categories = post.Categories.Select(cat => cat.Name).ToArray(), - tags = post.Keywords + Title = post.Title, + Categories = post.Categories.Select(cat => cat.Name).ToArray(), + Tags = post.Keywords }; + if (post.Author != null) frontMatter.Author = post.Author.Name; + if (post.DatePublished != new DateTime()) frontMatter.Date = post.DatePublished.ToString("yyyy-MM-dd HH:mm:ss"); + + return frontMatter; + } + private class PostFrontMatter { - public string title { get; set; } - public string author { get; set; } - public string date { get; set; } + [YamlMember(Alias = "title")] + public string Title { get; set; } - public string[] categories { get; set; } - public string tags { get; set; } + [YamlMember(Alias = "author")] + public string Author { get; set; } + + [YamlMember(Alias = "date")] + public string Date { get; set; } + + [YamlMember(Alias = "layout")] + public string Layout { get; set; } = "post"; + + [YamlMember(Alias = "categories")] + public string[] Categories { get; set; } + + [YamlMember(Alias = "tags")] + public string Tags { get; set; } + + public string Serialize() => (new Serializer().Serialize(this)); + public static PostFrontMatter Deserialize(string yaml) => (new Deserializer().Deserialize(yaml)); } } }