private Page CreatePage(SiteContext context, IConfiguration config, string file, bool isPost)
{
try
{
if (pageCache.ContainsKey(file))
return pageCache[file];
var content = SafeReadContents(file);
var relativePath = MapToOutputPath(context, file);
var scopedDefaults = context.Config.Defaults.ForScope(relativePath);
var header = scopedDefaults.Merge(content.YamlHeader());
if (header.ContainsKey("published") && header["published"].ToString().ToLower() == "false")
{
return null;
}
var page = new Page
{
Title = header.ContainsKey("title") ? header["title"].ToString() : "this is a post",
Date = header.ContainsKey("date") ? DateTime.Parse(header["date"].ToString()) : file.Datestamp(fileSystem),
Content = content,
Filepath = isPost ? GetPathWithTimestamp(context.OutputFolder, file) : GetFilePathForPage(context, file),
File = file,
Bag = header,
};
// resolve categories and tags
if (isPost)
{
page.Categories = ResolveCategories(context, header, page);
if (header.ContainsKey("tags"))
page.Tags = header["tags"] as IEnumerable<string>;
}
// resolve permalink
if (header.ContainsKey("permalink"))
{
page.Url = linkHelper.EvaluatePermalink(header["permalink"].ToString(), page);
}
else if (isPost && config.ContainsKey("permalink"))
{
page.Url = linkHelper.EvaluatePermalink(config["permalink"].ToString(), page);
}
else
{
page.Url = linkHelper.EvaluateLink(context, page);
}
// resolve id
page.Id = page.Url.Replace(".html", string.Empty).Replace("index", string.Empty);
// always write date back to Bag as DateTime
page.Bag["date"] = page.Date;
// The GetDirectoryPage method is reentrant, we need a cache to stop a stack overflow :)
pageCache.Add(file, page);
page.DirectoryPages = GetDirectoryPages(context, config, Path.GetDirectoryName(file), isPost).ToList();
return page;
}
catch (Exception e)
{
Tracing.Info(String.Format("Failed to build post from File: {0}", file));
Tracing.Info(e.Message);
Tracing.Debug(e.ToString());
}
return null;
}