module doc_reform.output.xmls;
template outputXHTMLs() {
  import doc_reform.output;
  import
    std.digest.sha,
    std.file,
    std.outbuffer,
    std.uri,
    std.zip,
    std.conv : to;
  import
    doc_reform.output.create_zip_file,
    doc_reform.output.xmls,
    doc_reform.output.xmls_css;
  mixin DocReformOutputRgxInit;
  struct outputXHTMLs {
    static auto rgx = Rgx();
    string div_delimit(
                 string  part,
      return ref string  previous_part
    ){
      string delimit = "";
      string delimit_ = "";
      if (part != previous_part) {
        switch (part) {
        case "head":
          delimit_ ~= "\n<div class=\"doc_title\">\n" ;
          break;
        case "toc":
          delimit_ ~= "\n<div class=\"doc_toc\">\n" ;
          break;
        case "bookindex":
          delimit_ ~= "\n<div class=\"doc_bookindex\">\n" ;
          break;
        default:
          delimit_ ~= "\n<div class=\"doc_" ~ part ~ "\">\n" ;
          break;
        }
        if (previous_part.length > 0) {
          delimit ~= "\n</div>";
        }
        previous_part = part;
        delimit ~=  delimit_;
      }
      // you also need to close the last div, introduce a footer?
      return delimit;
    }
    string special_characters_text(string _txt){
      _txt = _txt
        .replaceAll(rgx.xhtml_ampersand,    "&#38;")
        .replaceAll(rgx.xhtml_quotation,    "&#34;")
        .replaceAll(rgx.xhtml_less_than,    "&#60;")
        .replaceAll(rgx.xhtml_greater_than, "&#62;")
        .replaceAll(rgx.nbsp_char,          " ");
      return _txt;
    }
    string special_characters(O)(
      const  O         obj,
    ){
      string _txt = special_characters_text(obj.text);
      if (!(obj.metainfo.is_a == "code")) {
        _txt = (_txt)
          .replaceAll(rgx.xhtml_line_break,   "<br />");
      }
      return _txt;
    }
    string font_face(string _txt){
      _txt = _txt
        .replaceAll(rgx.inline_emphasis,    ("<em>$1</em>"))
        .replaceAll(rgx.inline_bold,        ("<b>$1</b>"))
        .replaceAll(rgx.inline_underscore,  ("<u>$1</u>"))
        .replaceAll(rgx.inline_italics,     ("<i>$1</i>"))
        .replaceAll(rgx.inline_superscript, ("<sup>$1</sup>"))
        .replaceAll(rgx.inline_subscript,   ("<sub>$1</sub>"))
        .replaceAll(rgx.inline_strike,      ("<del>$1</del>"))
        .replaceAll(rgx.inline_insert,      ("<ins>$1</ins>"))
        .replaceAll(rgx.inline_mono,        ("<tt>$1</tt>"))
        .replaceAll(rgx.inline_cite,        ("<cite>$1</cite>"));
      return _txt;
    }
    string _xhtml_anchor_tags(O)(O obj) {
      const(string[]) anchor_tags = obj.tags.anchor_tags;
      string tags="";
      if (anchor_tags.length > 0) {
        foreach (tag; anchor_tags) {
          if (!(tag.empty)) {
            tags ~= "<a name=\"" ~ tag ~ "\"></a>";
          }
        }
      }
      return tags;
    }
    string header_metadata(M)(
      M  doc_matters,
    ) {
      string _publisher="Publisher"; // TODO
      string o;
      o = format(q"┃<!-- DocReform header metadata -->
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <meta name="dc.title" content="%s" />
    <meta name="dc.author" content="%s" />
    <meta name="dc.publisher" content="%s" />
    <meta name="dc.date" content="%s" />
    <meta name="dc.date.created" content="%s" />
    <meta name="dc.date.issued" content="%syear" />
    <meta name="dc.date.available" content="%syear" />
    <meta name="dc.date.valid" content="%syear" />
    <meta name="dc.date.modified" content="%syear" />
    <meta name="dc.language" content="%s" />
    <meta name="dc.rights" content="%s" />
    <meta name="generator" content="%s" />
    <link rel="generator" href="%s" />
    <!-- DocReform header metadata -->┃",
        doc_matters.conf_make_meta.meta.title_full,
        doc_matters.conf_make_meta.meta.creator_author,
        _publisher,
        doc_matters.conf_make_meta.meta.date_published,
        doc_matters.conf_make_meta.meta.date_created,
        doc_matters.conf_make_meta.meta.date_issued,
        doc_matters.conf_make_meta.meta.date_available,
        doc_matters.conf_make_meta.meta.date_valid,
        doc_matters.conf_make_meta.meta.date_modified,
        doc_matters.src.language,
        doc_matters.conf_make_meta.meta.rights_copyright,
        doc_matters.opt.action.debug_do ? "" : doc_matters.generator_program.name_and_version,
        doc_matters.generator_program.url_home,
      );
      return o;
    }
    string site_info_button(M)(
      M  doc_matters,
    ) {
      string _locations;
      if (doc_matters.conf_make_meta.make.home_button_text.length > 0) {
        _locations = (doc_matters.conf_make_meta.make.home_button_text)
          .replaceAll(
            rgx.inline_link,
            ("<p class=\"tiny_left\"><a href=\"$2\">$1</a></p>"))
          .replaceAll(
            rgx.br_line, "")
          .replaceAll(
            rgx.br_nl, "");
      } else {
         _locations = "<p class=\"tiny_left\"><a href=\"http://www.doc-reform.org\">doc-reform</a></p>\n<p class=\"tiny_left\"><a href=\"http://git.sisudoc.org/software/doc-reform/\">sources / git</a></p>\n<p class=\"tiny_left\"><a href=\"http://www.sisudoc.org\">www.sisudoc.org</a></p>";
      }
      string o;
      o = format(q"┃<div class="flex-menu-option">
          %s
        </div>┃",
        _locations,
      );
      return o;
    }
    string inline_search_form(M)(
      M  doc_matters,
    ) {
      string _action="http://www.sisudoc.org/cgi-bin/search.cgi";
      string _db="DocReform.7a.manual";
      string o;
      string _form;
      if (doc_matters.opt.action.workon) {
        _form = format(q"┃
    <a name="search"></a>
    <form method="get" action="%s" target="_top">
    <font size="2">
    <input type="text" name="s1" size="24" maxlength="255" />
    <input type="hidden" name="db" value="%s" />
    <input type="hidden" name="ltd" value="1000" />
    <input type="hidden" name="off" value="0" />
    <input type="hidden" name="doc" value="live-manual" /><br />
    <input type="submit" name="search" value="search doc" />
    <input type="submit" name="search" value="search db" />
    </font></form>
          ┃",
        _action,
        _db,
        );
      } else {
        _form = format(q"┃%s┃",
          "",
        );
      }
      o = format(q"┃<div class="flex-menu-option">%s
        </div>┃",
        _form,
      );
      return o;
    }
    string html_head(M)(
      M  doc_matters,
      string type,
    ) {
    string _manifest = "";
    if (doc_matters.opt.action.workon) {
      _manifest = format(q"┃
         <a href="../../manifest/sisu.en.html" target="_top">
           <font face="verdana, arial, georgia, tahoma, sans-serif, helvetica, times, roman" size="2">
             [&nbsp;document&nbsp;manifest&nbsp;]
           </font>
         </a>
         ┃",
      );
    }
      string o;
      o = format(q"┃<!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8" />
      <title>
        %s%s
      </title>
      <!-- metadata -->
      %s
      <!-- metadata -->
      <link rel="generator" href="http://www.sisudoc.org/" />
      <link rel="shortcut icon" href="%s" />
      <link href="%s" rel="stylesheet" />
    </head>
    <body lang="%s">
    <a name="top" id="top"></a>
    <div class='delimit headband'>
      <div class="flex-menu-bar">
        %s
        <div class="flex-menu-option">
          %s
        </div>
        %s%s┃",
        doc_matters.conf_make_meta.meta.title_full,
        (doc_matters.conf_make_meta.meta.creator_author.empty) ? ""
          : ", " ~ doc_matters.conf_make_meta.meta.creator_author,
        header_metadata(doc_matters),
        ((type == "seg") ? "../../../" : "../../") ~ "image/rb7.ico",
        ((type == "seg")
          ? "../../../css/html_seg.css"
          : "../../css/html_scroll.css"),
        doc_matters.src.language,
        site_info_button(doc_matters),
        _manifest,
        inline_search_form(doc_matters),
        ((type == "seg") ? "" : "\n</div>"),
      );
      return o;
    }
    string epub3_seg_head(M)(
      M  doc_matters,
    ) {
      string html_base = format(q"┃<!DOCTYPE html>
    <html>┃",
    );
      string html_simple = format(q"┃<!DOCTYPE html>
    <html
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:epub="http://www.idpf.org/2007/ops"
      lang="%s" xml:lang="%s">┃",
        doc_matters.src.language,
        doc_matters.src.language,
      );
      string html_strict = format(q"┃<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:epub="http://www.idpf.org/2007/ops"
      lang="%s" xml:lang="%s">┃",
        doc_matters.src.language,
        doc_matters.src.language,
      );
      string o;
      o = format(q"┃%s
    <head>
      <title>
        %s%s
      </title>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
      <meta name="dc.title" content="%s" />
      <meta name="dc.author" content="%s" />
      <meta name="dc.publisher" content="FIX" />
      <meta name="dc.date" content="%s" />
      <meta name="dc.date.created" content="%s" />
      <meta name="dc.date.issued" content="%s" />
      <meta name="dc.date.available" content="%s" />
      <meta name="dc.date.valid" content="%s" />
      <meta name="dc.date.modified" content="%s" />
      <meta name="dc.language" content="%s" />
      <meta name="dc.rights" content="%s" />
      <meta name="generator" content="%s" />
      <link rel="generator" href="%s" />
      <link rel="shortcut icon" href="../_sisu/image/d.ico" />
      <link rel="stylesheet" href="css/epub.css" type="text/css" id="main-css" />
    </head>
    <body lang="%s">
    <a name="top" id="top"></a>┃",
        html_simple,
        doc_matters.conf_make_meta.meta.title_full,
        (doc_matters.conf_make_meta.meta.creator_author.empty) ? ""
          : ", " ~ doc_matters.conf_make_meta.meta.creator_author,
        doc_matters.conf_make_meta.meta.title_full,
        (doc_matters.conf_make_meta.meta.creator_author.empty) ? ""
          : ", " ~ doc_matters.conf_make_meta.meta.creator_author,
        doc_matters.conf_make_meta.meta.date_published,
        doc_matters.conf_make_meta.meta.date_created,
        doc_matters.conf_make_meta.meta.date_issued,
        doc_matters.conf_make_meta.meta.date_available,
        doc_matters.conf_make_meta.meta.date_valid,
        doc_matters.conf_make_meta.meta.date_modified,
        doc_matters.src.language,
        doc_matters.conf_make_meta.meta.rights_copyright,
        doc_matters.generator_program.name_and_version,
        doc_matters.generator_program.url_home,
        doc_matters.src.language,
      );
      return o;
    }
    string tail() {
      string o;
      o = format(q"┃
      <a name="bottom" id="bottom"></a>
      <a name="end" id="end"></a>
    </body>
    </html>┃");
      return o;
    }
    string inline_images(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix    = ".html",
      string          _xml_type = "seg",
    ) {
      string _img_pth;
      if (_xml_type == "epub") {
        _img_pth = "image/";
      } else if (_xml_type == "scroll") {
        _img_pth = "../../image/";
      } else if (_xml_type == "seg") {
        _img_pth = "../../../image/";
      }
      if (_txt.match(rgx.inline_image)) {
        _txt = _txt
          .replaceAll(rgx.inline_image,
            ("$1<img src=\""
              ~ _img_pth
              ~ "$3\" width=\"$4\" height=\"$5\" naturalsizeflag=\"0\" align=\"bottom\" border=\"0\" /> $6"))
          .replaceAll(
            rgx.inline_link_empty,
            ("$1"));
      }
      return _txt;
    }
    string inline_links(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string         _suffix   = ".html",
      string         _xml_type = "seg",
    ) {
      string seg_lvs;
      if (obj.has.inline_links) {
        if  (obj.metainfo.is_a != "code") {
          _txt = replaceAll!(m =>
              m[1] ~ "┤"
              ~ (replaceAll!(n =>
                  n["type"] ~ n["path"] ~ (n["file"].encodeComponent)
                )((obj.stow.link[m["num"].to!ulong]).to!string, rgx.uri_identify_components))
              ~ "├"
            )(_txt, rgx.inline_link_number_only);
        }
        if ((_txt.match(rgx.mark_internal_site_lnk))
        && (_xml_type == "scroll")) { // conditions reversed to avoid: gdc compiled program run segfault
          _txt = _txt.replaceAll(
            rgx.inline_seg_link,
            "$1");
        }
        if (_xml_type == "seg" || _xml_type == "epub") {
          seg_lvs = (_xml_type == "epub") ? "seg_lv1_to_4" : "seg_lv4";
          foreach (m; _txt.match(rgx.inline_link_hash)) {
            if (m.captures[3] in doc_matters.has.tag_associations) {
              if (
                m.captures[3]
                == doc_matters.has.tag_associations[(m.captures[3])][seg_lvs]
              ) {
                _txt = _txt.replaceFirst(
                  rgx.inline_link_hash,
                  "┥$1┝┤$3" ~ _suffix ~ "├"
                );
              } else {
                _txt = _txt.replaceFirst(
                  rgx.inline_link_hash,
                  "┥$1┝┤"
                  ~ doc_matters.has.tag_associations[(m.captures[3])][seg_lvs]
                  ~ _suffix ~ "#" ~ "$3"
                  ~ "├"
                );
              }
            } else {
              if (!(doc_matters.opt.action.quiet)) {
                writeln(
                  "WARNING on internal document links, anchor to link <<"
                   ~ m.captures[3]
                   ~ ">> not found in document, "
                   ~ "anchor: " ~ m.captures[3]
                   ~ " document: " ~ doc_matters.src.filename
                );
              }
            }
          }
        }
        _txt = _txt
          .replaceAll(
            rgx.inline_link_fn_suffix,
            ("$1" ~ _suffix))
          .replaceAll(
            rgx.inline_link,
            ("<a href=\"$2\">$1</a>"))
          .replaceAll(
            rgx.mark_internal_site_lnk,
            "");
      }
      debug(markup_links) {
        if (_txt.match(rgx.inline_link)) {
          writeln(__LINE__,
            " (missed) markup link identified (",
            obj.has.inline_links,
            "): ", obj.metainfo.is_a, ": ",
            obj.text
          );
        }
      }
      debug(markup) {
        if (_txt.match(rgx.inline_link)) {
          writeln(__LINE__,
            " (missed) markup link identified (",
            obj.has.inline_links,
            "): ", obj.metainfo.is_a, ": ",
            obj.text
          );
        }
      }
      return _txt;
    }
    string inline_notes_scroll(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
    ) {
      if (obj.has.inline_notes_reg) {
        _txt = font_face(_txt);
        _txt = _txt.replaceAll(
          rgx.inline_notes_al_regular_number_note,
          ("<a href=\"#note_$1\"><note id=\"noteref_$1\">&#160;<sup>$1</sup> </note></a>")
        );
      }
      debug(markup_endnotes) {
        if (_txt.match(rgx.inline_notes_al_regular_number_note)) {
          writeln(__LINE__, " (missed) markup endnote: ", obj.metainfo.is_a, ": ", obj.text);
        }
      }
      debug(markup) {
        if (_txt.match(rgx.inline_notes_al_regular_number_note)) {
          writeln(__LINE__, " (missed) markup endnote: ", obj.metainfo.is_a, ": ", obj.text);
        }
      }
      return _txt;
    }
    auto inline_notes_seg(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
    ) {
      string[] _endnotes;
      if (obj.has.inline_notes_reg) {
        _txt = font_face(_txt);
        /+ need markup for text, and separated footnote +/
        foreach(m; _txt.matchAll(rgx.inline_notes_al_regular_number_note)) {
          _endnotes ~= format(
            "%s%s%s%s\n  %s%s%s%s%s\n  %s\n%s",
            "<p class=\"endnote\">",
            "<a href=\"#noteref_",
            m.captures[1],
            "\">",
            "<note id=\"note_",
            m.captures[1],
            "\">&#160;<sup>",
            m.captures[1],
            ".</sup></note></a>",
            m.captures[2],
            "</p>"
          );
        }
        _txt = _txt.replaceAll(
          rgx.inline_notes_al_regular_number_note,
          ("<a href=\"#note_$1\"><note id=\"noteref_$1\">&#160;<sup>$1</sup> </note></a>")
        );
      } else if (_txt.match(rgx.inline_notes_al_regular_number_note)) {
        debug(markup) {
          writeln(__LINE__, " endnote: ", obj.metainfo.is_a, ": ", obj.text);
        }
      }
      auto t = tuple(
        _txt,
        _endnotes,
      );
      return t;
    }
    string inline_markup_scroll(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
    ) {
      if (obj.metainfo.dummy_heading
        && (obj.metainfo.is_a == "toc" || obj.metainfo.is_a == "heading")) {
        _txt = "";
      } else {
        _txt = inline_images(_txt, obj, doc_matters, _suffix, "scroll");
        _txt = inline_links(_txt, obj, doc_matters, _suffix, "scroll");
        _txt = inline_notes_scroll(_txt, obj, doc_matters);
      }
      return _txt;
    }
    auto inline_markup_seg(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "seg",
    ) {
      if (obj.metainfo.dummy_heading
        && ((_xml_type == "epub"
        && (obj.metainfo.is_a == "toc" || obj.metainfo.is_a == "heading"))
        || obj.metainfo.is_a == "heading")
      ) {
        _txt = "";
      } else {
        _txt = inline_images(_txt, obj, doc_matters, _suffix, _xml_type); // TODO
        _txt = inline_links(_txt, obj, doc_matters, _suffix, _xml_type); // TODO
      }
      auto t = inline_notes_seg(_txt, obj, doc_matters);
      return t;
    }
    string lev4_heading_subtoc(O,M)(
      const        O  obj,
                   M  doc_matters,
    ) {
      char[] lev4_subtoc;
      lev4_subtoc ~= "  <div class=\"nav\">\n";
      foreach (subtoc; obj.tags.lev4_subtoc) {
        if (auto m = subtoc.match(rgx.inline_link_subtoc)) {
          auto indent = (m.captures[1].to!int - 3).to!string; // css assumptions based on use of em for left margin & indent
          auto text = m.captures[2].to!string;
          text = font_face(text);
          auto link = m.captures[3].to!string;
          lev4_subtoc ~= subtoc.replaceFirst(rgx.inline_link_subtoc,
            format(q"┃    <p class="minitoc" indent="h%si%s">
          ۰ <a href="%s">%s</a>
        </p>
    ┃",
              indent,
              indent,
              link,
              text,
          ));
        }
      }
      lev4_subtoc ~= "  </div>\n";
      return lev4_subtoc.to!string;
    }
    auto nav_pre_next_svg(O,M)(
      const        O  obj,
                   M  doc_matters,
    ) {
      string prev, next, toc;
      if (obj.tags.segment_anchor_tag_epub == "toc") {
        toc = "";
        prev = "";
      } else {
        toc = format(q"┃<a href="toc.html" target="_top">
              <div class="toc-button menu">
                <svg viewbox="0 0 100 100">
                  <path d="M4,10h24c1.104,0,2-0.896,2-2s-0.896-2-2-2H4C2.896,6,2,6.896,2,8S2.896,10,4,10z M28,14H4c-1.104,0-2,0.896-2,2  s0.896,2,2,2h24c1.104,0,2-0.896,2-2S29.104,14,28,14z M28,22H4c-1.104,0-2,0.896-2,2s0.896,2,2,2h24c1.104,0,2-0.896,2-2  S29.104,22,28,22z"/>
                </svg>
              </div>
            </a>┃",
        );
      }
      if (obj.tags.segname_prev == "") {
        prev = "";
      } else {
        prev = format(q"┃<a href="%s.html" target="_top">
              <div class="prev-next-button previous">
                <svg viewbox="0 0 100 100">
                  <path class="arrow" d="M 50,0 L 60,10 L 20,50 L 60,90 L 50,100 L 0,50 Z" transform=" translate(15,0)">
                </svg>
              </div>
            </a>┃",
          obj.tags.segname_prev,
        );
      }
      if (obj.tags.segname_next == "") {
        next = "";
      } else {
        next = format(q"┃<a href="%s.html" target="_top">
              <div class="prev-next-button next">
                <svg viewbox="0 0 100 100">
                  <path class="arrow" d="M 50,0 L 60,10 L 20,50 L 60,90 L 50,100 L 0,50 Z "transform="translate(85,100) rotate(180) ">
                </svg>
              </div>
            </a>┃",
          obj.tags.segname_next,
        );
      }
      string _toc_pre_next = format(q"┃        <div class="flex-menu-option">
          <div class="nav-bar">
            %s
            %s
            %s
          </div>
        </div>
      </div>
    </div>┃",
        toc,
        prev,
        next,
      );
      string _pre_next = format(q"┃        <div class="flex-menu-option">
          <div class="nav-bar">
            %s
            %s
          </div>
        </div>
      </div>
    </div>┃",
        prev,
        next,
      );
      struct bar {
        string toc_pre_next() {
          return _toc_pre_next;
        }
        string pre_next() {
          return _pre_next;
        }
      }
      return bar();
    }
    string heading(O)(
      string            _txt,
      const        O    obj,
      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");
      string tags = _xhtml_anchor_tags(obj);
      string heading_lev_anchor_tag;
      string _horizontal_rule = "<hr />";
      if ((_xml_type != "html")
      || (obj.metainfo.heading_lev_markup == 0 || obj.metainfo.heading_lev_markup > 4)) {
        _horizontal_rule = "";
      }
      _txt = font_face(_txt);
      string o;
      heading_lev_anchor_tag = (obj.tags.heading_lev_anchor_tag.empty)
        ? ""
        : "<a name=\"" ~ obj.tags.heading_lev_anchor_tag ~ "\"></a>";
      if (_txt.empty) {
        o = format(q"┃%s
      ┃",
        _horizontal_rule,
        );
      } else if (!(obj.metainfo.identifier.empty)) {
        o = format(q"┃%s
      <div class="substance">
        <label class="ocn"><a href="#%s" class="lnkocn">%s</a></label>
        <h%s class="%s" id="%s"><a name="%s"></a>%s%s
          %s
        </h%s>
      </div>┃",
        _horizontal_rule,
        obj.metainfo.identifier,
        (obj.metainfo.object_number.empty) ? "" : obj.metainfo.identifier,
        obj.metainfo.heading_lev_markup,
        obj.metainfo.is_a,
        obj.metainfo.identifier,
        obj.metainfo.identifier,
        tags,
        heading_lev_anchor_tag,
        _txt,
        obj.metainfo.heading_lev_markup,
        );
      } else {
        o = format(q"┃%s
      <div class="substance">
        <h%s class="%s">%s%s
          %s
        </h%s>
      </div>┃",
          _horizontal_rule,
          obj.metainfo.heading_lev_markup,
          obj.metainfo.is_a,
          tags,
          heading_lev_anchor_tag,
          _txt,
          obj.metainfo.heading_lev_markup,
        );
      }
      return o;
    }
    string heading_scroll(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
    ) {
      _txt = inline_markup_scroll(_txt, obj, doc_matters, _suffix);
      string o = heading(_txt, obj);
      return o;
    }
    auto heading_seg(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      auto t = inline_markup_seg(_txt, obj, doc_matters, _suffix, _xml_type);
      _txt = t[0];
      string[] _endnotes = t[1];
      string o = heading(_txt, obj, _xml_type);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    string para(O)(
      string            _txt,
      const        O    obj,
    ) {
      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");
      string tags = _xhtml_anchor_tags(obj);
      _txt = font_face(_txt);
      string o;
      _txt = (obj.attrib.bullet) ? ("●&#160;&#160;" ~ _txt) : _txt;
      _txt = _txt.replaceFirst(rgx.inline_link_anchor,
         "<a name=\"$1\"></a>");
      if (!(obj.metainfo.identifier.empty)) {
        o = format(q"┃  <div class="substance">
      <label class="ocn"><a href="#%s" class="lnkocn">%s</a></label>
      <p class="%s" indent="h%si%s" id="%s">%s
        %s
      </p>
    </div>┃",
          obj.metainfo.identifier,
          (obj.metainfo.object_number.empty) ? "" : obj.metainfo.identifier,
          obj.metainfo.is_a,
          obj.attrib.indent_hang,
          obj.attrib.indent_base,
          obj.metainfo.identifier,
          tags,
          _txt
        );
      } else {
        o = format(q"┃  <div class="substance">
      <p class="%s" indent="h%si%s">%s
        %s
      </p>
    </div>┃",
          obj.metainfo.is_a,
          obj.attrib.indent_hang,
          obj.attrib.indent_base,
          tags,
          _txt
        );
      }
      return o;
    }
    string para_scroll(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
    ) {
      _txt = inline_markup_scroll(_txt, obj, doc_matters, _suffix);
      string o = para(_txt, obj);
      return o;
    }
    auto para_seg(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      auto t = inline_markup_seg(_txt, obj, doc_matters, _suffix, _xml_type);
      _txt = t[0].to!string;
      string[] _endnotes = t[1];
      string o = para(_txt, obj);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    string quote(O)(
      string          _txt,
      const        O  obj,
    ) {
      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)) {
        o = format(q"┃  <div class="substance">
      <label class="ocn"><a href="#%s" class="lnkocn">%s</a></label>
      <p class="%s" id="%s">
        %s
      </p>
    </div>┃",
          obj.metainfo.identifier,
          (obj.metainfo.object_number.empty) ? "" : obj.metainfo.identifier,
          obj.metainfo.is_a,
          obj.metainfo.identifier,
          _txt
        );
      } else {
        o = format(q"┃  <div class="substance">
      <p class="%s">
        %s
      </p>
    </div>┃",
          obj.metainfo.is_a,
          _txt
        );
      }
      return o;
    }
    string quote_scroll(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
    ) {
      _txt = inline_markup_scroll(_txt, obj, doc_matters, _suffix);
      string o = quote(_txt, obj);
      return o;
    }
    auto quote_seg(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      auto t = inline_markup_seg(_txt, obj, doc_matters, _suffix, _xml_type);
      _txt = t[0].to!string;
      string[] _endnotes = t[1];
      string o = quote(_txt, obj);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    string group(O)(
      string            _txt,
      const        O    obj,
    ) {
      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)) {
        o = format(q"┃  <div class="substance">
      <label class="ocn"><a href="#%s" class="lnkocn">%s</a></label>
      <p class="%s" id="%s">
        %s
      </p>
    </div>┃",
          obj.metainfo.identifier,
          (obj.metainfo.object_number.empty) ? "" : obj.metainfo.identifier,
          obj.metainfo.is_a,
          obj.metainfo.identifier,
          _txt
        );
      } else {
        o = format(q"┃  <div class="substance">
      <p class="%s">
        %s
      </p>
    </div>┃",
          obj.metainfo.is_a,
          _txt
        );
      }
      return o;
    }
    string group_scroll(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      _txt = inline_markup_scroll(_txt, obj, doc_matters, _suffix);
      string o = group(_txt, obj);
      return o;
    }
    auto group_seg(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      auto t = inline_markup_seg(_txt, obj, doc_matters, _suffix, _xml_type);
      _txt = t[0].to!string;
      string[] _endnotes = t[1];
      string o = group(_txt, obj);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    string block(O)(
      string            _txt,
      const        O    obj,
    ) {
      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)) {
        o = format(q"┃  <div class="substance">
      <label class="ocn"><a href="#%s" class="lnkocn">%s</a></label>
      <p class="%s" id="%s">%s</p>
    </div>┃",
          obj.metainfo.identifier,
          (obj.metainfo.object_number.empty) ? "" : obj.metainfo.identifier,
          obj.metainfo.is_a,
          obj.metainfo.identifier,
          _txt.stripRight
        );
      } else {
        o = format(q"┃  <div class="substance">
      <p class="%s">%s</p>
    </div>┃",
          obj.metainfo.is_a,
          _txt.stripRight
        );
      }
      return o;
    }
    string block_scroll(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      _txt = inline_markup_scroll(_txt, obj, doc_matters, _suffix);
      string o = block(_txt, obj);
      return o;
    }
    auto block_seg(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      auto t = inline_markup_seg(_txt, obj, doc_matters, _suffix, _xml_type);
      _txt = t[0].to!string;
      string[] _endnotes = t[1];
      string o = block(_txt, obj);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    string verse(O)(
      string            _txt,
      const        O    obj,
    ) {
      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)) {
        o = format(q"┃  <div class="substance">
          <label class="ocn"><a href="#%s" class="lnkocn">%s</a></label>
          <p class="%s" id="%s">%s</p>
        </div>┃",
          obj.metainfo.identifier,
          (obj.metainfo.object_number.empty) ? "" : obj.metainfo.identifier,
          obj.metainfo.is_a,
          obj.metainfo.identifier,
          _txt
        );
      } else {
        o = format(q"┃  <div class="substance">
          <p class="%s">%s</p>
        </div>┃",
          obj.metainfo.is_a,
          _txt
        );
      }
      return o;
    }
    string verse_scroll(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      _txt = inline_markup_scroll(_txt, obj, doc_matters, _suffix);
      string o = verse(_txt, obj);
      return o;
    }
    auto verse_seg(O,M)(
      string          _txt,
      const        O  obj,
                   M  doc_matters,
      string          _suffix = ".html",
      string          _xml_type = "html",
    ) {
      auto t = inline_markup_seg(_txt, obj, doc_matters, _suffix, _xml_type);
      _txt = t[0].to!string;
      string[] _endnotes = t[1];
      string o = verse(_txt, obj);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    auto tablarize(O)(
      string            _txt,
      const        O    obj,
    ) {
      string[] _table_rows = (_txt).split(rgx.table_delimiter_row);
      string[] _table_cols;
      string _table;
      string _tablenote;
      foreach(row_idx, row; _table_rows) {
        _table_cols = row.split(rgx.table_delimiter_col);
          _table ~= "<tr>";
          foreach(col_idx, cell; _table_cols) {
            if ((_table_cols.length == 1)
            && (_table_rows.length <= row_idx+2)) {
              _tablenote ~= cell;
            } else {
              string _col_is = (row_idx == 0 && obj.table.heading) ? "th" : "td";
              string _align = ("style=\"text-align:"
              ~ ((obj.table.column_aligns[col_idx] == "l")
              ? "left\"" : "right\""));
              _table ~= "<" ~ _col_is ~ " width=\"" ~ obj.table.column_widths[col_idx].to!string ~ "%\" " ~ _align ~ ">";
              _table ~= cell;
              _table ~= "</" ~ _col_is ~ ">";
            }
          }
          _table ~= "</tr>";
        }
      auto t = tuple(
        _table,
        _tablenote,
      );
      return t;
    }
    string table(O)(
      string            _txt,
      const        O    obj,
    ) {
      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");
      string tags = _xhtml_anchor_tags(obj);
      _txt = font_face(_txt);
      auto t = tablarize(_txt, obj);
      _txt = t[0];
      string _note = t[1];
      string o;
      o = format(q"┃  <div class="substance">
      <label class="ocn"><a href="#%s" class="lnkocn">%s</a></label>
      <p class="%s" id="%s">%s
        <table summary="normal text css" width="95%%" border="0" bgcolor="white" cellpadding="2" align="center">
          %s
        </table>
        %s
      </p>
    </div>┃",
        obj.metainfo.object_number,
        obj.metainfo.object_number,
        obj.metainfo.is_a,
        obj.metainfo.object_number,
        tags,
        _txt,
        _note
      );
      return o;
    }
    string code(O)(
      string            _txt,
      const        O    obj,
    ) {
      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"┃  <div class="substance">
      <label class="ocn"><a href="#%s" class="lnkocn">%s</a></label>
      <pre><p class="%s" id="%s">%s</p></pre>
    </div>┃",
          obj.metainfo.identifier,
          (obj.metainfo.object_number.empty) ? "" : obj.metainfo.identifier,
          obj.metainfo.is_a,
          obj.metainfo.identifier,
          _txt
        );
      } else {
        o = format(q"┃  <div class="substance">
      <pre><p class="%s">%s</p></pre>
    </div>┃",
          obj.metainfo.is_a,
          _txt
        );
      }
      return o;
    }
  }
}