From 45196f5448e1f09a40344952a60d377791dbdad3 Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Fri, 19 Jul 2019 21:01:28 +1000 Subject: [PATCH] ssg: StaticSitePage impl --- .../Clients/StaticSite/StaticSitePage.cs | 90 ++++++++++++++++++- .../Clients/StaticSite/StaticSitePost.cs | 2 +- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSitePage.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSitePage.cs index 58cc4703..ea63cfc3 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSitePage.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSitePage.cs @@ -12,7 +12,7 @@ using OpenLiveWriter.Extensibility.BlogClient; namespace OpenLiveWriter.BlogClient.Clients.StaticSite { - public class StaticSitePage : StaticSitePost + public class StaticSitePage : StaticSiteItem { public StaticSitePage(StaticSiteConfig config) : base(config) { @@ -22,6 +22,94 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite { } + public override string FilePathById + { + get + { + if (_filePathById != null) return _filePathById; + var foundFile = Directory.GetFiles(Path.Combine(SiteConfig.LocalSitePath, SiteConfig.PagesPath), "*.html") + .Where(pageFile => + { + try + { + var page = LoadFromFile(Path.Combine(SiteConfig.LocalSitePath, SiteConfig.PagesPath, pageFile), SiteConfig); + if (page.Id == Id) return true; + } + catch { } + + return false; + }).DefaultIfEmpty(null).FirstOrDefault(); + return _filePathById = (foundFile == null ? null : Path.Combine(SiteConfig.LocalSitePath, SiteConfig.PagesPath, foundFile)); + } + + protected set => _filePathById = value; + } + + /// + /// Get the site path for the published post + /// eg. /2019/01/slug.html + /// + public override string SitePath + { + // TODO Generate permalink based on parent IDs + get => throw new NotImplementedException(); + } + + /// + /// Gets on-disk filename based on slug + /// + /// Post slug + /// File name with prepended date + protected override string GetFileNameForProvidedSlug(string slug) + { + // TODO, get slug for all previous posts and prepend + return $"{slug}{PUBLISH_FILE_EXTENSION}"; + } + + /// + /// Gets a path based on file name and posts path + /// + /// + /// Path containing pages path + protected override string GetFilePathForProvidedSlug(string slug) + { + return Path.Combine( + SiteConfig.LocalSitePath, + SiteConfig.PagesPath, + GetFileNameForProvidedSlug(slug)); + } + + /// + /// Load published page from a specified file path + /// + /// Path to published page file + /// StaticSiteConfig to instantiate page with + /// A loaded StaticSitePage + public static StaticSitePage LoadFromFile(string pageFilePath, StaticSiteConfig config) + { + var page = new StaticSitePage(config); + page.LoadFromFile(pageFilePath); + return page; + } + + /// + /// Get all valid pages in PagesPath + /// + /// An IEnumerable of StaticSitePage + public static IEnumerable GetAllPages(StaticSiteConfig config) => + Directory.GetFiles(Path.Combine(config.LocalSitePath, config.PagesPath), "*.html") + .Select(pageFile => + { + try + { + return LoadFromFile(Path.Combine(config.LocalSitePath, config.PagesPath, pageFile), config); + } + catch { return null; } + }) + .Where(p => p != null); + + public static StaticSitePage GetPageById(StaticSiteConfig config, string id) + => GetAllPages(config).Where(page => page.Id == id).DefaultIfEmpty(null).FirstOrDefault(); } } diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSitePost.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSitePost.cs index 9e0a5477..6787d5b6 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSitePost.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSitePost.cs @@ -31,7 +31,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite { try { - var post = StaticSitePost.LoadFromFile(Path.Combine(SiteConfig.LocalSitePath, SiteConfig.PostsPath, postFile), SiteConfig); + var post = LoadFromFile(Path.Combine(SiteConfig.LocalSitePath, SiteConfig.PostsPath, postFile), SiteConfig); if (post.Id == Id) return true; } catch { }