private static async void BuildSite() { var model = GetContent(); string masterTemplate = await GetTemplate("master"); Razor.Compile(masterTemplate, typeof(Master), "master"); // start with the index page string templateName = "index"; Console.WriteLine("Generating {0}", templateName); string indexTemplate = await GetTemplate(templateName); var groupedModel = model .GroupBy(p => p.PublishedOn.Year) .Select(p => new Year { Name = p.Key.ToString(), Months = p .GroupBy(m => m.PublishedOn.ToString("MMMM")) .Select(m => new Month { Name = m.Key, Posts = m.ToArray() }) .ToArray() }) .ToArray(); string result = Razor.Parse(indexTemplate, new { Years = groupedModel }); await SaveFile("Joel Martinez", result, string.Format("{0}.html", templateName)); // the 'about' page string abouttemplate = await GetTemplate("about"); result = Razor.Parse(abouttemplate); SaveFile("About Joel Martinez", result, "/about/"); // now generate each individual content page string postTemplate = await GetTemplate("post"); Razor.Compile(postTemplate, typeof(Post), "post"); Parallel.ForEach(model, post => { string postResult = Razor.Run("post", post); Master master = new Master() { Title = post.Title, Content = postResult }; // set up meta tags master.Meta["twitter:card"] = "summary"; master.Meta["twitter:creator"] = "@joelmartinez"; master.Meta["og:title"] = post.Title; master.Meta["og:description"] = post.BodySummary; if (post.HasImage) { master.Meta["og:image"] = post.ImageUrl; } SaveFile(master, post.UrlPath); }); }
private static async Task SaveFile(Master master, string path) { string content = Razor.Run("master", master); if (path.First() == '/') path = path.Substring(1, path.Length - 1); if (path.Last() == '/') path += "index.html"; path = Path.Combine("out", path); Directory.CreateDirectory(Path.GetDirectoryName(path)); Console.WriteLine("Persisting to {0}", path); using (StreamWriter writer = new StreamWriter(path)) { await writer.WriteAsync(content); } }
private static async Task SaveFile(Master master, string path) { string content = Razor.Run("master", master); path = await SaveFile(content, path); }