コード例 #1
0
 public EpubChapterData(EpubChapter source, int indent)
 {
     Title    = source.Title.Trim();
     FileName = source.FileName();
     Anchor   = source.HashLocation; // Was renamed from .Anchor; 5cdfc156747a38f26e008950e8a7043b1d45b4c7 2018-04-18
     Indent   = indent;
 }
コード例 #2
0
        public void SetsChapterPreviousNext()
        {
            var book = EpubReader.Read(Cwd.Combine(@"Samples/epub-assorted/iOS Hackers Handbook.epub"));

            EpubChapter previousChapter = null;
            var         currentChapter  = book.TableOfContents[0];

            currentChapter.Previous.Should().Be(previousChapter);

            for (var i = 1; i <= 77; ++i)
            {
                previousChapter = currentChapter;
                currentChapter  = currentChapter.Next;

                previousChapter.Next.Should().Be(currentChapter);
                currentChapter.Previous.Should().Be(previousChapter);
            }

            EpubChapter nextChapter = null;

            currentChapter.Next.Should().Be(nextChapter);

            for (var i = 1; i <= 77; ++i)
            {
                nextChapter    = currentChapter;
                currentChapter = currentChapter.Previous;

                currentChapter.Next.Should().Be(nextChapter);
                nextChapter.Previous.Should().Be(currentChapter);
            }

            currentChapter.Previous.Should().BeNull();
        }
コード例 #3
0
        private void WriteChapter(EpubChapter epubChapter, Layout <View> layout)
        {
            if (epubChapter.SubChapters != null && epubChapter.SubChapters.Count > 0)
            {
                OpenBookChapterButton button = new OpenBookChapterButton(epubChapter)
                {
                    Text = string.Format(CultureInfo.InvariantCulture, epubChapter.Title)
                };

                button.Clicked += OnClickOpenBookChapterButton;

                layout.Children.Add(button);

                foreach (EpubChapter chapter in epubChapter.SubChapters)
                {
                    WriteChapter(chapter, layout);
                }
            }
            else
            {
                OpenBookChapterButton button = new OpenBookChapterButton(epubChapter)
                {
                    Text = string.Format(CultureInfo.InvariantCulture, epubChapter.Title)
                };

                button.Clicked += OnClickOpenBookChapterButton;

                layout.Children.Add(button);
            }
        }
コード例 #4
0
        private void FixupHtmlOrderedAdd(EpubChapter chapter)
        {
            if (chapter == null)
            {
                return;
            }

            if (!IsInFixup(chapter.AbsolutePath))
            {
                var htmlForChapter = FindHtml(chapter);
                if (htmlForChapter == null)
                {
                    App.Error($"ERROR: unable to find match HTML for chapter {chapter.Title}");
                }
                else
                {
                    _ResourcesHtmlOrdered.Add(htmlForChapter);
                }
            }

            if (chapter.SubChapters != null)
            {
                foreach (var subChapter in chapter.SubChapters)
                {
                    FixupHtmlOrderedAdd(subChapter);
                }
            }
        }
コード例 #5
0
        private static List <EpubChapter> LoadChaptersFromNcx(string ncxAbsolutePath,
                                                              IEnumerable <NcxNavPoint> navigationPoints, EpubChapter parentChapter = null)
        {
            var result   = new List <EpubChapter>();
            var previous = parentChapter;

            foreach (var navigationPoint in navigationPoints)
            {
                var chapter = new EpubChapter
                {
                    Title    = navigationPoint.NavLabelText,
                    Parent   = parentChapter,
                    Previous = previous
                };

                if (previous != null)
                {
                    previous.Next = chapter;
                }

                var href = new Href(navigationPoint.ContentSrc);
                chapter.RelativePath = href.Path;
                chapter.AbsolutePath = href.Path.ToAbsolutePath(ncxAbsolutePath);
                chapter.HashLocation = href.HashLocation;
                chapter.SubChapters  = LoadChaptersFromNcx(ncxAbsolutePath, navigationPoint.NavPoints, chapter);
                result.Add(chapter);

                previous = chapter.SubChapters.Any() ? chapter.SubChapters.Last() : chapter;
            }

            return(result);
        }
コード例 #6
0
        public static EpubBookExt TextToEpub(string fileString)
        {
            fileString = HtmlEncoder.Default.Encode(fileString);
            fileString = @"<html>\n<head>\n</head>\n<body>\n<pre id='START_OF_FILE'>\n" + fileString + "</pre></body></html>";

            var epubBook = new EpubBookExt(null)
            {
                Resources = new EpubResources()
            };
            var file = new EpubTextFile()
            {
                TextContent = fileString,
                //FileName = "Contents.html",
                AbsolutePath = "./Contents.html",
                Href = "Contents.html", // why not?
                ContentType = EpubSharp.Format.EpubContentType.Xml,
                MimeType = "text/html",
                Content = System.Text.Encoding.UTF8.GetBytes(fileString),
            };

            epubBook.Resources.Html.Add(file);
            var bookChapter = new EpubChapter()
            {
                Title = "Entire Contents",
                //FileName = "Contents.html",
                AbsolutePath = "./Contents.html",
                HashLocation = "START_OF_FILE",
                //Anchor = "START_OF_FILE"
            };
            epubBook.TableOfContents.Add(bookChapter);
            epubBook.FixupHtmlOrdered();
            return epubBook;
        }
コード例 #7
0
        private async void PrintChapter(EpubChapter chapter)
        {
            HtmlDocument htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(chapter.HtmlContent);
            StringBuilder sb = new StringBuilder();

            foreach (HtmlNode node in htmlDocument.DocumentNode.SelectNodes("//text()"))
            {
                sb.AppendLine(node.InnerText.Trim());
            }
            string chapterTitle = chapter.Title;
            string chapterText  = sb.ToString();

            //Console.WriteLine("------------ ", chapterTitle, "------------ ");
            //Console.WriteLine(chapterText);
            //Console.WriteLine();

            @ChapterContent.Inlines.Add(chapterText);
            @ChapterContent.FontWeight = FontWeights.Bold;

            foreach (EpubChapter subChapter in chapter.SubChapters)
            {
                PrintChapter(subChapter);
            }
        }
コード例 #8
0
 public static BookLocation FromChapter(EpubChapter chapter)
 {
     return(new BookLocation()
     {
         Location = chapter.HashLocation,  //.Anchor, -- hashlocation in this context is the part of the url after the hash like example.com/page#section31
         HtmlFileName = chapter.FileName(),
     });
 }
コード例 #9
0
 private static void PrintChapterTitle(EpubChapter chapter, int identLevel)
 {
     Console.Write(new string(' ', identLevel * 2));
     Console.WriteLine(chapter.Title);
     foreach (EpubChapter subChapter in chapter.SubChapters)
     {
         PrintChapterTitle(subChapter, identLevel + 1);
     }
 }
コード例 #10
0
 public Chapter(EpubChapter item, int index, int startLength, int level = 0, int hashIndex = 0)
 {
     Index       = index;
     Title       = item.Title;
     Link        = item.AbsolutePath;
     StartLength = startLength;
     Hash        = item.HashLocation;
     Level       = level;
     HashIndex   = hashIndex;
 }
コード例 #11
0
        private void Chapters_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = Chapters.SelectedIndex;

            //MessageBox.Show(index.ToString());
            ChapterContent.Inlines.Clear();
            EpubChapter chapter = epubBook.Chapters[index];

            PrintChapter(chapter);
            TextSize.Text = ChapterContent.FontSize.ToString();
        }
コード例 #12
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);
                }
            }
        }
コード例 #13
0
 private EpubTextFile FindHtml(EpubChapter chapter)
 {
     foreach (var html in Resources.Html)
     {
         if (html.AbsolutePath == chapter.AbsolutePath)
         {
             return(html);
         }
     }
     return(null);
 }
コード例 #14
0
ファイル: EpubTests.cs プロジェクト: w0pr2009a/EpubSharp
        private void AssertChapter(EpubChapter expected, EpubChapter actual)
        {
            Assert.Equal(expected.RelativePath, actual.RelativePath);
            Assert.Equal(expected.HashLocation, actual.HashLocation);
            Assert.Equal(expected.Title, actual.Title);

            Assert.Equal(expected.SubChapters.Count, actual.SubChapters.Count);
            for (var i = 0; i < expected.SubChapters.Count; ++i)
            {
                AssertChapter(expected.SubChapters[i], actual.SubChapters[i]);
            }
        }
コード例 #15
0
        private void AssertChapter(EpubChapter expected, EpubChapter actual)
        {
            Assert.Equal(expected.Anchor, actual.Anchor);
            Assert.Equal(expected.FileName, actual.FileName);
            Assert.Equal(expected.Title, actual.Title);

            Assert.Equal(expected.SubChapters.Count, actual.SubChapters.Count);
            for (var i = 0; i < expected.SubChapters.Count; ++i)
            {
                AssertChapter(expected.SubChapters[i], actual.SubChapters[i]);
            }
        }
コード例 #16
0
 String obtenerContenidoCap(EpubChapter chapter, int sub_pagina)
 {
     if (paginacion[1] == 0)
     {
         capitulo_actual = chapter;
         return(chapter.HtmlContent);
     }
     else
     {
         capitulo_actual = chapter.SubChapters[sub_pagina - 1];
         return(chapter.HtmlContent);
     }
 }
コード例 #17
0
        private void TestChapter(EpubChapter chapter)
        {
            // Test the chapter
            Assert.NotNull(chapter);
            Assert.False(string.IsNullOrEmpty(chapter.Title));
            Assert.False(string.IsNullOrEmpty(chapter.HtmlContent));

            // Recursively test sub chapters if any exist
            foreach (EpubChapter subChapter in chapter.SubChapters)
            {
                TestChapter(subChapter);
            }
        }
コード例 #18
0
        public static string FileName(this EpubChapter epub)
        {
            var str       = epub.AbsolutePath;
            var lastIndex = str.LastIndexOf('/');

            if (lastIndex < 0)
            {
                return(str);
            }
            var retval = str.Substring(lastIndex + 1);

            return(retval);
        }
コード例 #19
0
        private List <ChapterViewModel> GetChapters(List <EpubChapter> epubChapters)
        {
            List <ChapterViewModel> result = new List <ChapterViewModel>();

            for (int index = 0; index < epubChapters.Count; index++)
            {
                EpubChapter             epubChapter      = epubChapters[index];
                List <ChapterViewModel> subChapters      = GetChapters(epubChapter.SubChapters);
                ChapterViewModel        chapterViewModel = new ChapterViewModel(epubChapter.Title, subChapters,
                                                                                epubChapter.HtmlContent, epubChapter.ContentFileName);
                result.Add(chapterViewModel);
            }
            return(result);
        }
コード例 #20
0
ファイル: EpubReader.cs プロジェクト: d1mnewz/EpubNet
        private static async Task <List <EpubChapter> > ReadChapters(IEnumerable <EpubChapterRef> chapterRefs)
        {
            var result = new List <EpubChapter>();

            foreach (var chapterRef in chapterRefs)
            {
                var chapter = new EpubChapter
                {
                    Title           = chapterRef.Title,
                    ContentFileName = chapterRef.ContentFileName,
                    Anchor          = chapterRef.Anchor,
                    HtmlContent     = await chapterRef.ReadHtmlContentAsync().ConfigureAwait(false),
                    SubChapters     = await ReadChapters(chapterRef.SubChapters).ConfigureAwait(false)
                };
                result.Add(chapter);
            }

            return(result);
        }
コード例 #21
0
        private static async Task <List <EpubChapter> > ReadChapters(List <EpubChapterRef> chapterRefs)
        {
            List <EpubChapter> result = new List <EpubChapter>();

            foreach (EpubChapterRef chapterRef in chapterRefs)
            {
                EpubChapter chapter = new EpubChapter
                {
                    Title           = chapterRef.Title,
                    ContentFileName = chapterRef.ContentFileName,
                    Anchor          = chapterRef.Anchor
                };
                chapter.HtmlContent = await chapterRef.ReadHtmlContentAsync().ConfigureAwait(false);

                chapter.SubChapters = await ReadChapters(chapterRef.SubChapters).ConfigureAwait(false);

                result.Add(chapter);
            }
            return(result);
        }
コード例 #22
0
ファイル: Program.cs プロジェクト: hansenms/EpubSentiment
        static void AppendChapter(ref SentimentRequest request, EpubChapter chapter)
        {
            HtmlDocument htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(chapter.HtmlContent);

            StringBuilder sb = new StringBuilder();

            foreach (HtmlNode node in htmlDocument.DocumentNode.SelectNodes("//text()"))
            {
                sb.AppendLine(node.InnerText.Trim());
            }

            string chapterText = sb.ToString();

            int maxCharacters = 3 * 1024; //Max characters that we will send to sentiment API
            int chunks        = (int)Math.Ceiling((double)chapterText.Length / (double)maxCharacters);
            int charsPerChunk = (int)Math.Ceiling((double)chapterText.Length / (double)chunks);

            int offset = 0;

            for (int i = 0; i < chunks; ++i)
            {
                if (offset + charsPerChunk > chapterText.Length)
                {
                    charsPerChunk = chapterText.Length - offset;
                }

                var    testText = chapterText.Substring(offset, charsPerChunk);
                string chunkID  = "CHUNKDOCUMENT" + i;
                var    doc      = new SentimentDocument()
                {
                    Id = chunkID, Text = testText, Language = "en"
                };

                request.Documents.Add(doc);

                offset += charsPerChunk;
            }
        }
コード例 #23
0
        private List <ChapterViewModel> GetChapters(List <EpubChapter> epubChapters, List <EpubChapter> epubChapters1, List <EpubChapter> epubChapters2)
        {
            List <ChapterViewModel> result = new List <ChapterViewModel>();

            for (int index = 0; index < epubChapters.Count; index++)
            {
                EpubChapter             epubChapter1 = epubChapters1[index];
                List <ChapterViewModel> subChapters1 = GetChapters(epubChapter1.SubChapters);
                EpubChapter             epubChapter2 = epubChapters2[index];
                List <ChapterViewModel> subChapters2 = GetChapters(epubChapter2.SubChapters);
                EpubChapter             epubChapter0 = epubChapters[index];

                List <ChapterViewModel> subChapters0 = GetChapters(epubChapter0.SubChapters, epubChapter1.SubChapters, epubChapter2.SubChapters);

                ChapterViewModel chapterViewModel1 = new ChapterViewModel(epubChapter1.Title, subChapters1, epubChapter1.HtmlContent, epubChapter1.HtmlId);
                ChapterViewModel chapterViewModel2 = new ChapterViewModel(epubChapter2.Title, subChapters2, epubChapter2.HtmlContent, epubChapter2.HtmlId);
                ChapterViewModel chapterViewModel  = new ChapterViewModel(epubChapter0.Title, subChapters0, epubChapter0.HtmlContent, epubChapter0.HtmlId, new List <ChapterViewModel> {
                    chapterViewModel1, chapterViewModel2
                });
                result.Add(chapterViewModel);
            }
            return(result);
        }
コード例 #24
0
        private static string PrintChapter(EpubChapter chapter)
        {
            HtmlDocument htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(chapter.HtmlContent);
            StringBuilder sb = new StringBuilder();

            foreach (HtmlNode node in htmlDocument.DocumentNode.SelectNodes("//text()"))
            {
                sb.AppendLine(node.InnerText.Trim());
            }
            return(sb.ToString());

            /*string chapterTitle = chapter.Title;
             * string chapterText = sb.ToString();
             * Console.WriteLine("------------ ", chapterTitle, "------------ ");
             * Console.WriteLine(chapterText);
             * Console.WriteLine();
             * foreach (EpubChapter subChapter in chapter.SubChapters)
             * {
             *  PrintChapter(subChapter);
             * }*/
        }
コード例 #25
0
 public OpenBookChapterButton(EpubChapter chapter)
 {
     this.chapter = chapter;
 }
コード例 #26
0
 public static string ToPlainText(this EpubChapter chapter) => GetContentAsPlainText(chapter.HtmlContent);
コード例 #27
0
        private static List <EpubChapter> LoadChaptersFromNav(string navAbsolutePath, XElement element,
                                                              EpubChapter parentChapter = null)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            var ns = element.Name.Namespace;

            var result   = new List <EpubChapter>();
            var previous = parentChapter;

            var ol = element.Element(ns + NavElements.Ol);

            if (ol == null)
            {
                return(result);
            }

            foreach (var li in ol.Elements(ns + NavElements.Li))
            {
                var chapter = new EpubChapter
                {
                    Parent   = parentChapter,
                    Previous = previous
                };

                if (previous != null)
                {
                    previous.Next = chapter;
                }

                var link = li.Element(ns + NavElements.A);
                if (link != null)
                {
                    var id = link.Attribute("id")?.Value;
                    if (id != null)
                    {
                        chapter.Id = id;
                    }

                    var url = link.Attribute("href")?.Value;
                    if (url != null)
                    {
                        var href = new Href(url);
                        chapter.RelativePath = href.Path;
                        chapter.HashLocation = href.HashLocation;
                        chapter.AbsolutePath = chapter.RelativePath.ToAbsolutePath(navAbsolutePath);
                    }

                    var titleTextElement = li.Descendants().FirstOrDefault(e => !string.IsNullOrWhiteSpace(e.Value));
                    if (titleTextElement != null)
                    {
                        chapter.Title = titleTextElement.Value;
                    }

                    if (li.Element(ns + NavElements.Ol) != null)
                    {
                        chapter.SubChapters = LoadChaptersFromNav(navAbsolutePath, li, chapter);
                    }

                    result.Add(chapter);

                    previous = chapter.SubChapters.Any() ? chapter.SubChapters.Last() : chapter;
                }
            }

            return(result);
        }