/+
- Name: SisuDoc 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 - 2024 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.sisudoc.org]
    [https://www.doc-reform.org]

  - Git
    [https://git.sisudoc.org/]

+/
module sisudoc.meta.metadoc_curate_topics;
@safe:
  import
    std.algorithm,
    std.array,
    std.exception,
    std.regex,
    std.stdio,
    std.string,
    std.conv : to;
  import
    sisudoc.meta.defaults,
    sisudoc.meta.rgx;
  mixin spineCurateMetadata;
  mixin InternalMarkup;
template spineMetaDocCuratesTopics() {
  static auto mkup = InlineMarkup();
  void spineMetaDocCuratesTopics(H,M,O)(
    H  hvst,
    M  _make_and_meta_struct,
    O  _opt_action,
  ) {
      string inline_search_form(M)(
        M  _make_and_meta_truct,
      ) {
        string o;
        string _form;
        if (_opt_action.html_link_search) {
          o = format(q"┃
      <div class="flex-menu-option">
      <!-- SiSU Spine Search -->
      <form action="%s" target="_top" method="POST" accept-charset="UTF-8" id="search">
      <font size="2">
      <input type="text" name="sf" size="24" maxlength="255">%s
      <input type="hidden" name="sml" value="1000">
      <input type="hidden" name="ec" value="on">
      <input type="hidden" name="url" value="on">
      <button type="submit" form="search">㏈ ፨</button>
      </font></form>
      <!-- SiSU Spine Search -->
      </div>┃",
          _make_and_meta_struct.conf.w_srv_cgi_action,
          (_make_and_meta_struct.conf.w_srv_db_sqlite_filename.empty)
            ? ""
            : "\n    <input type=\"hidden\" name=\"db\" value=\""
              ~ _make_and_meta_struct.conf.w_srv_db_sqlite_filename
              ~ "\">",
          );
        } else {
          o = "";
        }
        return o;
      }
      auto min_repeat_number = 42;
      string[] _document_topic_register;
      string[] _topic_register;
      string[] _sub_topic_register;
      string[] topics = [];
      string _auth = "";
      foreach(k, doc_curate; hvst.curates) {
        _topic_register = [];
        foreach(topic; doc_curate.topic_register_arr.sort) {
          _sub_topic_register = [];
          string _spaces;
          string[] subject_tree = topic.split(mkup.sep);
          switch (subject_tree.length) {
          case 1:
            hvst.subject_trees[subject_tree[0]]["_a"]["_a"]["_a"] ~= doc_curate;
            break;
          case 2:
            hvst.subject_trees[subject_tree[0]][subject_tree[1]]["_a"]["_a"] ~= doc_curate;
            break;
          case 3:
            hvst.subject_trees[subject_tree[0]][subject_tree[1]][subject_tree[2]]["_a"] ~= doc_curate;
            break;
          case 4:
            hvst.subject_trees[subject_tree[0]][subject_tree[1]][subject_tree[2]][subject_tree[3]] ~= doc_curate;
            break;
          default:
            break;
          }
          _topic_register ~= _sub_topic_register.join("\n");
        }
        auto char_repeat_number = (doc_curate.title.length
          + doc_curate.author.length + 16);
        char_repeat_number = (char_repeat_number > min_repeat_number)
        ? char_repeat_number
        : min_repeat_number;
        _document_topic_register ~= format(
          "\"%s\", %s%s\n%s",
          doc_curate.title,
          doc_curate.author,
          (doc_curate.date_published.length > 0) ? " (" ~ doc_curate.date_published ~ ")" : "",
          _topic_register.sort!("toUpper(a) < toUpper(b)", SwapStrategy.unstable).release.join("\n"),
        );
      }
string theme_dark_0 = format(q"┃
  body {
    color                    : #CCCCCC;
    background               : #000000;
    background-color         : #000000;
  }
  a:link {
    color                    : #FFFFFF;
    text-decoration          : none;
  }
  a:visited {
    color                    : #999999;
    text-decoration          : none;
  }
  a:hover {
    color                    : #000000;
    background-color         : #555555;
  }
  a:hover img {
    background-color         : #000000;
  }
  a:active {
    color                    : #888888;
    text-decoration          : underline;
  }
  a.lev0:hover {
    color                    : #FFFFFF;
    background-color         : #000000;
  }
  a.lev1:hover {
    color                    : #FFFFFF;
    background               : #333333;
  }
  a.lev2:hover {
    color                    : #FFFFFF;
    background               : #555555;
  }
  a.lev3:hover {
    color                    : #FFFFFF;
    background               : #777777;
  }
  a.lnkicon:link {
    text-decoration          : none;
  }
  a.lnkicon:visited {
    text-decoration          : none;
  }
  a.lnkicon:hover {
    font-size                : 160%%;
  }
  a:hover img {
    background-color         : #FFFFFF;
  }
  input, select, textarea {
    font-size                : 150%%;
  }
  input {
    color                    : #FFFFFF;
    background-color         : #777777;
  }
┃");
string theme_light_0 = format(q"┃
  body {
    color                    : #000000;
    background               : #FFFFFF;
    background-color         : #FFFFFF;
  }
  a:link {
    color                    : #003399;
    text-decoration          : none;
  }
  a:visited {
    color                    : #003399;
    text-decoration          : none;
  }
  a:hover {
    color                    : #000000;
    background-color         : #f9f9aa;
  }
  a:hover img {
    background-color         : #FFFFFF;
  }
  a:active {
    color                    : #003399;
    text-decoration          : underline;
  }
  a.lev0:hover {
    color                    : #000000;
    background-color         : #FFFFFF;
  }
  a.lev1:hover {
    color                    : #FFFFFF;
    background               : #444444;
  }
  a.lev2:hover {
    background               : #888888;
  }
  a.lev3:hover {
    background               : #BBBBBB;
  }
  a.lnkicon:link {
    text-decoration          : none;
  }
  a.lnkicon:visited {
    text-decoration          : none;
  }
  a.lnkicon:hover {
    font-size                : 160%%;
  }
  a:hover img {
    background-color         : #FFFFFF;
  }
  input, select, textarea {
    font-size                : 150%%;
  }
  input {
    color                    : #000000;
    background-color         : #FFFFFF;
  }
┃");
string theme_dark_1 = format(q"┃
  h1 {
    color                    : #FFFFFF;
    background               : #000000;
  }
  p.letter {
    color                    : #FFFFFF;
    background               : #333333;
  }
  p.lev0 {
    color                    : #FFFFFF;
    background               : #000000;
  }
  p.lev1 {
    color                    : #FFFFFF;
    background               : #333333;
  }
  p.lev2 {
    background               : #555555;
  }
  p.lev3 {
    background               : #777777;
  }
  p.lev4 {
    background               : #AAAAAA;
  }
  p.lev5 {
  }
┃");
string theme_light_1 = format(q"┃
  h1 {
    color                    : #FFFFFF;
    background               : #1A3A7A;
  }
  p.letter {
    color                    : #FFFFFF;
    background               : #1A3A7A;
  }
  p.lev0 {
    color                    : #FFFFFF;
    background               : #000000;
  }
  p.lev1 {
    color                    : #FFFFFF;
    background               : #444444;
  }
  p.lev2 {
    background               : #888888;
  }
  p.lev3 {
    background               : #BBBBBB;
  }
  p.lev4 {
    background               : #EEEEEE;
  }
  p.lev5 {
  }
┃");
      topics ~= format(q"┃<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>⌘ Curated metadata - ⌘ Topics</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="dc.title" content= "metadata curate, Authors & Topics - information Structuring Universe, Structured information Serialised Units" />
<meta name="dc.subject" content= "document structuring, ebook, publishing, PDF, LaTeX, XML, ODF, SQL, postgresql, sqlite, electronic book, electronic publishing, electronic document, electronic citation, data structure, citation systems, granular search, digital library" />
<meta name="generator" content="spine" />
<link rel="generator" href="https://sisudoc.org" />
<link href="./css/curate.css" rel="stylesheet">
<style TYPE="text/css">
/* spine curate css default stylesheet */%s
  .norm, .bold {
    line-height              : 150%%;
    margin-left              : 1em;
    margin-right             : 2em;
    margin-top               : 10px;
    margin-bottom            : 0px;
    text-indent              : 0mm;
  }
  p, h0, h1, h2, h3, h4, h5, h6, h7 {
    display                  : block;
    font-family              : verdana, arial, georgia, tahoma, sans-serif, helvetica, times, roman;
    font-size                : 100%%;
    font-weight              : normal;
    line-height              : 150%%;
    /* text-align            : justify; */
    margin-left              : 1em;
    text-indent              : 0mm;
    margin-top               : 2px;
    margin-bottom            : 2px;
    margin-right             : 6px;
    text-align               : left;
  }
  h0, h1, h2, h3, h4, h5, h6, h7 { text-shadow: .2em .2em .3em #999999; }
  h1 {
    font-size                : 120%%;
    font-weight              : bold;
    color                    : #FFFFFF;
    background               : #000088;
    margin-left              : 0em;
  }
  p.work {
    font-size                : 80%%;
    margin-left              : 5em;
    margin-top               : 0px;
    margin-bottom            : 0px;
    margin-right             : 6px;
    text-align               : left;
  }
  p.author {
    font-size                : 100%%;
    margin-left              : 2em;
    margin-top               : 0px;
    margin-bottom            : 0px;
    margin-right             : 6px;
    text-align               : left;
  }
  p.publication {
    font-size                : 90%%;
    margin-left              : 4em;
    margin-top               : 0px;
    margin-bottom            : 0px;
    margin-right             : 6px;
    text-align               : left;
  }
  p.letter {
    font-weight              : bold;
    font-size                : 80%%;
    margin-left              : 1em;
    margin-top               : 0px;
    margin-bottom            : 0px;
    margin-right             : 6px;
    text-align               : left;
  }
  p.icons, .icons_center {
    font-size                : 100%%;
    margin-top               : 0px;
    margin-bottom            : 0px;
    margin-right             : 6px;
  }
  p.icons {
    text-align               : left;
  }
  p.lev0 {
    font-size                : 120%%;
    margin-left              : 1em;
  }
  p.lev1 {
    font-size                : 115%%;
    margin-left              : 2em;
  }
  p.lev2 {
    font-size                : 110%%;
    margin-left              : 3em;
  }
  p.lev3 {
    font-size                : 105%%;
    margin-left              : 4em;
  }
  p.lev4 {
    font-size                : 100%%;
    margin-left              : 5em;
  }
  p.lev5 {
    font-size                : 95%%;
    margin-left              : 6em;
  }%s
  /* flex */
  .flex-menu-bar {
    display                  : -webkit-flex;
    display                  : flex;
    -webkit-flex-wrap        : wrap;
    -webkit-align-items      : center;
    align-items              : center;
    width                    : 100%%;
    margin-left              : 0%%;
    margin-right             : 2%%;
    background-color         : inherited;
  }
  .flex-menu-option {
    background-color         : inherited;
    margin-right             : 4px;
  }
  .flex-list {
    display                  : -webkit-flex;
    display                  : flex;
    -webkit-align-items      : center;
    display                  : block;
    align-items              : center;
    width                    : 100%%;
    background-color         : inherited;
  }
  .flex-list-item {
    background-color         : inherited;
    margin                   : 4px;
  }
</style>
<link rel="shortcut icon" href="../_sisu/image/rb7.ico" />
</head>
<body lang="en" xml:lang="en">
<a name="top" id="top"></a>
<a name="up" id="up"></a>
<a name="start" id="start"></a>
<h1>⌘ Curated metadata - ⌘ Topics (output organised by language &amp; filetype)</h1>
<div class="flex-menu-bar">
<div class="flex-menu-option">
<p class="icons">[<a href="../../index.html" class="lnkicon">&nbsp;⟰&nbsp;HOME&nbsp;</a>&nbsp;|&nbsp;<a href="../index.html" class="lnkicon">&nbsp;≅&nbsp;Collection&nbsp;</a>]
 [<a href="authors.html" class="lnkicon">&nbsp;⌘&nbsp;Curated&nbsp;metadata&nbsp;-&nbsp;&#x1F58B;&nbsp;Authors&nbsp;</a>]
</p>
</div>
 %s
</div>
<p><a href="#A" class="lnkicon">A</a>,&nbsp;<a href="#B" class="lnkicon">B</a>,&nbsp;<a href="#C" class="lnkicon">C</a>,&nbsp;<a href="#D" class="lnkicon">D</a>,&nbsp;<a href="#E" class="lnkicon">E</a>,&nbsp;<a href="#F" class="lnkicon">F</a>,&nbsp;<a href="#G" class="lnkicon">G</a>,&nbsp;<a href="#H" class="lnkicon">H</a>,&nbsp;<a href="#I" class="lnkicon">I</a>,&nbsp;<a href="#J" class="lnkicon">J</a>,&nbsp;<a href="#K" class="lnkicon">K</a>,&nbsp;<a href="#L" class="lnkicon">L</a>,&nbsp;<a href="#M" class="lnkicon">M</a>,&nbsp;<a href="#N" class="lnkicon">N</a>,&nbsp;<a href="#O" class="lnkicon">O</a>,&nbsp;<a href="#P" class="lnkicon">P</a>,&nbsp;<a href="#Q" class="lnkicon">Q</a>,&nbsp;<a href="#R" class="lnkicon">R</a>,&nbsp;<a href="#S" class="lnkicon">S</a>,&nbsp;<a href="#T" class="lnkicon">T</a>,&nbsp;<a href="#U" class="lnkicon">U</a>,&nbsp;<a href="#V" class="lnkicon">V</a>,&nbsp;<a href="#W" class="lnkicon">W</a>,&nbsp;<a href="#X" class="lnkicon">X</a>,&nbsp;<a href="#Y" class="lnkicon" class="lnkicon">Y</a>,&nbsp;<a href="#Z" class="lnkicon">Z</a>,&nbsp;
<p></p>
<hr />
┃",
  _opt_action.css_theme_default ? theme_light_0 : theme_dark_0,
  _opt_action.css_theme_default ? theme_light_1 : theme_dark_1,
  inline_search_form(_make_and_meta_struct),
) ~ "\n";
      char _prev_k = "_".to!char;
      int _kn;
      foreach(k0;
        hvst.subject_trees.keys
        .sort!("toUpper(a) < toUpper(b)", SwapStrategy.unstable)
      ) {
        if (k0.toUpper.to!(char[])[0] != _prev_k) {
          topics ~= format(q"┃<p class="letter"><a name="%s">%s</a></p><p class="book_index_lev1"><a name="a"></a></p>┃",
            k0.toUpper.to!(char[])[0],
            k0.toUpper.to!(char[])[0],
          );
          _prev_k = k0.toUpper.to!(char[])[0];
        }
        if (k0 != "_a") {
          topics ~= format(q"┃<p class="lev0"><a name="%s" class="lev0">%s</a></p>┃",
            k0.translate([' ' : "_"]), k0,) ~ "\n";
          if (_opt_action.show_curate_topics) {
            writeln("", k0);
          }
          if ("_a" in hvst.subject_trees[k0]) {
            foreach (t_a_;
              hvst.subject_trees[k0]["_a"]["_a"]["_a"]
              .multiSort!("toUpper(a.title) < toUpper(b.title)", "a.author < b.author", SwapStrategy.unstable)
            ) {
              _auth = [];
              if (t_a_.author_arr.length < 2) {
                _auth = format(q"┃ <a href="authors.html#%s">%s</a>┃",
                  t_a_.author_surname.translate([' ' : "_"]),
                  t_a_.author,
                );
              } else {
                foreach (a; t_a_.author_arr) {
                  _auth ~= format(q"┃ <a href="authors.html#%s">%s</a>,┃",
                    t_a_.author_surname.translate([' ' : "_"]),
                    a,
                  );
                }
              }
              topics ~= format(q"┃<p class="work"><a href="%s">"%s"</a> - %s [<a href="%s">&nbsp;%s&nbsp;</a>]┃",
                t_a_.path_html_segtoc,
                t_a_.title,
                _auth,
                t_a_.path_html_metadata,
                t_a_.language,
              ) ~ "\n";
              if (_opt_action.show_curate_topics) {
                writeln("- ", t_a_.title, " - ", t_a_.author);
              }
            }
          }
          foreach(k1;
            hvst.subject_trees[k0].keys
            .sort!("toUpper(a) < toUpper(b)", SwapStrategy.unstable)
          ) {
            if (k1 != "_a") {
              topics ~= format(q"┃<p class="lev1"><a name="%s.%s" class="lev1">%s</a></p>┃",
                k0.translate([' ' : "_"]),
                k1.translate([' ' : "_"]), k1,) ~ "\n";
              if (_opt_action.show_curate_topics) {
                writeln("  ", k1);
              }
              if ("_a" in hvst.subject_trees[k0][k1]) {
                foreach (t_a_;
                  hvst.subject_trees[k0][k1]["_a"]["_a"]
                  .multiSort!("toUpper(a.title) < toUpper(b.title)", "a.author < b.author", SwapStrategy.unstable)
                ) {
                  _auth = [];
                  if (t_a_.author_arr.length < 2) {
                    _auth = format(q"┃ <a href="authors.html#%s">%s</a>┃",
                      t_a_.author_surname.translate([' ' : "_"]),
                      t_a_.author,
                    );
                  } else {
                    foreach (a; t_a_.author_arr) {
                      _auth ~= format(q"┃ <a href="authors.html#%s">%s</a>,┃",
                        t_a_.author_surname.translate([' ' : "_"]),
                        a,
                      );
                    }
                  }
                  topics ~= format(q"┃<p class="work"><a href="%s">%s</a> - %s [<a href="%s">&nbsp;%s&nbsp;</a>]┃",
                    t_a_.path_html_segtoc,
                    t_a_.title,
                    _auth,
                    t_a_.path_html_metadata,
                    t_a_.language,
                  ) ~ "\n";
                  if (_opt_action.show_curate_topics) {
                    writeln("  - ", t_a_.title, " - ", t_a_.author);
                  }
                }
              }
            }
            foreach(k2;
              hvst.subject_trees[k0][k1].keys
              .sort!("toUpper(a) < toUpper(b)", SwapStrategy.unstable)
            ) {
              if (k2 != "_a") {
                topics ~= format(q"┃<p class="lev2"><a name="%s.%s.%s" class="lev2">%s</a></p>┃",
                  k0.translate([' ' : "_"]), k1.translate([' ' : "_"]),
                  k2.translate([' ' : "_"]), k2,) ~ "\n";
                if (_opt_action.show_curate_topics) {
                  writeln("    ", k2);
                }
                if ("_a" in hvst.subject_trees[k0][k1][k2]) {
                  foreach (t_a_;
                    hvst.subject_trees[k0][k1][k2]["_a"]
                    .multiSort!("toUpper(a.title) < toUpper(b.title)", "a.author < b.author", SwapStrategy.unstable)
                  ) {
                    _auth = [];
                    if (t_a_.author_arr.length < 2) {
                      _auth = format(q"┃ <a href="authors.html#%s">%s</a>┃",
                        t_a_.author_surname.translate([' ' : "_"]),
                        t_a_.author,
                      );
                    } else {
                      foreach (a; t_a_.author_arr) {
                        _auth ~= format(q"┃ <a href="authors.html#%s">%s</a>,┃",
                          t_a_.author_surname.translate([' ' : "_"]),
                          a,
                        );
                      }
                    }
                    topics ~= format(q"┃<p class="work"><a href="%s">%s</a> - %s [<a href="%s">&nbsp;%s&nbsp;</a>]┃",
                      t_a_.path_html_segtoc,
                      t_a_.title,
                      _auth,
                      t_a_.path_html_metadata,
                      t_a_.language,
                    ) ~ "\n";
                    if (_opt_action.show_curate_topics) {
                      writeln("    - ", t_a_.title, " - ", t_a_.author);
                    }
                  }
                }
              }
              foreach(k3;
                hvst.subject_trees[k0][k1][k2].keys
                .sort!("toUpper(a) < toUpper(b)", SwapStrategy.unstable)
              ) {
                if (k3 != "_a") {
                  topics ~= format(q"┃<p class="lev3"><a name="%s.%s.%s.%s" class="lev3">%s</a></p>┃",
                    k0.translate([' ' : "_"]), k1.translate([' ' : "_"]), k2.translate([' ' : "_"]),
                    k3.translate([' ' : "_"]), k3,) ~ "\n";
                  if (_opt_action.show_curate_topics) {
                    writeln("      ", k3);
                  }
                  {
                    foreach (t_a_;
                      hvst.subject_trees[k0][k1][k2][k3]
                      .multiSort!("toUpper(a.title) < toUpper(b.title)", "a.author < b.author", SwapStrategy.unstable)
                    ) {
                      _auth = [];
                      if (t_a_.author_arr.length < 2) {
                        _auth = format(q"┃<a href="authors.html#%s">%s</a>┃",
                          t_a_.author_surname.translate([' ' : "_"]),
                          t_a_.author,
                        );
                      } else {
                        foreach (a; t_a_.author_arr) {
                          _auth ~= format(q"┃ <a href="authors.html#%s">%s</a>,┃",
                            t_a_.author_surname.translate([' ' : "_"]),
                            a,
                          );
                        }
                      }
                      topics ~= format(q"┃ <p class="work"><a href="%s">%s</a> - %s [<a href="%s">&nbsp;%s&nbsp;</a>]┃",
                        t_a_.path_html_segtoc,
                        t_a_.title,
                        _auth,
                        t_a_.path_html_metadata,
                        t_a_.language,
                      ) ~ "\n";
                      if (_opt_action.show_curate_topics) {
                        writeln("      - ", t_a_.title, " - ", t_a_.author);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      topics
      ~= format(q"┃
<hr />
<a name="bottom" id="bottom"></a>
<a name="down" id="down"></a>
<a name="end" id="end"></a>
<a name="finish" id="finish"></a>
<a name="stop" id="stop"></a>
<a name="credits"></a>
</body>
</html>
┃") ~ "\n";
    import sisudoc.io_out.paths_output;
    auto out_pth = spinePathsHTML!()(_make_and_meta_struct.conf.output_path, "");
    try {
      auto f = File(out_pth.curate("topics.html"), "w");
      foreach (o; topics) {
        f.writeln(o);
      }
    } catch (ErrnoException ex) {
      // Handle error
    }
  }
}