template outputEPub() {
private import
std.algorithm,
std.array,
std.container,
std.exception,
std.file,
std.getopt,
std.json,
std.process,
std.stdio,
std.path,
std.range,
std.regex,
std.string,
std.traits,
std.typecons,
std.uni,
std.utf,
ao_defaults;
import
ao_rgx,
output_xhtmls;
mixin InternalMarkup;
mixin outputXHTMLs;
string epub_mimetypes() {
string o;
o = format(q"¶application/epub+zip¶");
return o;
}
string epub_container_xml() {
string o;
o = format(q"¶
¶");
return o;
}
string epub_oebps_content(C,M)(C contents, M doc_matters) {
string uuid = "18275d951861c77f78acd05672c9906924c59f18a2e0ba06dad95959693e9bd8"; // TODO shared elsewhere
string content = format(q"¶
%s
%s
en
%s
Copyright: %s
ox/current/en/epub/sisu_markup.epub
urn:uuid:%s
¶",
uuid,
doc_matters.dochead_meta["title"]["full"], // title
(doc_matters.dochead_meta["creator"]["author"].empty) ? "" : " by " ~ doc_matters.dochead_meta["creator"]["author"], // author
(doc_matters.dochead_meta["creator"]["author"].empty) ? "" : " by " ~ doc_matters.dochead_meta["creator"]["author"], // author
(doc_matters.dochead_meta["date"]["published"].empty) ? "" : " by " ~ doc_matters.dochead_meta["date"]["published"], // date
(doc_matters.dochead_meta["rights"]["copyright"].empty) ? "" : " by " ~ doc_matters.dochead_meta["rights"]["copyright"], // rights
uuid,
uuid,
);
foreach (sect; doc_matters.keys_seq_seg) {
foreach (obj; contents[sect]) {
}
}
return content;
}
string epub_oebps_toc(C,M)(C contents, M doc_matters) {
int counter = 0;
string uuid = "18275d951861c77f78acd05672c9906924c59f18a2e0ba06dad95959693e9bd8"; // TODO shared elsewhere
auto markup = InlineMarkup();
enum DomTags { none, open, close, close_and_open, open_still, }
string toc = format(q"¶
%s%s
%s
%s
¶",
doc_matters.dochead_meta["title"]["full"], // title
(doc_matters.dochead_meta["creator"]["author"].empty) ? "" : " by " ~ doc_matters.dochead_meta["creator"]["author"], // author
uuid, // uuid
"3", // content depth
doc_matters.dochead_meta["title"]["full"], // title
(doc_matters.dochead_meta["creator"]["author"].empty) ? "" : doc_matters.dochead_meta["creator"]["author"], // author
);
foreach (sect; doc_matters.keys_seq_seg) {
foreach (obj; contents[sect]) {
if (obj.is_a == "heading") {
foreach_reverse (k; 0 .. 7) {
switch (obj.dom_markedup[k]) {
case DomTags.close :
toc ~= "";
break;
case DomTags.close_and_open :
++counter;
toc ~= "";
toc ~= format(q"¶
%s
¶",
counter,
obj.text,
obj.segment_anchor_tag, // lev < 4 [no link]; lev == 4 [filename] markup.xhtml; lev > 4 [filename#ocn] (links done in segment_anchor_tag)
);
break;
case DomTags.open :
++counter;
toc ~= format(q"¶
%s
¶",
counter,
obj.text,
obj.segment_anchor_tag, // lev < 4 [no link]; lev == 4 [filename] markup.xhtml; lev > 4 [filename#ocn] (fix links in segment_anchor_tag)
);
break;
default :
break;
}
}
}
}
}
toc ~= format(q"¶
¶");
return toc;
}
void outputEPub(C,T)(
auto ref const C contents,
auto ref T doc_matters,
) {
mixin SiSUrgxInit;
auto xhtml_format = outputXHTMLs();
auto rgx = Rgx();
// string[] toc;
string[][string] doc_epub;
string[] doc;
string segment_filename;
string[] top_level_headings = ["","","",""];
auto mimetypes = epub_mimetypes;
auto meta_inf_container_xml = epub_container_xml;
auto oebps_toc_ncx = epub_oebps_toc(contents, doc_matters);
auto oebps_content_opf = epub_oebps_content(contents, doc_matters);
foreach (part; doc_matters.keys_seq_seg) {
foreach (obj; contents[part]) {
if (obj.is_a == "heading") {
switch (obj.heading_lev_markup) {
case 0: .. case 3:
/+ fill buffer, and replace with new levels from 1 to 3 +/
switch (obj.heading_lev_markup) {
case 0:
top_level_headings[0] = "";
top_level_headings[1] = "";
top_level_headings[2] = "";
top_level_headings[3] = "";
goto default;
case 1:
top_level_headings[1] = "";
top_level_headings[2] = "";
top_level_headings[3] = "";
goto default;
case 2:
top_level_headings[2] = "";
top_level_headings[3] = "";
goto default;
case 3:
top_level_headings[3] = "";
goto default;
default:
top_level_headings[obj.heading_lev_markup] = xhtml_format.heading(obj);
break;
}
break;
case 4:
segment_filename = obj.segment_anchor_tag;
doc_epub[segment_filename] ~= xhtml_format.seg_head(doc_matters.dochead_meta);
foreach (top_level_heading; top_level_headings) {
doc_epub[segment_filename] ~= top_level_heading;
}
doc_epub[segment_filename] ~= xhtml_format.heading(obj);
break;
case 5: .. case 7:
doc_epub[segment_filename] ~= xhtml_format.heading(obj);
break;
default:
if ((doc_matters.opt_action_bool["debug"])) {
writeln(__FILE__, ":", __LINE__, ": ", obj.is_a);
}
break;
}
} else if (obj.use == "frontmatter") {
switch (obj.is_of) {
case "para":
switch (obj.is_a) {
case "toc":
doc_epub[segment_filename] ~= xhtml_format.toc(obj);
break;
default:
// writeln(__FILE__, ":", __LINE__, ": ", obj.is_a);
break;
}
break;
default:
// writeln(__FILE__, ":", __LINE__, ": ", obj.is_a);
break;
}
} else if (obj.use == "body") {
switch (obj.is_of) {
case "para":
switch (obj.is_a) {
case "para":
doc_epub[segment_filename] ~= xhtml_format.para(obj);
break;
default:
// writeln(__FILE__, ":", __LINE__, ": ", obj.is_a);
break;
}
break;
case "block":
switch (obj.is_a) {
case "poem": // double check why both poem & verse
break;
case "verse":
doc_epub[segment_filename] ~= xhtml_format.nugget(obj);
break;
case "group":
doc_epub[segment_filename] ~= xhtml_format.nugget(obj);
break;
case "block":
doc_epub[segment_filename] ~= xhtml_format.nugget(obj);
break;
case "quote":
doc_epub[segment_filename] ~= xhtml_format.nugget(obj);
break;
case "table":
doc_epub[segment_filename] ~= xhtml_format.para(obj); //
break;
case "code":
doc_epub[segment_filename] ~= xhtml_format.code(obj);
break;
default:
if ((doc_matters.opt_action_bool["debug"])) {
writeln(__FILE__, ":", __LINE__, ": ", obj.is_a);
}
break;
}
break;
default:
// writeln(__FILE__, ":", __LINE__, ": ", obj.is_of);
break;
}
} else if (obj.use == "backmatter") {
switch (obj.is_of) {
case "para":
switch (obj.is_a) {
case "endnote":
doc_epub[segment_filename] ~= xhtml_format.endnote(obj);
break;
case "glossary":
doc_epub[segment_filename] ~= xhtml_format.para(obj);
break;
case "bibliography":
doc_epub[segment_filename] ~= xhtml_format.para(obj);
break;
case "bookindex":
doc_epub[segment_filename] ~= xhtml_format.para(obj);
break;
case "blurb":
doc_epub[segment_filename] ~= xhtml_format.para(obj);
break;
default:
// writeln(__FILE__, ":", __LINE__, ": ", obj.is_a);
break;
}
break;
default:
if ((doc_matters.opt_action_bool["debug"])) {
writeln(__FILE__, ":", __LINE__, ": ", obj.is_a);
}
break;
}
}
}
}
epub_write_output_files(
doc_matters,
doc_epub,
mimetypes,
meta_inf_container_xml,
oebps_toc_ncx,
oebps_content_opf,
);
}
void epub_write_output_files(C,EpD,Mt,Mic,Ot,Oc)(
C doc_matters,
EpD doc_epub,
Mt mimetypes,
Mic meta_inf_container_xml,
Ot oebps_toc_ncx,
Oc oebps_content_opf,
) {
debug(asserts){
static assert(is(typeof(doc_epub) == string[][string]));
static assert(is(typeof(mimetypes) == string));
static assert(is(typeof(meta_inf_container_xml) == string));
static assert(is(typeof(oebps_toc_ncx) == string));
static assert(is(typeof(oebps_content_opf) == string));
}
mixin SiSUpaths;
auto pth_epub = EpubPaths();
auto xhtml_format = outputXHTMLs();
try {
if (!exists(pth_epub.doc_meta_inf(doc_matters.source_filename))) {
mkdirRecurse(pth_epub.doc_meta_inf(doc_matters.source_filename));
}
if (!exists(pth_epub.doc_oebps_css(doc_matters.source_filename))) {
mkdirRecurse(pth_epub.doc_oebps_css(doc_matters.source_filename));
}
if (!exists(pth_epub.doc_oebps_image(doc_matters.source_filename))) {
mkdirRecurse(pth_epub.doc_oebps_image(doc_matters.source_filename));
}
/+ OEBPS/[segments].xhtml +/
foreach (seg_filename; doc_matters.segnames) {
auto f = File(pth_epub.fn_oebps_content_xhtml(doc_matters.source_filename, seg_filename), "w");
/+ // f.writeln(seg_head); // not needed built and inserted earlier +/
foreach (docseg; doc_epub[seg_filename]) {
f.writeln(docseg);
}
f.writeln(xhtml_format.tail); // needed for each lev4
}
/+ mimetypes +/
auto f = File(pth_epub.fn_mimetypes(doc_matters.source_filename), "w");
f.writeln(mimetypes);
/+ META-INF/container.xml +/
f = File(pth_epub.fn_dmi_container_xml(doc_matters.source_filename), "w");
f.writeln(meta_inf_container_xml);
/+ OEBPS/toc.ncx +/
f = File(pth_epub.fn_oebps_toc_ncx(doc_matters.source_filename), "w");
f.writeln(oebps_toc_ncx);
/+ OEBPS/content.opf +/
f = File(pth_epub.fn_oebps_content_opf(doc_matters.source_filename), "w");
f.writeln(oebps_content_opf);
}
catch (ErrnoException ex) {
// Handle error
}
}
}