示例#1
0
        static void CreateOpf(Book2 book)
        {
            string path    = "./tmp/content.opf";
            string content = tpl_content;

            content = content.Replace("___BOOK_ID___", book.id);
            content = content.Replace("___BOOK_NAME___", book.name);
            string manifest = "";
            string spine    = "";

            for (int i = 0; i < book.chapters.Length; i++)
            {
                var chapter = book.chapters[i];
                // mainifest
                string tocLine = "";
                tocLine   = string.Format("<item id=\"chapter{0}\" href=\"chapter{0}.html\" media-type=\"application/xhtml+xml\"/>\r\n", i);
                manifest += tocLine;
                // spine
                string spineLine = "";
                spineLine = string.Format("<itemref idref=\"chapter{0}\" linear=\"yes\"/>\r\n", i);
                spine    += spineLine;
            }
            content = content.Replace("___MANIFEST___", manifest);
            content = content.Replace("___SPINE___", spine);
            File.WriteAllText(path, content);
        }
示例#2
0
        static void CreateCover(Book2 book)
        {
            string path    = "./tmp/cover.html";
            string content = tpl_cover;

            content = content.Replace("___BOOK_NAME___", book.name);
            content = content.Replace("___BOOK_AUTHOR___", book.author);
            File.WriteAllText(path, content);
        }
示例#3
0
        static void CreateBookToc(Book2 book)
        {
            string path       = "./tmp/book-toc.html";
            string content    = tpl_book_toc;
            string tocContent = "";

            for (int i = 0; i < book.chapters.Length; i++)
            {
                var    chapter = book.chapters[i];
                string tocLine = string.Format("<dt class=\"tocl2\"><a href=\"chapter{0}.html\">{1}</a></dt>\r\n",
                                               i, chapter.title);
                tocContent += tocLine;
            }
            content = content.Replace("___CONTENT___", tocContent);
            File.WriteAllText(path, content);
        }
示例#4
0
 public static void Book2Mobi(Book2 book, string savePath)
 {
     LoadTemplates();
     //create tmp
     if (Directory.Exists("./tmp"))
     {
         Directory.Delete("./tmp", true);
     }
     Directory.CreateDirectory("./tmp");
     CreateCover(book);
     CreateChapters(book);
     CreateStyle(book);
     CreateBookToc(book);
     CreateNaxToc(book);
     CreateOpf(book);
     gen(savePath);
 }
示例#5
0
        public static void Book2Txt(Book2 book, string savePath)
        {
            string txt = "";

            for (int i = 0; i < book.chapters.Length; i++)
            {
                var chapterInfo = book.chapters[i];
                txt += chapterInfo.title + "\r\n";
                txt += chapterInfo.body + "\r\n";
            }

            string saveDir = Path.GetDirectoryName(savePath);

            if (!Directory.Exists(saveDir))
            {
                Directory.CreateDirectory(saveDir);
            }
            File.WriteAllText(savePath, txt);
        }
示例#6
0
        static void CreateNaxToc(Book2 book)
        {
            string path    = "./tmp/toc.ncx";
            string content = tpl_toc;

            content = content.Replace("___BOOK_ID___", book.id);
            content = content.Replace("___BOOK_NAME___", book.name);
            string tocContent = "";

            for (int i = 0; i < book.chapters.Length; i++)
            {
                var    chapter = book.chapters[i];
                string tocLine = string.Format("<navPoint id=\"chapter{0}\" playOrder=\"{1}\">\r\n", i, i + 1);
                tocLine    += string.Format("<navLabel><text>{0}</text></navLabel>\r\n", chapter.title);
                tocLine    += string.Format("<content src=\"chapter{0}.html\"/>\r\n</navPoint>\r\n", i);
                tocContent += tocLine;
            }
            content = content.Replace("___NAV___", tocContent);
            File.WriteAllText(path, content);
        }
示例#7
0
 static void CreateChapters(Book2 book)
 {
     for (int i = 0; i < book.chapters.Length; i++)
     {
         var    chapter = book.chapters[i];
         string path    = "./tmp/chapter" + i + ".html";
         string content = tpl_chapter;
         content = content.Replace("___CHAPTER_ID___", "Chapter " + i);
         content = content.Replace("___CHAPTER_NAME___", chapter.title);
         string chapterContent = chapter.body;
         chapterContent = chapterContent.Replace("\r", "");
         var ps = chapterContent.Split('\n');
         chapterContent = "";
         foreach (var p in ps)
         {
             string pStr = "<p class=\"a\">";
             pStr           += "  " + p;
             pStr           += "</p>";
             chapterContent += pStr;
         }
         content = content.Replace("___CONTENT___", chapterContent);
         File.WriteAllText(path, content);
     }
 }
        private void Backgroundworker_download_DoWork(object sender, DoWorkEventArgs e)
        {
            var                chapters       = mChapers;
            var                pb             = progressbar_download;
            var                label          = label_downloadinfo;
            string             savePath       = e.Argument.ToString();
            List <ChapterInfo> chaperInfoList = new List <ChapterInfo>();

            for (int i = 0; i < chapters.Length; i++)
            {
                if (backgroundworker_download.CancellationPending)
                {
                    return;
                }
                var    chapter  = chapters[i];
                float  progress = (i + 1) / (float)chapters.Length;
                string info     = string.Format("正在下载:{0} {1}/{2} {3:F2}%", chapter.title, i + 1, chapters.Length + 1,
                                                progress * 100);
                backgroundworker_download.ReportProgress(i, info);

                while (true)
                {
                    bool   downloadSucess = false;
                    string errMsg         = "";
                    for (int j = 0; j < 3; j++)
                    {
                        try {
                            var chapterInfo = LibZhuiShu.getChapter(chapter.link);
                            if (chapterInfo != null)
                            {
                                chaperInfoList.Add(chapterInfo);
                                downloadSucess = true;
                                break;
                            }
                        } catch (Exception ex) {
                            errMsg = ex.Message;
                            loglibrary.LogHelper.Error(ex);
                            loglibrary.LogHelper.Flush();
                        }
                    }
                    if (!downloadSucess)
                    {
                        var result = MessageBox.Show(errMsg, "章节 " + chapter.title + " 下载失败", MessageBoxButtons.AbortRetryIgnore);
                        if (result == DialogResult.Abort)
                        {
                            return;
                        }
                        else if (result == DialogResult.Ignore)
                        {
                            var emptyChaper = new ChapterInfo()
                            {
                                title = chapter.title,
                                body  = "本章下载失败了,失败原因:\n " + errMsg
                            };
                            chaperInfoList.Add(emptyChaper);
                            downloadSucess = true;
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            backgroundworker_download.ReportProgress(chapters.Length, "正在生成电子书请稍后....");
            string ext  = Path.GetExtension(savePath);
            Book2  book = new Book2()
            {
                name   = mBook.title,
                author = mBook.author,
                id     = mBook._id
            };
            var result2 = Lib2.API.Convert(chaperInfoList);

            book.chapters   = result2;
            Global.env.book = book;
            if (ext.ToLower() == ".txt")
            {
                Kindlegen.Book2Txt(book, savePath);
            }
            else if (ext.ToLower() == ".mobi")
            {
                Kindlegen.Book2Mobi(book, savePath);
            }
            MessageBox.Show("下载完成,文件保存在:" + savePath);
            if (checkBox1.Checked)
            {
                try
                {
                    KindleSender.Service.Program.Main();
                    Configuration config = new Configuration()
                    {
                        SmtpServer   = textBox4.Text,
                        SmtpPort     = 25,
                        SmtpPassword = textBox3.Text,
                        SmtpUserName = textBox5.Text,
                        KindleMail   = textBox1.Text,
                        FolderPath   = savePath.Replace(Path.GetFileName(savePath), string.Empty)
                    };
                    Global.env.config = config;
                    FileSender sen = new FileSender(config);
                    sen.Send(savePath);
                    MessageBox.Show("已完成!");
                }
                catch (Exception ee)
                {
                    loglibrary.LogHelper.Error(ee);
                    loglibrary.LogHelper.Flush();
                    MessageBox.Show("错误!\n 错误信息为:" + ee.Message);
                }
            }
        }
示例#9
0
        static void CreateStyle(Book2 book)
        {
            string path = "./tmp/style.css";

            File.WriteAllText(path, tpl_style);
        }