private static List <EpubChapter> LoadChapters(EpubBook book, List <EpubNavigationPoint> navigationPoints, ZipArchive epubArchive) { List <EpubChapter> result = new List <EpubChapter>(); foreach (EpubNavigationPoint navigationPoint in navigationPoints) { EpubChapter chapter = new EpubChapter(); chapter.Title = navigationPoint.NavigationLabels.First().Text; int contentSourceAnchorCharIndex = navigationPoint.Content.Source.IndexOf('#'); if (contentSourceAnchorCharIndex == -1) { chapter.ContentFileName = navigationPoint.Content.Source; } else { chapter.ContentFileName = navigationPoint.Content.Source.Substring(0, contentSourceAnchorCharIndex); chapter.Anchor = navigationPoint.Content.Source.Substring(contentSourceAnchorCharIndex + 1); } EpubTextContentFile htmlContentFile; if (!book.Content.Html.TryGetValue(chapter.ContentFileName, out htmlContentFile)) { throw new Exception(String.Format("Incorrect EPUB manifest: item with href = \"{0}\" is missing", chapter.ContentFileName)); } chapter.HtmlContent = htmlContentFile.Content; chapter.SubChapters = LoadChapters(book, navigationPoint.ChildNavigationPoints, epubArchive); result.Add(chapter); } return(result); }
private static List <EpubChapter> LoadChapters(EpubBook book, List <EpubNavigationPoint> navigationPoints, ZipArchive epubArchive, EpubChapter parentChapter = null) { List <EpubChapter> result = new List <EpubChapter>(); foreach (EpubNavigationPoint navigationPoint in navigationPoints) { EpubChapter chapter = new EpubChapter(); chapter.Book = book; chapter.Title = navigationPoint.NavigationLabels.First().Text; int contentSourceAnchorCharIndex = navigationPoint.Content.Source.IndexOf('#'); if (contentSourceAnchorCharIndex == -1) { chapter.ContentFileName = navigationPoint.Content.Source; } else { chapter.ContentFileName = navigationPoint.Content.Source.Substring(0, contentSourceAnchorCharIndex); chapter.Anchor = navigationPoint.Content.Source.Substring(contentSourceAnchorCharIndex + 1); } EpubTextContentFile htmlContentFile; if (!book.Content.Html.TryGetValue(chapter.ContentFileName, out htmlContentFile)) { throw new Exception(String.Format("Incorrect EPUB manifest: item with href = \"{0}\" is missing", chapter.ContentFileName)); } chapter.HtmlContent = htmlContentFile.Content; chapter.SubChapters = LoadChapters(book, navigationPoint.ChildNavigationPoints, epubArchive, chapter); result.Add(chapter); } //if (result.Count == 0 && parentChapter != null) //{ // var html = parentChapter.HtmlContent; // HtmlDocument doc = new HtmlDocument(); // doc.LoadHtml(html); // int count = 0; // foreach (var node in doc.DocumentNode.QuerySelectorAll("p")) // { // EpubChapter chapter = new EpubChapter(); // chapter.Title = node.InnerText.Substring(0, Math.Min(node.InnerText.Length, 80)); // if (chapter.Title.Length < node.InnerText.Length) // chapter.Title = chapter.Title.Substring(0, chapter.Title.LastIndexOf(" ")); // node.SetAttributeValue("id", "p" + count); // count++; // chapter.HtmlId = node.Id; // chapter.HtmlContent = node.InnerHtml; // chapter.SubChapters = LoadChapters(book, new List<EpubNavigationPoint>(), epubArchive); // result.Add(chapter); // } // parentChapter.HtmlContent = doc.DocumentNode.InnerHtml; //} return(result); }
private static async Task <List <EpubChapter> > ReadChapters(List <EpubChapterRef> chapterRefs) { List <EpubChapter> result = new List <EpubChapter>(); foreach (EpubChapterRef chapterRef in chapterRefs) { EpubChapter chapter = new EpubChapter { Title = chapterRef.Title, ContentFileName = chapterRef.ContentFileName, Anchor = chapterRef.Anchor }; chapter.HtmlContent = await chapterRef.ReadHtmlContentAsync().ConfigureAwait(false); chapter.SubChapters = await ReadChapters(chapterRef.SubChapters).ConfigureAwait(false); result.Add(chapter); } return(result); }