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 BlogPostCategory[] GetCategories(string blogId) =>
StaticSitePost.GetAllPosts(Config)
StaticSitePost.GetAllPosts(Config, false)
.SelectMany(post => post.BlogPost.Categories.Select(cat => cat.Name))
.Distinct()
.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>
/// <returns></returns>
public BlogPost[] GetRecentPosts(string blogId, int maxPosts, bool includeCategories, DateTime? now) =>
StaticSitePost.GetAllPosts(Config)
StaticSitePost.GetAllPosts(Config, true)
.Select(post => post.BlogPost)
.Where(post => post != null && (now == null || post.DatePublished < now))
.OrderByDescending(post => post.DatePublished)
@ -93,7 +93,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
etag = "";
// 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)
@ -107,10 +107,17 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
etag = "";
// 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 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;
NewPost(blogId, post, newCategoryContext, publish, out etag, out remotePost);
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)
{
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();
}
//if (!publish && !Options.SupportsPostAsDraft)
//{
// Trace.Fail("Static site does not support drafts, cannot post.");
// throw new BlogClientPostAsDraftUnsupportedException();
//}
remotePost = null;
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)
{
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();
}
//if (!publish && !Options.SupportsPostAsDraft)
//{
// Trace.Fail("Static site does not support drafts, cannot post.");
// throw new BlogClientPostAsDraftUnsupportedException();
//}
remotePost = null;
etag = "";

View File

@ -24,6 +24,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
protected StaticSiteConfig SiteConfig;
public BlogPost BlogPost { get; private set; }
public bool IsDraft { get; set; } = false;
public StaticSiteItem(StaticSiteConfig config)
{
@ -37,6 +38,13 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
BlogPost = blogPost;
}
public StaticSiteItem(StaticSiteConfig config, BlogPost blogPost, bool isDraft)
{
SiteConfig = config;
BlogPost = blogPost;
IsDraft = isDraft;
}
public virtual StaticSiteItemFrontMatter FrontMatter
{
get => StaticSiteItemFrontMatter.GetFromBlogPost(SiteConfig.FrontMatterKeys, BlogPost);
@ -189,6 +197,20 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
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>
/// Save the post to the correct directory
/// </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;
public override string FilePathById {
get
{
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 =>
{
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;
}
catch { }
return false;
}).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;
@ -75,7 +80,7 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
{
return Path.Combine(
SiteConfig.LocalSitePath,
SiteConfig.PostsPath,
ItemRelativeDir,
GetFileNameForProvidedSlug(slug));
}
@ -96,19 +101,26 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite
/// Get all valid posts in PostsPath
/// </summary>
/// <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")
.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 =>
{
try
{
return LoadFromFile(Path.Combine(config.LocalSitePath, config.PostsPath, postFile), config);
return LoadFromFile(postFile, config);
}
catch { return null; }
})
.Where(p => p != null);
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();
}
}