Ejemplo n.º 1
0
        internal void SetTocAnchors(XDocument tocDocument)
        {
            Console.WriteLine("setting TOC chapter anchors...");

            var xhtml = PublicationNamespaces.Xhtml;

            var anchors = tocDocument.Root
                          .Element(xhtml + "body")
                          .Element(xhtml + "div")
                          .Elements(xhtml + "a");

            XElement templatedChapterElement = null;
            var      newChapterElementList   = new List <XElement>();
            var      hrefTemplate            = GetTOCHrefTemplate();

            _chapterSet.Keys
            .Select((chapterId, i) => new { chapterId, i })
            .ForEachInEnumerable(a =>
            {
                var chapterId = a.chapterId;
                var i         = a.i;

                var chapterElement = anchors.SingleOrDefault(item =>
                {
                    var href = item.Attribute("href").Value;
                    return(href == string.Format(hrefTemplate, chapterId));
                });

                var canAddNavPoint        = (chapterElement == null) && (i > 0);
                var isFirstChapterIdError = (chapterElement == null) && (i == 0);
                var isFirstChapterId      = (chapterElement != null) && (i == 0);

                if (isFirstChapterIdError)
                {
                    PublicationContext.Throw(string.Format("ERROR: cannot find templated element {0}", chapterId));
                }
                else if (isFirstChapterId)
                {
                    templatedChapterElement = chapterElement;
                    this.SetTocAnchor(templatedChapterElement, chapterId);
                }
                else if (canAddNavPoint)
                {
                    var @new = GetTOCAnchor(chapterId);
                    this.SetTocAnchor(@new, chapterId);
                    newChapterElementList.Add(new XElement(xhtml + "br"));
                    newChapterElementList.Add(@new);
                }
            });

            if (!newChapterElementList.Any())
            {
                return;
            }

            Console.WriteLine("adding new elements under templated element...");
            templatedChapterElement.AddAfterSelf(newChapterElementList.ToArray());
        }
        internal void SetManifestItemElementsForChapters()
        {
            Console.WriteLine("setting manifest item elements for chapters...");

            var opf = PublicationNamespaces.IdpfOpenPackagingFormat;

            var items = _idpfDocument.Root
                        .Element(opf + "manifest")
                        .Elements(opf + "item");

            XElement templatedChapterElement = null;
            var      newChapterElementList   = new List <XElement>();

            _chapterSet.Keys
            .Select((chapterId, i) => new { chapterId, i })
            .ForEachInEnumerable(a =>
            {
                var chapterId = a.chapterId;
                var i         = a.i;

                var chapterElement = items.SingleOrDefault(item =>
                {
                    var id = item.Attribute("id").Value;
                    return(chapterId.Equals(id));
                });

                var canAddNavPoint        = (chapterElement == null) && (i > 0);
                var isFirstChapterIdError = (chapterElement == null) && (i == 0);
                var isFirstChapterId      = (chapterElement != null) && (i == 0);

                if (isFirstChapterIdError)
                {
                    PublicationContext.Throw(string.Format("ERROR: cannot find templated element {0}", chapterId));
                }
                else if (isFirstChapterId)
                {
                    templatedChapterElement = chapterElement;
                    this.SetManifestItem(templatedChapterElement, chapterId);
                }
                else if (canAddNavPoint)
                {
                    var @new = GetManifestItem(chapterId);
                    this.SetManifestItem(@new, chapterId);
                    newChapterElementList.Add(@new);
                }
            });

            if (!newChapterElementList.Any())
            {
                return;
            }

            Console.WriteLine("adding new elements under templated element...");
            templatedChapterElement.AddAfterSelf(newChapterElementList.ToArray());
        }
        internal void SetChapterNavPoints(IEnumerable <XElement> navPoints)
        {
            Console.WriteLine("setting navPoint elements for chapters...");

            XElement templatedChapterElement = null;
            var      newChapterElementList   = new List <XElement>();

            _chapterSet.Keys
            .Select((chapterId, i) => new { chapterId, i })
            .ForEachInEnumerable(a =>
            {
                var chapterId = a.chapterId;
                var i         = a.i;

                var chapterElement = navPoints.SingleOrDefault(navPoint =>
                {
                    var id = navPoint.Attribute("id").Value;
                    return(chapterId.Equals(id));
                });

                var canAddNavPoint        = (chapterElement == null) && (i > 0);
                var isFirstChapterIdError = (chapterElement == null) && (i == 0);
                var isFirstChapterId      = (chapterElement != null) && (i == 0);

                if (isFirstChapterIdError)
                {
                    PublicationContext.Throw(string.Format("ERROR: cannot find templated element {0}", chapterId));
                }
                else if (isFirstChapterId)
                {
                    templatedChapterElement = chapterElement;
                    this.SetChapterNavPointText(templatedChapterElement, _chapterSet[chapterId]);
                }
                else if (canAddNavPoint)
                {
                    var @new = GetNavPoint(chapterId);
                    this.SetChapterNavPointText(@new, _chapterSet[chapterId]);
                    newChapterElementList.Add(@new);
                }
            });

            if (!newChapterElementList.Any())
            {
                return;
            }

            Console.WriteLine("adding new elements under templated element...");
            templatedChapterElement.AddAfterSelf(newChapterElementList.ToArray());
        }