private static Epub3NavOl ReadEpub3NavOl(XElement epub3NavOlNode)
        {
            Epub3NavOl epub3NavOl = new Epub3NavOl();

            foreach (XAttribute navOlNodeAttribute in epub3NavOlNode.Attributes())
            {
                string attributeValue = navOlNodeAttribute.Value;
                switch (navOlNodeAttribute.GetLowerCaseLocalName())
                {
                case "hidden":
                    epub3NavOl.IsHidden = true;
                    break;
                }
            }
            epub3NavOl.Lis = new List <Epub3NavLi>();
            foreach (XElement navOlChildNode in epub3NavOlNode.Elements())
            {
                switch (navOlChildNode.GetLowerCaseLocalName())
                {
                case "li":
                    Epub3NavLi epub3NavLi = ReadEpub3NavLi(navOlChildNode);
                    epub3NavOl.Lis.Add(epub3NavLi);
                    break;
                }
            }
            return(epub3NavOl);
        }
        private static Epub3NavLi ReadEpub3NavLi(XElement epub3NavLiNode)
        {
            Epub3NavLi epub3NavLi = new Epub3NavLi();

            foreach (XElement navLiChildNode in epub3NavLiNode.Elements())
            {
                switch (navLiChildNode.GetLowerCaseLocalName())
                {
                case "a":
                    Epub3NavAnchor epub3NavAnchor = ReadEpub3NavAnchor(navLiChildNode);
                    epub3NavLi.Anchor = epub3NavAnchor;
                    break;

                case "span":
                    Epub3NavSpan epub3NavSpan = ReadEpub3NavSpan(navLiChildNode);
                    epub3NavLi.Span = epub3NavSpan;
                    break;

                case "ol":
                    Epub3NavOl epub3NavOl = ReadEpub3NavOl(navLiChildNode);
                    epub3NavLi.ChildOl = epub3NavOl;
                    break;
                }
            }
            return(epub3NavLi);
        }
        private static void AdjustRelativePath(Epub3NavOl nav, string rootFolder)
        {
            if (nav == null)
            {
                return;
            }

            foreach (var li in nav.Lis)
            {
                if (li.Anchor?.Href != null && li.Anchor.Href.Length > 0 && li.Anchor.Href[0] != '/')
                {
                    var relativeHref = $"{rootFolder}/{li.Anchor.Href}";
                    li.Anchor.Href = relativeHref;
                }

                AdjustRelativePath(li.ChildOl, rootFolder);
            }
        }
        private static Epub3Nav ReadEpub3Nav(XElement navNode)
        {
            Epub3Nav epub3Nav = new Epub3Nav();

            foreach (XAttribute navNodeAttribute in navNode.Attributes())
            {
                string attributeValue = navNodeAttribute.Value;
                switch (navNodeAttribute.GetLowerCaseLocalName())
                {
                case "type":
                    epub3Nav.Type = StructuralSemanticsPropertyParser.Parse(attributeValue);
                    break;

                case "hidden":
                    epub3Nav.IsHidden = true;
                    break;
                }
            }
            foreach (XElement navChildNode in navNode.Elements())
            {
                switch (navChildNode.GetLowerCaseLocalName())
                {
                case "h1":
                case "h2":
                case "h3":
                case "h4":
                case "h5":
                case "h6":
                    epub3Nav.Head = navChildNode.Value.Trim();
                    break;

                case "ol":
                    Epub3NavOl epub3NavOl = ReadEpub3NavOl(navChildNode);
                    epub3Nav.Ol = epub3NavOl;
                    break;
                }
            }
            return(epub3Nav);
        }
Example #5
0
        private static List <EpubNavigationItemRef> GetNavigationItems(EpubBookRef bookRef, Epub3NavOl epub3NavOl)
        {
            List <EpubNavigationItemRef> result = new List <EpubNavigationItemRef>();

            if (epub3NavOl != null && epub3NavOl.Lis != null)
            {
                foreach (Epub3NavLi epub3NavLi in epub3NavOl.Lis)
                {
                    if (epub3NavLi != null && (epub3NavLi.Anchor != null || epub3NavLi.Span != null))
                    {
                        if (epub3NavLi.Anchor != null)
                        {
                            Epub3NavAnchor        navAnchor         = epub3NavLi.Anchor;
                            EpubNavigationItemRef navigationItemRef = EpubNavigationItemRef.CreateAsLink();
                            navigationItemRef.Title = GetFirstNonEmptyHeader(navAnchor.Text, navAnchor.Title, navAnchor.Alt);
                            navigationItemRef.Link  = new EpubNavigationItemLink(navAnchor.Href);
                            navigationItemRef.HtmlContentFileRef = GetHtmlContentFileRef(bookRef, navigationItemRef.Link.ContentFileName);
                            navigationItemRef.NestedItems        = GetNavigationItems(bookRef, epub3NavLi.ChildOl);
                            result.Add(navigationItemRef);
                        }
                        else if (epub3NavLi.Span != null)
                        {
                            Epub3NavSpan          navSpan           = epub3NavLi.Span;
                            EpubNavigationItemRef navigationItemRef = EpubNavigationItemRef.CreateAsHeader();
                            navigationItemRef.Title       = GetFirstNonEmptyHeader(navSpan.Text, navSpan.Title, navSpan.Alt);
                            navigationItemRef.NestedItems = GetNavigationItems(bookRef, epub3NavLi.ChildOl);
                            result.Add(navigationItemRef);
                        }
                    }
                }
            }
            return(result);
        }