public static Post GetFileData(FileInfo file, SnowSettings snowSettings) { var rawPost = File.ReadAllText(file.FullName); var fileNameMatches = FileNameRegex.Match(file.Name); var rawSettings = string.Empty; if (!fileNameMatches.Success) { file.Name.OutputIfDebug(" - Skipping file: "); " - File does not match the format {year}-{month}-{day}-{slug}.(md|markdown)".OutputIfDebug(); return new Post.MissingPost(); } var result = MarkdownFileParser.ParseDataFromFile(rawPost); var settings = ParseSettings(result.Header); var year = fileNameMatches.Groups["year"].Value; var month = fileNameMatches.Groups["month"].Value; var day = fileNameMatches.Groups["day"].Value; var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug(); var date = DateTime.ParseExact(year + month + day, "yyyyMMdd", CultureInfo.InvariantCulture); /// if a 'date' property is found in markdown file header, that date will be used instead of the date in the file name if (settings.ContainsKey("date")) { try { date = DateTime.Parse((string)settings["date"]); } finally { /// do nothing, let the current 'date' be as is } } var bodySerialized = Markdown.Transform(result.Body); var excerptSerialized = Markdown.Transform(result.Excerpt ?? string.Empty); var postHeader = new Post { FileName = file.Name, MarkdownHeader = rawSettings, Content = bodySerialized, ContentExcerpt = excerptSerialized, Settings = settings, Year = date.Year, Month = date.Month, Day = date.Day, Date = date, Url = slug }; postHeader.SetSnowSettings(snowSettings); postHeader.SetHeaderSettings(settings); return postHeader; }
public static Post GetFileData(FileInfo file, Browser browser, SnowSettings snowSettings) { var response = browser.Get("/post/" + HttpUtility.UrlEncodeUnicode(file.Name)); var rawPost = File.ReadAllText(file.FullName); var fileNameMatches = FileNameRegex.Match(file.Name); var settings = new Dictionary<string, dynamic>(); var rawSettings = string.Empty; if (!fileNameMatches.Success) { throw new ApplicationException("File " + file.Name + " does not match the format {year}-{month}-{day}-{slug}.(md|markdown)"); } //Get the Header info from a Post Markdown File //Find the first index of --- var startOfSettingsIndex = rawPost.IndexOf("---", StringComparison.InvariantCultureIgnoreCase); if (startOfSettingsIndex >= 0) { //Find the second index of --- after the first var endOfSettingsIndex = rawPost.IndexOf("---", startOfSettingsIndex + 3, StringComparison.InvariantCultureIgnoreCase); //If we find the 2nd index, parse the settings //Otherwise we assume there's no header or settings... if (endOfSettingsIndex >= 0) { rawSettings = rawPost.Substring(startOfSettingsIndex + 3, endOfSettingsIndex - 3); settings = ParseSettings(rawSettings); } } var year = fileNameMatches.Groups["year"].Value; var month = fileNameMatches.Groups["month"].Value; var day = fileNameMatches.Groups["day"].Value; var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug(); var date = DateTime.ParseExact(year + month + day, "yyyyMMdd", CultureInfo.InvariantCulture); var result = new Post { FileName = file.Name, MarkdownHeader = rawSettings, Content = response.Body.AsString(), Settings = settings, Year = date.Year, Month = date.Month, Day = date.Day, Date = date, Url = slug }; result.SetSnowSettings(snowSettings); result.SetHeaderSettings(settings); return result; }
public void Should_parse_layout() { var settings = new Dictionary<string, object>() { {"layout", "layout"} }; var post = new Post(); post.SetHeaderSettings(settings); Assert.Equal("layout", post.Layout); }
public void Should_parse_email() { var settings = new Dictionary<string, object>() { {"email", "*****@*****.**"} }; var post = new Post(); post.SetHeaderSettings(settings); Assert.Equal("*****@*****.**", post.Email); }
public void Should_parse_parse_author() { var settings = new Dictionary<string, object>() { {"author", "author name"} }; var post = new Post(); post.SetHeaderSettings(settings); Assert.Equal("author name", post.Author); }
public void Should_parse_category() { var settings = new Dictionary<string, object>() { {"category", "1, 2"} }; var post = new Post(); post.SetHeaderSettings(settings); Assert.Equal("1", post.Categories.First()); Assert.Equal("2", post.Categories.Last()); }
public static Post GetFileData(FileInfo file, SnowSettings snowSettings) { var rawPost = File.ReadAllText(file.FullName); var fileNameMatches = FileNameRegex.Match(file.Name); var rawSettings = string.Empty; if (!fileNameMatches.Success) { file.Name.OutputIfDebug(" - Skipping file: "); " - File does not match the format {year}-{month}-{day}-{slug}.(md|markdown)".OutputIfDebug(); return new Post.MissingPost(); } var result = MarkdownFileParser.ParseDataFromFile(rawPost); var settings = ParseSettings(result.Header); var year = fileNameMatches.Groups["year"].Value; var month = fileNameMatches.Groups["month"].Value; var day = fileNameMatches.Groups["day"].Value; var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug(); var date = DateTime.ParseExact(year + month + day, "yyyyMMdd", CultureInfo.InvariantCulture); var bodySerialized = Markdown.Transform(result.Body); var excerptSerialized = Markdown.Transform(result.Excerpt ?? string.Empty); var postHeader = new Post { FileName = file.Name, MarkdownHeader = rawSettings, Content = bodySerialized, ContentExcerpt = excerptSerialized, Settings = settings, Year = date.Year, Month = date.Month, Day = date.Day, Date = date, Url = slug }; postHeader.SetSnowSettings(snowSettings); postHeader.SetHeaderSettings(settings); return postHeader; }
public void Should_sort_categories_ascending() { // Arrange var post = new Post(); var settings = new Dictionary<string, object> { {"categories", "b, A, C, .Net"} }; // Act post.SetHeaderSettings(settings); // Assert var categories = post.Categories.ToArray(); Assert.Equal(".Net", categories[0]); Assert.Equal("A", categories[1]); Assert.Equal("b", categories[2]); Assert.Equal("C", categories[3]); }
public static Post GetFileData(FileInfo file, Browser browser, SnowSettings snowSettings) { var response = browser.Get("/post/" + HttpUtility.UrlEncodeUnicode(file.Name)); var rawPost = File.ReadAllText(file.FullName); var fileNameMatches = FileNameRegex.Match(file.Name); var rawSettings = string.Empty; if (!fileNameMatches.Success) { file.Name.OutputIfDebug(" - Skipping file: "); " - File does not match the format {year}-{month}-{day}-{slug}.(md|markdown)".OutputIfDebug(); return new Post.MissingPost(); } var result = ParseDataFromFile(rawPost); var settings = ParseSettings(result.Item1); var year = fileNameMatches.Groups["year"].Value; var month = fileNameMatches.Groups["month"].Value; var day = fileNameMatches.Groups["day"].Value; var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug(); var date = DateTime.ParseExact(year + month + day, "yyyyMMdd", CultureInfo.InvariantCulture); var postHeader = new Post { FileName = file.Name, MarkdownHeader = rawSettings, Content = response.Body.AsString(), Settings = settings, Year = date.Year, Month = date.Month, Day = date.Day, Date = date, Url = slug }; postHeader.SetSnowSettings(snowSettings); postHeader.SetHeaderSettings(settings); return postHeader; }
public void Should_parse_series() { var settings = new Dictionary<string, object>() { { "series", new Series { Current = 2, Name = "Series name", Parts = new SortedList<int, Series.Part> { { 1, new Series.Part { Name = "Part1", Url = "/url/to/part1" } }, { 2, new Series.Part { Name = "Part2", Url = "/url/to/part2" } }, } } } }; var post = new Post(); post.SetHeaderSettings(settings); var series = post.Series; Assert.Equal(2, series.Parts.Count); }
public void Should_parse_meta_description() { var settings = new Dictionary<string, object>() { {"metadescription", "meta description"} }; var post = new Post(); post.SetHeaderSettings(settings); Assert.Equal("meta description", post.MetaDescription); }
public void Published_should_support_private() { var settings = new Dictionary<string, object>() { {"published", "private"} }; var post = new Post(); post.SetHeaderSettings(settings); Assert.Equal(Published.Private, post.Published); }
public void Published_should_default_to_true_when_unknown_value() { var settings = new Dictionary<string, object>() { {"published", ""} }; var post = new Post(); post.SetHeaderSettings(settings); Assert.Equal(Published.True, post.Published); }
public void Should_parse_title() { var settings = new Dictionary<string, object>() { {"title", "Post title"} }; var post = new Post(); post.SetHeaderSettings(settings); Assert.Equal("Post title", post.Title); }