void GenContent() { GenHtml genHtml = new GenHtml(this); string patchfile_path = Path.Combine(dir, "patch_t2s/patch.csv"); string[] patchs = new string[0]; if (cc_option == ChineseConvertOption.T2S && File.Exists(patchfile_path)) { patchs = File.ReadAllLines(patchfile_path); } for (int i = 0; i < txt_nums.Count; i++) { string f = txt_paths[i]; string[] lines = File.ReadAllLines(f); if (cc != null) { for (int j = 0; j < lines.Length; j++) { lines[j] = cc.Convert(lines[j]); } foreach (var patch in patchs) { string[] xx = patch.Split(','); if (xx[0] == txt_nums[i]) { int line_num; if ( int.TryParse(xx[1], out line_num) && line_num <= lines.Length && line_num > 0 ) { string target = cc.Convert(xx[2]); int c = Util.CountMatches(lines[line_num - 1], target); lines[line_num - 1] = lines[line_num - 1].Replace(target, xx[3]); if (c == 0) { Log.Warn("Cannot Find cc patch target:" + xx[2]); } else { Log.Info(string.Format("CC patched {0} times for {1}", c, xx[2])); } } else { Log.Warn("Bad Line Number:" + xx[1]); } } } } string body = genHtml.Gen(lines); if (f.EndsWith("info.txt") || f.EndsWith("info.atxt")) { body = Regex.Replace(body, "<p>(.*?:)", "<p class=\"atxt_keyvalue\">$1"); body = "<div class=\"atxt_info\" epub:type=\"acknowledgements\">" + body; if (addInfo) { body += "<p>AeroNovelTool EPUB生成器 by AE 生成于" + DateTime.Now + "</p>"; } body += "</div>"; } if (f.EndsWith("EOB.txt") || f.EndsWith("EOB.atxt")) { body = "<div class=\"atxt_info\">" + body + "</div>"; } string xhtml = xhtml_temp.Replace("{❤title}", title).Replace("{❤body}", body); TextEpubItemFile item = new TextEpubItemFile("OEBPS/Text/" + xhtml_names[i], xhtml); epub.items.Add(item); Log.Info("Add xhtml: " + item.fullName + " (title:" + txt_titles[i] + ")"); } }
public static Epub Gen(string dir) { Epub epub = new Epub("template.zip"); string uid = "urn:uuid:" + Guid.NewGuid().ToString(); string xhtml_temp; { TextItem t = epub.GetItem <TextItem>("OEBPS/Text/template.xhtml"); xhtml_temp = t.data; epub.items.Remove(t); } string spine = ""; string items = ""; GenHtml genHtml = new GenHtml(); string[] files = Directory.GetFiles(dir); foreach (string f in files) { Match m = Regex.Match(Path.GetFileName(f), AeroNovel.filename_reg); if (!m.Success) { continue; } string no = m.Groups[1].Value; string chaptitle = m.Groups[2].Value; string[] lines = File.ReadAllLines(f); string body = genHtml.Gen(lines); if (f.Contains("info.txt")) { body = "<div class=\"info\" epub:type=\"acknowledgements\">" + body + "<p>AeroNovelTool EPUB生成器by AE " + DateTime.Now + "</p>" + "<p>推荐使用阅读器:<br/>Apple Books<br/>Microsoft Edge<br/>Kindle(使用Kindlegen 转换)<br/></p>" + "</div>"; //File.WriteAllText("info.txt",body); } string xhtml = xhtml_temp.Replace("{❤title}", chaptitle).Replace("{❤body}", body); string name = "t" + no + ".xhtml"; if (Regex.Match(Path.GetFileNameWithoutExtension(f), "[a-zA-Z0-9]").Success) { name = "t" + Path.GetFileNameWithoutExtension(f) + ".xhtml"; } TextItem i = new TextItem("OEBPS/Text/" + name, xhtml); epub.items.Add(i); items += string.Format("<item id=\"{0}\" href=\"Text/{0}\" media-type=\"application/xhtml+xml\"/>\n", name); spine += string.Format("<itemref idref=\"{0}\"/>", name); Log.log("[Info]Proc Text No." + no + ": " + chaptitle); txt_nums.Add(no); txt_titles.Add(chaptitle); xhtml_names.Add(name); } string img = Path.Combine(dir, "Images"); if (Directory.Exists(img)) { foreach (var f in Directory.GetFiles(img)) { if (f.Contains(".jpg")) { NormalItem i = new NormalItem("OEBPS/Images/" + Path.GetFileName(f), File.ReadAllBytes(f)); epub.items.Add(i); items += string.Format("<item id=\"{0}\" href=\"Images/{0}\" media-type=\"image/jpeg\"/>\n", Path.GetFileName(f)); Log.log("[Info]Add image: " + f); } } } string meta = File.ReadAllText(Path.Combine(dir, "meta.txt")); meta = meta.Replace("{urn:uuid}", uid); string title = Regex.Match(meta, "<dc:title.*?>(.*?)</dc:title>").Groups[1].Value; TextItem toc = epub.GetItem <TextItem>("OEBPS/toc.ncx"); toc.data = GenTOC(File.ReadAllLines(Path.Combine(dir, "toc.txt")), uid, title, toc.data); TextItem opf = epub.GetItem <TextItem>("OEBPS/content.opf"); opf.data = string.Format(opf.data, meta, items, spine); epub.ReadMeta(); return(epub); }