Ejemplo n.º 1
0
        public static List <EpubChapterRef> GetChapters(EpubBookRef bookRef, List <EpubNavigationPoint> navigationPoints)
        {
            List <EpubChapterRef> result = new List <EpubChapterRef>();

            foreach (EpubNavigationPoint navigationPoint in navigationPoints)
            {
                string contentFileName;
                string anchor;
                int    contentSourceAnchorCharIndex = navigationPoint.Content.Source.IndexOf('#');
                if (contentSourceAnchorCharIndex == -1)
                {
                    contentFileName = navigationPoint.Content.Source;
                    anchor          = null;
                }
                else
                {
                    contentFileName = navigationPoint.Content.Source.Substring(0, contentSourceAnchorCharIndex);
                    anchor          = navigationPoint.Content.Source.Substring(contentSourceAnchorCharIndex + 1);
                }
                EpubTextContentFileRef htmlContentFileRef;
                if (!bookRef.Content.Html.TryGetValue(contentFileName, out htmlContentFileRef))
                {
                    throw new Exception(String.Format("Incorrect EPUB manifest: item with href = \"{0}\" is missing.", contentFileName));
                }
                EpubChapterRef chapterRef = new EpubChapterRef(htmlContentFileRef);
                chapterRef.ContentFileName = contentFileName;
                chapterRef.Anchor          = anchor;
                chapterRef.Title           = navigationPoint.NavigationLabels.First().Text;
                chapterRef.SubChapters     = GetChapters(bookRef, navigationPoint.ChildNavigationPoints);
                result.Add(chapterRef);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public static List <EpubChapterRef> GetChapters(EpubBookRef bookRef, EpubSpine spine,
                                                        List <EpubNavigationPoint> navigationPoints)
        {
            var result = new List <EpubChapterRef>();

            for (var s = 0; s < spine.Count; s++)
            {
                var    itemRef = spine[s];
                string contentFileName;
                string anchor;
                contentFileName = WebUtility.UrlDecode(bookRef.Schema.Package.Manifest
                                                       .FirstOrDefault(e => e.Id == itemRef.IdRef)?.Href);
                anchor = null;
                if (!bookRef.Content.Html.TryGetValue(contentFileName, out var htmlContentFileRef))
                {
                    throw new Exception(string.Format("Incorrect EPUB manifest: item with href = \"{0}\" is missing.",
                                                      contentFileName));
                }
                var chapterRef = new EpubChapterRef(htmlContentFileRef);
                chapterRef.ContentFileName = contentFileName;
                chapterRef.Anchor          = anchor;
                chapterRef.Parent          = null;
                var navPoint = navigationPoints.LastOrDefault(nav =>
                                                              spine.Take(s + 1)
                                                              .Select(sp => bookRef.Schema.Package.Manifest.FirstOrDefault(e => e.Id == sp.IdRef)?.Href)
                                                              .Contains(nav.Content.Source.Split('#')[0]));
                if (navPoint != null)
                {
                    chapterRef.Title = navPoint.NavigationLabels.First().Text;
                }
                else
                {
                    chapterRef.Title = $"Chapter {s + 1}";
                }
                chapterRef.SubChapters = new List <EpubChapterRef>();
                result.Add(chapterRef);
            }

            return(result);
        }