/+
- Name: Spine, Doc Reform [a part of]
  - Description: documents, structuring, processing, publishing, search
    - static content generator

  - Author: Ralph Amissah
    [ralph.amissah@gmail.com]

  - Copyright: (C) 2015 - 2022 Ralph Amissah, All Rights
    Reserved.

  - License: AGPL 3 or later:

    Spine (SiSU), a framework for document structuring, publishing and
    search

    Copyright (C) Ralph Amissah

    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU AFERO General Public License as published by the
    Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
    more details.

    You should have received a copy of the GNU General Public License along with
    this program. If not, see [https://www.gnu.org/licenses/].

    If you have Internet connection, the latest version of the AGPL should be
    available at these locations:
    [https://www.fsf.org/licensing/licenses/agpl.html]
    [https://www.gnu.org/licenses/agpl.html]

  - Spine (by Doc Reform, related to SiSU) uses standard:
    - docReform markup syntax
      - standard SiSU markup syntax with modified headers and minor modifications
    - docReform object numbering
      - standard SiSU object citation numbering & system

  - Homepages:
    [https://www.doc_reform.org]
    [https://www.sisudoc.org]

  - Git
    [https://git.sisudoc.org/projects/?p=software/spine.git;a=summary]

+/
module doc_reform.io_out.epub3;
template outputEPub3() {
  import
    std.file,
    std.outbuffer,
    std.uri,
    std.zip,
    std.conv : to;
  import
    doc_reform.io_out,
    doc_reform.io_out.create_zip_file,
    doc_reform.io_out.xmls,
    doc_reform.io_out.xmls_css;
  mixin InternalMarkup;
  mixin outputXHTMLs;
  static auto rgx = RgxO();
  @safe string special_characters_text(string _txt) {
    _txt = _txt
      .replaceAll(rgx.xhtml_ampersand,    "&")  // "&"
      .replaceAll(rgx.xhtml_quotation,    """) // """
      .replaceAll(rgx.xhtml_less_than,    "<")   // "<"
      .replaceAll(rgx.xhtml_greater_than, ">")   // ">"
      .replaceAll(rgx.br_line,            "<br />")
      .replaceAll(rgx.br_line_inline,     "<br />")
      .replaceAll(rgx.br_line_spaced,     "<br />\n<br />")
      .replaceAll(rgx.nbsp_char,          " ");
    return _txt;
  }
  @safe string epub3_mimetypes() {
    string o;
    o = format(q"┃application/epub+zip┃") ~ "\n";
    return o;
  }
  @safe string epub3_container_xml() {
    string o;
    o = format(q"┃<?xml version="1.0" encoding="utf-8"?>┃") ~ "\n";
    o ~= format(q"┃<container version="1.0"
  xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  <rootfiles>
    <rootfile full-path="OEBPS/content.opf"
      media-type="application/oebps-package+xml" />
  </rootfiles>┃") ~ "\n</container>\n";
    return o;
  }
  @safe string epub3_oebps_content(D,M,P)(D doc_abstraction, M doc_matters, P parts) {
    auto xhtml_format = outputXHTMLs();
    auto pth_epub3 = spinePathsEPUB!()(doc_matters.output_path, doc_matters.src.language);
    string _uuid = "18275d951861c77f78acd05672c9906924c59f18a2e0ba06dad95959693e9bd8"; // TODO sort uuid in doc_matters!
    string content = format(q"┃<?xml version="1.0" encoding="utf-8"?>
  <package version="3.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="uid" prefix="rendition: http://www.idpf.org/vocab/rendition/#">
    <metadata
      xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
      xmlns:dcterms="https://purl.org/dc/terms/"
      xmlns:dc="https://purl.org/dc/elements/1.1/"
      unique-identifier="urn:uuid:%s" version="2.0">
      <dc:title id="title">%s</dc:title>
      <meta refines="#title" property="title-type">main</meta>
      <dc:title id="subtitle">%s</dc:title>
      <meta refines="#subtitle" property="title-type">subtitle</meta>
      <dc:creator file-as="%s" id="aut">%s</dc:creator>
      <dc:language>%s</dc:language>
      <dc:date id="published">%s</dc:date>
      <dc:rights>Copyright: %s</dc:rights>
      <dc:identifier scheme="URI">%s</dc:identifier>
      <dc:identifier id="bookid">urn:uuid:%s</dc:identifier>
    </metadata>
    <manifest>
      <item id="css" href="%s" media-type="text/css"/>
      <item id="nav" href="toc_nav.xhtml" media-type="application/xhtml+xml" properties="nav" />
  ┃",
      _uuid,
      xhtml_format.special_characters_text(doc_matters.conf_make_meta.meta.title_main),
      (doc_matters.conf_make_meta.meta.title_sub.empty)
        ? "" : xhtml_format.special_characters_text(doc_matters.conf_make_meta.meta.title_sub),
      (doc_matters.conf_make_meta.meta.creator_author.empty)
        ? "" : xhtml_format.special_characters_text(doc_matters.conf_make_meta.meta.creator_author),
      (doc_matters.conf_make_meta.meta.creator_author.empty)
        ? "" : xhtml_format.special_characters_text(doc_matters.conf_make_meta.meta.creator_author),
      doc_matters.src.language,                                   // language, fix (needed in dochead metadata)
      (doc_matters.conf_make_meta.meta.date_published.empty)
        ? "" : xhtml_format.special_characters_date(doc_matters.conf_make_meta.meta.date_published),
      (doc_matters.conf_make_meta.meta.rights_copyright.empty)
        ? "" : xhtml_format.special_characters_text(doc_matters.conf_make_meta.meta.rights_copyright),
      _uuid,
      _uuid,
      (pth_epub3.fn_oebps_css).chompPrefix("OEBPS/"),
    );
    content ~= parts["manifest_documents"];
    // TODO sort jpg & png
    foreach (image; doc_matters.srcs.image_list) {
      content ~= format(q"┃      <item id="%s" href="%s/%s" media-type="image/%s" />
  ┃",
        image.baseName.stripExtension,
        (pth_epub3.doc_oebps_image).chompPrefix("OEBPS/"),
        image,
        image.extension.chompPrefix("."),
      );
    }
    content ~= "  " ~ "</manifest>"         ~ "\n  ";
    content ~= "  " ~ "<spine>"             ~ "\n  ";
    content ~= parts["spine"];
    content ~= "  " ~ "</spine>"            ~ "\n  ";
    content ~= "  " ~ "<guide>"             ~ "\n  ";
    content ~= parts["guide"];
    content ~= "  " ~ "</guide>"            ~ "\n  ";
    content ~= ""   ~ "</package>";
    debug(epubmanifest) {
      foreach (part; doc_matters.has.keys_seq.seg) { // TODO
        foreach (obj; doc_abstraction[part]) {
          if (obj.metainfo.is_a == "heading") {
            if (obj.metainfo.heading_lev_markup == 4) {
              writefln(
                "%s~ [%s.xhtml] %s",
                obj.marked_up_level,
                obj.tags.segment_anchor_tag_epub,
                obj.text
              );
            } else if (obj.metainfo.heading_lev_markup > 4) {
              writefln(
                "%s~ [%s.xhtml#%s] %s",
                obj.marked_up_level,
                obj.tags.segment_anchor_tag_epub,
                obj.metainfo.object_number,
                obj.text
              );
            }
          }
        }
      }
    }
    return content;
  }
  @safe string epub3_oebps_toc_nav_xhtml(D,I)(D doc_abstraction, I doc_matters) {
    enum DomTags { none, open, close, close_and_open, open_still, }
    auto markup = InlineMarkup();
    static auto rgx = RgxO();
    string toc;
    bool _new_title_set = false;
    string toc_head = format(q"┃<html xmlns="https://www.w3.org/1999/xhtml"
      xmlns:epub="http://www.idpf.org/2007/ops">
  <head>
    <title>%s</title>
  </head>
  <body>
  <section epub:type="frontmatter toc">
    <header>
      <h1>Contents</h1>
    </header>
    <nav epub:type="toc" id="toc">
  ┃",
            (doc_matters.conf_make_meta.meta.title_full).special_characters_text,
          );
    string _toc_nav_tail = "";
    // writeln(doc_matters.has.keys_seq.seg); // DEBUG line
    foreach (sect; doc_matters.has.keys_seq.seg) {
      foreach (obj; doc_abstraction[sect]) {
        if ((sect == "head") && (obj.metainfo.is_a == "heading")) {
          toc = toc_head;
        }
        if (sect == "tail") { // skip
        } else if ((sect != "tail") && (obj.metainfo.is_a == "heading")) {
          string _txt = obj.text.replaceAll(rgx.inline_notes_al_gen, "").strip;
          foreach_reverse (n; 0 .. 7) {
            string k = n.to!string;
            switch (obj.metainfo.dom_structure_collapsed_tags_status[n]) {
            case DomTags.none :
              break;
            case DomTags.close :
              toc ~= markup.indent_by_spaces_provided((n + 1), "    ") ~ "</li>" ~ "\n";
              toc ~= markup.indent_by_spaces_provided(n, "    ") ~ "</ol>" ~ "\n";
              break;
            case DomTags.close_and_open :
              toc ~= markup.indent_by_spaces_provided((n + 1), "    ") ~ "</li>" ~ "\n";
              goto default;
            case DomTags.open :
              if (!(_new_title_set)) {
                toc ~= markup.indent_by_spaces_provided(n, "    ") ~ "<ol>" ~ "\n";
              }
              goto default;
            default :
              if ((obj.metainfo.dom_structure_collapsed_tags_status[n] == DomTags.close_and_open ||
                   obj.metainfo.dom_structure_collapsed_tags_status[n] == DomTags.open
              )) {
                if ((sect == "head") && (obj.metainfo.is_a == "heading")) {
                  toc ~= format(q"┃      <li>
        <a href="_the_title.xhtml">%s</a>
      </li>┃",
                    obj.text
                      .replaceAll(rgx.inline_notes_al_gen, "")
                      .replaceAll(rgx.br_line_inline, "<br />")
                      .strip,
                  );
                  toc ~= "\n";
                  _new_title_set = true;
                } else {
                  _new_title_set = false;
                  string _hashtag = "";
                  if ((obj.metainfo.heading_lev_markup <= 4) && (obj.metainfo.ocn == 0)) {
                    _hashtag = "#" ~ obj.metainfo.ocn.to!string;
                  }
                  string _href = "<a href=\""
                    ~ obj.tags.segment_anchor_tag_epub ~ ".xhtml"
                    ~ _hashtag
                    ~ "\">";
                  toc ~= markup.indent_by_spaces_provided((n + 1), "    ") ~ "<li>" ~ "\n"
                  ~ markup.indent_by_spaces_provided((n + 2), "    ")
                  ~ _href ~ _txt.special_characters_text ~ "</a>" ~ "\n";
                }
              }
              break;
            }
            if (doc_matters.has.keys_seq.seg[doc_matters.has.keys_seq.seg.length - 2] == sect) {
              // writeln(n, ": ", sect, ": ", _txt, " - ", obj.metainfo.dom_structure_collapsed_tags_status); // DEBUG
              // read last heading (heading prior to closing) and determine what those instructions imply still need to be done
              // CLOSE // DomTags { 0 none, 1 open, 2 close, 3 close_and_open, 4 open_still, }
              if (n == 6) {_toc_nav_tail = "";}
              switch (obj.metainfo.dom_structure_collapsed_tags_status[n]) {
              case 0: case 2:
              // case DomTags.none: case DomTags.close:
                break;
              case 1: case 3: case 4:
              // case DomTags.open: case DomTags.close_and_open: case DomTags.open_still:
                if (n != 0) {
                  _toc_nav_tail ~= "   " ~ markup.indent_by_spaces_provided((n + 1), "  ") ~ "</li>" ~ "\n";
                  _toc_nav_tail ~= "   " ~ markup.indent_by_spaces_provided(n, "  ") ~ "</ol>" ~ "\n";
                }
                break;
              default :
                break;
              }
              if (n == 0) {
                _toc_nav_tail ~="  </nav>
  </section>
  </body>
  </html>\n";
              }
            }
          }
        }
      }
    }
    toc ~= _toc_nav_tail;
    return toc;
  }
  @system void outputEPub3(D,I)(
    const D    doc_abstraction,
          I    doc_matters,
  ) {
    mixin spineRgxOut;
    auto xhtml_format = outputXHTMLs();
    static auto rgx = RgxO();
    string[] doc;
    string segment_filename;
    string[] top_level_headings = ["","","",""];
    string[string] oepbs_content_parts;
    string suffix = ".xhtml";
    struct writeOut { /+ epub specific documents +/
      /+ fixed output +/
      string mimetypes;
      string meta_inf_container_xml;
      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();
    foreach (part; doc_matters.has.keys_seq.seg) {
      foreach (obj; doc_abstraction[part]) {
        string _txt = xhtml_format.special_characters_breaks_indents_bullets(obj);
        if (obj.metainfo.is_a == "heading") {
          assert(part == "head" || "toc" || "body" || "endnotes" || "glossary" || "bibliography" || "bookindex" || "blurb" || "tail");
          switch (obj.metainfo.heading_lev_markup) {
          case 0: .. case 3:
            /+ fill buffer, and replace with new levels from 1 to 3 +/
            switch (obj.metainfo.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:
              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);
              Tuple!(string, string[]) t = xhtml_format.heading_seg(_txt, obj, doc_matters, suffix, "epub");
              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;
            epubWrite.doc_epub3[segment_filename] ~= xhtml_format.epub3_seg_head(doc_matters);
            Tuple!(string, string[]) t = xhtml_format.heading_seg(_txt, obj, doc_matters, suffix, "epub");
            epubWrite.doc_epub3[segment_filename] ~= t[0];
            epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
            break;
          case 5: .. case 7:
            Tuple!(string, string[]) t = xhtml_format.heading_seg(_txt, obj, doc_matters, suffix, "epub");
            epubWrite.doc_epub3[segment_filename] ~= t[0];
            epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
            break;
          case 8: .. case 9:
            { /+ debug +/
              if (doc_matters.opt.action.debug_do_epub) {
                writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_a, ": ", obj.metainfo.heading_lev_markup);
                writeln(__FILE__, ":", __LINE__, ": ", obj.text);
              }
            }
            break;
          default:
            { /+ debug +/
              if (doc_matters.opt.action.debug_do_epub) {
                writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_a, ": ", obj.metainfo.heading_lev_markup);
              }
            }
            break;
          }
        } else {
          assert(part == "head" || "toc" || "body" || "endnotes" || "glossary" || "bibliography" || "bookindex" || "blurb" || "tail");
          Tuple!(string, string[]) t;
          switch (obj.metainfo.is_of_part) {
          case "frontmatter":             assert(part == "head" || "toc");
            switch (obj.metainfo.is_of_type) {
            case "para":
              switch (obj.metainfo.is_a) {
              case "toc":
                t = xhtml_format.para_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0];
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              default:
                { /+ debug +/
                  if (doc_matters.opt.action.debug_do_epub) {
                    writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_a);
                  }
                }
                break;
              }
              break;
            default:
              { /+ debug +/
                if (doc_matters.opt.action.debug_do_epub) {
                  writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_of_type);
                }
              }
              break;
            }
            break;
          case "body":                    assert(part == "body");
            switch (obj.metainfo.is_of_type) {
            case "para":
              switch (obj.metainfo.is_a) {
              case "para":
                t = xhtml_format.para_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0];
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              default:
                { /+ debug +/
                  if (doc_matters.opt.action.debug_do_epub) {
                    writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_a);
                  }
                }
                break;
              }
              break;
            case "block":
              switch (obj.metainfo.is_a) {
              case "quote":
                t = xhtml_format.quote_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0].to!string;
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              case "group":
                t = xhtml_format.group_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0].to!string;
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              case "block":
                t = xhtml_format.block_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0].to!string;
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              case "poem":
                break;
              case "verse":
                t = xhtml_format.verse_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0].to!string;
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              case "code":
                epubWrite.doc_epub3[segment_filename] ~= xhtml_format.code(_txt, obj, doc_matters);
                break;
              case "table":
                epubWrite.doc_epub3[segment_filename] ~= xhtml_format.table(_txt, obj, doc_matters);
                epubWrite.doc_epub3_endnotes[segment_filename] ~= "";
                break;
              default:
                { /+ debug +/
                  if (doc_matters.opt.action.debug_do_epub) {
                    writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_a);
                  }
                }
                break;
              }
              break;
            default:
              { /+ debug +/
                if (doc_matters.opt.action.debug_do_epub) {
                  writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_of_type);
                }
              }
              break;
            }
            break;
          case "backmatter":
            assert(part == "endnotes" || "glossary" || "bibliography" || "bookindex" || "blurb" || "tail");
            switch (obj.metainfo.is_of_type) {
            case "para":
              switch (obj.metainfo.is_a) {
              case "endnote":             assert(part == "endnotes");
                t = xhtml_format.para_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0];
                break;
              case "glossary":            assert(part == "glossary");
                t = xhtml_format.para_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0];
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              case "bibliography":        assert(part == "bibliography");
                t = xhtml_format.para_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0];
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              case "bookindex":           assert(part == "bookindex");
                t = xhtml_format.para_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0];
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              case "blurb":               assert(part == "blurb");
                t = xhtml_format.para_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0];
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              case "tail":                assert(part == "tail");
                t = xhtml_format.para_seg(_txt, obj, doc_matters, suffix, "epub");
                epubWrite.doc_epub3[segment_filename] ~= t[0];
                epubWrite.doc_epub3_endnotes[segment_filename] ~= t[1];
                break;
              default:
                { /+ debug +/
                  if (doc_matters.opt.action.debug_do_epub) {
                    writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_a);
                  }
                }
                break;
              }
              break;
            default:
              { /+ debug +/
                if (doc_matters.opt.action.debug_do_epub) {
                  writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_of_type);
                }
              }
              break;
            }
            break;
          case "comment":
            break;
          default:
            { /+ debug +/
              if (doc_matters.opt.action.debug_do_epub) {
                writeln(__FILE__, ":", __LINE__, ": ", obj.metainfo.is_of_part);
              }
            }
            break;
          }
        }
        if (obj.metainfo.is_a == "heading") {
          assert(obj.text.length > 0);
          if (obj.metainfo.heading_lev_markup <= 4) {
            oepbs_content_parts["manifest_documents"] ~=
              format(q"┃<item id="%s.xhtml" href="%s.xhtml" media-type="application/xhtml+xml" />
              ┃",
              obj.tags.segment_anchor_tag_epub,
              obj.tags.segment_anchor_tag_epub,
            );
            oepbs_content_parts["spine"] ~=
              format(q"┃<itemref idref="%s.xhtml" linear="yes" />
              ┃",
              obj.tags.segment_anchor_tag_epub,
            );
            oepbs_content_parts["guide"] ~=
              format(q"┃<reference type="%s" href="%s" />
              ┃",
              obj.tags.segment_anchor_tag_epub,
              obj.tags.segment_anchor_tag_epub,
            );
          } else if (obj.metainfo.heading_lev_markup > 4) {
            oepbs_content_parts["manifest_documents"] ~=
              format(q"┃<item id="%s.xhtml#%s" href="%s.xhtml#%s" media-type="application/xhtml+xml" />
              ┃",
              obj.tags.segment_anchor_tag_epub,
              obj.metainfo.object_number,
              obj.tags.segment_anchor_tag_epub,
              obj.metainfo.object_number,
            );
            oepbs_content_parts["spine"] ~=
              format(q"┃<itemref idref="%s.xhtml#%s" linear="yes" />
              ┃",
              obj.tags.segment_anchor_tag_epub,
              obj.metainfo.object_number,
            );
            oepbs_content_parts["guide"] ~=
              format(q"┃<reference type="%s#%s" href="%s#%s" />
              ┃",
              obj.tags.segment_anchor_tag_epub,
              obj.metainfo.object_number,
              obj.tags.segment_anchor_tag_epub,
              obj.metainfo.object_number,
            );
          }
        }
      }
    }
    /+ epub specific documents +/
    epubWrite.mimetypes              = epub3_mimetypes;
    epubWrite.meta_inf_container_xml = epub3_container_xml;
    epubWrite.oebps_toc_nav_xhtml    = doc_abstraction.epub3_oebps_toc_nav_xhtml(doc_matters);
    epubWrite.oebps_content_opf      = doc_abstraction.epub3_oebps_content(doc_matters, oepbs_content_parts);
    epubWrite.epub3_write_output_files(doc_matters);
  }
  @system void epub3_write_output_files(W,M)(
    W epub_write,
    M doc_matters,
  ) {
    debug(asserts) {
      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_content_opf)      == string));
    }
    static auto rgx = RgxO();
    auto pth_epub3 = spinePathsEPUB!()(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_epub)
      ? 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;
      }
      { /+ debug +/
        if (doc_matters.opt.action.debug_do_epub) {
          if (!exists(pth_epub3.dbg_doc_meta_inf(doc_matters.src.filename))) {
            pth_epub3.dbg_doc_meta_inf(doc_matters.src.filename).mkdirRecurse;
          }
          if (!exists(pth_epub3.dbg_doc_oebps_css(doc_matters.src.filename))) {
            pth_epub3.dbg_doc_oebps_css(doc_matters.src.filename).mkdirRecurse;
          }
          if (!exists(pth_epub3.dbg_doc_oebps_image(doc_matters.src.filename))) {
            pth_epub3.dbg_doc_oebps_image(doc_matters.src.filename).mkdirRecurse;
          }
        }
      }
      { /+ OEBPS/[segments].xhtml (the document contents) +/
        foreach (seg_filename; doc_matters.has.segnames_lv_0_to_4) {
          string fn = pth_epub3.fn_oebps_content_xhtml(seg_filename);
          auto zip_arc_member_file = new ArchiveMember();
          zip_arc_member_file.name = fn;
          auto zip_data = new OutBuffer();
          { /+ debug +/
            if (doc_matters.opt.action.debug_do_epub) {
              string fn_dbg = pth_epub3.dbg_fn_oebps_content_xhtml(doc_matters.src.filename, seg_filename);
              auto f = File(fn_dbg, "w");
              foreach (docseg; epub_write.doc_epub3[seg_filename]) {
                f.writeln(docseg);
              }
              foreach (docseg; epub_write.doc_epub3_endnotes[seg_filename]) {
                f.writeln(docseg);
              }
              f.writeln(xhtml_format.tail(doc_matters));
            }
          }
          foreach (docseg; epub_write.doc_epub3[seg_filename]) {
            zip_data.write(docseg.dup);
          }
          foreach (docseg; epub_write.doc_epub3_endnotes[seg_filename]) {
            zip_data.write(docseg.dup);
          }
          zip_data.write(xhtml_format.tail(doc_matters).dup);
          zip_arc_member_file.expandedData = zip_data.toBytes();
          zip.addMember(zip_arc_member_file);
          /+ create the zip file +/
          createZipFile!()(fn_epub, zip.build());
        }
      }
      string fn;
      string fn_dbg;
      File f;
      { /+ mimetypes (identify zip file type) +/
        { /+ debug +/
          if (doc_matters.opt.action.debug_do_epub) {
            fn_dbg = pth_epub3.dbg_fn_mimetypes(doc_matters.src.filename);
            File(fn_dbg, "w").writeln(epub_write.mimetypes);
          }
        }
        fn = pth_epub3.fn_mimetypes;
        EPUBzip(epub_write.mimetypes, fn);
      }
      { /+  META-INF/container.xml (identify doc root) +/
        { /+ debug +/
          if (doc_matters.opt.action.debug_do_epub) {
            fn_dbg = pth_epub3.dbg_fn_dmi_container_xml(doc_matters.src.filename);
            File(fn_dbg, "w").writeln(epub_write.meta_inf_container_xml);
          }
        }
        fn = pth_epub3.fn_dmi_container_xml;
        EPUBzip(epub_write.meta_inf_container_xml, fn);
      }
      { /+ OEBPS/toc_nav.xhtml (navigation toc epub3) +/
        { /+ debug +/
          if (doc_matters.opt.action.debug_do_epub) {
            fn_dbg = pth_epub3.dbg_fn_oebps_toc_nav_xhtml(doc_matters.src.filename);
            File(fn_dbg, "w").writeln(epub_write.oebps_toc_nav_xhtml);
          }
        }
        fn = pth_epub3.fn_oebps_toc_nav_xhtml;
        EPUBzip(epub_write.oebps_toc_nav_xhtml, fn);
      }
      { /+ OEBPS/content.opf (doc manifest) +/
        { /+ debug +/
          if (doc_matters.opt.action.debug_do_epub) {
            fn_dbg = pth_epub3.dbg_fn_oebps_content_opf(doc_matters.src.filename);
            File(fn_dbg, "w").writeln(epub_write.oebps_content_opf);
          }
        }
        fn = pth_epub3.fn_oebps_content_opf;
        EPUBzip(epub_write.oebps_content_opf, fn);
      }
      { /+ OEBPS/_dr/image (images) +/
        foreach (image; doc_matters.srcs.image_list) {
          { /+ debug +/
            if (doc_matters.opt.action.debug_do_epub) {
              if (doc_matters.opt.action.vox_gt2) {
                writeln(
                  doc_matters.src.image_dir_path, "/", image, " -> ",
                  pth_epub3.dbg_doc_oebps_image(doc_matters.src.filename), "/", image
                );
              }
              if (exists(doc_matters.src.image_dir_path ~ "/" ~ image)) {
                (doc_matters.src.image_dir_path ~ "/" ~ image)
                .copy((pth_epub3.dbg_doc_oebps_image(doc_matters.src.filename)) ~ "/" ~ image);
              }
            }
          }
          auto fn_src = doc_matters.src.image_dir_path ~ "/" ~ image;
          auto fn_out =  pth_epub3.doc_oebps_image ~ "/" ~ image;
          if (exists(fn_src)) {
            {
              auto zip_arc_member_file = new ArchiveMember();
              zip_arc_member_file.name = fn_out;
              auto zip_data = new OutBuffer();
              zip_data.write(cast(char[]) ((fn_src).read));
              zip_arc_member_file.expandedData = zip_data.toBytes();
              zip.addMember(zip_arc_member_file);
              createZipFile!()(fn_epub, zip.build());
            }
          }
        }
      }
      { /+ OEBPS/epub.css +/
        auto css = spineCss(doc_matters);
        { /+ debug +/
          if (doc_matters.opt.action.debug_do_epub) {
            fn_dbg = pth_epub3.dbg_fn_oebps_css(doc_matters.src.filename);
            File(fn_dbg, "w").writeln(css.epub);
          }
        }
        fn = pth_epub3.fn_oebps_css;
        auto zip_arc_member_file = new ArchiveMember();
        zip_arc_member_file.name = fn;
        auto zip_data = new OutBuffer();
        zip_data.write(css.epub.dup);
        zip_arc_member_file.expandedData = zip_data.toBytes();
        zip.addMember(zip_arc_member_file);
        createZipFile!()(fn_epub, zip.build());
      }
    } catch (ErrnoException ex) {
      // Handle error
    }
    if (doc_matters.opt.action.vox_gt0) {
      writeln(" ", fn_epub);
    }
    debug(epub_archive) {
      if (exists(fn_epub)) {
        try {
          auto zipped = new ZipArchive((fn_epub).read);
          foreach (filename, member; zipped.directory) {
            auto data = zipped.expand(member);
            writeln(filename, " length ", data.length);
          }
        } catch (ZipException ex) {
          // Handle errors
        }
      }
    }
  }
}