private static async Task HandlePostAsync(Post post, Settings settings, TemplatingOptions options, IServiceProvider services, IRazorViewEngine engine, string type) { var httpContext = new DefaultHttpContext { RequestServices = services }; var routeData = new RouteData(); var actionDescriptor = new ActionDescriptor(); var modelStateDictionary = new ModelStateDictionary(); var modelMetadataProvider = new EmptyModelMetadataProvider(); var tempDataProvider = new VirtualTempDataProvider(); var htmlHelperOptions = new HtmlHelperOptions(); var actionContext = new ActionContext(httpContext, routeData, actionDescriptor, modelStateDictionary); var viewDataDictionary = new ViewDataDictionary(modelMetadataProvider, modelStateDictionary); var tempDataDictionary = new TempDataDictionary(httpContext, tempDataProvider); viewDataDictionary.Model = post; using (var stringWriter = new StringWriter()) { var view = engine.GetView(".", $"/Templates/{type}.cshtml", true); var viewContext = new ViewContext(actionContext, view.View, viewDataDictionary, tempDataDictionary, stringWriter, htmlHelperOptions); await view.View.RenderAsync(viewContext); var result = stringWriter.ToString(); var directory = Path.Combine(options.OutputDirectory, post.Slug); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } File.WriteAllText(Path.Combine(directory, "index.html"), result); await DownloadImagesFromPostAsync(post.HTML, settings, options); if (type == "post") { //amp it view = engine.GetView(".", $"/Templates/post-amp.cshtml", true); viewContext = new ViewContext(actionContext, view.View, viewDataDictionary, tempDataDictionary, stringWriter, htmlHelperOptions); await view.View.RenderAsync(viewContext); result = stringWriter.ToString(); directory = Path.Combine(directory, "amp"); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } File.WriteAllText(Path.Combine(directory, "index.html"), result); } } }
private static async Task HandleIndexPageAsync(Post[] posts, Settings settings, TemplatingOptions options, IServiceProvider services, IRazorViewEngine engine) { var postsToDisplay = 27; var httpContext = new DefaultHttpContext { RequestServices = services }; var routeData = new RouteData(); var actionDescriptor = new ActionDescriptor(); var modelStateDictionary = new ModelStateDictionary(); var modelMetadataProvider = new EmptyModelMetadataProvider(); var tempDataProvider = new VirtualTempDataProvider(); var htmlHelperOptions = new HtmlHelperOptions(); var actionContext = new ActionContext(httpContext, routeData, actionDescriptor, modelStateDictionary); var viewDataDictionary = new ViewDataDictionary(modelMetadataProvider, modelStateDictionary); var tempDataDictionary = new TempDataDictionary(httpContext, tempDataProvider); var imageDirectory = Path.Combine(options.OutputDirectory, "content", "images", "index"); var index = new IndexPage { Options = options, Settings = settings }; posts = posts.OrderByDescending(x => x.PublishedAt).ToArray(); if (!Directory.Exists(imageDirectory)) { Directory.CreateDirectory(imageDirectory); } if (!string.IsNullOrWhiteSpace(settings.CoverImage)) { using var client = new HttpClient(); var imageResult = await client.GetAsync(new Uri(options.GhostUrl, settings.CoverImage)); if (imageResult.IsSuccessStatusCode) { using var imageStream = await imageResult.Content.ReadAsStreamAsync(); using var memoryStream = new MemoryStream(); await imageStream.CopyToAsync(memoryStream); var widths = new[] { 600, 1000, 2000 }; foreach (var width in widths) { memoryStream.Position = 0; using var image = await Image.LoadAsync(memoryStream); index.ImageHeight = image.Height; index.ImageWidth = image.Width; image.Mutate(context => context.Resize(width, 0)); await image.SaveAsPngAsync(Path.Combine(imageDirectory, width + ".png")); } } } viewDataDictionary.Model = index; var batch = 1; while (posts.Length > 0) { var directory = options.OutputDirectory; if (batch > 1) { directory = Path.Combine(directory, "index", batch.ToString()); } if (batch == 1) { index.Previous = null; } else if (batch == 2) { index.Previous = "/"; } else { index.Previous = "/index/" + (batch - 1) + "/"; } if (posts.Length > postsToDisplay) { index.Next = "/index/" + (batch + 1) + "/"; } else { index.Next = null; } using (var stringWriter = new StringWriter()) { var view = engine.GetView(".", $"/Templates/index.cshtml", true); var viewContext = new ViewContext(actionContext, view.View, viewDataDictionary, tempDataDictionary, stringWriter, htmlHelperOptions); index.PostsToDisplay = posts.Take(postsToDisplay).ToArray(); await view.View.RenderAsync(viewContext); var result = stringWriter.ToString(); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } File.WriteAllText(Path.Combine(directory, "index.html"), result); } posts = posts.Skip(postsToDisplay).ToArray(); batch++; } }
private static async Task HandleTagAsync(Tag tag, Post[] posts, Settings settings, TemplatingOptions options, IServiceProvider services, IRazorViewEngine engine) { var postsToDisplay = 27; var httpContext = new DefaultHttpContext { RequestServices = services }; var routeData = new RouteData(); var actionDescriptor = new ActionDescriptor(); var modelStateDictionary = new ModelStateDictionary(); var modelMetadataProvider = new EmptyModelMetadataProvider(); var tempDataProvider = new VirtualTempDataProvider(); var htmlHelperOptions = new HtmlHelperOptions(); posts = posts.Where(x => x.Tags.Any(x => x.Id == tag.Id)).OrderByDescending(x => x.PublishedAt).ToArray(); tag.PostCount = posts.Length; var actionContext = new ActionContext(httpContext, routeData, actionDescriptor, modelStateDictionary); var viewDataDictionary = new ViewDataDictionary(modelMetadataProvider, modelStateDictionary); var tempDataDictionary = new TempDataDictionary(httpContext, tempDataProvider); viewDataDictionary.Model = tag; var batch = 1; while (posts.Length > 0) { var directory = Path.Combine(options.OutputDirectory, "tag", tag.Slug); if (batch > 1) { directory = Path.Combine(directory, batch.ToString()); } if (batch == 1) { tag.Previous = null; } else if (batch == 2) { tag.Previous = tag.Url; } else { tag.Previous = tag.Url + (batch - 1) + "/"; } if (posts.Length > postsToDisplay) { tag.Next = tag.Url + (batch + 1) + "/"; } else { tag.Next = null; } using (var stringWriter = new StringWriter()) { var view = engine.GetView(".", $"/Templates/tag.cshtml", true); var viewContext = new ViewContext(actionContext, view.View, viewDataDictionary, tempDataDictionary, stringWriter, htmlHelperOptions); tag.PostsToDisplay = posts.Take(postsToDisplay).ToArray(); await view.View.RenderAsync(viewContext); var result = stringWriter.ToString(); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } File.WriteAllText(Path.Combine(directory, "index.html"), result); } posts = posts.Skip(postsToDisplay).ToArray(); batch++; } }