Пример #1
0
        private static List <EpubChapter> LoadChaptersFromNcx(string ncxAbsolutePath, IEnumerable <NcxNavPoint> navigationPoints, EpubChapter parentChapter = null)
        {
            var result   = new List <EpubChapter>();
            var previous = parentChapter;

            foreach (var navigationPoint in navigationPoints)
            {
                var chapter = new EpubChapter
                {
                    Title    = navigationPoint.NavLabelText,
                    Parent   = parentChapter,
                    Previous = previous
                };

                if (previous != null)
                {
                    previous.Next = chapter;
                }

                var href = new Href(navigationPoint.ContentSrc);
                chapter.RelativePath = href.Path;
                chapter.AbsolutePath = href.Path.ToAbsolutePath(ncxAbsolutePath);
                chapter.HashLocation = href.HashLocation;
                chapter.SubChapters  = LoadChaptersFromNcx(ncxAbsolutePath, navigationPoint.NavPoints, chapter);
                result.Add(chapter);

                previous = chapter.SubChapters.Any() ? chapter.SubChapters.Last() : chapter;
            }
            return(result);
        }
Пример #2
0
        private static List <EpubChapter> LoadChaptersFromNav(string navAbsolutePath, XElement element, EpubChapter parentChapter = null)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            var ns = element.Name.Namespace;

            var result   = new List <EpubChapter>();
            var previous = parentChapter;

            var ol = element.Element(ns + NavElements.Ol);

            if (ol == null)
            {
                return(result);
            }

            foreach (var li in ol.Elements(ns + NavElements.Li))
            {
                var chapter = new EpubChapter
                {
                    Parent   = parentChapter,
                    Previous = previous
                };

                if (previous != null)
                {
                    previous.Next = chapter;
                }

                var link = li.Element(ns + NavElements.A);
                if (link != null)
                {
                    var id = link.Attribute("id")?.Value;
                    if (id != null)
                    {
                        chapter.Id = id;
                    }

                    var url = link.Attribute("href")?.Value;
                    if (url != null)
                    {
                        var href = new Href(url);
                        chapter.RelativePath = href.Path;
                        chapter.HashLocation = href.HashLocation;
                        chapter.AbsolutePath = chapter.RelativePath.ToAbsolutePath(navAbsolutePath);
                    }

                    var titleTextElement = li.Descendants().FirstOrDefault(e => !string.IsNullOrWhiteSpace(e.Value));
                    if (titleTextElement != null)
                    {
                        chapter.Title = titleTextElement.Value;
                    }

                    if (li.Element(ns + NavElements.Ol) != null)
                    {
                        chapter.SubChapters = LoadChaptersFromNav(navAbsolutePath, li, chapter);
                    }
                    result.Add(chapter);

                    previous = chapter.SubChapters.Any() ? chapter.SubChapters.Last() : chapter;
                }
            }

            return(result);
        }