aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sdp/source_sisupod.d
blob: cf15348682eda36e037197cad24dda378393da2d (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
template SiSUpod() {
  private import
    std.algorithm,
    std.array,
    std.container,
    std.digest.sha,
    std.exception,
    std.file,
    std.getopt,
    std.json,
    std.outbuffer,
    std.path,
    std.process,
    std.range,
    std.regex,
    std.stdio,
    std.string,
    std.traits,
    std.typecons,
    std.uni,
    std.utf,
    std.zip,
    std.conv : to;
  import
    create_zip_file,
    defaults,
    output_rgx,
    output_xhtmls;
  void SiSUpod(T)(T doc_matters) {
    debug(asserts) {
      // static assert(is(typeof(doc_matters) == tuple));
    }
    mixin SiSUoutputRgxInit;
    mixin SiSUpaths;
    auto pth_sisupod = SiSUpodPathsZipped();
    auto pth_sisupod_filesystem = SiSUpodPathsFilesystemArchive();
    mixin SiSUlanguageCodes;
    auto lang = Lang();
    auto rgx = Rgx();
    assert (doc_matters.source_filename.match(rgx.src_fn));
    try {
      /+ create directory structure +/
      if (!exists(pth_sisupod_filesystem.doc(doc_matters.source_filename))) {
        pth_sisupod_filesystem.doc(doc_matters.source_filename).mkdirRecurse;
      }
      if (!exists(pth_sisupod_filesystem.conf(doc_matters.source_filename))) {
        pth_sisupod_filesystem.conf(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(doc_matters.source_filename))) {
        pth_sisupod_filesystem.image(doc_matters.source_filename).mkdirRecurse;
      }
      if (!exists(pth_sisupod_filesystem.doc_lng(doc_matters.source_filename, doc_matters.language))) {
        pth_sisupod_filesystem.doc_lng(doc_matters.source_filename, doc_matters.language).mkdirRecurse;
      }
      debug(sisupod) {
        writeln(__LINE__, ": ",
          doc_matters.source_filename, " -> ",
          pth_sisupod_filesystem.fn_doc(
          doc_matters.source_filename,
          doc_matters.language
        ));
      }
      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(
              "_sisu/image/", image, " -> ",
              pth_sisupod.image(doc_matters.source_filename), "/", image
            );
          }
          auto fn_src = "_sisu/image/"~ image;
          auto fn_out =  pth_sisupod.image(doc_matters.source_filename).to!string ~ "/" ~ image;
          auto fn_out_filesystem =  pth_sisupod_filesystem.image(doc_matters.source_filename).to!string ~ "/" ~ image;
          if (exists(fn_src)) {
            fn_src.copy(fn_out_filesystem);
            {
              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 = "_sisu/sisu_document_make"; // check (_sisu/sisu_document_make)
        auto fn_out = pth_sisupod.conf(doc_matters.source_filename).to!string ~ "/" ~ "sisu_document_make";
        auto fn_out_filesystem = pth_sisupod_filesystem.conf(doc_matters.source_filename).to!string ~ "/" ~ "sisu_document_make";
        if (exists(fn_src)) {
          fn_src.copy(fn_out_filesystem);
          {
            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, doc_matters.language).to!string;
        auto fn_out_filesystem = pth_sisupod_filesystem.fn_doc(doc_matters.source_filename, doc_matters.language).to!string;
        if (exists(fn_src)) {
          fn_src.copy(fn_out_filesystem);
          {
            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,
                  doc_matters.language
              ));
            }
            auto fn_src = insert_file;
            auto fn_out = pth_sisupod.fn_doc_insert(
              doc_matters.source_filename,
              insert_file,
              doc_matters.language
            ).to!string;
            auto fn_out_filesystem = pth_sisupod_filesystem.fn_doc_insert(
              doc_matters.source_filename,
              insert_file,
              doc_matters.language
            ).to!string;
            if (exists(fn_src)) {
              fn_src.copy(fn_out_filesystem);
              {
                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());
              }
            }
          }
        }
      }
      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); // filename == member.name
                // Use data
              }
            }
            catch (ZipException ex) {
              // Handle errors
            }
            if (doc_matters.source_filename == "en/the_wealth_of_networks.yochai_benkler.sst") {
              assert(
                ((data).sha256Of).toHexString
                == "DDE0013C13C6A4F06D4BE72087E2CDEF47697CA38A6A2D65BA7207DB6B144271",
                "\nsisupod: sha256 value for "
                ~ doc_matters.source_filename
                ~ " has changed, is now: "
                ~ ((data).sha256Of).toHexString
              );
            }
            if (doc_matters.source_filename == "en/sisu_markup_stress_test.sst") {
              assert(
                ((data).sha256Of).toHexString
                == "112C0AEDD2518A1803D91A7CF5785274A3116C0779A631782D0C0813B212C68A",
                "\nsisupod: sha256 value for "
                ~ doc_matters.source_filename
                ~ " has changed, is now: "
                ~ ((data).sha256Of).toHexString
              );
            }
          }
        }
          catch (ErrnoException ex)
        {
          // Handle errors
        }
      }
      
    }
    catch (ErrnoException ex) {
      // Handle error
    }
  }
}