ssg: StaticSitePage impl

This commit is contained in:
Nick Vella 2019-07-19 21:01:28 +10:00
parent 5ee2844533
commit 45196f5448
2 changed files with 90 additions and 2 deletions

View File

@ -12,7 +12,7 @@ using OpenLiveWriter.Extensibility.BlogClient;
namespace OpenLiveWriter.BlogClient.Clients.StaticSite namespace OpenLiveWriter.BlogClient.Clients.StaticSite
{ {
public class StaticSitePage : StaticSitePost public class StaticSitePage : StaticSiteItem
{ {
public StaticSitePage(StaticSiteConfig config) : base(config) 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;
}
/// <summary>
/// Get the site path for the published post
/// eg. /2019/01/slug.html
/// </summary>
public override string SitePath
{
// TODO Generate permalink based on parent IDs
get => throw new NotImplementedException();
}
/// <summary>
/// Gets on-disk filename based on slug
/// </summary>
/// <param name="slug">Post slug</param>
/// <returns>File name with prepended date</returns>
protected override string GetFileNameForProvidedSlug(string slug)
{
// TODO, get slug for all previous posts and prepend
return $"{slug}{PUBLISH_FILE_EXTENSION}";
}
/// <summary>
/// Gets a path based on file name and posts path
/// </summary>
/// <param name="slug"></param>
/// <returns>Path containing pages path</returns>
protected override string GetFilePathForProvidedSlug(string slug)
{
return Path.Combine(
SiteConfig.LocalSitePath,
SiteConfig.PagesPath,
GetFileNameForProvidedSlug(slug));
}
/// <summary>
/// Load published page from a specified file path
/// </summary>
/// <param name="pageFilePath">Path to published page file</param>
/// <param name="config">StaticSiteConfig to instantiate page with</param>
/// <returns>A loaded StaticSitePage</returns>
public static StaticSitePage LoadFromFile(string pageFilePath, StaticSiteConfig config)
{
var page = new StaticSitePage(config);
page.LoadFromFile(pageFilePath);
return page;
}
/// <summary>
/// Get all valid pages in PagesPath
/// </summary>
/// <returns>An IEnumerable of StaticSitePage</returns>
public static IEnumerable<StaticSitePage> 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();
} }
} }

View File

@ -31,7 +31,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
{ {
try 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; if (post.Id == Id) return true;
} }
catch { } catch { }