Skip to content

Commit

Permalink
Add support for layout and front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Dec 28, 2015
1 parent c405ff3 commit 81fa7b7
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 128 deletions.
30 changes: 27 additions & 3 deletions src/managed/OpenLiveWriter.BlogClient/Clients/GitHubEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;

namespace OpenLiveWriter.BlogClient.Clients
{
Expand Down Expand Up @@ -153,7 +154,8 @@ public class PageMetadata
// Predefined Variables for Posts
public DateTime date;
// User defined dynamic variables
public IDictionary<string, Object> userDefinedMetadata;
public YamlStream userDefinedMetadata;


public PageMetadata()
{
Expand All @@ -162,7 +164,9 @@ public PageMetadata()

public PageMetadata(YamlMappingNode rootNode)
{
this.userDefinedMetadata = new Dictionary<string, object>();
var userMetadataRootNode = new YamlMappingNode();
this.userDefinedMetadata = new YamlStream(new YamlDocument(userMetadataRootNode));

this.categories = new List<string>();
this.tags = new List<string>();
if (rootNode != null)
Expand Down Expand Up @@ -222,13 +226,33 @@ public PageMetadata(YamlMappingNode rootNode)
this.tags.AddRange(tagsNode.Select(o => o.ToString()));
break;
default:
this.userDefinedMetadata.Add(node.Value, entry.Value);
userMetadataRootNode.Add(node.Value, entry.Value);
break;
}

}
}
}

public string UserDefinedMetadata
{
get
{
var sb = new StringBuilder();
userDefinedMetadata.Save(new StringWriter(sb));
var result = sb.ToString().TrimEnd(new[] { '\n', '\r' });

if (result.Length < 3)
{
return result;
}
else
{
// TODO: why YamlDotNet append ellipsis to the end of the output?
return result.Substring(0, result.Length - 3);
}
}
}
}
public static class StringExtension
{
Expand Down
21 changes: 14 additions & 7 deletions src/managed/OpenLiveWriter.BlogClient/Clients/GitHubPageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class GitHubPageClient : BlogClientBase, IBlogClient
private const string BasicAuthFormat = "{0}:{1}";
private const string BlogIdFormat = "{0}/{1}";
private const string BlogManifestFormat = "{0}/olwmanifest.xml";
private const string GitHubPageUserOrOrgSiteFormat = "{0}.github.io";
private const string GitHubPageProjectSiteFormat = "{0}.github.io/{1}";

private readonly Uri _postApiUrl;
private string _githubRepoOwner;
Expand Down Expand Up @@ -86,7 +88,10 @@ public GitHubPageClient(Uri postApiUrl, IBlogCredentialsAccessor credentials)
SupportsPostAsDraft = true,
SupportsPages = true,
KeywordsAsTags = true,
SupportsGetKeywords = true
SupportsGetKeywords = true,
SupportsSlug = true,
SupportFrontMatter = true,
SupportLayout = true
};

_clientOptions = clientOptions;
Expand Down Expand Up @@ -137,10 +142,10 @@ private BlogInfo[] GetUsersBlogsInternal()

if (String.IsNullOrEmpty(blog.cname))
{
homepageUrl = String.Equals(_githubRepoName, String.Format("{0}.github.io", _githubRepoOwner),
homepageUrl = String.Equals(_githubRepoName, String.Format(GitHubPageUserOrOrgSiteFormat, _githubRepoOwner),
StringComparison.InvariantCultureIgnoreCase)
? _githubRepoName
: String.Format("{0}.github.io/{1}", _githubRepoOwner, _githubRepoName);
: String.Format(GitHubPageProjectSiteFormat, _githubRepoOwner, _githubRepoName);

}
else
Expand Down Expand Up @@ -257,7 +262,9 @@ public BlogPost GetPost(string blogId, string postId)
Categories = postMetadata.categories != null ? postMetadata.categories.Select( o => new BlogPostCategory(o) ).ToArray()
: new BlogPostCategory[] { new BlogPostCategory(postMetadata.category) },
Keywords = String.Join(",", postMetadata.tags),
Excerpt = postMetadata.excerpt
Excerpt = postMetadata.excerpt,
Layout = postMetadata.layout,
FrontMatter = postMetadata.UserDefinedMetadata
};

return blogPost;
Expand Down Expand Up @@ -377,7 +384,7 @@ private XmlDocument GetManifest(string blogId)

private string GetHomePageUrl(string repoOwner, string repoName)
{
if (String.Format("{0}.github.io", repoOwner).Equals(repoName, StringComparison.InvariantCultureIgnoreCase))
if (String.Format(GitHubPageUserOrOrgSiteFormat, repoOwner).Equals(repoName, StringComparison.InvariantCultureIgnoreCase))
{
return repoName;
}
Expand All @@ -393,12 +400,12 @@ private string GetHomePageUrl(string repoOwner, string repoName)
return blog.cname;
}

return String.Format("{0}.github.io/{1}", repoOwner, repoName);
return String.Format(GitHubPageProjectSiteFormat, repoOwner, repoName);
}

private string GetBranch(string repoOwner, string repoName)
{
if (String.Equals(repoName, String.Format("{0}.github.io", repoOwner),
if (String.Equals(repoName, String.Format(GitHubPageUserOrOrgSiteFormat, repoOwner),
StringComparison.InvariantCultureIgnoreCase))
{
return "master";
Expand Down
17 changes: 17 additions & 0 deletions src/managed/OpenLiveWriter.BlogClient/Providers/BlogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,18 @@ public int MaxPostTitleLength
set { _maxPostTitleLength = value; }
}

public bool SupportFrontMatter
{
get { return _supportFrontMatter; }
set { _supportFrontMatter = value; }
}

public bool SupportLayout
{
get { return _supportlayout; }
set { _supportlayout = value; }
}

public const string CHARACTER_SET = "characterSet";
public const string SUPPORTS_EMBEDS = "supportsEmbeds";
public const string SUPPORTS_SCRIPTS = "supportsScripts";
Expand Down Expand Up @@ -495,6 +507,8 @@ public static IBlogClientOptions ApplyOptionOverrides(OptionReader optionReader,
clientOptions.SupportsPageParent = ReadBool(optionReader("supportsPageParent"), existingOptions.SupportsPageParent);
clientOptions.SupportsPageOrder = ReadBool(optionReader("supportsPageOrder"), existingOptions.SupportsPageOrder);
clientOptions.SupportsPageTrackbacks = ReadBool(optionReader("supportsPageTrackbacks"), existingOptions.SupportsPageTrackbacks);
clientOptions.SupportFrontMatter = ReadBool(optionReader("supportFrontMatter"), existingOptions.SupportFrontMatter);
clientOptions.SupportLayout = ReadBool(optionReader("supportLayout"), existingOptions.SupportLayout);

// Writer capabilities
clientOptions.LinkToSkyDriveSelfPage = ReadBool(optionReader("linkToSkyDriveSelfPage"), existingOptions.LinkToSkyDriveSelfPage);
Expand Down Expand Up @@ -692,6 +706,9 @@ private static SupportsFeature ReadSupportsFeature(string supportsFeatureValue,
private bool _supportsCustomDateUpdate = true;
private bool _supportsPostAsDraft = true;

private bool _supportFrontMatter = false;
private bool _supportlayout = false;

private SupportsFeature _supportsScripts = SupportsFeature.Unknown;
private SupportsFeature _supportsEmbeds = SupportsFeature.Unknown;
private SupportsFeature _supportsImageUpload = SupportsFeature.Yes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@
<supportsPostAsDraft>Yes</supportsPostAsDraft>
<futurePublishDateWarning>No</futurePublishDateWarning>
<keywordsAsTags>Yes</keywordsAsTags>
<supportFrontMatter>Yes</supportFrontMatter>
<supportLayout>Yes</supportLayout>
</options>
</provider>
</providers>
14 changes: 14 additions & 0 deletions src/managed/OpenLiveWriter.Extensibility/BlogClient/BlogPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ public string Excerpt
}
private string _excerpt = String.Empty;

public string FrontMatter
{
get { return _frontmatter; }
set { _frontmatter = value; }
}
private string _frontmatter = String.Empty;

public string Layout
{
get { return _layout; }
set { _layout = value; }
}
private string _layout = String.Empty;

public string[] PingUrlsPending
{
get { return _pingUrlsPending.Clone() as string[]; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ public interface IBlogClientOptions : IEditorOptions
bool SupportsPostSynchronization { get; }
bool SupportsAutoUpdate { get; }

/// <summary>
/// Below concepts are from static stie generators. As lack of
/// external storage, most of popular static sites are using
/// front matter to hold all metadata. Meanwhile users are allowed
/// to customize layouts directly as the themes/layouts are treated
/// as content as well.
/// </summary>
bool SupportFrontMatter { get; }
bool SupportLayout { get; }

/// Fault-matching
string InvalidPostIdFaultCodePattern { get; }
string InvalidPostIdFaultStringPattern { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ private void ManageLayout()
|| _clientOptions.SupportsPingPolicy
|| _clientOptions.SupportsAuthor
|| _clientOptions.SupportsSlug
|| _clientOptions.SupportsPassword;
|| _clientOptions.SupportsPassword
|| _clientOptions.SupportFrontMatter
|| _clientOptions.SupportLayout;
linkViewAll.Visible = showViewAll;
CategoryVisible = false;
TagsVisible = false;
Expand All @@ -267,7 +269,9 @@ private void ManageLayout()
|| _clientOptions.SupportsSlug
|| _clientOptions.SupportsPassword
|| _clientOptions.SupportsExcerpt
|| _clientOptions.SupportsTrackbacks;
|| _clientOptions.SupportsTrackbacks
|| _clientOptions.SupportFrontMatter
|| _clientOptions.SupportLayout;

bool showTags = (_clientOptions.SupportsKeywords && (_clientOptions.KeywordsAsTags || _clientOptions.SupportsGetKeywords));
Visible = showViewAll
Expand Down

0 comments on commit 81fa7b7

Please sign in to comment.