예제 #1
0
        public void AddRemoveTitleTest()
        {
            var writer = new EpubWriter();

            writer.SetTitle("Title1");
            var epub = WriteAndRead(writer);

            Assert.AreEqual("Title1", epub.Title);

            writer.SetTitle("Title2");
            epub = WriteAndRead(writer);
            Assert.AreEqual("Title2", epub.Title);

            writer.RemoveTitle();
            epub = WriteAndRead(writer);
            Assert.IsNull(epub.Title);

            writer.RemoveTitle();
        }
예제 #2
0
        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 EpubBuilder WithTitle(string title)
 {
     _writer.SetTitle(title);
     return(this);
 }
예제 #4
0
        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");
        }