#+TITLE: sdp output sisupod
#+AUTHOR: Ralph Amissah
#+EMAIL: [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+DESCRIPTION for documents - structuring, publishing in multiple formats and search
#+KEYWORDS
#+LANGUAGE: en
#+STARTUP: indent content
#+OPTIONS: H:3 num:nil toc:t \n:nil @:t ::t |:t ^:nil _:nil -:t f:t *:t <:t
#+OPTIONS: TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc
#+OPTIONS: author:nil email:nil creator:nil timestamp:nil
#+PROPERTY: header-args :padline no :exports code :noweb yes
#+EXPORT_SELECT_TAGS: export
#+EXPORT_EXCLUDE_TAGS: noexport
#+FILETAGS: :sdp:rel:output:sisupod:
#+TAGS: assert(a) class(c) debug(d) mixin(m) sdp(s) tangle(T) template(t) WEB(W) noexport(n)

[[./sdp.org][sdp]]  [[./][org/]]
[[./output_hub.org][output_hub]]
* sisupod                                         :module:sdp:source_sisupod:
** module template

#+BEGIN_SRC d :tangle ../src/sdp/output/source_sisupod.d
module sdp.output.source_sisupod;
template SiSUpod() {
  <<output_imports>>
  void SiSUpod(T)(T doc_matters) {
    <<source_sisupod_init>>
    try {
      <<source_sisupod_mkdirs>>
      <<source_sisupod_copy>>
      <<source_sisupod_zip>>
    }
    catch (ErrnoException ex) {
      // Handle error
    }
  }
}
#+END_SRC

** output imports

#+name: output_imports
#+BEGIN_SRC d
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;
#+END_SRC

** mkdir

#+name: source_sisupod_init
#+BEGIN_SRC d
debug(asserts) {
  // static assert(is(typeof(doc_matters) == tuple));
}
mixin SiSUoutputRgxInit;
string pwd = doc_matters.environment["pwd"];
auto src_path_info = doc_matters.src_path_info;
string lng = doc_matters.language;
auto pth_sisudoc_src = doc_matters.src_path_info;
auto pth_sisupod = SiSUpathsSisupodZipped!()(src_path_info, lng);
auto pth_sisupod_filesystem = SiSUpathsSisupodFileSystem!()(src_path_info, lng);
mixin SiSUlanguageCodes;
auto lang = Lang();
auto rgx = Rgx();
assert (doc_matters.source_filename.match(rgx.src_fn));
#+END_SRC

#+name: source_sisupod_mkdirs
#+BEGIN_SRC d
/+ create directory structure +/
if (doc_matters.opt_action_bool["source"]) {
  if (!exists(pth_sisupod_filesystem.text_root(doc_matters.source_filename))) {
    pth_sisupod_filesystem.text_root(doc_matters.source_filename).mkdirRecurse;
  }
  if (!exists(pth_sisupod_filesystem.conf_root(doc_matters.source_filename))) {
    pth_sisupod_filesystem.conf_root(doc_matters.source_filename).mkdirRecurse;
  }
  if (!exists(pth_sisupod_filesystem.media_root(doc_matters.source_filename))) {
    pth_sisupod_filesystem.media_root(doc_matters.source_filename).mkdirRecurse;
  }
  if (!exists(pth_sisupod_filesystem.css(doc_matters.source_filename))) {
    pth_sisupod_filesystem.css(doc_matters.source_filename).mkdirRecurse;
  }
  if (!exists(pth_sisupod_filesystem.image_root(doc_matters.source_filename))) {
    pth_sisupod_filesystem.image_root(doc_matters.source_filename).mkdirRecurse;
  }
  if (!exists(pth_sisupod_filesystem.doc_lng(doc_matters.source_filename))) {
    pth_sisupod_filesystem.doc_lng(doc_matters.source_filename).mkdirRecurse;
  }
}
#+END_SRC

** copy

#+name: source_sisupod_copy
#+BEGIN_SRC d
debug(sisupod) {
  writeln(__LINE__, ": ",
    doc_matters.source_filename, " -> ",
    pth_sisupod_filesystem.fn_doc(
    doc_matters.source_filename,
  ));
}
auto zip = new ZipArchive();
auto fn_sisupod = pth_sisupod.sisupod_filename(doc_matters.source_filename);
{ /+ bundle images +/
  foreach (image; doc_matters.image_list) {
    debug(sisupodimages) {
      writeln(
        pth_sisudoc_src.image_root.to!string, "/", image, " -> ",
        pth_sisupod.image_root(doc_matters.source_filename), "/", image
      );
    }
    auto fn_src = pth_sisudoc_src.image_root.to!string ~ "/" ~ image;
    auto fn_out =  pth_sisupod.image_root(doc_matters.source_filename).to!string ~ "/" ~ image;
    auto fn_out_filesystem =  pth_sisupod_filesystem.image_root(doc_matters.source_filename).to!string ~ "/" ~ image;
    if (exists(fn_src)) {
      if (doc_matters.opt_action_bool["source"]) {
        fn_src.copy(fn_out_filesystem);
      }
      if (doc_matters.opt_action_bool["sisupod"]) {
        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!()(pth_sisupod.sisupod_filename(fn_src), zip.build());
      }
    }
  }
}
{ /+ bundle sisu_document_make +/
  auto fn_src = pth_sisudoc_src.conf_root.to!string ~ "/" ~ "sisu_document_make"; // check (_sisu/sisu_document_make)
  auto fn_out = pth_sisupod.conf_root(doc_matters.source_filename).to!string ~ "/" ~ "sisu_document_make";
  auto fn_out_filesystem = pth_sisupod_filesystem.conf_root(doc_matters.source_filename).to!string ~ "/" ~ "sisu_document_make";
  if (exists(fn_src)) {
    if (doc_matters.opt_action_bool["source"]) {
      fn_src.copy(fn_out_filesystem);
    }
    if (doc_matters.opt_action_bool["sisupod"]) {
      auto zip_arc_member_file = new ArchiveMember();
      zip_arc_member_file.name = fn_out;
      auto zip_data = new OutBuffer();
      zip_data.write((fn_src).readText);
      zip_arc_member_file.expandedData = zip_data.toBytes();
      zip.addMember(zip_arc_member_file);
      createZipFile!()(pth_sisupod.sisupod_filename(fn_src), zip.build());
    }
  }
}
{ /+ bundle primary file +/
  auto fn_src = doc_matters.source_filename;
  auto fn_out = pth_sisupod.fn_doc(doc_matters.source_filename).to!string;
  auto fn_out_filesystem = pth_sisupod_filesystem.fn_doc(doc_matters.source_filename).to!string;
  if (exists(fn_src)) {
    if (doc_matters.opt_action_bool["source"]) {
      fn_src.copy(fn_out_filesystem);
    }
    if (doc_matters.opt_action_bool["sisupod"]) {
      auto zip_arc_member_file = new ArchiveMember();
      zip_arc_member_file.name = fn_out;
      auto zip_data = new OutBuffer();
      zip_data.write((fn_src).readText);
      zip_arc_member_file.expandedData = zip_data.toBytes();
      zip.addMember(zip_arc_member_file);
      createZipFile!()(pth_sisupod.sisupod_filename(fn_src), zip.build());
    }
  }
}
{ /+ bundle insert files +/
  if (doc_matters.file_insert_list.length > 0) {
    foreach (insert_file; doc_matters.file_insert_list) {
      debug(sisupod) {
        writeln(
          insert_file, " -> ",
          pth_sisupod.fn_doc_insert(
            doc_matters.source_filename,
            insert_file,
        ));
      }
      auto fn_src = insert_file;
      auto fn_out = pth_sisupod.fn_doc_insert(
        doc_matters.source_filename,
        insert_file,
      ).to!string;
      auto fn_out_filesystem = pth_sisupod_filesystem.fn_doc_insert(
        doc_matters.source_filename,
        insert_file,
      ).to!string;
      if (exists(fn_src)) {
        if (doc_matters.opt_action_bool["source"]) {
          fn_src.copy(fn_out_filesystem);
        }
        if (doc_matters.opt_action_bool["sisupod"]) {
          auto zip_arc_member_file = new ArchiveMember();
          zip_arc_member_file.name = insert_file;
          auto zip_data = new OutBuffer();
          zip_data.write((fn_src).readText);
          zip_arc_member_file.expandedData = zip_data.toBytes();
          zip.addMember(zip_arc_member_file);
          createZipFile!()(pth_sisupod.sisupod_filename(fn_src), zip.build());
        }
      }
    }
  }
}
#+END_SRC

** sha256 of sisupod.zip, zip debug, read zip archive

#+name: source_sisupod_copy
#+BEGIN_SRC d
if (exists(fn_sisupod)) {
  try {
    auto data = (cast(byte[]) (fn_sisupod).read);
    writefln("%-(%02x%) %s", data.sha256Of, fn_sisupod);
    debug(sisupod) {
      try {
        auto zipped = new ZipArchive((fn_sisupod).read);
        foreach (filename, member; zipped.directory) {
          auto data = zipped.expand(member);
          writeln("> ", filename, " length ", data.length);
        }
      }
      catch (ZipException ex) {
        // Handle errors
      }
      if (doc_matters.source_filename == "sisudoc/text/en/the_wealth_of_networks.yochai_benkler.sst") {
        assert(
          ((data).sha256Of).toHexString
          == "626F83A31ED82F42CF528E922C1643498A137ABA3F2E5AFF8A379EA79EA22A1E",
          "\nsisupod: sha256 value for "
          ~ doc_matters.source_filename
          ~ " has changed, is now: "
          ~ ((data).sha256Of).toHexString
        );
      }
      if (doc_matters.source_filename == "sisudoc/text/en/sisu_markup_stress_test.sst") {
        assert(
          ((data).sha256Of).toHexString
          == "AAE0C87AB3F6D5F7385AEEA6EE661F56D40475CFE87AD930C78C9FE07FFB0D91",
          "\nsisupod: sha256 value for "
          ~ doc_matters.source_filename
          ~ " has changed, is now: "
          ~ ((data).sha256Of).toHexString
        );
      }
    }
  }
  catch (ErrnoException ex) {
    // Handle errors
  }
}
#+END_SRC