ssg: oops, forgot drafts support

This commit is contained in:
Nick Vella 2019-08-05 22:30:36 +10:00
parent bcac35626d
commit 9db6fdd6e9
3 changed files with 67 additions and 15 deletions

View File

@ -58,7 +58,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
public BlogInfo[] GetUsersBlogs() => new BlogInfo[0]; public BlogInfo[] GetUsersBlogs() => new BlogInfo[0];
public BlogPostCategory[] GetCategories(string blogId) => public BlogPostCategory[] GetCategories(string blogId) =>
StaticSitePost.GetAllPosts(Config) StaticSitePost.GetAllPosts(Config, false)
.SelectMany(post => post.BlogPost.Categories.Select(cat => cat.Name)) .SelectMany(post => post.BlogPost.Categories.Select(cat => cat.Name))
.Distinct() .Distinct()
.Select(cat => new BlogPostCategory(cat)) .Select(cat => new BlogPostCategory(cat))
@ -75,7 +75,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
/// <param name="now">If null, then includes future posts. If non-null, then only includes posts before the *UTC* 'now' time.</param> /// <param name="now">If null, then includes future posts. If non-null, then only includes posts before the *UTC* 'now' time.</param>
/// <returns></returns> /// <returns></returns>
public BlogPost[] GetRecentPosts(string blogId, int maxPosts, bool includeCategories, DateTime? now) => public BlogPost[] GetRecentPosts(string blogId, int maxPosts, bool includeCategories, DateTime? now) =>
StaticSitePost.GetAllPosts(Config) StaticSitePost.GetAllPosts(Config, true)
.Select(post => post.BlogPost) .Select(post => post.BlogPost)
.Where(post => post != null && (now == null || post.DatePublished < now)) .Where(post => post != null && (now == null || post.DatePublished < now))
.OrderByDescending(post => post.DatePublished) .OrderByDescending(post => post.DatePublished)
@ -93,7 +93,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
etag = ""; etag = "";
// Create a StaticSitePost on the provided post // Create a StaticSitePost on the provided post
return DoNewItem(new StaticSitePost(Config, post)); return DoNewItem(new StaticSitePost(Config, post, !publish));
} }
public bool EditPost(string blogId, BlogPost post, INewCategoryContext newCategoryContext, bool publish, out string etag, out XmlDocument remotePost) public bool EditPost(string blogId, BlogPost post, INewCategoryContext newCategoryContext, bool publish, out string etag, out XmlDocument remotePost)
@ -107,10 +107,17 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
etag = ""; etag = "";
// Create a StaticSitePost on the provided post // Create a StaticSitePost on the provided post
var ssgPost = new StaticSitePost(Config, post); var ssgPost = new StaticSitePost(Config, post, !publish);
if(ssgPost.FilePathById == null) if(ssgPost.FilePathById == null)
{ {
// If we are publishing and there exists a draft with this ID, delete it.
if (publish)
{
var filePath = new StaticSitePost(Config, post, true).FilePathById;
if (filePath != null) File.Delete(filePath);
}
// Existing post could not be found to edit, call NewPost instead; // Existing post could not be found to edit, call NewPost instead;
NewPost(blogId, post, newCategoryContext, publish, out etag, out remotePost); NewPost(blogId, post, newCategoryContext, publish, out etag, out remotePost);
return true; return true;
@ -157,11 +164,17 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
public string NewPage(string blogId, BlogPost page, bool publish, out string etag, out XmlDocument remotePost) public string NewPage(string blogId, BlogPost page, bool publish, out string etag, out XmlDocument remotePost)
{ {
if (!publish && !Options.SupportsPostAsDraft) if(!publish)
{ {
Trace.Fail("Static site does not support drafts, cannot post."); Trace.Fail("Posting pages as drafts not yet implemented.");
throw new BlogClientPostAsDraftUnsupportedException(); throw new BlogClientPostAsDraftUnsupportedException();
} }
//if (!publish && !Options.SupportsPostAsDraft)
//{
// Trace.Fail("Static site does not support drafts, cannot post.");
// throw new BlogClientPostAsDraftUnsupportedException();
//}
remotePost = null; remotePost = null;
etag = ""; etag = "";
@ -171,11 +184,16 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
public bool EditPage(string blogId, BlogPost page, bool publish, out string etag, out XmlDocument remotePost) public bool EditPage(string blogId, BlogPost page, bool publish, out string etag, out XmlDocument remotePost)
{ {
if (!publish && !Options.SupportsPostAsDraft) if (!publish)
{ {
Trace.Fail("Static site does not support drafts, cannot post."); Trace.Fail("Posting pages as drafts not yet implemented.");
throw new BlogClientPostAsDraftUnsupportedException(); throw new BlogClientPostAsDraftUnsupportedException();
} }
//if (!publish && !Options.SupportsPostAsDraft)
//{
// Trace.Fail("Static site does not support drafts, cannot post.");
// throw new BlogClientPostAsDraftUnsupportedException();
//}
remotePost = null; remotePost = null;
etag = ""; etag = "";

View File

@ -24,6 +24,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
protected StaticSiteConfig SiteConfig; protected StaticSiteConfig SiteConfig;
public BlogPost BlogPost { get; private set; } public BlogPost BlogPost { get; private set; }
public bool IsDraft { get; set; } = false;
public StaticSiteItem(StaticSiteConfig config) public StaticSiteItem(StaticSiteConfig config)
{ {
@ -37,6 +38,13 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
BlogPost = blogPost; BlogPost = blogPost;
} }
public StaticSiteItem(StaticSiteConfig config, BlogPost blogPost, bool isDraft)
{
SiteConfig = config;
BlogPost = blogPost;
IsDraft = isDraft;
}
public virtual StaticSiteItemFrontMatter FrontMatter public virtual StaticSiteItemFrontMatter FrontMatter
{ {
get => StaticSiteItemFrontMatter.GetFromBlogPost(SiteConfig.FrontMatterKeys, BlogPost); get => StaticSiteItemFrontMatter.GetFromBlogPost(SiteConfig.FrontMatterKeys, BlogPost);
@ -189,6 +197,20 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
protected abstract string GetSlugFromPublishFileName(string publishFileName); protected abstract string GetSlugFromPublishFileName(string publishFileName);
/// <summary>
/// If the item is a Post and a Draft, returns the Drafts dir, otherwise returns the regular dir
/// </summary>
protected string ItemRelativeDir =>
IsDraft && !BlogPost.IsPage && SiteConfig.DraftsEnabled ?
SiteConfig.DraftsPath
: (
BlogPost.IsPage ?
SiteConfig.PagesPath
:
SiteConfig.PostsPath
);
/// <summary> /// <summary>
/// Save the post to the correct directory /// Save the post to the correct directory
/// </summary> /// </summary>

View File

@ -27,25 +27,30 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
{ {
} }
public StaticSitePost(StaticSiteConfig config, BlogPost blogPost, bool isDraft) : base(config, blogPost, isDraft)
{
}
protected override string GetSlugFromPublishFileName(string publishFileName) => FILENAME_SLUG_REGEX.Match(publishFileName).Groups[1].Value; protected override string GetSlugFromPublishFileName(string publishFileName) => FILENAME_SLUG_REGEX.Match(publishFileName).Groups[1].Value;
public override string FilePathById { public override string FilePathById {
get get
{ {
if (_filePathById != null) return _filePathById; if (_filePathById != null) return _filePathById;
var foundFile = Directory.GetFiles(Path.Combine(SiteConfig.LocalSitePath, SiteConfig.PostsPath), "*.html")
var foundFile = Directory.GetFiles(Path.Combine(SiteConfig.LocalSitePath, ItemRelativeDir), "*.html")
.Where(postFile => .Where(postFile =>
{ {
try try
{ {
var post = LoadFromFile(Path.Combine(SiteConfig.LocalSitePath, SiteConfig.PostsPath, postFile), SiteConfig); var post = LoadFromFile(Path.Combine(SiteConfig.LocalSitePath, ItemRelativeDir, postFile), SiteConfig);
if (post.Id == Id) return true; if (post.Id == Id) return true;
} }
catch { } catch { }
return false; return false;
}).DefaultIfEmpty(null).FirstOrDefault(); }).DefaultIfEmpty(null).FirstOrDefault();
return _filePathById = (foundFile == null ? null : Path.Combine(SiteConfig.LocalSitePath, SiteConfig.PostsPath, foundFile)); return _filePathById = (foundFile == null ? null : Path.Combine(SiteConfig.LocalSitePath, ItemRelativeDir, foundFile));
} }
protected set => _filePathById = value; protected set => _filePathById = value;
@ -75,7 +80,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
{ {
return Path.Combine( return Path.Combine(
SiteConfig.LocalSitePath, SiteConfig.LocalSitePath,
SiteConfig.PostsPath, ItemRelativeDir,
GetFileNameForProvidedSlug(slug)); GetFileNameForProvidedSlug(slug));
} }
@ -96,19 +101,26 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
/// Get all valid posts in PostsPath /// Get all valid posts in PostsPath
/// </summary> /// </summary>
/// <returns>An IEnumerable of StaticSitePost</returns> /// <returns>An IEnumerable of StaticSitePost</returns>
public static IEnumerable<StaticSiteItem> GetAllPosts(StaticSiteConfig config) => public static IEnumerable<StaticSiteItem> GetAllPosts(StaticSiteConfig config, bool includeDrafts) =>
Directory.GetFiles(Path.Combine(config.LocalSitePath, config.PostsPath), "*.html") Directory.GetFiles(Path.Combine(config.LocalSitePath, config.PostsPath), "*.html")
.Select(fileName => Path.Combine(config.LocalSitePath, config.PostsPath, fileName)) // Create full paths
.Concat(includeDrafts && config.DraftsEnabled ? // Collect drafts if they're enabled
Directory.GetFiles(Path.Combine(config.LocalSitePath, config.DraftsPath), "*.html")
.Select(fileName => Path.Combine(config.LocalSitePath, config.DraftsPath, fileName)) // Create full paths
:
new string[] { } // Drafts are not enabled or were not requested
)
.Select(postFile => .Select(postFile =>
{ {
try try
{ {
return LoadFromFile(Path.Combine(config.LocalSitePath, config.PostsPath, postFile), config); return LoadFromFile(postFile, config);
} }
catch { return null; } catch { return null; }
}) })
.Where(p => p != null); .Where(p => p != null);
public static StaticSiteItem GetPostById(StaticSiteConfig config, string id) public static StaticSiteItem GetPostById(StaticSiteConfig config, string id)
=> GetAllPosts(config).Where(post => post.Id == id).DefaultIfEmpty(null).FirstOrDefault(); => GetAllPosts(config, true).Where(post => post.Id == id).DefaultIfEmpty(null).FirstOrDefault();
} }
} }