public static async Task BuildAllISPages(Dictionary <string, ISPage> pageMap, string roomId, string dirPath) { String baseHtml = null; ISPage page = null; try { baseHtml = await ReadHtml(roomId); } catch (Exception e) { throw new ISParseException("Could not read Infinite Story webpage", e); } try { page = ISPageHtmlParser.ParseRawISHtml(baseHtml, roomId, dirPath); } catch (Exception e) { throw new ISParseException("Could not parse Infinite Story webpage", e); } pageMap.Add(roomId, page); List <Task> waitTasks = new List <Task>(); foreach (String childRoomId in page.Choices.Select(x => x.RoomId)) { if (!pageMap.ContainsKey(childRoomId)) { waitTasks.Add(BuildAllISPages(pageMap, childRoomId, dirPath)); } } Task.WaitAll(waitTasks.ToArray()); }
private static Dictionary <string, ISPage> BuildPageMap(string baseRoom, string dirPath) { var pageMap = new Dictionary <string, ISPage>(); ISPageHtmlParser.BuildAllISPages(pageMap, baseRoom, dirPath).Wait(); return(pageMap); }
private void BuildHtml(Dictionary <string, ISPage> pageMap, string roomId, string dirPath) { String baseHtml = ISPageHtmlParser.ReadHtml(roomId).Result; ISPage page = ISPageHtmlParser.ParseRawISHtml(baseHtml, roomId, dirPath); pageMap.Add(roomId, page); SavePage(roomId, page, dirPath); foreach (String childRoomId in page.Choices.Select(x => x.RoomId)) { if (!pageMap.ContainsKey(childRoomId)) { BuildHtml(pageMap, childRoomId, dirPath); } } }
private void BuildPDF(Dictionary <string, ISPage> pageMap, string baseRoom, Dictionary <string, int> chapterPageMap, bool fakeRun) { if (fakeRun) { ISPageHtmlParser.BuildAllISPages(pageMap, baseRoom, filePath).Wait(); } //now that we have all the pages, we'll have to clean them up and decide on pages Dictionary <string, int> ISPageToPhysicalPageMap = new Dictionary <string, int>(); int currentPage = 1; int currentChapter = 1; Random r = new Random(123456); List <string> pagesLeft = new List <string>(pageMap.Count); foreach (string x in pageMap.Keys) { pagesLeft.Add(x); } using (MemoryStream memStream = new MemoryStream()) { Document pdfDoc = new Document(); iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, memStream); HeaderFooter evnt = new HeaderFooter(); if (fakeRun) { evnt.pageByTitle = chapterPageMap; } else { evnt.pageByTitle = new Dictionary <string, int>(); } if (!fakeRun) { writer.PageEvent = evnt; } pdfDoc.Open(); pdfDoc.AddAuthor("test"); pdfDoc.AddTitle("testTitle"); while (pagesLeft.Any()) { string pageToAdd = pagesLeft.First(); if (currentPage > 1) { pagesLeft.Skip(r.Next(pagesLeft.Count)).First(); } pagesLeft.Remove(pageToAdd); if (fakeRun) { chapterPageMap.Add(pageToAdd, writer.PageNumber + 1); } ISPageToPhysicalPageMap.Add(pageToAdd, currentPage); var chapter = GetPDFPage(pageMap[pageToAdd], int.Parse(pageToAdd), chapterPageMap); pdfDoc.Add(chapter); int actualPageLength = fakeRun ? 1 : chapterPageMap[pageToAdd]; currentPage += actualPageLength; currentChapter++; } pdfDoc.Close(); writer.Close(); if (!fakeRun) { File.WriteAllBytes(Path.Combine(filePath, fileName), memStream.GetBuffer()); } } }