public GenEpub() { TextEpubItemFile t = epub.GetFile <TextEpubItemFile>("OEBPS/Text/template.xhtml"); xhtml_temp = t.text; epub.items.Remove(t); }
static void Save(TextEpubItemFile i, string dir) { string p = dir + "/" + i.fullName; File.WriteAllText(p, i.text); Log.Note("Saved:" + p); }
void GetCss() { var css = Directory.GetFiles(dir, "*.css"); if (css.Length > 0) { TextEpubItemFile cssi = epub.GetFile <TextEpubItemFile>("OEBPS/Styles/Style.css"); cssi.text += "\r\n\r\n" + File.ReadAllText(css[0]); Log.Info("Css added:" + css[0]); } }
public GenEpub(ChineseConvertOption cc_option = ChineseConvertOption.None) { this.cc_option = cc_option; if (cc_option == ChineseConvertOption.T2S) { Log.Note("Chinese Convert: T2S"); cc = new ChineseConvert(); cc.Prepare(); } TextEpubItemFile t = epub.GetFile <TextEpubItemFile>("OEBPS/Text/template.xhtml"); xhtml_temp = t.text; epub.items.Remove(t); }
public static TextEpubItemFile[] KakuyomuWork(string url) { Log.Info("Kakuyomu Work"); string raw = GetSource(url); string title = (new XFragment(raw, raw.IndexOf("<h1 id=\"workTitle\">"))).root.childs[0].innerXHTML; Log.Info("" + title); XFragment toc = new XFragment(raw, raw.IndexOf("<div id=\"table-of-contents\">")); var list = toc.root.childs[0].childs[1].childs[0]; TextEpubItemFile[] xhtmls = new TextEpubItemFile[list.childs.Count]; for (int i = 0; i < xhtmls.Length; i++) { xhtmls[i] = KakuyomuEpisode("https://kakuyomu.jp" + list.childs[i].childs[0].tag.GetAttribute("href")); xhtmls[i].fullName = $"[EP{Util.Number(i + 1)}]" + xhtmls[i].fullName; } return(xhtmls); }
public EpubFile Gen(string dir) { if (cc_option == ChineseConvertOption.T2S) { Log.Note("Chinese Convert: T2S"); cc = new ChineseConvert(); cc.Prepare(); } if (!indentAdjust) { Log.Note("Option: No indent adjustion."); } if (!addInfo) { Log.Note("Qption: Do not add generation info."); } this.dir = dir; string metaPath = Path.Combine(dir, "meta.txt"); if (File.Exists(Path.Combine(dir, "meta3.txt"))) { metaPath = Path.Combine(dir, "meta3.txt"); version = "3.0"; xhtml_temp = Regex.Replace(xhtml_temp, "<!DOCTYPE html([\\s\\S]*?)>", "<!DOCTYPE html>"); } if (File.Exists(Path.Combine(dir, "macros.txt"))) { Log.Info("Read macros.txt"); string[] macros_raw = File.ReadAllLines(Path.Combine(dir, "macros.txt")); macros = new Dictionary <string, string>(); foreach (string macro in macros_raw) { string[] s = macro.Split('\t'); if (s.Length < 2) { Log.Warn("Macro defination is not complete. Use tab to separate: " + macro); } macros.Add(s[0], s[1]); } } string meta = File.ReadAllText(metaPath); meta = meta.Replace("{urn:uuid}", uid); uid = Regex.Match(meta, "<dc:identifier id=\"BookId\">(.*?)</dc:identifier>").Groups[1].Value; meta = meta.Replace("{date}", DateTime.Today.ToString("yyyy-MM-ddT00:00:00Z")); if (cc != null) { meta = cc.Convert(meta); } if (cc_option == ChineseConvertOption.T2S) { meta = meta.Replace("<dc:language>zh-tw</dc:language>", "<dc:language>zh</dc:language>", true, null); } title = Regex.Match(meta, "<dc:title.*?>(.*?)</dc:title>").Groups[1].Value; GenFileNames(); GenContent(); GetImage(); GetCss(); TextEpubItemFile toc = epub.GetFile <TextEpubItemFile>("OEBPS/toc.ncx"); TextEpubItemFile nav = epub.GetFile <TextEpubItemFile>("OEBPS/nav.xhtml"); if (version == "2.0") { epub.items.Remove(nav); var tocDocuments = GenTOC(File.ReadAllLines(Path.Combine(dir, "toc.txt")), uid, title, toc.text); toc.text = tocDocuments.Item1; } else { var tocDocuments = GenTOC(File.ReadAllLines(Path.Combine(dir, "toc.txt")), uid, title, toc.text, nav.text); toc.text = tocDocuments.Item1; nav.text = tocDocuments.Item2; items += " <item id=\"nav.xhtml\" href=\"nav.xhtml\" media-type=\"application/xhtml+xml\" properties=\"nav\"/>"; } TextEpubItemFile opf = epub.GetFile <TextEpubItemFile>("OEBPS/content.opf"); opf.text = string.Format(opf.text, meta, items, spine, version); epub.ReadMeta(); return(epub); }
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] + ")"); } }
static void ProcXHTML(TextEpubItemFile i) { Log.Note("Process " + i.fullName); string name = Path.GetFileNameWithoutExtension(i.fullName); string r = i.text.Replace("\r", "").Replace("\n", ""); Match m = Regex.Match(r, "<body(.*)</body>"); if (!m.Success) { Log.Error("body?"); return; } r = m.Groups[0].Value; XFragment f = new XFragment(r, 0); string txt = ""; string counter = ""; foreach (var p in f.parts) { if (p.GetType() == typeof(XText)) { string trimed = Util.Trim(p.originalText); txt += trimed; counter += trimed; continue; } if (p.GetType() == typeof(XTag)) { XTag p0 = (XTag)p; if (p.type == PartType.tag_start) { switch (p0.tagname) { case "h1": case "h2": case "h3": case "h4": case "h5": case "h6": txt += "[" + p0.tagname + "]"; continue; case "body": continue; } } if (p.type == PartType.tag_end) { switch (p0.tagname) { case "h1": case "h2": case "h3": case "h4": case "h5": case "h6": txt += "[/" + p0.tagname + "]\r\n"; continue; case "body": continue; } } if (p.type == PartType.tag_end && p0.tagname == "div") { txt += "</div>\r\n"; continue; } if (p.type == PartType.tag_start && p0.tagname == "p") { continue; } if (p.type == PartType.tag_end && p0.tagname == "p") { txt += "\r\n"; continue; } txt += p0.originalText; } } if (Util.Trim(counter).Length > 0) { File.WriteAllText(output_dir + name + ".txt", txt); } }