// http://jekyllrb.com/docs/permalinks/ public string EvaluatePermalink(string permalink, Page page) { if (BuiltInPermalinks.ContainsKey(permalink)) { permalink = BuiltInPermalinks[permalink]; } if (!permalink.EndsWith("/") && string.IsNullOrEmpty(Path.GetExtension(permalink))) { permalink += "/"; } permalink = permalink.Replace(":categories", string.Join("/", page.Categories.ToArray())); permalink = permalink.Replace(":dashcategories", string.Join("-", page.Categories.ToArray())); permalink = permalink.Replace(":year", page.Date.Year.ToString(CultureInfo.InvariantCulture)); permalink = permalink.Replace(":month", page.Date.ToString("MM")); permalink = permalink.Replace(":day", page.Date.ToString("dd")); permalink = permalink.Replace(":title", GetTitle(page.File)); permalink = permalink.Replace(":y_day", page.Date.DayOfYear.ToString("000")); permalink = permalink.Replace(":short_year", page.Date.ToString("yy")); permalink = permalink.Replace(":i_month", page.Date.Month.ToString()); permalink = permalink.Replace(":i_day", page.Date.Day.ToString()); permalink = permalink.Replace(":slug", SlugifyFilter.Slugify(GetSlugFromFrontMatter(page) ?? GetTitle(page.File))); if (permalink.Contains(":category")) { var matches = CategoryRegex.Matches(permalink); if (matches != null && matches.Count > 0) { foreach (Match match in matches) { var replacementValue = string.Empty; int categoryIndex; if (match.Success) { if (int.TryParse(match.Groups[1].Value, out categoryIndex) && categoryIndex > 0) { replacementValue = page.Categories.Skip(categoryIndex - 1).FirstOrDefault(); } else if (page.Categories.Any()) { replacementValue = page.Categories.First(); } } permalink = permalink.Replace(match.Value, replacementValue); } } } permalink = SlashesRegex.Replace(permalink, "/"); return(permalink); }
public void Post_Already_Exists() { fileSystem.Directory.CreateDirectory(BaseSite + PostsFolder); var postTitle = "Post title"; var postName = string.Format("{0}-{1}.md", DateTime.Today.ToString("yyyy-MM-dd"), SlugifyFilter.Slugify(postTitle)); var ingredient = new Logic.Recipe.Ingredient(fileSystem, postTitle, BaseSite, false); ingredient.Create(); ingredient.Create(); Assert.Contains(string.Format("The \"{0}\" file already exists", postName), trace.ToString()); }
public void Post_Has_Correct_Content() { fileSystem.Directory.CreateDirectory(BaseSite + PostsFolder); var postTitle = "Post title"; var expectedContent = string.Format("---\r\n layout: post \r\n title: {0}\r\n comments: true\r\n---\r\n", postTitle); var postName = string.Format("{0}-{1}.md", DateTime.Today.ToString("yyyy-MM-dd"), SlugifyFilter.Slugify(postTitle)); var ingredient = new Logic.Recipe.Ingredient(fileSystem, "Post title", BaseSite, false); ingredient.Create(); Assert.Equal(expectedContent, fileSystem.File.ReadAllText(fileSystem.Path.Combine(BaseSite + PostsFolder, postName))); }
public void Post_Is_Created() { fileSystem.Directory.CreateDirectory(BaseSite + PostsFolder); var postTitle = "Post title"; var postName = string.Format("{0}-{1}.md", DateTime.Today.ToString("yyyy-MM-dd"), SlugifyFilter.Slugify(postTitle)); var ingredient = new Logic.Recipe.Ingredient(fileSystem, postTitle, BaseSite, false); ingredient.Create(); Assert.True(fileSystem.File.Exists(fileSystem.Path.Combine(BaseSite + PostsFolder, postName))); }
public void Create() { var postPath = fileSystem.Path.Combine(directory, !this.withDrafts ? @"_posts" : @"_drafts"); var postName = string.Format("{0}-{1}.md", DateTime.Today.ToString("yyyy-MM-dd"), SlugifyFilter.Slugify(title)); var pageContents = string.Format("---\r\n layout: post \r\n title: {0}\r\n comments: true\r\n---\r\n", title); if (!fileSystem.Directory.Exists(postPath)) { Tracing.Info("{0} folder not found", postPath); return; } if (fileSystem.File.Exists(fileSystem.Path.Combine(postPath, postName))) { Tracing.Info("The \"{0}\" file already exists", postName); return; } fileSystem.File.WriteAllText(fileSystem.Path.Combine(postPath, postName), pageContents); Tracing.Info("Created the \"{0}\" post ({1})", title, postName); }
public void Transform(SiteContext siteContext) { var layout = "layout"; var layoutConfigKey = $"{this.folderName}_pages_layout"; if (this.stopFolderGeneration) { return; } if (siteContext.Config.ContainsKey(layoutConfigKey)) { layout = siteContext.Config[layoutConfigKey].ToString(); } foreach (var name in this.GetNames(siteContext)) { var p = new Page { Content = $"---\r\n layout: {layout} \r\n {this.folderName}: {name} \r\n---\r\n", File = Path.Combine(siteContext.SourceFolder, this.folderName, SlugifyFilter.Slugify(name), "index.html"), Filepath = Path.Combine(siteContext.OutputFolder, this.folderName, SlugifyFilter.Slugify(name), "index.html"), OutputFile = Path.Combine(siteContext.OutputFolder, this.folderName, SlugifyFilter.Slugify(name), "index.html"), Bag = $"---\r\n layout: {layout} \r\n {this.folderName}: {name} \r\n---\r\n".YamlHeader() }; p.Url = new LinkHelper().EvaluateLink(siteContext, p); siteContext.Pages.Add(p); } }
public void Transform_CategoryArePresent_OnePageForEachTagIsGenerated() { var context = CreateContext(new[] { "C#", "category" }); new CategoriesFolderGenerator().Transform(context); Assert.That(context.Pages[0].File, Is.EqualTo(Path.Combine(context.SourceFolder, "category", SlugifyFilter.Slugify("C#"), "index.html"))); Assert.That(context.Pages[1].File, Is.EqualTo(Path.Combine(context.SourceFolder, "category", SlugifyFilter.Slugify("category"), "index.html"))); }
public void Transform_CategoryIsPresent_PageHasExpectedProperties() { var context = CreateContext(new[] { "C#" }); new CategoriesFolderGenerator().Transform(context); Assert.That(context.Pages[0].File, Is.EqualTo(Path.Combine(context.SourceFolder, "category", SlugifyFilter.Slugify("C#"), "index.html"))); Assert.That(context.Pages[0].Filepath, Is.EqualTo(Path.Combine(context.OutputFolder, "category", SlugifyFilter.Slugify("C#"), "index.html"))); Assert.That(context.Pages[0].OutputFile, Is.EqualTo(Path.Combine(context.OutputFolder, "category", SlugifyFilter.Slugify("C#"), "index.html"))); Assert.That(context.Pages[0].Url, Is.EqualTo(@"/category/csharp/index.html")); }