From 12693cbe0ebf16a8a3be1e4d72e3802b54c918ee Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Thu, 24 Jan 2019 10:30:39 -0500 Subject: output_xmls epub use struct --- src/doc_reform/output/epub3.d | 204 +++++++++++++++++++----------------------- src/doc_reform/output/xmls.d | 35 +++++++- 2 files changed, 125 insertions(+), 114 deletions(-) (limited to 'src') diff --git a/src/doc_reform/output/epub3.d b/src/doc_reform/output/epub3.d index 1df1216..c94c240 100644 --- a/src/doc_reform/output/epub3.d +++ b/src/doc_reform/output/epub3.d @@ -300,14 +300,25 @@ template outputEPub3() { mixin DocReformOutputRgxInit; auto xhtml_format = outputXHTMLs(); auto rgx = Rgx(); - string[][string] doc_epub3; - string[][string] doc_epub3_endnotes; string[] doc; string segment_filename; string[] top_level_headings = ["","","",""]; string[string] oepbs_content_parts; string suffix = ".xhtml"; - string[] doc_parts_; + struct writeOut { /+ epub specific documents +/ + /+ fixed output +/ + string mimetypes; + string meta_inf_container_xml; + string oebps_toc_ncx; + string oebps_toc_nav_xhtml; + /+ variable output +/ + string oebps_content_opf; + string[][string] doc_epub3; + string[][string] doc_epub3_endnotes; + string[] doc_parts; + } + auto epubWrite = writeOut(); + // writeln(doc_matters.xml.keys_seq.seg); foreach (part; doc_matters.xml.keys_seq.seg) { foreach (obj; doc_abstraction[part]) { string _txt = xhtml_format.special_characters(obj); @@ -336,25 +347,25 @@ template outputEPub3() { top_level_headings[3] = ""; goto default; default: - doc_parts_ ~= obj.tags.segment_anchor_tag_epub; - doc_epub3[obj.tags.segment_anchor_tag_epub] ~= xhtml_format.epub3_seg_head(doc_matters); + epubWrite.doc_parts ~= obj.tags.segment_anchor_tag_epub; + epubWrite.doc_epub3[obj.tags.segment_anchor_tag_epub] ~= xhtml_format.epub3_seg_head(doc_matters); auto t = xhtml_format.heading_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[obj.tags.segment_anchor_tag_epub] ~= t[0]; - doc_epub3_endnotes[obj.tags.segment_anchor_tag_epub] ~= t[1]; + epubWrite.doc_epub3[obj.tags.segment_anchor_tag_epub] ~= t[0]; + epubWrite.doc_epub3_endnotes[obj.tags.segment_anchor_tag_epub] ~= t[1]; break; } break; case 4: segment_filename = obj.tags.segment_anchor_tag_epub; - doc_epub3[segment_filename] ~= xhtml_format.epub3_seg_head(doc_matters); + epubWrite.doc_epub3[segment_filename] ~= xhtml_format.epub3_seg_head(doc_matters); auto t = xhtml_format.heading_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case 5: .. case 7: auto t = xhtml_format.heading_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case 8: .. case 9: { /+ debug +/ @@ -383,8 +394,8 @@ template outputEPub3() { switch (obj.metainfo.is_a) { case "toc": auto t = xhtml_format.para_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; default: { /+ debug +/ @@ -412,8 +423,8 @@ template outputEPub3() { switch (obj.metainfo.is_a) { case "para": auto t = xhtml_format.para_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; default: { /+ debug +/ @@ -429,32 +440,32 @@ template outputEPub3() { switch (obj.metainfo.is_a) { case "quote": auto t = xhtml_format.quote_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0].to!string; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0].to!string; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case "group": auto t = xhtml_format.group_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0].to!string; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0].to!string; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case "block": auto t = xhtml_format.block_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0].to!string; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0].to!string; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case "poem": break; case "verse": auto t = xhtml_format.verse_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0].to!string; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0].to!string; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case "code": - doc_epub3[segment_filename] ~= xhtml_format.code(obj, _txt); + epubWrite.doc_epub3[segment_filename] ~= xhtml_format.code(obj, _txt); break; case "table": - doc_epub3[segment_filename] ~= xhtml_format.table(obj, _txt); - doc_epub3_endnotes[segment_filename] ~= ""; + epubWrite.doc_epub3[segment_filename] ~= xhtml_format.table(obj, _txt); + epubWrite.doc_epub3_endnotes[segment_filename] ~= ""; break; default: { /+ debug +/ @@ -483,27 +494,27 @@ template outputEPub3() { switch (obj.metainfo.is_a) { case "endnote": assert(part == "endnotes"); auto t = xhtml_format.para_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; break; case "glossary": assert(part == "glossary"); auto t = xhtml_format.para_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case "bibliography": assert(part == "bibliography"); auto t = xhtml_format.para_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case "bookindex": assert(part == "bookindex"); auto t = xhtml_format.para_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; case "blurb": assert(part == "blurb"); auto t = xhtml_format.para_seg(doc_matters, obj, _txt, suffix, "epub"); - doc_epub3[segment_filename] ~= t[0]; - doc_epub3_endnotes[segment_filename] ~= t[1]; + epubWrite.doc_epub3[segment_filename] ~= t[0]; + epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1]; break; default: { /+ debug +/ @@ -585,48 +596,47 @@ template outputEPub3() { } } /+ epub specific documents +/ - auto mimetypes = epub3_mimetypes; - auto meta_inf_container_xml = epub3_container_xml; - auto oebps_toc_ncx = epub2_oebps_toc_ncx(doc_abstraction, doc_matters); - auto oebps_toc_nav_xhtml = epub3_oebps_toc_nav_xhtml(doc_abstraction, doc_matters); - auto oebps_content_opf = epub3_oebps_content(doc_abstraction, doc_matters, oepbs_content_parts); - epub3_write_output_files( - doc_matters, - doc_epub3, - doc_epub3_endnotes, - mimetypes, - meta_inf_container_xml, - oebps_toc_nav_xhtml, - oebps_toc_ncx, - oebps_content_opf, - doc_parts_, - ); + epubWrite.mimetypes = epub3_mimetypes; + epubWrite.meta_inf_container_xml = epub3_container_xml; + epubWrite.oebps_toc_ncx = epub2_oebps_toc_ncx(doc_abstraction, doc_matters); + epubWrite.oebps_toc_nav_xhtml = epub3_oebps_toc_nav_xhtml(doc_abstraction, doc_matters); + epubWrite.oebps_content_opf = epub3_oebps_content(doc_abstraction, doc_matters, oepbs_content_parts); + epubWrite.epub3_write_output_files(doc_matters); } - void epub3_write_output_files(M,D,E,Mt,Mic,Otnx,Otn,Oc)( - M doc_matters, - D doc_epub3, - E doc_epub3_endnotes, - Mt mimetypes, - Mic meta_inf_container_xml, - Otnx oebps_toc_nav_xhtml, - Otn oebps_toc_ncx, - Oc oebps_content_opf, - string[] doc_parts_, + void epub3_write_output_files(W,I)( + W epub_write, + I doc_matters, ) { debug(asserts) { - static assert(is(typeof(doc_epub3) == string[][string])); - static assert(is(typeof(mimetypes) == string)); - static assert(is(typeof(meta_inf_container_xml) == string)); - static assert(is(typeof(oebps_toc_nav_xhtml) == string)); - static assert(is(typeof(oebps_toc_ncx) == string)); - static assert(is(typeof(oebps_content_opf) == string)); + static assert(is(typeof(epub_write.doc_epub3) == string[][string])); + static assert(is(typeof(epub_write.mimetypes) == string)); + static assert(is(typeof(epub_write.meta_inf_container_xml) == string)); + static assert(is(typeof(epub_write.oebps_toc_nav_xhtml) == string)); + static assert(is(typeof(epub_write.oebps_toc_ncx) == string)); + static assert(is(typeof(epub_write.oebps_content_opf) == string)); } + static auto rgx = Rgx(); auto pth_epub3 = DocReformPathsEPUB!()(doc_matters.output_path, doc_matters.src.language); auto xhtml_format = outputXHTMLs(); /+ zip file +/ auto fn_epub = pth_epub3.epub_file(doc_matters.src.filename); auto zip = new ZipArchive(); // ZipArchive zip = new ZipArchive(); /+ zip archive member files +/ + void EPUBzip()(string contents, string fn) { + auto zip_arc_member_file = new ArchiveMember(); + zip_arc_member_file.name = fn; + auto zip_data = new OutBuffer(); + (doc_matters.opt.action.debug_do) + ? zip_data.write(contents.dup) + : zip_data.write(contents.dup + .replaceAll(rgx.spaces_line_start, "") + .replaceAll(rgx.newline, "") + .strip + ); + zip_arc_member_file.expandedData = zip_data.toBytes(); + zip.addMember(zip_arc_member_file); + createZipFile!()(fn_epub, zip.build()); + } try { if (!exists(pth_epub3.base)) { pth_epub3.base.mkdirRecurse; @@ -654,19 +664,19 @@ template outputEPub3() { if (doc_matters.opt.action.debug_do) { string fn_dbg = pth_epub3.dbg_fn_oebps_content_xhtml(doc_matters.src.filename, seg_filename); auto f = File(fn_dbg, "w"); - foreach (docseg; doc_epub3[seg_filename]) { + foreach (docseg; epub_write.doc_epub3[seg_filename]) { f.writeln(docseg); } - foreach (docseg; doc_epub3_endnotes[seg_filename]) { + foreach (docseg; epub_write.doc_epub3_endnotes[seg_filename]) { f.writeln(docseg); } f.writeln(xhtml_format.tail); } } - foreach (docseg; doc_epub3[seg_filename]) { + foreach (docseg; epub_write.doc_epub3[seg_filename]) { zip_data.write(docseg.dup); } - foreach (docseg; doc_epub3_endnotes[seg_filename]) { + foreach (docseg; epub_write.doc_epub3_endnotes[seg_filename]) { zip_data.write(docseg.dup); } zip_data.write(xhtml_format.tail.dup); @@ -683,81 +693,51 @@ template outputEPub3() { { /+ debug +/ if (doc_matters.opt.action.debug_do) { fn_dbg = pth_epub3.dbg_fn_mimetypes(doc_matters.src.filename); - File(fn_dbg, "w").writeln(mimetypes); + File(fn_dbg, "w").writeln(epub_write.mimetypes); } } fn = pth_epub3.fn_mimetypes(doc_matters.src.filename); - auto zip_arc_member_file = new ArchiveMember(); - zip_arc_member_file.name = fn; - auto zip_data = new OutBuffer(); - zip_data.write(mimetypes.dup); - zip_arc_member_file.expandedData = zip_data.toBytes(); - zip.addMember(zip_arc_member_file); - createZipFile!()(fn_epub, zip.build()); + EPUBzip(epub_write.mimetypes, fn); } { /+ META-INF/container.xml (identify doc root) +/ { /+ debug +/ if (doc_matters.opt.action.debug_do) { fn_dbg = pth_epub3.dbg_fn_dmi_container_xml(doc_matters.src.filename); - File(fn_dbg, "w").writeln(meta_inf_container_xml); + File(fn_dbg, "w").writeln(epub_write.meta_inf_container_xml); } } fn = pth_epub3.fn_dmi_container_xml(doc_matters.src.filename); - auto zip_arc_member_file = new ArchiveMember(); - zip_arc_member_file.name = fn; - auto zip_data = new OutBuffer(); - zip_data.write(meta_inf_container_xml.dup); - zip_arc_member_file.expandedData = zip_data.toBytes(); - zip.addMember(zip_arc_member_file); - createZipFile!()(fn_epub, zip.build()); + EPUBzip(epub_write.meta_inf_container_xml, fn); } { /+ OEBPS/toc_nav.xhtml (navigation toc epub3) +/ { /+ debug +/ if (doc_matters.opt.action.debug_do) { fn_dbg = pth_epub3.dbg_fn_oebps_toc_nav_xhtml(doc_matters.src.filename); - File(fn_dbg, "w").writeln(oebps_toc_nav_xhtml); + File(fn_dbg, "w").writeln(epub_write.oebps_toc_nav_xhtml); } } fn = pth_epub3.fn_oebps_toc_nav_xhtml(doc_matters.src.filename); - auto zip_arc_member_file = new ArchiveMember(); - zip_arc_member_file.name = fn; - auto zip_data = new OutBuffer(); - zip_data.write(oebps_toc_nav_xhtml.dup); - zip_arc_member_file.expandedData = zip_data.toBytes(); - zip.addMember(zip_arc_member_file); - createZipFile!()(fn_epub, zip.build()); + EPUBzip(epub_write.oebps_toc_nav_xhtml, fn); } { /+ OEBPS/toc.ncx (navigation toc epub2) +/ { /+ debug +/ if (doc_matters.opt.action.debug_do) { fn_dbg = pth_epub3.dbg_fn_oebps_toc_ncx(doc_matters.src.filename); - File(fn_dbg, "w").writeln(oebps_toc_ncx); + File(fn_dbg, "w").writeln(epub_write.oebps_toc_ncx); } } fn = pth_epub3.fn_oebps_toc_ncx(doc_matters.src.filename); - auto zip_arc_member_file = new ArchiveMember(); - zip_arc_member_file.name = fn; - auto zip_data = new OutBuffer(); - zip_data.write(oebps_toc_ncx.dup); - zip_arc_member_file.expandedData = zip_data.toBytes(); - zip.addMember(zip_arc_member_file); - createZipFile!()(fn_epub, zip.build()); + EPUBzip(epub_write.oebps_toc_ncx, fn); } { /+ OEBPS/content.opf (doc manifest) +/ { /+ debug +/ if (doc_matters.opt.action.debug_do) { fn_dbg = pth_epub3.dbg_fn_oebps_content_opf(doc_matters.src.filename); - File(fn_dbg, "w").writeln(oebps_content_opf); + File(fn_dbg, "w").writeln(epub_write.oebps_content_opf); } } fn = pth_epub3.fn_oebps_content_opf(doc_matters.src.filename); - auto zip_arc_member_file = new ArchiveMember(); - zip_arc_member_file.name = fn; - auto zip_data = new OutBuffer(); - zip_data.write(oebps_content_opf.dup); - zip_arc_member_file.expandedData = zip_data.toBytes(); - zip.addMember(zip_arc_member_file); - createZipFile!()(fn_epub, zip.build()); + EPUBzip(epub_write.oebps_content_opf, fn); } { /+ OEBPS/_sisu/image (images) +/ foreach (image; doc_matters.srcs.image_list) { diff --git a/src/doc_reform/output/xmls.d b/src/doc_reform/output/xmls.d index 41787ae..fe02338 100644 --- a/src/doc_reform/output/xmls.d +++ b/src/doc_reform/output/xmls.d @@ -379,8 +379,7 @@ template outputXHTMLs() { rgx.inline_link_hash, "┥$1┝┤" ~ doc_matters.xml.tag_associations[(m.captures[3])][seg_lvs] - ~ _suffix - ~ "#" ~ "$3" + ~ _suffix ~ "#" ~ "$3" ~ "├" ); } @@ -627,6 +626,10 @@ template outputXHTMLs() { string _txt, string _xml_type = "html", ) { + assert(obj.metainfo.is_of_part == "body" || "frontmatter" || "backmatter"); + assert(obj.metainfo.is_of_section == "body" || "toc" || "endnotes" || "glossary" || "bibliography" || "bookindex" || "blurb"); + assert(obj.metainfo.is_of_type == "para"); + assert(obj.metainfo.is_a == "heading"); auto tags = _xhtml_anchor_tags(obj); string heading_lev_anchor_tag; string _horizontal_rule = "
"; @@ -709,6 +712,10 @@ template outputXHTMLs() { const O obj, string _txt, ) { + assert(obj.metainfo.is_of_part == "body" || "frontmatter" || "backmatter"); + assert(obj.metainfo.is_of_section == "body" || "toc" || "endnotes" || "glossary" || "bibliography" || "bookindex" || "blurb"); + assert(obj.metainfo.is_of_type == "para"); + assert(obj.metainfo.is_a == "para" || "toc" || "endnotes" || "glossary" || "bibliography" || "bookindex" || "blurb"); auto tags = _xhtml_anchor_tags(obj); _txt = font_face(_txt); string o; @@ -778,6 +785,10 @@ template outputXHTMLs() { const O obj, string _txt, ) { + assert(obj.metainfo.is_of_part == "body"); + assert(obj.metainfo.is_of_section == "body" || "glossary" || "bibliography" || "bookindex" || "blurb"); + assert(obj.metainfo.is_of_type == "block"); + assert(obj.metainfo.is_a == "quote"); _txt = font_face(_txt); string o; if (!(obj.metainfo.identifier.empty)) { @@ -837,6 +848,10 @@ template outputXHTMLs() { const O obj, string _txt, ) { + assert(obj.metainfo.is_of_part == "body"); + assert(obj.metainfo.is_of_section == "body" || "glossary" || "bibliography" || "bookindex" || "blurb"); + assert(obj.metainfo.is_of_type == "block"); + assert(obj.metainfo.is_a == "group"); _txt = font_face(_txt); string o; if (!(obj.metainfo.identifier.empty)) { @@ -897,6 +912,10 @@ template outputXHTMLs() { const O obj, string _txt, ) { + assert(obj.metainfo.is_of_part == "body"); + assert(obj.metainfo.is_of_section == "body" || "glossary" || "bibliography" || "bookindex" || "blurb"); + assert(obj.metainfo.is_of_type == "block"); + assert(obj.metainfo.is_a == "block"); _txt = font_face(_txt); string o; if (!(obj.metainfo.identifier.empty)) { @@ -953,6 +972,10 @@ template outputXHTMLs() { const O obj, string _txt, ) { + assert(obj.metainfo.is_of_part == "body"); + assert(obj.metainfo.is_of_section == "body" || "glossary" || "bibliography" || "bookindex" || "blurb"); + assert(obj.metainfo.is_of_type == "block"); + assert(obj.metainfo.is_a == "verse"); _txt = font_face(_txt); string o; if (!(obj.metainfo.identifier.empty)) { @@ -1042,6 +1065,10 @@ template outputXHTMLs() { const O obj, string _txt, ) { + assert(obj.metainfo.is_of_part == "body"); + assert(obj.metainfo.is_of_section == "body"); + assert(obj.metainfo.is_of_type == "block"); + assert(obj.metainfo.is_a == "table"); auto tags = _xhtml_anchor_tags(obj); _txt = font_face(_txt); auto t = tablarize(obj, _txt); @@ -1086,6 +1113,10 @@ template outputXHTMLs() { const O obj, string _txt, ) { + assert(obj.metainfo.is_of_part == "body"); + assert(obj.metainfo.is_of_section == "body"); + assert(obj.metainfo.is_of_type == "block"); + assert(obj.metainfo.is_a == "code"); string o; if (!(obj.metainfo.identifier.empty)) { o = format(q"¶
-- cgit v1.2.3