aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sdp/ao_conf_make_meta_native.d
blob: 899b59f86c4d5e17e857882d60a397b6b12f3605 (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
/++
  native headers using<br>@title:<BR>:subtitle:<BR>type tags<BR>
  extract native/orig header return associative array
+/
module sdp.ao_conf_make_meta_native;
template SiSUheaderExtractNative() {
  private import
    std.exception,
    std.regex,
    std.stdio,
    std.string,
    std.traits,
    std.typecons,
    std.utf,
    std.conv : to;
  private import
    sdp.ao_rgx,
    sdp.ao_defaults;
  struct HeaderDocMetadataAndMakeNativeToAA {
    mixin SiSUregisters;
    mixin SiSUrgxInitFlags;
    mixin SiSUrgxInit;
    auto rgx = Rgx();
    enum State { off, on }
    string hm, hs;
    auto header_metadata_and_make_aa(H,Me,Ma)(
      H  header,
      Me dochead_meta,
      Ma dochead_make
    )
    in {
      debug(asserts){
        static assert(is(typeof(header)       == string));
        static assert(is(typeof(dochead_meta) == string[string][string]));
        static assert(is(typeof(dochead_make) == string[string][string]));
      }
    }
    body {
      scope(exit) {
        destroy(header);
        destroy(dochead_meta);
        destroy(dochead_make);
      }
      if (auto t = header.match(rgx.native_header_main)) {
        char[][] header_obj_spl =
          (cast(char[]) header).split(rgx.line_delimiter_ws_strip);
        auto hm = to!string(t.captures[1]);
        if (hm.match(rgx.main_headers)) {
          foreach (line; header_obj_spl) {
            if (auto m = line.match(rgx.native_header_main)) {
              if (!empty(m.captures[2])) {
                if (hm == "creator") {
                  dochead_meta[hm]["author"] =
                    to!string(m.captures[2]);
                } else if (hm == "title") {
                  dochead_meta[hm]["main"] =
                    to!string(m.captures[2]);
                } else if (hm == "publisher") {
                  dochead_meta[hm]["name"] =
                    to!string(m.captures[2]);
                }
              }
            } else if (auto s = match(line, rgx.native_header_sub)) {
              if (!empty(s.captures[2])) {
                auto hs = to!string(s.captures[1]);
                if ((hm == "make" )
                && (dochead_make[hm])) {
                  switch (hm) {
                  case "make":
                    if (hs.match(rgx.native_subhead_make)) {
                      if (dochead_make[hm][hs]) {
                        dochead_make[hm][hs] = to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  default:
                    break;
                  }
                } else if (dochead_meta[hm]) {
                  switch (hm) {
                  case "creator":
                    if (hs.match(rgx.native_subhead_creator)) {
                      if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "title":
                    if (hs.match(rgx.native_subhead_title)) {
                      if ((hs == "subtitle")
                      && (dochead_meta[hm]["sub"])) {
                        dochead_meta[hm]["sub"] =
                          to!string(s.captures[2]);
                      } else if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "rights":
                    if (hs.match(rgx.native_subhead_rights)) {
                      if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "date":
                    if (hs.match(rgx.native_subhead_date)) {
                      if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "original":
                    if (hs.match(rgx.native_subhead_original)) {
                      if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "classify":
                    if (hs.match(rgx.native_subhead_classify)) {
                      if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "identifier":
                    if (hs.match(rgx.native_subhead_identifier)) {
                      if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "notes":
                    if (hs.match(rgx.native_subhead_notes)) {
                      if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "publisher":
                    if (hs.match(rgx.native_subhead_publisher)) {
                      if (dochead_meta[hm][hs]) {
                        dochead_meta[hm][hs] =
                          to!string(s.captures[2]);
                      }
                    } else {
                      writeln("not a valid header type:", hm, ":", hs);
                      destroy(hm);
                      destroy(hs);
                    }
                    break;
                  case "links":
                    destroy(hm);
                    destroy(hs);
                    break;
                  default:
                    break;
                  }
                }
              }
            }
          }
        } else {
          writeln("not a valid header type:", hm);
        }
      }
      auto t = tuple(dochead_meta, dochead_make);
      static assert(t.length==2);
      return t;
    }
    private auto native_header_extract(L,Lo,O,T)(
      L                line,
      return ref Lo    line_occur,
      return ref O     an_object,
      return ref T     type
    ) {
      debug(asserts){
        static assert(is(typeof(line)       == char[]));
        static assert(is(typeof(line_occur) == int[string]));
        static assert(is(typeof(an_object)  == string[string]));
        static assert(is(typeof(type)       == int[string]));
      }
      if (line.matchFirst(rgx.native_header_make)) {   /+ matched header_make +/
        debug(header1) { /+ writeln(line); +/ }
        type["header"]      = State.on;
        type["header_make"] = State.on;
        type["header_meta"] = State.off;
        ++line_occur["header_make"];
        an_object["body_nugget"] ~= line ~= "\n";
      } else if (line.matchFirst(rgx.native_header)) { /+ matched header_metadata +/
        /+ (generic header match and not previously caught by header_make) +/
        debug(header1) { /+ writeln(line); +/ }
        type["header"]      = State.on;
        type["header_make"] = State.off;
        type["header_meta"] = State.on;
        ++line_occur["header_meta"];
        an_object["body_nugget"] ~= line ~= "\n";
      } else if (type["header_make"] == State.on
      && (line_occur["header_make"] > State.off)) {     /+ header_make flag set +/
        if (line.matchFirst(rgx.native_header_sub)) {  /+ sub-header +/
          debug(header1) { /+ writeln(line); +/ }
          ++line_occur["header_make"];
          an_object["body_nugget"] ~= line ~= "\n";
        }
      } else if (type["header_meta"] == State.on
      && (line_occur["header_meta"] > State.off)) {     /+ header_metadata flag set +/
        if (line.matchFirst(rgx.native_header_sub)) {  /+ sub-header +/
          debug(header1) { /+ writeln(line); +/ }
          ++line_occur["header_meta"];
          an_object["body_nugget"] ~= line ~= "\n";
        }
      }
      return an_object;
    }
    auto header_reset_states_common(Lo,O,T)(
      return ref Lo    line_occur,
      return ref O     an_object,
      return ref T     type
    ) {
      debug(asserts){
        static assert(is(typeof(line_occur) == int[string]));
        static assert(is(typeof(an_object)  == string[string]));
        static assert(is(typeof(type)       == int[string]));
      }
      line_occur["header_make"] = State.off;
      line_occur["header_meta"] = State.off;
      type["header"] = State.off;
      an_object.remove("body_nugget");
      an_object.remove("is");
      an_object.remove("attrib");
    }
    private auto headerNativeToAA(Hn)(Hn src_header) {
      debug(asserts){
        static assert(is(typeof(src_header) == char[]));
      }
      auto type = flags_type_init;
      type = [
       "header"          : State.off,
       "header_make"     : State.off,
       "header_meta"     : State.off,
      ];
      string[string] an_object;
      int[string] line_occur;
      auto dochead_make = make_aa;
      auto dochead_meta = meta_aa;
      auto set_header = HeaderDocMetadataAndMakeNativeToAA();
      char[][] source_header_arr =
        (cast(char[]) src_header).split(rgx.newline_eol_delimiter);
      foreach(header_line; source_header_arr) {
        if (auto m = header_line.matchFirst(rgx.comment)) {
          /+ matched comment +/
          debug(comment) {
          }
          header_reset_states_common(line_occur, an_object, type);
        } else if ((header_line.matchFirst(rgx.native_header))
        || (type["header_make"] == State.on
        && (line_occur["header_make"] > State.off))
        || (type["header_meta"] == State.on
        && (line_occur["header_meta"] > State.off))) {
          if (header_line.length == 0) {
            /+ header_make instructions (current line empty) +/
            auto dochead_metadata_and_make =
              set_header.header_metadata_and_make_aa(strip(an_object["body_nugget"]), dochead_meta, dochead_make);
            static assert(!isTypeTuple!(dochead_metadata_and_make));
            dochead_meta = dochead_metadata_and_make[0];
            dochead_make = dochead_metadata_and_make[1];
            header_reset_states_common(line_occur, an_object, type);
            type["header_make"] = State.off;
            type["header_meta"] = State.off;
            debug(headersdlang) {
              writeln(dochead_metadata_and_make);
            }
          } else {
            an_object = native_header_extract(header_line, line_occur, an_object, type);
          }
        }
      }
      auto t = tuple(
        dochead_make,
        dochead_meta,
      );
      return t;
    }
  }
}