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; }
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(); }
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); } }
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); } } }
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); }
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; }
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); } }
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(), }); }
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); } }
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; }
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(); }
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); } } }
private EpubTextFile FindHtml(EpubChapter chapter) { foreach (var html in Resources.Html) { if (html.AbsolutePath == chapter.AbsolutePath) { return(html); } } return(null); }
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]); } }
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]); } }
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); } }
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); } }
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); }
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); }
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); }
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); }
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; } }
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); }
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); * }*/ }
public OpenBookChapterButton(EpubChapter chapter) { this.chapter = chapter; }
public static string ToPlainText(this EpubChapter chapter) => GetContentAsPlainText(chapter.HtmlContent);
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); }