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()); }
public static ISPage ParseRawISHtml(String html, String roomId, string dirPath) { var page = new ISPage(roomId, html); int mainStart = html.IndexOf(STORY_MAIN); int mainEnd = html.IndexOf(END_MAIN); int choiceEnd = html.IndexOf(END_CHOICES); if (choiceEnd > 0) { List <Choice> choices = ParseChoices(html.Substring(mainEnd + END_MAIN.Length, choiceEnd - mainEnd - END_MAIN.Length)); page.Choices = choices; } else { //we're at a game over screen choiceEnd = html.IndexOf(THE_END_FOOTER, mainEnd); page.EndText = html.Substring(mainEnd + END_MAIN.Length, choiceEnd - mainEnd - END_MAIN.Length); } String newHtml = html.Substring(mainStart, choiceEnd - mainStart); String mainTextHtml = html.Substring(mainStart, mainEnd - mainStart); page.Contents = ParseSaveAndFixImages(mainTextHtml, dirPath); return(page); }
private async Task WriteEPub() { try { using (var ePubStream = File.Create(filePath)) { using (var writer = await EPubWriter.CreateWriterAsync( ePubStream, Path.GetFileNameWithoutExtension(filePath), "Generator", baseRoom, leaveOpen: true)) { ISPage firstPage = pageMap.Values.Where(x => x.RoomId.Equals(baseRoom)).First(); await writer.AddChapterAsync(firstPage.RoomId + ".html", firstPage.RoomId, GetEpubPageContents(firstPage)); foreach (ISPage page in pageMap.Values.Where(x => !x.RoomId.Equals(baseRoom))) { await writer.AddChapterAsync(page.RoomId + ".html", page.RoomId, GetEpubPageContents(page)); } // Add a chapter with string content as x-html //await writer.AddChapterAsync( // "FirstChapter.xhtml", // "First Chapter Title", // File.ReadAllText("FirstChapter.xhtml")); // Streaming mode //var chapterStream = writer.GetChapterStream( // "SecondChapter.xhtml", // "Second Chapter - Streaming"); // Fill the chapter-stream // await chapterStream.WriteAsync(...) // Add an image from the disk //await writer.AddResourceAsync("MyLogo.png", "image/png", File.ReadAllBytes("MyLogo.png")); // Streaming mode for images //var imageStream = writer.GetResourceStream("MyBackground.jpg", "image/jpeg"); // Fill the image-stream // await imageStream.WriteAsync(...) // Must be called async at the end to write down the TOC of the ePub await writer.WriteEndOfPackageAsync(); //ePubStream.Flush(); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private static void SetAnchor(ISPage iSPage, Chapter chapter, Dictionary<string, int> pageMap) { Anchor anchorTarget = new Anchor(iSPage.RoomId); anchorTarget.Name = iSPage.RoomId; Paragraph targetParagraph = new Paragraph(); targetParagraph.Add(anchorTarget); chapter.Add(targetParagraph); }
private static void SetAnchor(ISPage iSPage, Chapter chapter, Dictionary <string, int> pageMap) { Anchor anchorTarget = new Anchor(iSPage.RoomId); anchorTarget.Name = iSPage.RoomId; Paragraph targetParagraph = new Paragraph(); targetParagraph.Add(anchorTarget); chapter.Add(targetParagraph); }
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); } } }
public static string GetHtmlContents(ISPage page) { StringBuilder html = new StringBuilder(page.Contents); if (page.EndText != null) { html.AppendLine(page.EndText); } else { html.AppendLine(CHOICES_START_XML.Replace("X", page.Choices.Count.ToString())); foreach (Choice choice in page.Choices) { html.AppendLine(CHOICES_LINE_XML.Replace("PAGEID", choice.RoomId).Replace("TITLEINFO", choice.ChoiceId + "," + choice.RoomId).Replace("CHOICETEXT", choice.Text)); } html.AppendLine(CHOICES_END_XML); } return(html.ToString()); }
public static string GetHtmlContents(ISPage page) { StringBuilder html = new StringBuilder(page.Contents); if (page.EndText != null) { html.AppendLine(page.EndText); } else { html.AppendLine(CHOICES_START_XML.Replace("X", page.Choices.Count.ToString())); foreach (Choice choice in page.Choices) { html.AppendLine(CHOICES_LINE_XML.Replace("PAGEID", choice.RoomId).Replace("TITLEINFO", choice.ChoiceId + "," + choice.RoomId).Replace("CHOICETEXT", choice.Text)); } html.AppendLine(CHOICES_END_XML); } return html.ToString(); }
//private static int chapterCount = 1; public static Chapter HtmlToPdfString(ISPage iSPage, int chapterNum, Dictionary<string, int> pageMap) { String htmlInput = iSPage.Contents; Chapter chapter = new Chapter(chapterNum.ToString(), 1); Font currentFont = FontFactory.GetFont(FontFactory.HELVETICA); SetAnchor(iSPage, chapter, pageMap); WriteContents(htmlInput, chapter); if (String.IsNullOrWhiteSpace(iSPage.EndText)) { WriteChoices(iSPage, pageMap, chapter); } else { WriteEnd(iSPage, chapter); } return chapter; }
//private static int chapterCount = 1; public static Chapter HtmlToPdfString(ISPage iSPage, int chapterNum, Dictionary <string, int> pageMap) { String htmlInput = iSPage.Contents; Chapter chapter = new Chapter(chapterNum.ToString(), 1); Font currentFont = FontFactory.GetFont(FontFactory.HELVETICA); SetAnchor(iSPage, chapter, pageMap); WriteContents(htmlInput, chapter); if (String.IsNullOrWhiteSpace(iSPage.EndText)) { WriteChoices(iSPage, pageMap, chapter); } else { WriteEnd(iSPage, chapter); } return(chapter); }
private static void WriteChoices(ISPage iSPage, Dictionary <string, int> pageMap, Chapter chapter) { chapter.Add(new iTextSharp.text.Phrase(String.Format("\nYou have {0} choices:\n", iSPage.Choices.Count))); foreach (Choice choice in iSPage.Choices) { if (pageMap.ContainsKey(choice.RoomId)) { Anchor anchor = new Anchor(String.Format("{0} - Page {1}\n", choice.Text, pageMap[choice.RoomId])); anchor.Reference = "#" + choice.RoomId; Paragraph paragraph = new Paragraph(); paragraph.Add(anchor); chapter.Add(paragraph); //Chunk chunk = new Chunk(String.Format("{0} - Page {1}\n", choice.Text, pageMap[choice.RoomId])); //PdfAction action = PdfAction.GotoLocalPage(pageMap[choice.RoomId], new PdfDestination(0), ); //chunk.SetAction(action); } else { chapter.Add(new iTextSharp.text.Phrase(String.Format("{0} - Page {1}\n", choice.Text, "XXX"))); } } }
private static void SavePage(string roomId, ISPage page, string dirPath) { File.AppendAllText(Path.Combine(dirPath, roomId + ".html"), GetHtmlContents(page)); }
private static void WriteEnd(ISPage iSPage, Chapter chapter) { chapter.Add(new iTextSharp.text.Phrase(iSPage.EndText, FontFactory.GetFont(FontFactory.HELVETICA_BOLD))); }
private static void WriteChoices(ISPage iSPage, Dictionary<string, int> pageMap, Chapter chapter) { chapter.Add(new iTextSharp.text.Phrase(String.Format("\nYou have {0} choices:\n", iSPage.Choices.Count))); foreach (Choice choice in iSPage.Choices) { if(pageMap.ContainsKey(choice.RoomId)) { Anchor anchor = new Anchor(String.Format("{0} - Page {1}\n", choice.Text, pageMap[choice.RoomId])); anchor.Reference = "#" + choice.RoomId; Paragraph paragraph = new Paragraph(); paragraph.Add(anchor); chapter.Add(paragraph); //Chunk chunk = new Chunk(String.Format("{0} - Page {1}\n", choice.Text, pageMap[choice.RoomId])); //PdfAction action = PdfAction.GotoLocalPage(pageMap[choice.RoomId], new PdfDestination(0), ); //chunk.SetAction(action); } else { chapter.Add(new iTextSharp.text.Phrase(String.Format("{0} - Page {1}\n", choice.Text, "XXX"))); } } }
private string GetEpubPageContents(ISPage page) { return(HtmlWriter.GetHtmlContents(page)); }
private static Chapter GetPDFPage(ISPage iSPage, int chapterNum, Dictionary <string, int> map) { Chapter chap = PdfChapterHelper.HtmlToPdfString(iSPage, chapterNum, map); return(chap); }
private static Chapter GetPDFPage(ISPage iSPage, int chapterNum, Dictionary<string, int> map) { Chapter chap = PdfChapterHelper.HtmlToPdfString(iSPage, chapterNum, map); return chap; }
private string GetEpubPageContents(ISPage page) { return HtmlWriter.GetHtmlContents(page); }
public static ISPage ParseRawISHtml(String html, String roomId, string dirPath) { var page = new ISPage(roomId, html); int mainStart = html.IndexOf(STORY_MAIN); int mainEnd = html.IndexOf(END_MAIN); int choiceEnd = html.IndexOf(END_CHOICES); if (choiceEnd > 0) { List<Choice> choices = ParseChoices(html.Substring(mainEnd + END_MAIN.Length, choiceEnd - mainEnd - END_MAIN.Length)); page.Choices = choices; } else { //we're at a game over screen choiceEnd = html.IndexOf(THE_END_FOOTER, mainEnd); page.EndText = html.Substring(mainEnd + END_MAIN.Length, choiceEnd - mainEnd - END_MAIN.Length); } String newHtml = html.Substring(mainStart, choiceEnd - mainStart); String mainTextHtml = html.Substring(mainStart, mainEnd - mainStart); page.Contents = ParseSaveAndFixImages(mainTextHtml, dirPath); return page; }