예제 #1
0
        /// <summary>
        /// Gets the table of contents for an EBook.
        /// </summary>
        /// <param name="ebook"></param>
        /// <returns></returns>
        internal static async Task <TableOfContents> GetTableOfContentsAsync(this EBook ebook)
        {
            var hashRegex        = new Regex(@"(?<=.html)(#.*)");
            var relativeLocation = ebook.Manifest[ebook.Spine.Toc].ContentLocation;
            var tocFile          = await ebook._rootFolder.GetFileFromPathAsync(Path.Combine(Path.GetDirectoryName(ebook.ContentLocation), relativeLocation));

            var xml = await FileIO.ReadTextAsync(tocFile);

            var doc = XDocument.Parse(xml);
            var ns  = doc.Root.GetDefaultNamespace();

            var tableOfContents = new TableOfContents
            {
                Title = doc.Element(ns + "ncx").Element(ns + "docTitle").Element(ns + "text").Value
            };

            var navMapNode = doc.Element(ns + "ncx").Element(ns + "navMap");

            IEnumerable <NavPoint> ParseNavPoints(XElement node, int level)
            {
                var navPoints     = new List <NavPoint>();
                var navPointNodes = node.Elements(ns + "navPoint").ToList();

                foreach (var navPointNode in navPointNodes)
                {
                    var navPoint = new NavPoint
                    {
                        ContentPath = hashRegex.Replace(navPointNode.Element(ns + "content")?.Attribute("src").Value, string.Empty),
                        Id          = navPointNode.Attribute("id")?.Value,
                        Level       = level,
                        PlayOrder   = int.Parse(navPointNode.Attribute("playOrder")?.Value),
                        Text        = navPointNode.Element(ns + "navLabel")?.Element(ns + "text")?.Value
                    };

                    foreach (var subNavPoint in ParseNavPoints(navPointNode, level + 1).ToList())
                    {
                        navPoint.Items.Add(subNavPoint);
                    }

                    navPoints.Add(navPoint);
                }

                return(navPoints);
            }

            foreach (var navPoint in ParseNavPoints(navMapNode, 0).ToList())
            {
                tableOfContents.Items.Add(navPoint);
            }

            return(tableOfContents);
        }
예제 #2
0
        public static async Task <string> GetContentsAsync(this EBook ebook, NavPoint navPoint)
        {
            var manifestItem = ebook.Manifest.FirstOrDefault(a => Path.GetFileName(a.Value.ContentLocation) == Path.GetFileName(navPoint.ContentPath)).Value;

            return(await ebook.GetContentsAsync(manifestItem));
        }
예제 #3
0
        public static string GetContents(this EBook eBook, NavPoint navPoint, bool embedImages = false, bool embedStyles = false)
        {
            var manifestItem = eBook.Manifest.SingleOrDefault(item => Path.GetFileName(item.Value.ContentLocation) == Path.GetFileName(navPoint.ContentPath));

            return(eBook.GetContents(manifestItem.Value, embedImages, embedStyles));
        }
예제 #4
0
        public static TableOfContents GetTableOfContents(this EBook eBook)
        {
            if (eBook.Spine == null)
            {
                eBook.Spine = eBook.GetSpine();
            }

            var tocXml          = File.ReadAllText(eBook.Manifest[eBook.Spine.Toc].ContentLocation);
            var doc             = XDocument.Parse(tocXml);
            var ns              = doc.Root.GetDefaultNamespace();
            var ncxNode         = doc.Element(ns + "ncx");
            var tableOfContents = new TableOfContents
            {
                Title = ncxNode.Element(ns + "docTitle").Element(ns + "text").Value
            };
            var navMapNode = ncxNode.Element(ns + "navMap");

            IEnumerable <NavPoint> ParseNavPoints(XElement node, int level)
            {
                var navPoints     = new List <NavPoint>();
                var navPointNodes = node.Elements(ns + "navPoint").ToList();

                foreach (var navPointNode in navPointNodes)
                {
                    var filePath    = navPointNode.Element(ns + "content").Attribute("src").Value;
                    var pathBuilder = new StringBuilder();

                    foreach (var c in filePath)
                    {
                        if (c == '#' && pathBuilder.ToString().EndsWith(".html"))
                        {
                            break;
                        }

                        pathBuilder.Append(c);
                    }

                    var navPoint = new NavPoint
                    {
                        ContentPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(eBook.Manifest[eBook.Spine.Toc].ContentLocation), pathBuilder.ToString())),
                        Id          = navPointNode.Attribute("id")?.Value,
                        Level       = level,
                        PlayOrder   = int.Parse(navPointNode.Attribute("playOrder")?.Value),
                        Text        = navPointNode.Element(ns + "navLabel")?.Element(ns + "text")?.Value
                    };

                    foreach (var subNavPoint in ParseNavPoints(navPointNode, level + 1).ToList())
                    {
                        navPoint.Items.Add(subNavPoint);
                    }

                    navPoints.Add(navPoint);
                }

                return(navPoints);
            }

            foreach (var navPoint in ParseNavPoints(navMapNode, 0).ToList())
            {
                tableOfContents.Items.Add(navPoint);
            }

            return(tableOfContents);
        }