module sdp.output.xmls;
template outputXHTMLs() {
  import sdp.output;
  import
    std.digest.sha,
    std.file,
    std.outbuffer,
    std.zip,
    std.conv : to;
  import
    sdp.output.create_zip_file,
    sdp.output.xmls,
    sdp.output.xmls_css;
  mixin SiSUoutputRgxInit;
  struct outputXHTMLs {
    auto rgx = Rgx();
    string special_characters(O)(
      auto return ref const O    obj,
      string                     _txt
    ){
      _txt = (_txt)
        .replaceAll(rgx.xhtml_ampersand,    "&")
        .replaceAll(rgx.xhtml_quotation,    """)
        .replaceAll(rgx.xhtml_less_than,    "<")
        .replaceAll(rgx.xhtml_greater_than, ">")
        .replaceAll(rgx.nbsp_char,          " ");
      if (!(obj.is_a == "code")) {
        _txt = (_txt)
          .replaceAll(rgx.xhtml_line_break,   "
");
      }
      return _txt;
    }
    string font_face(string _txt){
      _txt = (_txt)
        .replaceAll(rgx.inline_emphasis,    ("$1"))
        .replaceAll(rgx.inline_bold,        ("$1"))
        .replaceAll(rgx.inline_underscore,  ("$1"))
        .replaceAll(rgx.inline_italics,     ("$1"))
        .replaceAll(rgx.inline_superscript, ("$1"))
        .replaceAll(rgx.inline_subscript,   ("$1"))
        .replaceAll(rgx.inline_strike,      ("$1"))
        .replaceAll(rgx.inline_insert,      ("$1"))
        .replaceAll(rgx.inline_mono,        ("$1"))
        .replaceAll(rgx.inline_cite,        ("$1"));
      return _txt;
    }
    string _xhtml_anchor_tags(const(string[]) anchor_tags) {
      string tags="";
      if (anchor_tags.length > 0) {
        foreach (tag; anchor_tags) {
          if (!(tag.empty)) {
            tags ~= "";
          }
        }
      }
      return tags;
    }
    auto html_scroll_head(Dm)(
      Dm doc_matters,
    ) {
      string o;
      o = format(q"¶
    
    
      
        
          %s%s
        
        
        
        
        
        
        
        
        
        
        
        
        
      
      
      
      
      
    
    
    ¶",
        doc_matters.dochead_meta["title"]["full"],
        (doc_matters.dochead_meta["creator"]["author"].empty) ? "" : ", " ~ doc_matters.dochead_meta["creator"]["author"],
        doc_matters.language,
      );
      return o;
    }
    auto html_seg_head(Dm)(
      Dm doc_matters,
    ) {
      string o;
      o = format(q"¶
    
    
      
        
          %s%s
        
        
        
        
        
        
        
        
        
        
        
        
        
        
      
      
      
      
      
    
    
    ¶",
        doc_matters.dochead_meta["title"]["full"],
        (doc_matters.dochead_meta["creator"]["author"].empty) ? ""
          : ", " ~ doc_matters.dochead_meta["creator"]["author"],
        doc_matters.language,
      );
      return o;
    }
    auto epub3_seg_head(Dm)(
      Dm doc_matters,
    ) {
      string html_base = format(q"¶
    ¶",
    );
      string html_simple = format(q"¶
    ¶",
        doc_matters.language,
        doc_matters.language,
      );
      string html_strict = format(q"¶
    ¶",
        doc_matters.language,
        doc_matters.language,
      );
      string o;
      o = format(q"¶%s
    
      
        %s%s
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    
    ¶",
        html_simple,
        doc_matters.dochead_meta["title"]["full"],
        (doc_matters.dochead_meta["creator"]["author"].empty) ? ""
          : ", " ~ doc_matters.dochead_meta["creator"]["author"],
        doc_matters.dochead_meta["title"]["full"],
        (doc_matters.dochead_meta["creator"]["author"].empty) ? ""
          : ", " ~ doc_matters.dochead_meta["creator"]["author"],
        doc_matters.language,
      );
      return o;
    }
    auto tail() {
      string o;
      o = format(q"¶  
      
    
    ¶");
      return o;
    }
    auto inline_links(O)(
      auto return ref const O obj,
      string                  _txt,
      string                  _suffix    = ".html",
      string                  seg_scroll = "seg",
    ) {
      if (obj.inline_links) {
        if ((seg_scroll == "scroll")
        && _txt.match(rgx.mark_internal_site_lnk)) {
          _txt = (_txt).replaceAll(
            rgx.inline_seg_link,
            "$1");
        }
        _txt = (_txt).replaceAll(
          rgx.inline_link_fn_suffix,
          ("$1" ~ _suffix));
        _txt = (_txt).replaceAll(
          rgx.inline_link,
          ("$1"));
        _txt = (_txt).replaceAll(
          rgx.mark_internal_site_lnk,
          ""
        );
      }
      debug(markup_links) {
        if (_txt.match(rgx.inline_link)) {
          writeln(__LINE__,
            " (missed) markup link identified (",
            obj.inline_links,
            "): ", obj.is_a, ": ",
            obj.text
          );
        }
      }
      debug(markup) {
        if (_txt.match(rgx.inline_link)) {
          writeln(__LINE__,
            " (missed) markup link identified (",
            obj.inline_links,
            "): ", obj.is_a, ": ",
            obj.text
          );
        }
      }
      return _txt;
    }
    auto inline_notes_scroll(O)(
      auto return ref const O   obj,
      string                    _txt,
    ) {
      if (obj.inline_notes_reg) {
        _txt = (_txt).replaceAll(
          rgx.inline_notes_delimiter_al_regular_number_note,
          (" $1 ")
        );
      }
      debug(markup_endnotes) {
        if (_txt.match(rgx.inline_notes_delimiter_al_regular_number_note)) {
          writeln(__LINE__, " (missed) markup endnote: ", obj.is_a, ": ", obj.text);
        }
      }
      debug(markup) {
        if (_txt.match(rgx.inline_notes_delimiter_al_regular_number_note)) {
          writeln(__LINE__, " (missed) markup endnote: ", obj.is_a, ": ", obj.text);
        }
      }
      return _txt;
    }
    auto inline_notes_seg(O)(
      auto return ref const O     obj,
      string                      _txt,
    ) {
      string[] _endnotes;
      if (obj.inline_notes_reg) {
        /+ need markup for text, and separated footnote +/
        foreach(m; _txt.matchAll(rgx.inline_notes_delimiter_al_regular_number_note)) {
          _endnotes ~= format(
            "%s%s%s%s\n  %s%s%s%s%s\n  %s\n%s",
            "",
            "",
            " ",
            m.captures[1],
            ".",
            m.captures[2],
            "
"
          );
        }
        _txt = (_txt).replaceAll(
          rgx.inline_notes_delimiter_al_regular_number_note,
          (" $1 ")
        );
      } else if (_txt.match(rgx.inline_notes_delimiter_al_regular_number_note)) {
        debug(markup) {
          writeln(__LINE__, " endnote: ", obj.is_a, ": ", obj.text);
        }
      }
      auto t = tuple(
        _txt,
        _endnotes,
      );
      return t;
    }
    auto inline_markup_scroll(O)(
      auto return ref const O  obj,
      string                   _txt,
      string                   _suffix = ".html",
    ) {
      _txt = inline_links(obj, _txt, _suffix, "scroll");
      _txt = inline_notes_scroll(obj, _txt);
      return _txt;
    }
    auto inline_markup_seg(O)(
      auto return ref const O  obj,
      string                   _txt,
      string                   _suffix = ".html",
    ) {
      _txt = inline_links(obj, _txt, _suffix, "seg");
      auto t = inline_notes_seg(obj, _txt);
      return t;
    }
    auto toc_seg(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      string o;
      o = format(q"¶  ¶",
      obj.is_a,
      obj.indent_hang,
      obj.indent_base,
      _txt,
      );
      return o;
    }
    auto heading(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _type="html",
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      string _horizontal_rule = "
";
      if ((_type != "html")
      || (obj.heading_lev_markup == 0 || obj.heading_lev_markup > 4)) {
        _horizontal_rule = "";
      }
      string o;
      if (obj.obj_cite_number.empty) {
        o = format(q"¶%s
      
        %s
          %s
        
      
¶",
          _horizontal_rule,
          obj.heading_lev_markup,
          obj.is_a,
          tags,
          _txt,
          obj.heading_lev_markup,
        );
      } else {
        o = format(q"¶%s
      ¶",
        _horizontal_rule,
        obj.obj_cite_number,
        obj.obj_cite_number,
        obj.heading_lev_markup,
        obj.is_a,
        obj.obj_cite_number,
        obj.obj_cite_number,
        tags,
        _txt,
        obj.heading_lev_markup,
        );
      }
      return o;
    }
    auto heading_scroll(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      _txt = inline_markup_scroll(obj, _txt, _suffix);
      string o = heading(obj, _txt);
      return o;
    }
    auto heading_seg(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
      string                     _type = "html",
    ) {
      auto t = inline_markup_seg(obj, _txt, _suffix);
      _txt = t[0];
      string[] _endnotes = t[1];
      string o = heading(obj, _txt, _type);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    auto para(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      _txt = font_face(_txt);
      string o;
      _txt = (obj.bullet) ? ("●  " ~ _txt) : _txt;
      if (obj.obj_cite_number.empty) {
        o = format(q"¶  ¶",
          obj.is_a,
          obj.indent_hang,
          obj.indent_base,
          tags,
          _txt
        );
      } else {
        o = format(q"¶  ¶",
          obj.obj_cite_number,
          obj.obj_cite_number,
          obj.is_a,
          obj.indent_hang,
          obj.indent_base,
          obj.obj_cite_number,
          tags,
          _txt
        );
      }
      return o;
    }
    auto para_scroll(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      _txt = inline_markup_scroll(obj, _txt, _suffix);
      string o = para(obj, _txt);
      return o;
    }
    auto para_seg(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto t = inline_markup_seg(obj, _txt, _suffix);
      _txt = to!string(t[0]);
      string[] _endnotes = t[1];
      string o = para(obj, _txt);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    auto quote(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      _txt = font_face(_txt);
      string o;
      if (obj.obj_cite_number.empty) {
        o = format(q"¶  ¶",
          obj.is_a,
          _txt
        );
      } else {
        o = format(q"¶  ¶",
          obj.obj_cite_number,
          obj.obj_cite_number,
          obj.is_a,
          obj.obj_cite_number,
          _txt
        );
      }
      return o;
    }
    auto quote_scroll(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      _txt = inline_markup_scroll(obj, _txt, _suffix);
      string o = quote(obj, _txt);
      return o;
    }
    auto quote_seg(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto t = inline_markup_seg(obj, _txt, _suffix);
      _txt = to!string(t[0]);
      string[] _endnotes = t[1];
      string o = quote(obj, _txt);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    auto group(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      _txt = font_face(_txt);
      string o;
      if (obj.obj_cite_number.empty) {
        o = format(q"¶  ¶",
          obj.is_a,
          _txt
        );
      } else {
        o = format(q"¶  ¶",
          obj.obj_cite_number,
          obj.obj_cite_number,
          obj.is_a,
          obj.obj_cite_number,
          _txt
        );
      }
      return o;
    }
    auto group_scroll(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      _txt = inline_markup_scroll(obj, _txt, _suffix);
      string o = group(obj, _txt);
      return o;
    }
    auto group_seg(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto t = inline_markup_seg(obj, _txt, _suffix);
      _txt = to!string(t[0]);
      string[] _endnotes = t[1];
      string o = group(obj, _txt);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    auto block(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      _txt = font_face(_txt);
      string o;
      if (obj.obj_cite_number.empty) {
        o = format(q"¶  ¶",
          obj.is_a,
          _txt.stripRight
        );
      } else {
        o = format(q"¶  ¶",
          obj.obj_cite_number,
          obj.obj_cite_number,
          obj.is_a,
          obj.obj_cite_number,
          _txt.stripRight
        );
      }
      return o;
    }
    auto block_scroll(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      _txt = inline_markup_scroll(obj, _txt, _suffix);
      string o = block(obj, _txt);
      return o;
    }
    auto block_seg(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto t = inline_markup_seg(obj, _txt, _suffix);
      _txt = to!string(t[0]);
      string[] _endnotes = t[1];
      string o = block(obj, _txt);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    auto verse(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      _txt = font_face(_txt);
      string o;
      if (obj.obj_cite_number.empty) {
          o = format(q"¶  ¶",
          obj.is_a,
          _txt
        );
      } else {
        o = format(q"¶  ¶",
          obj.obj_cite_number,
          obj.obj_cite_number,
          obj.is_a,
          obj.obj_cite_number,
          _txt
        );
      }
      return o;
    }
    auto verse_scroll(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      _txt = inline_markup_scroll(obj, _txt, _suffix);
      string o = verse(obj, _txt);
      return o;
    }
    auto verse_seg(O)(
      auto return ref const O    obj,
      string                     _txt,
      string                     _suffix = ".html",
    ) {
      auto t = inline_markup_seg(obj, _txt, _suffix);
      _txt = to!string(t[0]);
      string[] _endnotes = t[1];
      string o = verse(obj, _txt);
      auto u = tuple(
        o,
        _endnotes,
      );
      return u;
    }
    auto tablarize(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      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 ~= "";
          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 ~= "
";
        }
      auto t = tuple(
        _table,
        _tablenote,
      );
      return t;
    }
    auto table(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      auto tags = _xhtml_anchor_tags(obj.anchor_tags);
      _txt = font_face(_txt);
      auto t = tablarize(obj, _txt);
      _txt = t[0];
      string _note = t[1];
      string o;
      o = format(q"¶  ¶",
        obj.obj_cite_number,
        obj.obj_cite_number,
        obj.is_a,
        obj.obj_cite_number,
        tags,
        _txt,
        _note
      );
      return o;
    }
    auto endnote(O)(
      auto return ref const O    obj,
      string                     _txt,
    ) {
      string o;
      o = format(q"¶    
      %s
    
¶",
        obj.is_a,
        obj.indent_hang,
        obj.indent_base,
        _txt
      );
      return o;
    }
    auto code(O)(
      auto return ref const O  obj,
      string                   _txt,
    ) {
      string o;
      if (obj.obj_cite_number.empty) {
          o = format(q"¶  ¶",
          obj.is_a,
          _txt
        );
      } else {
        o = format(q"¶  ¶",
          obj.obj_cite_number,
          obj.obj_cite_number,
          obj.is_a,
          obj.obj_cite_number,
          _txt
        );
      }
      return o;
    }
  }
}