public void ClearChaptersTest() { var writer = new EpubWriter(); writer.AddChapter("Chapter 1", "bla bla bla"); writer.AddChapter("Chapter 2", "foo bar"); writer.AddChapter("Chapter 3", "fooz barz"); var epub = WriteAndRead(writer); Assert.AreEqual(3, epub.TableOfContents.Count); writer = new EpubWriter(epub); writer.ClearChapters(); epub = WriteAndRead(writer); Assert.AreEqual(0, epub.TableOfContents.Count); }
public void CanAddChapterTest() { var writer = new EpubWriter(); var chapters = new[] { writer.AddChapter("Chapter 1", "bla bla bla"), writer.AddChapter("Chapter 2", "foo bar") }; var epub = WriteAndRead(writer); Assert.AreEqual("Chapter 1", chapters[0].Title); Assert.AreEqual("Chapter 2", chapters[1].Title); Assert.AreEqual(2, epub.TableOfContents.Count); for (var i = 0; i < chapters.Length; ++i) { Assert.AreEqual(chapters[i].Title, epub.TableOfContents[i].Title); Assert.AreEqual(chapters[i].FileName, epub.TableOfContents[i].FileName); Assert.AreEqual(chapters[i].Anchor, epub.TableOfContents[i].Anchor); Assert.AreEqual(0, chapters[i].SubChapters.Count); Assert.AreEqual(0, epub.TableOfContents[i].SubChapters.Count); } }
public void CanAddChapterTest() { var writer = new EpubWriter(); var chapters = new[] { writer.AddChapter("Chapter 1", "bla bla bla"), writer.AddChapter("Chapter 2", "foo bar") }; var epub = WriteAndRead(writer); Assert.Equal("Chapter 1", chapters[0].Title); Assert.Equal("Chapter 2", chapters[1].Title); Assert.Equal(2, epub.TableOfContents.Count); for (var i = 0; i < chapters.Length; ++i) { Assert.Equal(chapters[i].Title, epub.TableOfContents[i].Title); Assert.Equal(chapters[i].RelativePath, epub.TableOfContents[i].RelativePath); Assert.Equal(chapters[i].HashLocation, epub.TableOfContents[i].HashLocation); Assert.Equal(0, chapters[i].SubChapters.Count); Assert.Equal(0, epub.TableOfContents[i].SubChapters.Count); } }
public EpubBuilder WithChapters(IEnumerable <Chapter> chapters) { foreach (var chapter in chapters.Where(c => c.IsValid)) { foreach (var image in chapter.Images) { _writer.AddFile(image.Path, image.Content, image.Format.ToEpubContentType()); } Console.WriteLine($"Добавляем часть {chapter.Title.CoverQuotes()}"); _writer.AddChapter(chapter.Title, chapter.Content); } return(this); }
public void GenerateEpub(Settings options, string[] files) { EpubWriter writer = new EpubWriter(); writer.AddAuthor(options.Author); if (!string.IsNullOrWhiteSpace(options.Cover)) { using (var ms = new MemoryStream()) { try { var img = Image.FromFile(options.GetPathStr(options.Cover), true); img.Save(ms, img.RawFormat); writer.SetCover(ms.ToArray(), ImageFormat.Png); writer.AddChapter("Cover", GeneratePageTemplate("<img src='cover.png' />")); } catch (FileNotFoundException e) { LastStatus = $"Failed to find Cover Image at {options.GetPathStr(options.Cover)}. Defaulting to blank cover image. If you have a proper cover, please provide a relative path to it in the settings json file"; var img = new Bitmap(1, 1); img.SetPixel(0, 0, Color.White); img.Save(ms, SysImg.ImageFormat.Png); writer.SetCover(ms.ToArray(), ImageFormat.Png); } } } writer.SetTitle(options.Title); int maxLength = files.Length * 2 + 1; for (int i = 0; i < files.Length; i++) { string file = files[i]; FileInfo info = new FileInfo(file); string text = string.Empty; try { OnProgress(i * 2, maxLength); text = ConvertToHtml(info, writer); } catch (Exception ex) { throw new Exception($"Unable to convert file: {file}", ex); } writer.AddChapter(info.Name.Replace($".{FileType}", ""), text); OnProgress(i * 2 + 1, maxLength); } if (files.Length == 0) { string path = Path.GetDirectoryName(options.Path); if (path == ".") { path = Environment.CurrentDirectory; } LastStatus = $"No {FileType} files found in {path}"; } writer.Write(options.GetTitlePath()); OnProgress(maxLength, maxLength); }
public static void Main() { var writer = new EpubWriter(); writer.AddAuthor("Furesoft"); var uris = new[] { "https://rss.golem.de/rss.php?tp=dev&feed=RSS2.0", "https://www.heise.de/rss/heiseplus-atom.xml", "https://www.spiegel.de/schlagzeilen/tops/index.rss", "https://www.spiegel.de/schlagzeilen/index.rss", "https://www.welt.de/feeds/topnews.rss", }; var feedinfo = new List <object>(); var ids = new Dictionary <string, string>(); foreach (var uri in uris) { var feed = FeedReader.ReadAsync(uri).Result; var id = Guid.NewGuid().ToString(); feedinfo.Add(new { title = feed.Title, id }); ids.Add(feed.Title, id); } var noc = Template.Parse(Resources.TOC).Render(new { info = feedinfo }); writer.AddFile("toc.ncx", noc, EpubSharp.Format.EpubContentType.Xml); foreach (var uri in uris) { var feed = FeedReader.ReadAsync(uri).Result; if (feed.ImageUrl != null) { var webClient = new WebClient(); var logo = webClient.DownloadData(feed.ImageUrl); writer.AddFile(Path.GetFileName(feed.ImageUrl), logo, EpubSharp.Format.EpubContentType.ImageJpeg); } foreach (var item in feed.Items) { if (item.Content != null) { var d = new HtmlDocument(); d.LoadHtml(item.Content); var nodes = d.DocumentNode.SelectNodes("img"); if (nodes != null) { foreach (var n in nodes) { var url = n.Attributes["src"].Value; //download image var webClient = new WebClient(); var logo = webClient.DownloadData(url); writer.AddFile(Path.GetFileName(url), logo, EpubSharp.Format.EpubContentType.ImageJpeg); //set new uri n.Attributes["src"].Value = Path.GetFileName(url); } var ms = new MemoryStream(); d.Save(ms); item.Content = Encoding.UTF8.GetString(ms.ToArray()); } } } var template = Template.Parse(Resources.Template); var scriptObject1 = new ScriptObject { { "logo", Path.GetFileName(feed.ImageUrl) }, { "feed", feed } }; var context = new TemplateContext(); context.PushGlobal(scriptObject1); var result = template.Render(context); writer.AddChapter(feed.Title, result, ids[feed.Title]); } writer.SetCover(Resources.cover, ImageFormat.Jpeg); writer.SetTitle($"epaper_{DateTime.Now.Date}"); writer.Write($"epaper_{DateTime.Now.Date.ToShortDateString()}.epub"); }