Пример #1
0
 private static void TestEpubFile(string epubFilePath, Dictionary <string, int> filesByVersion, List <string> filesWithErrors)
 {
     Console.WriteLine($"File: {epubFilePath}");
     Console.WriteLine("-----------------------------------");
     try
     {
         using (EpubBookRef bookRef = EpubReader.OpenBook(epubFilePath))
         {
             string epubVersionString = bookRef.Schema.Package.GetVersionString();
             if (filesByVersion.ContainsKey(epubVersionString))
             {
                 filesByVersion[epubVersionString]++;
             }
             else
             {
                 filesByVersion[epubVersionString] = 1;
             }
             Console.WriteLine($"EPUB version: {epubVersionString}");
             Console.WriteLine($"Total files: {bookRef.Content.AllFiles.Count}, HTML files: {bookRef.Content.Html.Count}," +
                               $" CSS files: {bookRef.Content.Css.Count}, image files: {bookRef.Content.Images.Count}, font files: {bookRef.Content.Fonts.Count}.");
             Console.WriteLine($"Reading order: {bookRef.GetReadingOrder().Count} file(s).");
             Console.WriteLine("Navigation:");
             foreach (EpubNavigationItemRef navigationItemRef in bookRef.GetNavigation())
             {
                 PrintNavigationItem(navigationItemRef, 0);
             }
         }
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception.ToString());
         filesWithErrors.Add(epubFilePath);
     }
     Console.WriteLine();
 }
Пример #2
0
 public static void Run(string filePath)
 {
     using (EpubBookRef bookRef = EpubReader.OpenBook(filePath))
     {
         Console.WriteLine("Navigation:");
         foreach (EpubNavigationItemRef navigationItemRef in bookRef.GetNavigation())
         {
             PrintNavigationItem(navigationItemRef, 0);
         }
     }
     Console.WriteLine();
 }
        public static string Run(string filePath)
        {
            StringBuilder result = new StringBuilder();

            using (EpubBookRef bookRef = EpubReader.OpenBook(filePath))
            {
                result.AppendLine("Navigation:");
                foreach (EpubNavigationItemRef navigationItemRef in bookRef.GetNavigation())
                {
                    result.Append(PrintNavigationItem(navigationItemRef, 0));
                }
            }
            result.AppendLine();
            return(result.ToString());
        }
Пример #4
0
        /// <summary>
        /// Display the whole XHTML file (actually just the body innerhtml). This is generally needed to display the whole chapter/book if hyperlinks link to a targets in another chapter - in this case we display the whole book (if the whole book is in one xhtml file)
        /// </summary>
        /// <param name="ePubDisplayModel"></param>
        /// <param name="epubBookRef"></param>
        public void FindAndProcessXHTML(ref EpubDisplayModel ePubDisplayModel, EpubBookRef epubBookRef)
        {
            // build TOC
            foreach (var item in epubBookRef.GetNavigation())//.GetChapters())
            {
                ePubDisplayModel.TOC_Items.Add(new EpubLink()
                {
                    LinkTitle = item.Title
                });                                                                        // add TOC to display model
            }

            var foundContent = epubBookRef.Content.Html.Where(x => x.Key.EndsWith(_processAction, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

            if (!string.IsNullOrEmpty(foundContent.Key))
            {
                var foundHtml = foundContent.Value.ReadContentAsText();

                var doc = new HtmlDocument();
                doc.LoadHtml(foundHtml ?? "");

                var bodyNodes = doc.DocumentNode.SelectSingleNode("//body"); // select everything inside the <body> tag
                ePubDisplayModel.ChapterHtml = bodyNodes.OuterHtml;
            }
        }
Пример #5
0
        /// <summary>
        /// Display the chapter. This might be a whole xhtml file or a section of an xhtml file between 2 'chapter' id's (or epub navnodes)
        /// </summary>
        /// <param name="ePubDisplayModel"></param>
        /// <param name="epubBookRef"></param>
        public void FindAndProcessChapter(ref EpubDisplayModel ePubDisplayModel, EpubBookRef epubBookRef)
        {
            EpubChapterHolder cHolder = new EpubChapterHolder();

            // Enumerating chapters
            var allChapters = epubBookRef.GetNavigation();//.GetChapters();

            for (int i = 0; i < allChapters.Count(); i++)
            {
                var    _chapter = allChapters[i];
                string encTitle = EpubHelpers.EncodeChapterTitleForUrl(_chapter.Title);

                if (encTitle == _processAction && !cHolder.CurrentChapter.IsValid()) // chapter found! select this chapter to display
                {
                    cHolder.CurrentChapter.ChapterIndex = i;
                    cHolder.CurrentChapter.EpubChapter  = _chapter;
                    cHolder.CurrentChapter.TitleAndLinkUrl.LinkTitle = _chapter.Title;
                }

                ePubDisplayModel.TOC_Items.Add(new EpubLink()
                {
                    LinkTitle = _chapter.Title
                });                                                                            // add TOC to display model
            }

            if (!cHolder.CurrentChapter.IsValid())                             // chapter not found - so get the default chapter and redirect to that
            {
                if (_startAtIndex < 0 || _startAtIndex >= allChapters.Count()) // check if the _startAtIndex property is within valid limits - if not then set the index to 0
                {
                    _startAtIndex = 0;
                }

                var _chapter = allChapters[_startAtIndex];
                cHolder.CurrentChapter.TitleAndLinkUrl.LinkTitle = _chapter.Title;

                ePubDisplayModel.RedirectToChapter = cHolder.CurrentChapter.TitleAndLinkUrl.LinkUrl;
                return; // exit this method as we need to perform a redirect in the calling controller rather than
            }

            ePubDisplayModel.TOC_Items[cHolder.CurrentChapter.ChapterIndex].IsCurrent = true;


            // try and find the previous chapter details
            int previousChapterIndex = cHolder.CurrentChapter.ChapterIndex - 1;

            if (previousChapterIndex >= 0) // check index is valid
            {
                var _chapter = allChapters[previousChapterIndex];
                cHolder.PreviousChapter.ChapterIndex = previousChapterIndex;
                cHolder.PreviousChapter.EpubChapter  = _chapter;
                cHolder.PreviousChapter.TitleAndLinkUrl.LinkTitle = _chapter.Title;

                ePubDisplayModel.Nav_PreviousChapterLink.LinkTitle = _chapter.Title;
            }

            // try and find the next chapter details
            int nextChapterIndex = cHolder.CurrentChapter.ChapterIndex + 1;

            if (nextChapterIndex < allChapters.Count()) // check index is valid
            {
                var _chapter = allChapters[nextChapterIndex];
                cHolder.NextChapter.ChapterIndex = nextChapterIndex;
                cHolder.NextChapter.EpubChapter  = _chapter;
                cHolder.NextChapter.TitleAndLinkUrl.LinkTitle = _chapter.Title;

                ePubDisplayModel.Nav_NextChapterLink.LinkTitle = _chapter.Title;
            }

            ePubDisplayModel.ChapterHtml = BuildChapterHtml(cHolder);
        }