aboutsummaryrefslogtreecommitdiffhomepage
path: root/org/out_src_pod.org
blob: 86680783e1427ea0659524c785d1bfe1f8e12bc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
-*- mode: org -*-
#+TITLE:       sisudoc spine (doc_reform) output pod
#+DESCRIPTION: documents - structuring, publishing in multiple formats & search
#+FILETAGS:    :spine:output:source:pod:
#+AUTHOR:      Ralph Amissah
#+EMAIL:       [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+COPYRIGHT:   Copyright (C) 2015 (continuously updated, current 2026) Ralph Amissah
#+LANGUAGE:    en
#+STARTUP:     content hideblocks hidestars noindent entitiespretty
#+PROPERTY:    header-args+ :eval never-export :exports code
#+PROPERTY:    header-args+ :noweb yes :padline no
#+PROPERTY:    header-args+ :results silent :cache no
#+PROPERTY:    header-args+ :mkdirp yes
#+OPTIONS:     H:3 num:nil toc:t \n:t ::t |:t ^:nil -:t f:t *:t
- magic single double-quote → " ← FIX changes hilighting behavior (occuring
  after it) in org document. INVESTIGATE (org-mode CONFIG?) FIND & FIX

- [[./doc-reform.org][doc-reform.org]]  [[./][org/]]
- [[./output_hub.org][output_hub]]

* pod
** _module template_ :module:

#+HEADER: :tangle "../src/sisudoc/outputs/io_out/source_pod.d"
#+HEADER: :noweb yes
#+BEGIN_SRC d
<<doc_header_including_copyright_and_license>>
module sisudoc.outputs.io_out.source_pod;
@system: // is not @safe: use: @system: or @trusted:
template spinePod() {
  <<output_imports>>
  void spinePod(T)(T doc_matters) {
    <<source_pod_init>>
    if (doc_matters.opt.action.source_or_pod) {
      try {
        /+ ↓ clean slate: remove the per-document pod directory before
           regeneration, but only on the first language of a multi-language
           document, so that subsequent languages' files are not wiped.
           .
           media/abstraction/ is left standing: the .ssp files there are
           written by the abstraction stage, which runs before this, and for
           a multi-language document they accumulate one language at a time.
           What is pruned from it instead is any .ssp that does not belong to
           a language this document has, which would otherwise linger from an
           earlier run and be bundled. That is done here rather than in the
           writer because pod actions run serially and the manifest's list of
           languages is to hand, where the writer may be one of several
           languages running at once.
        +/
        if (doc_matters.src.language == doc_matters.pod.manifest_list_of_languages[0]) {
          string doc_pod_dir = pths_pod.base_filesystem_(doc_matters.src.filename);
          string keep_dir
            = pths_pod.abstraction_root(doc_matters.src.filename).filesystem_open_zpod.to!string;
          if (exists(doc_pod_dir) && doc_pod_dir.isDir) {
            foreach (string _e; dirEntries(doc_pod_dir, SpanMode.shallow)) {
              if (_e.isDir) {
                if (keep_dir.startsWith(_e)) {   // media/, which holds abstraction/
                  foreach (string _m; dirEntries(_e, SpanMode.shallow)) {
                    if (_m == keep_dir) { continue; }
                    if (_m.isDir) { _m.rmdirRecurse; } else { _m.remove; }
                  }
                } else {
                  _e.rmdirRecurse;
                }
              } else {
                _e.remove;
              }
            }
          }
          if (exists(keep_dir) && keep_dir.isDir) {
            string _doc_uid_base
              = doc_matters.src.doc_uid_out[0 .. $ - doc_matters.src.lng.length];
            foreach (string _f; dirEntries(keep_dir, "*.ssp", SpanMode.shallow)) {
              bool _wanted = false;
              foreach (_lang; doc_matters.pod.manifest_list_of_languages) {
                if (_f.baseName == (_doc_uid_base ~ _lang ~ ".ssp")) {
                  _wanted = true;
                  break;
                }
              }
              if (!_wanted) { _f.remove; }
            }
          }
        }
        {
          podArchive_directory_tree(doc_matters, pths_pod);
        }
        {
          struct STsrcDigests {
            std.zip.ZipArchive     zip;
            string                 fn_pod;
            string[string][string] digests;
          }
          STsrcDigests _st;
          _st = pod_zipMakeReady(doc_matters, pths_pod, _st);
          {
            zipArchive(doc_matters, _st.fn_pod, _st.zip);
            if (doc_matters.src.language == doc_matters.pod.manifest_list_of_languages[$-1]) {
              zipArchiveDigest(doc_matters, _st.fn_pod, _st.digests);
            }
          }
        }
      } catch (ErrnoException ex) {
        // Handle error
      }
    }
  }
  <<source_pod_mkdirs>>
  <<struct_data_digest_and_length_return>>
  <<source_pod_archive_prepare_for_zip_and_get_digests>>
  <<source_pod_archive_zip>>
  <<zip_archive>>
  <<pod_source_files_digests>>
}
#+END_SRC

** init
*** imports

#+NAME: output_imports
#+BEGIN_SRC d
import std.digest.sha;
// import std.digest : toHexString;
import std.file;
import std.outbuffer;
import std.zip;
import std.conv : to;
import sisudoc.ocda.meta.rgx_files;
import sisudoc.outputs.io_out;
import sisudoc.ocda.io_in.carried_names;
import sisudoc.outputs.io_out.zip_pod;
import sisudoc.outputs.io_out.xmls;
#+END_SRC

*** init

#+NAME: source_pod_init
#+BEGIN_SRC d
debug(asserts) {
  // static assert(is(typeof(doc_matters) == tuple));
}
mixin spineRgxFiles;
string pwd = doc_matters.env.pwd;
auto pths_pod = spinePathsPods!()(doc_matters);
mixin spineLanguageCodes;
auto lang = Lang();
static auto rgx_files = RgxFiles();
assert (doc_matters.src.filename.match(rgx_files.src_fn));
#+END_SRC

** sha256

#+NAME: struct_data_digest_and_length_return
#+HEADER: :noweb yes
#+BEGIN_SRC d
// ↓ - object digest & length
struct ST_file_stat {
  ubyte[32] sha256hash;
  ulong     fileSize;
}
ST_file_stat getFileStat(F)(F filepath) @trusted {
  auto file = File(filepath, "r");
  ubyte[32] file_sha256 = digest!SHA256(file.byChunk(4096 * 1024));
  ulong fileSize = file.size;
  ST_file_stat _file;
  _file.sha256hash = file_sha256;
  _file.fileSize = fileSize;
  return _file;
}
// ↑ - object digest & length
#+END_SRC

** source pod archive, (prepare for zip) :copy:

#+NAME: source_pod_archive_prepare_for_zip_and_get_digests
#+BEGIN_SRC d
auto pod_zipMakeReady(M,P,S)(M doc_matters, P pths_pod, S _st) {
  auto pth_dr_doc_src = doc_matters.src_path_info;
  if (doc_matters.opt.action.vox_gt_3) { // correct
    writeln(__LINE__, ":", __FILE__, ":\n",
      doc_matters.src.filename, " -> ",
      pths_pod.fn_doc(doc_matters.src.filename, doc_matters.src.language).filesystem_open_zpod
    );
  }
  auto zip = new ZipArchive(); // needed
  auto fn_pod = pths_pod.pod_filename(doc_matters.src.filename).zpod;
  string[string][string] _digests;
  { // bundle images - get digest
    foreach (image; doc_matters.srcs.image_list) {
      if (doc_matters.opt.action.vox_gt_3) {
        writeln(
          pth_dr_doc_src.image_root.to!string, "/", image, " -> ",
          pths_pod.image_root(doc_matters.src.filename).zpod, "/", image
        );
      }
      auto fn_src_in = doc_matters.src.image_dir_path ~ "/" ~ image;
      auto fn_src_out_pod_zip_base
        = pths_pod.image_root(doc_matters.src.filename).zpod.to!string
        ~ "/" ~ image;
      auto fn_src_out_filesystem
        = pths_pod.image_root(doc_matters.src.filename).filesystem_open_zpod.to!string
        ~ "/" ~ image;
      if (exists(fn_src_in)) {
        debug(io) { writeln("(io debug) src out found: ", fn_src_in); }
        { // take DIGEST write to pod file digests.txt
          ST_file_stat file_ = getFileStat(fn_src_in);
          _digests["shared"]["images"] ~= file_.sha256hash.toHexString ~ "::" ~ file_.fileSize.to!string ~ " - " ~ image ~ "\n";
        }
        if (doc_matters.opt.action.source_or_pod) {
          fn_src_in.copy(fn_src_out_filesystem);
        }
        if (doc_matters.opt.action.source_or_pod) {
          zip = podArchive("file_path_bin", fn_src_in, fn_src_out_pod_zip_base, zip);
        }
      } else {
        if (doc_matters.opt.action.debug_do_pod && doc_matters.opt.action.vox_gt_2) {
          writeln("WARNING (io) src out NOT found (image): ", fn_src_in);
        }
      }
    }
  } { // the abstraction .ssp, written in place by --show-abstraction (only for --pod2)
    if (doc_matters.opt.action.pod2) {
      if (doc_matters.src.language == doc_matters.pod.manifest_list_of_languages[$-1]) { // wait until all language versions of .ssp written
        /+ the .ssp is written straight into pod/<doc>/media/abstraction/,
           so there is nothing to copy here: it is digested and added to
           the zip where it stands.
           doc_uid_out for any language follows the same pattern, differing
           only in the trailing ".{lng}". Strip the current language to
           reuse the base across all languages.
        +/
        string _doc_uid_base
          = doc_matters.src.doc_uid_out[0 .. $ - doc_matters.src.lng.length];
        foreach (_lang; doc_matters.pod.manifest_list_of_languages) { // do for all language versions
          string ssp_filename = _doc_uid_base ~ _lang ~ ".ssp";
          auto fn_src_out_pod_zip_base
            = pths_pod.abstraction_root(doc_matters.src.filename).zpod.to!string
            ~ "/" ~ ssp_filename;
          auto fn_src_in
            = pths_pod.abstraction_root(doc_matters.src.filename).filesystem_open_zpod.to!string
            ~ "/" ~ ssp_filename;
          if (exists(fn_src_in)) {
            debug(io) { writeln("(io debug) src out found: ", fn_src_in); }
            { // take DIGEST write to pod file digests.txt
              ST_file_stat file_ = getFileStat(fn_src_in);
              _digests[_lang]["ssp"] ~= file_.sha256hash.toHexString
                ~ "::" ~ file_.fileSize.to!string ~ " - " ~ ssp_filename ~ "\n";
            }
            zip = podArchive("file_path_text", fn_src_in, fn_src_out_pod_zip_base, zip);
          } else {
            if (doc_matters.opt.action.debug_do_pod && doc_matters.opt.action.vox_gt_2) {
              writeln("WARNING (io) src out NOT found (abstraction): ", fn_src_in);
            }
          }
          /+ ↓ the .ocda.db sits beside the pod rather than inside it: it
               carries the images itself, and putting it in the zip would
               store them twice. digested here so that digests.txt covers
               every artefact the pod directory holds for this document
          +/
          {
            string db_filename = _doc_uid_base ~ _lang ~ ".ocda.db";
            string fn_db = pths_pod.pod_dir_() ~ "/" ~ db_filename;
            if (exists(fn_db)) {
              ST_file_stat file_ = getFileStat(fn_db);
              _digests[_lang]["ocda_db"] ~= file_.sha256hash.toHexString
                ~ "::" ~ file_.fileSize.to!string ~ " - " ~ db_filename ~ "\n";
            }
          }
        }
      }
    }
  } { // bundle conf/document_make
    auto fn_src_in = ((doc_matters.src.is_pod)
      ? doc_matters.src.conf_dir_path
      : pth_dr_doc_src.conf_root).to!string
      ~ "/" ~ "document_make";
    auto fn_src_out_pod_zip_base
      = pths_pod.conf_root(doc_matters.src.filename).zpod.to!string ~ "/" ~ "document_make";
    auto fn_src_out_filesystem
      = pths_pod.conf_root(doc_matters.src.filename).filesystem_open_zpod.to!string
      ~ "/" ~ "document_make";
    if (exists(fn_src_in)) {
      debug(io) { writeln("(io debug) src out found: ", fn_src_in); }
      if (doc_matters.opt.action.source_or_pod) {
        fn_src_in.copy(fn_src_out_filesystem);
      }
      if (doc_matters.opt.action.source_or_pod) {
        zip = podArchive("file_path_text", fn_src_in, fn_src_out_pod_zip_base, zip);
      }
    } else {
      if (doc_matters.opt.action.debug_do_pod && doc_matters.opt.action.vox_gt_2) {
        writeln("WARNING (io) src out NOT found (document make): ", fn_src_in);
      }
    }
  } { /+ ↓ bundle tools/po4a, the translation working set
         .
         A directory walk, where every other thing the pod writer carries
         is named. A catalogue set is whatever the translator has, and
         naming .pot and .po files would mean the writer knowing the
         document's languages a second time, from a second place, to say
         what it already reads from the manifest.
         .
         Done once, on the last language, as the markup blocks are.
         .
         Every name is checked before it is carried, by the rule the pod
         reader applies to an archive it is handed. The writer is reading a
         directory someone else may have filled.
    +/
    if (doc_matters.src.is_pod
      && doc_matters.src.language == doc_matters.pod.manifest_list_of_languages[$-1]
    ) {
      mixin spineCarriedNames;
      enum string _po4a_root = "tools/po4a";
      string _tools_in = doc_matters.pod.manifest_path ~ "/" ~ _po4a_root;
      if (exists(_tools_in) && _tools_in.isDir) {
        foreach (string _f; dirEntries(_tools_in, SpanMode.depth)) {
          if (!(_f.isFile)) { continue; }
          string _rel = _po4a_root ~ "/" ~ _f[(_tools_in.length + 1) .. $];
          string _bad = validateCarriedPath(_rel);
          if (_bad.length > 0) {
            writeln("WARNING not carried into the pod: ", _bad);
            continue;
          }
          if (doc_matters.opt.action.source_or_pod) {
            string _out_fs = pths_pod.pod_root(doc_matters.src.filename)
              .filesystem_open_zpod.to!string ~ "/" ~ _rel;
            string _parent = _out_fs.dirName;
            if (!exists(_parent)) { _parent.mkdirRecurse; }
            _f.copy(_out_fs);
            zip = podArchive("file_path_bin", _f, "pod/" ~ _rel, zip);
          }
        }
      }
    }
  } { // pod manifest
    auto fn_src_in = doc_matters.src.file_with_absolute_path.to!string;
    auto fn_src_out_pod_zip_base
      = pths_pod.pod_manifest(doc_matters.src.filename).zpod.to!string;
    auto fn_src_out_filesystem
      = pths_pod.pod_manifest(doc_matters.src.filename).filesystem_open_zpod.to!string; // needed without root path
    auto fn_src_out_inside_pod
      = pths_pod.pod_manifest(doc_matters.src.filename).zpod.to!string; // needed without root path
    string[] filelist_src_out_pod_arr;
    string[] filelist_src_zpod_arr;
    if (exists(fn_src_in)) {
      debug(io) { writeln("(io debug) src in found: ", fn_src_in); }
      filelist_src_out_pod_arr ~= fn_src_out_pod_zip_base;
      filelist_src_zpod_arr ~= fn_src_out_inside_pod;
      {
        import dyaml;
        auto pod_filelist_yaml_string
          = File(pths_pod.fn_pod_filelist(doc_matters.src.filename).filesystem_open_zpod, "w");
        Node _pmy;
        string _pm = "doc:\n  filename: " ~ doc_matters.src.filename ~ "\n  language: " ~ doc_matters.pod.manifest_list_of_languages.to!string ~ "\n";
        if (doc_matters.opt.action.debug_do_pod && doc_matters.opt.action.vox_gt_2) {
          try {
            _pmy = Loader.fromString(_pm).load();
          } catch (ErrnoException ex) {
          } catch (Throwable) {
            writeln("ERROR failed to read config file content, not parsed as yaml");
          }
          writeln("pod filename:  ", _pmy["doc"]["filename"].get!string);
          writeln("pod languages: ", doc_matters.pod.manifest_list_of_languages.to!string);
          writeln("pod languages: ", doc_matters.src.language);
          // foreach(string _l; _pmy["doc"]["language"]) {
          //   writeln("language:      ", _l);
          // }
        }
        if (doc_matters.opt.action.source_or_pod) {
          pod_filelist_yaml_string.writeln(_pm);
        }
        if (doc_matters.opt.action.source_or_pod) {
          zip = podArchive("string", _pm, fn_src_out_pod_zip_base, zip);
        }
      }
    }
  } { // bundle primary file (.ssm/.sst) - get digest
    auto fn_src_in = doc_matters.src.file_with_absolute_path.to!string;
    auto fn_src_out_pod_zip_base
      = pths_pod.fn_doc(doc_matters.src.filename, doc_matters.src.language).zpod.to!string;
    auto fn_src_out_filesystem
      = pths_pod.fn_doc(doc_matters.src.filename, doc_matters.src.language).filesystem_open_zpod.to!string; // needed without root path:
    auto fn_src_out_inside_pod
      = pths_pod.fn_doc(doc_matters.src.filename, doc_matters.src.language).zpod.to!string; // needed without root path:
    string[] filelist_src_out_pod_arr;
    string[] filelist_src_zpod_arr;
    if (doc_matters.src.language == doc_matters.pod.manifest_list_of_languages[$-1]) { // wait until all language versions of .ssm parsed
      foreach (_lang; doc_matters.pod.manifest_list_of_languages) { // do for all language versions
        string fn_src_out_filesystem_lng
          = pths_pod.fn_doc(doc_matters.src.filename, _lang).filesystem_open_zpod.to!string;
        string _sstm = (doc_matters.pod.manifest_path ~ "/media/text/" ~ _lang ~ "/" ~ doc_matters.src.filename);
        string _pth_file_sstm;
        if (exists(_sstm))   { _pth_file_sstm = _sstm;
        } else if (exists(fn_src_in)) { _pth_file_sstm = fn_src_in;
        }
        if (exists(_pth_file_sstm)) { // what of language?
          debug(io) { writeln("(io debug) src in found: ", _pth_file_sstm); }
          { // take DIGEST write to pod file digests.txt
            ST_file_stat file_ = getFileStat(_pth_file_sstm);
            _digests[_lang]["sstm"] ~= file_.sha256hash.toHexString ~ "::" ~ file_.fileSize.to!string ~ " - " ~ doc_matters.src.filename ~ " - [" ~ _lang ~ "]";
          }
          filelist_src_out_pod_arr ~= fn_src_out_pod_zip_base;
          filelist_src_zpod_arr ~= fn_src_out_inside_pod;
          string _pod_to_markup_file = doc_matters.src.pod_name ~ "/" ~ "media/text/" ~ _lang ~ "/" ~ doc_matters.src.filename;
          if (doc_matters.opt.action.source_or_pod) {
            _pth_file_sstm.copy(fn_src_out_filesystem_lng);
          }
          if (doc_matters.opt.action.source_or_pod) {
            auto _rgx_sstm = regex(r"(?P<path_to_pod>\S+?)(?P<podname>[a-z_-]+)/(?P<from_root>media/text/)(?P<language>\S+?)/(?P<filename>\S+?\.ss[mt])");
            if (auto _x = _pth_file_sstm.match(_rgx_sstm)){
              if (doc_matters.src.lng == doc_matters.pod.manifest_list_of_languages[$-1]) { // again wait until all language versions of .ssm parsed
                string _path_to_pod = _x.captures["path_to_pod"];
                string _podname = _x.captures["podname"];
                string _root_to_lang = _x.captures["from_root"];
                string _language = _x.captures["language"]; // .ssi inserts expected to have same name across languages
                string _filename = _x.captures["filename"];
                foreach (_lang1; doc_matters.pod.manifest_list_of_languages) { // do for all language versions
                  string _pth_mkup_src_in = _path_to_pod ~ _podname ~ "/" ~ _root_to_lang ~ _lang1 ~ "/" ~ _filename;
                  string _pth_mkup_src_out = "pod/" ~ _root_to_lang ~ _lang1 ~ "/" ~ _filename;
                  // writeln("\nin:  ", _pth_mkup_src_in, "\nout: ", _pth_mkup_src_out); // DEBUG, REMOVE
                  zip = podArchive("file_path_text", _pth_mkup_src_in, _pth_mkup_src_out, zip);
                }
              }
            } else {
              zip = podArchive("file_path_text", _pth_file_sstm, fn_src_out_pod_zip_base, zip);
            }
          }
        } else {
          if (doc_matters.opt.action.debug_do_pod && doc_matters.opt.action.vox_gt_2) {
            writeln("WARNING (io) src in NOT found (markup source): \n", _sstm, "or in", fn_src_in);
          }
        }
      }
    }
  } { // bundle insert files (.ssi) - get digest
    if (doc_matters.srcs.file_insert_list.length > 0) {
      auto _rgx_ssi = regex(r"(?P<path_to_pod>\S+?)(?P<podname>[a-z_-]+)/(?P<from_root>media/text/)(?P<language>\S+?)/(?P<filename>\S+?\.ss[i])");
      foreach (insert_file; doc_matters.srcs.file_insert_list) {
        debug(pod) {
          writeln(
            insert_file, " -> ",
            pths_pod.fn_doc_insert(
              doc_matters.src.filename,
              insert_file,
              doc_matters.src.language,
            ).zpod
          );
        }
        if (auto _x = insert_file.match(_rgx_ssi)){
          if (doc_matters.src.lng == doc_matters.pod.manifest_list_of_languages[$-1]) {
            string _path_to_pod = _x.captures["path_to_pod"];
            string _podname = _x.captures["podname"];
            string _root_to_lang = _x.captures["from_root"];
            string _filename = _x.captures["filename"];
            /+ ↓ The digest key is the language being digested (as the sstm
                 digest above already does), not the language captured from the
                 resolved insert path.
            +/
            foreach (_lang; doc_matters.pod.manifest_list_of_languages) {
              string _pth_mkup_src_in = _path_to_pod ~ _podname ~ "/" ~ _root_to_lang ~ _lang ~ "/" ~ _filename;
              string _pth_mkup_src_out = "pod/" ~ _root_to_lang ~ _lang ~ "/" ~ _filename;
              /+ ↓ the digest must be taken inside this guard, not before it (getFileStat opens the file) +/
              if (exists(_pth_mkup_src_in)) {
                { // take DIGEST write to pod file digests.txt
                  ST_file_stat file_ = getFileStat(_pth_mkup_src_in);
                  _digests[_lang]["ssi"] ~= file_.sha256hash.toHexString ~ "::" ~ file_.fileSize.to!string ~ " - " ~ _filename ~ " - [" ~ _lang ~ "]" ~ "\n";
                }
                if (doc_matters.opt.action.source_or_pod) {
                  auto fn_src_out_filesystem // you need to change language sources
                    = pths_pod.fn_doc_insert(
                      doc_matters.src.filename, // doc_matters.src.filename
                      _pth_mkup_src_in, // insert_file
                      _lang,
                    ).filesystem_open_zpod.to!string;
                  _pth_mkup_src_in.copy(fn_src_out_filesystem); // check why here, thought dealt with elsewhere
                }
                if (doc_matters.opt.action.source_or_pod) {
                   zip = podArchive("file_path_text", _pth_mkup_src_in, _pth_mkup_src_out, zip);
                }
              } else {
                if (doc_matters.opt.action.debug_do_pod && doc_matters.opt.action.vox_gt_2) {
                  writeln("WARNING (io) src out NOT found (insert file): ", _pth_mkup_src_in);
                }
              }
            }
          }
        } else {
          auto fn_src_in = insert_file;
          /+ ↓ keyed on the document's own language, not on hard-coded "en". The
               emit loop looks each key up in the manifest's language list.
               FIX likely bug: insert_file still includes its path, and the
               line wants the filename as the pod carries it.
          +/
          if (exists(fn_src_in)) { // take DIGEST write to pod file digests.txt
            ST_file_stat file_ = getFileStat(fn_src_in);
            _digests[doc_matters.src.language]["ssi"] ~= file_.sha256hash.toHexString ~ "::" ~ file_.fileSize.to!string ~ " - " ~ insert_file ~ "\n";
          }
          auto fn_src_out_pod_zip_base
            = pths_pod.fn_doc_insert(
              doc_matters.src.filename,
              insert_file,
              doc_matters.src.language,
            ).zpod.to!string;
          auto fn_src_out_filesystem
            = pths_pod.fn_doc_insert(
              doc_matters.src.filename,
              insert_file,
              doc_matters.src.language,
            ).filesystem_open_zpod.to!string;
          if (exists(fn_src_in)) {
            debug(io) { writeln("(io debug) src out found: ", fn_src_in); }
            if (doc_matters.opt.action.source_or_pod) {
              fn_src_in.copy(fn_src_out_filesystem);
            }
            if (doc_matters.opt.action.source_or_pod) {
              zip = podArchive("file_path_text", fn_src_in, fn_src_out_pod_zip_base, zip);
            }
          } else {
            if (doc_matters.opt.action.debug_do_pod && doc_matters.opt.action.vox_gt_2) {
              writeln("WARNING (io) src out NOT found (insert file): ", fn_src_in);
            }
          }
        }
      }
    }
  }
  {
    _st.zip     = zip;
    _st.fn_pod  = fn_pod;
    _st.digests = _digests;
  }
  return _st;
}
#+END_SRC

** mkdir :mkdir:

- create directory structure

#+NAME: source_pod_mkdirs
#+BEGIN_SRC d
void podArchive_directory_tree(M,P)(M doc_matters, P pths_pod) { // create directory structure
  if (!exists(pths_pod.pod_dir_())) {
    // used both by pod zipped (& pod filesystem (unzipped) which makes its own recursive dirs)
    pths_pod.pod_dir_().mkdirRecurse;
  }
  if (doc_matters.opt.action.source_or_pod) {
    // if (doc_matters.opt.action.vox_gt_1) { writeln(" ", pths_pod.fn_pod_filelist(doc_matters.src.filename).filesystem_open_zpod); }
    if (!exists(pths_pod.text_root(doc_matters.src.filename).filesystem_open_zpod)) {
      pths_pod.text_root(doc_matters.src.filename).filesystem_open_zpod.mkdirRecurse;
    }
    if (!exists(pths_pod.conf_root(doc_matters.src.filename).filesystem_open_zpod)) {
      pths_pod.conf_root(doc_matters.src.filename).filesystem_open_zpod.mkdirRecurse;
    }
    if (!exists(pths_pod.media_root(doc_matters.src.filename).filesystem_open_zpod)) {
      pths_pod.media_root(doc_matters.src.filename).filesystem_open_zpod.mkdirRecurse;
    }
    if (!exists(pths_pod.css(doc_matters.src.filename).filesystem_open_zpod)) {
      pths_pod.css(doc_matters.src.filename).filesystem_open_zpod.mkdirRecurse;
    }
    if (doc_matters.opt.action.pod2) {
      if (!exists(pths_pod.abstraction_root(doc_matters.src.filename).filesystem_open_zpod)) {
        pths_pod.abstraction_root(doc_matters.src.filename).filesystem_open_zpod.mkdirRecurse;
      }
    }
    if (!exists(pths_pod.image_root(doc_matters.src.filename).filesystem_open_zpod)) {
      pths_pod.image_root(doc_matters.src.filename).filesystem_open_zpod.mkdirRecurse;
    }
    if (!exists(pths_pod.doc_lng(doc_matters.src.filename, doc_matters.src.language).filesystem_open_zpod)) {
      pths_pod.doc_lng(doc_matters.src.filename, doc_matters.src.language).filesystem_open_zpod.mkdirRecurse;
    }
  }
  if (!exists(pths_pod.pod_dir_() ~ "/index.html")) {
    import sisudoc.outputs.io_out.html_snippet;
    mixin htmlSnippet;
    auto f = File(pths_pod.pod_dir_() ~"/index.html", "w");
    f.writeln(format_html_blank_page_guide_home(
      "../../css/html_scroll.css",
      (doc_matters.opt.action.webserver_url_doc_root.length > 0)
        ? doc_matters.opt.action.webserver_url_doc_root
        : doc_matters.conf_make_meta.conf.w_srv_data_root_url,
      "../../index.html",
    ));
  }
}
#+END_SRC

** pod zip archive

#+NAME: source_pod_archive_zip
#+BEGIN_SRC d
@system auto podArchive(Z)(
  string _source_type,
  string _data_in,
  string _pth_out,
  Z zip
) {
  auto zip_arc_member_file = new ArchiveMember();
  zip_arc_member_file.name = _pth_out;
  auto zip_data = new OutBuffer();
  switch (_source_type) {
  case "file_path_bin":
    zip_data.write(cast(char[]) ((_data_in).read));
    goto default;
  case "file_path_text":
    zip_data.write((_data_in).readText);
    goto default;
  case "string":
    zip_data.write(_data_in);
    goto default;
  default:
    zip_arc_member_file.expandedData = zip_data.toBytes();
    zip.addMember(zip_arc_member_file);
  }
  return zip;
}
#+END_SRC

** zip archive

#+NAME: zip_archive
#+BEGIN_SRC d
void zipArchive(M,F,Z)(M doc_matters, F fn_pod, Z zip) {
  import sisudoc.ocda.zstd : zstdCompress, ZstdException;
  import sisudoc.ocda.io_in.carried_names : MAX_POD_ENTRY_COUNT;
  auto fn_src_in = doc_matters.src.filename;
  if (doc_matters.opt.action.pod) {
    /+ ↓ the writer is held to the number the reader enforces.
         Previously it was not: a document with enough parts produced an
         archive spine would write and then refused to read, reporting too many
         entries, so a good file looked corrupt. The count is a judgement call;
         a writer able to emit what the reader rejects is a fault whatever the
         number is.
         .
         An error rather than a warning, because the artefact would be
         unreadable: writing it and reporting success is the one outcome worth
         ruling out. The message names the count, the limit and the document, so
         that "this document has too many parts for one archive" is legible
         without a debugger.
    +/
    if (zip.directory.length > MAX_POD_ENTRY_COUNT) {
      writeln("ERROR >> pod archive not written: ", fn_pod);
      writeln("  ", doc_matters.src.filename_base, " would hold ",
        zip.directory.length, " members, over the ", MAX_POD_ENTRY_COUNT,
        " a pod archive may carry.");
      writeln("  a document's members are its languages times its markup",
        " and catalogue files; spine would not be able to read back what",
        " it wrote.");
      /+ ↓ an archive left by an earlier pass is removed.
           A missing artefact is an error anyone will notice; a quietly
           truncated one is not.
      +/
      if (exists(fn_pod)) {
        try {
          fn_pod.remove;
          writeln("  the incomplete archive left by an earlier language",
            " has been removed.");
        } catch (Exception ex) {
          writeln("WARNING could not remove the incomplete archive: ",
            fn_pod, " - ", ex.msg);
        }
      }
      return;
    }
    if (exists(doc_matters.src.file_with_absolute_path)) {
      try {
        /+ ↓ the archive is built exactly as before and then wrapped in one
             zstd frame. The wrap is here and not in createZipFile, which
             also writes every epub and every odt: those are real zips by
             their own specifications.
             .
             The compression gain is in whole-stream rather than per-member
             compression. A zip deflates each member on its own with a 32 KB
             window, which sees none of the redundancy between multiple language
             versions of a document and the catalogues that may repeat the
             English (original language): (live-manual is 10.31x smaller this
             way, where per-member deflate manages about 3x).
        +/
        auto _archive = cast(ubyte[]) zip.build();
        createZipFile!()(fn_pod,
          cast(void[]) zstdCompress(_archive, doc_matters.opt.action.pod_compression));
      } catch (ZstdException ex) {
        writeln("WARNING failed to compress pod archive: ", fn_pod, " - ", ex.msg);
      } catch (ErrnoException ex) {
        // Handle errors
      }
    } else { writeln("WARNING check missing source file(s): ", doc_matters.opt.action.pod); }
    if (!(exists(fn_pod))) { writeln("WARNING failed to create pod archive: ", fn_pod); }
  }
}
#+END_SRC

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

#+NAME: pod_source_files_digests
#+BEGIN_SRC d
void zipArchiveDigest(M,F,D)(M doc_matters, F fn_pod, D _digests) {
  import sisudoc.outputs.io_out.paths_output;
  auto pths_pod = spinePathsPods!()(doc_matters);
  char[] _zip_digest;
  try {
    if (!exists(pths_pod.pod_dir_())) {
      // used both by pod zipped (& pod filesystem (unzipped) which makes its own recursive dirs)
      pths_pod.pod_dir_().mkdirRecurse;
    }
  } catch (ErrnoException ex) {
    // Handle error
  }
  try {
    // if (doc_matters.opt.action.vox_gt_2) { writeln(" ", pths_pod.pod_dir_(), "/", doc_matters.src.filename_base, ".digests.txt"); }
    string _digest_fn = pths_pod.pod_dir_() ~ "/" ~ doc_matters.src.filename_base ~ ".digests.txt";
    // if (doc_matters.opt.action.vox_gt_2) { writeln(_digest_fn); }
    auto f = File(_digest_fn, "w");
    if (exists(fn_pod)) {
      try {
        auto data = (cast(byte[]) (fn_pod).read);
        // if (doc_matters.opt.action.vox_gt_2) { writeln(" ", doc_matters.src.filename, " > ", doc_matters.src.filename_base, pod_archive_suffix); }
        if (doc_matters.opt.action.pod) {
          _zip_digest = (data.sha256Of.toHexString ~ "::" ~ data.length.to!string ~ " - " ~ doc_matters.src.filename_base ~ pod_archive_suffix);
          if (doc_matters.opt.action.vox_gt_1) { writeln(" ", _zip_digest); }
          if (doc_matters.opt.action.vox_gt_1) { writeln(" ", pths_pod.pod_dir_(), "/", doc_matters.src.filename_base, "/"); }
          if (doc_matters.opt.action.vox_gt_1) { writeln(" ", _digest_fn); }
          f.writeln(_zip_digest);
        }
      } catch (ErrnoException ex) {
        // Handle errors
      }
    }
    foreach (_lang; doc_matters.pod.manifest_list_of_languages) {
      if (_lang in _digests) {
        if (("sstm" in _digests[_lang]) && (_digests[_lang]["sstm"].length > 0)) {
          // if (doc_matters.opt.action.vox_gt_2) { writeln(_digests[_lang]["sstm"]); }
          f.writeln(_digests[_lang]["sstm"]);
        }
        if (("ssi" in _digests[_lang]) && (_digests[_lang]["ssi"].length > 0)) {
          // if (doc_matters.opt.action.vox_gt_2) { writeln(_digests[_lang]["ssi"]); }
          f.writeln(_digests[_lang]["ssi"]);
        }
        if (("ssp" in _digests[_lang]) && (_digests[_lang]["ssp"].length > 0)) {
          f.writeln(_digests[_lang]["ssp"].strip);
        }
        if (("ocda_db" in _digests[_lang]) && (_digests[_lang]["ocda_db"].length > 0)) {
          f.writeln(_digests[_lang]["ocda_db"].strip);
        }
      }
    }
    if ("shared" in _digests) {
      if (("images" in _digests["shared"]) && (_digests["shared"]["images"].length > 0)) {
        // if (doc_matters.opt.action.vox_gt_2) { writeln(_digests["shared"]["images"]); }
        f.writeln(_digests["shared"]["images"]);
      }
    }
  } catch (ErrnoException ex) {
    // Handle error
  }
}
#+END_SRC

* org includes
** project version

#+NAME: spine_version
#+HEADER: :noweb yes
#+BEGIN_SRC emacs-lisp
<<./sisudoc_spine_version_info_and_doc_header_including_copyright_and_license.org:spine_project_version()>>
#+END_SRC

** year

#+NAME: year
#+HEADER: :noweb yes
#+BEGIN_SRC emacs-lisp
<<./sisudoc_spine_version_info_and_doc_header_including_copyright_and_license.org:year()>>
#+END_SRC

** document header including copyright & license

#+NAME: doc_header_including_copyright_and_license
#+HEADER: :noweb yes
#+BEGIN_SRC emacs-lisp
<<./sisudoc_spine_version_info_and_doc_header_including_copyright_and_license.org:spine_doc_header_including_copyright_and_license()>>
#+END_SRC

* __END__
** pod gitignore
*** pod/{pods/}

#+NAME: pod_gitignore
#+BEGIN_SRC shell
# git ls-files --others --exclude-from=.git/info/exclude
,*
!.gitignore
!COPYRIGHT
!README
!shell.nix
!packages.nix
!version.txt
!pod/
!pod/.dr/config_local_site
# pod.manifest
!pod/*/
!pod/*/pod.manifest
# conf/sisu_document_make
!pod/*/conf/
!pod/*/conf/sisu_document_make
# media/text
!pod/*/media/
!pod/*/media/text/
!pod/*/media/text/*/
!pod/*/media/text/*/*.sst
!pod/*/media/text/*/*.ssm
!pod/*/media/text/*/*.ssi
!pod/*/media/text/*/index.html.in
# media/image
!pod/*/media/image/
!pod/*/media/image/*.png
!pod/*/media/image/*.jpg
!pod/*/media/image/*.PNG
!pod/*/media/image/*.JPG
# tools/po4a
!pod/*/tools/
!pod/*/tools/Makefile
!pod/*/tools/bin/
!pod/*/tools/bin/*.sh
!pod/*/tools/po4a/
!pod/*/tools/po4a/pot/
!pod/*/tools/po4a/pot/*.sst.pot
!pod/*/tools/po4a/pot/*.ssm.pot
!pod/*/tools/po4a/pot/*.ssi.pot
!pod/*/tools/po4a/pot/index.html.in.pot
!pod/*/tools/po4a/po/
!pod/*/tools/po4a/po/*/
!pod/*/tools/po4a/po/*/*.sst.po
!pod/*/tools/po4a/po/*/*.ssm.po
!pod/*/tools/po4a/po/*/*.ssi.po
!pod/*/tools/po4a/po/*/index.html.in.po
# depreciate, separate from media/text use tools
!pod/*/media/text/Makefile
!pod/*/media/text/bin/
!pod/*/media/text/bin/*.sh
!pod/*/media/text/pot/
!pod/*/media/text/pot/*.sst.pot
!pod/*/media/text/pot/*.ssm.pot
!pod/*/media/text/pot/*.ssi.pot
!pod/*/media/text/pot/index.html.in.pot
!pod/*/media/text/po/
!pod/*/media/text/po/*/
!pod/*/media/text/po/*/*.sst.po
!pod/*/media/text/po/*/*.ssm.po
!pod/*/media/text/po/*/*.ssi.po
!pod/*/media/text/po/*/index.html.in.po
# skip
.dub/**
tmp/**
,*_.org
,*_.d
,*_.txt
,*_
,*~
\#*
,*.\#*
#+END_SRC

*** document pod structure

#+NAME: pod_gitignore
#+BEGIN_SRC shell
# git ls-files --others --exclude-from=.git/info/exclude
,*
!.gitignore
!pod.manifest
!conf/
!conf/sisu_document_make
!media/
!media/text/
!media/text/*/
!media/text/*/*.sst
!media/text/*/*.ssm
!media/text/*/*.ssi
!media/image/
!media/image/*.png
!media/image/*.jpg
!media/image/*.PNG
!media/image/*.JPG
#!media/**
#!*.sst
#!*.ssm
#!*.ssi
#!**/*.sst
#!**/*.ssm
#!**/*.ssi
#+END_SRC

*** pod po4a structure

#+NAME: pod_gitignore
#+BEGIN_SRC shell
# git ls-files --others --exclude-from=.git/info/exclude
,*
!.gitignore
!pot/
!pot/*.sst.pot
!pot/*.ssm.pot
!pot/*.ssi.pot
!po/
!po/*/
!po/*/*.sst.po
!po/*/*.ssm.po
!po/*/*.ssi.po
#+END_SRC