示例#1
0
        private static List<EpubChapter> LoadChapters(EpubBook book, List<EpubNavigationPoint> navigationPoints, ZipArchive epubArchive, EpubChapter parentChapter = null)
        {
            List<EpubChapter> result = new List<EpubChapter>();
            foreach (EpubNavigationPoint navigationPoint in navigationPoints)
            {
                EpubChapter chapter = new EpubChapter();
                chapter.Book = book;
                chapter.Title = navigationPoint.NavigationLabels.First().Text;
                int contentSourceAnchorCharIndex = navigationPoint.Content.Source.IndexOf('#');
                if (contentSourceAnchorCharIndex == -1)
                    chapter.ContentFileName = navigationPoint.Content.Source;
                else
                {
                    chapter.ContentFileName = navigationPoint.Content.Source.Substring(0, contentSourceAnchorCharIndex);
                    chapter.Anchor = navigationPoint.Content.Source.Substring(contentSourceAnchorCharIndex + 1);
                }
                EpubTextContentFile htmlContentFile;
                if (!book.Content.Html.TryGetValue(chapter.ContentFileName, out htmlContentFile))
                    throw new Exception(String.Format("Incorrect EPUB manifest: item with href = \"{0}\" is missing", chapter.ContentFileName));
                chapter.HtmlContent = htmlContentFile.Content;
                chapter.SubChapters = LoadChapters(book, navigationPoint.ChildNavigationPoints, epubArchive, chapter);
                result.Add(chapter);
            }
            //if (result.Count == 0 && parentChapter != null)
            //{
            //    var html = parentChapter.HtmlContent;
            //    HtmlDocument doc = new HtmlDocument();
            //    doc.LoadHtml(html);
            //    int count = 0;
            //    foreach (var node in doc.DocumentNode.QuerySelectorAll("p"))
            //    {
            //        EpubChapter chapter = new EpubChapter();
            //        chapter.Title = node.InnerText.Substring(0, Math.Min(node.InnerText.Length, 80));
            //        if (chapter.Title.Length < node.InnerText.Length)
            //            chapter.Title = chapter.Title.Substring(0, chapter.Title.LastIndexOf(" "));
            //        node.SetAttributeValue("id", "p" + count);
            //        count++;
            //        chapter.HtmlId = node.Id;
            //        chapter.HtmlContent = node.InnerHtml;
            //        chapter.SubChapters = LoadChapters(book, new List<EpubNavigationPoint>(), epubArchive);
            //        result.Add(chapter);
            //    }
            //    parentChapter.HtmlContent = doc.DocumentNode.InnerHtml;

            //}
            return result;
        }
示例#2
0
 private static List<EpubChapter> LoadChapters(EpubBook book, List<EpubNavigationPoint> navigationPoints, ZipArchive epubArchive)
 {
     List<EpubChapter> result = new List<EpubChapter>();
     foreach (EpubNavigationPoint navigationPoint in navigationPoints)
     {
         EpubChapter chapter = new EpubChapter();
         chapter.Title = navigationPoint.NavigationLabels.First().Text;
         int contentSourceAnchorCharIndex = navigationPoint.Content.Source.IndexOf('#');
         if (contentSourceAnchorCharIndex == -1)
             chapter.ContentFileName = navigationPoint.Content.Source;
         else
         {
             chapter.ContentFileName = navigationPoint.Content.Source.Substring(0, contentSourceAnchorCharIndex);
             chapter.Anchor = navigationPoint.Content.Source.Substring(contentSourceAnchorCharIndex + 1);
         }
         EpubTextContentFile htmlContentFile;
         if (!book.Content.Html.TryGetValue(chapter.ContentFileName, out htmlContentFile))
             throw new Exception(String.Format("Incorrect EPUB manifest: item with href = \"{0}\" is missing", chapter.ContentFileName));
         chapter.HtmlContent = htmlContentFile.Content;
         chapter.SubChapters = LoadChapters(book, navigationPoint.ChildNavigationPoints, epubArchive);
         result.Add(chapter);
     }
     return result;
 }
示例#3
0
        private void SearchInChapter(EpubChapter chapter)
        {
            if (chapter.SubChapters.Count > 0)
            {
                foreach (var item in chapter.SubChapters)
                {
                    SearchInChapter(item);
                }
            }
            else
            {
                try
                {
                    int results = 0;
                    var str = chapter.HtmlContent;
                    int index = str.ToLower().IndexOf(filter.ToLower(), StringComparison.InvariantCulture);
                    int lastResult = index + filter.Length;
                    while (index >= 0)
                    {
                        string before = "";
                        string originalFound = "";
                        string after = "";
                        if (index > 0)
                            before = str.Substring(0, index);
                        if (index + filter.Length < str.Length)
                            after = str.Substring(index + filter.Length);
                        originalFound = str.Substring(index, filter.Length);
                        SearchResults.Add(new SearchResult()
                        {
                            HtmlId = "res" + results,
                            BeforeResult = before.Substring(Math.Max(0, before.Length - 60)),
                            Result = originalFound,
                            AfterResult = after.Substring(0, Math.Min(60, after.Length)),
                            Reference = chapter
                        });
                        originalFound = "<result id=\"res" + results + "\">" + originalFound + "</result>";
                        lastResult = index + originalFound.Length;

                        str = before + originalFound + after;
                        results++;
                        index = str.ToLower().IndexOf(filter.ToLower(), lastResult, StringComparison.InvariantCulture);
                    }
                    chapter.HtmlContent = str;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }