aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sdp/ao_header_extract_sdlang.d
blob: 16a36e9d5230cf759e8e1f5989b4bb40a2f1a85c (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
/+
  extract sdl header return sdl
+/
template SiSUheaderExtractSDLang() {
  private import
    std.regex;
  private import
    ao_rgx;
  struct HeaderExtractSDL {
    mixin SiSUregisters;
    mixin RgxInit;
    auto rgx = Rgx();
    private auto sdlangToAAmake(string[string][string] conf, Tag conf_sdlang) {
      foreach (maintag, subtags; conf) {
        foreach (subtag, content; subtags) {
          if (!(conf_sdlang.maybe.tags[maintag].empty)) {
            if (!(conf_sdlang.tags[maintag][0].maybe.attributes[subtag].empty)
            && (conf_sdlang.tags[maintag][0].attributes[subtag][0].value.length > 1)) {
              writeln(conf_sdlang.tags[maintag][0].attributes[subtag][0].value);
              conf[maintag][subtag] =
                to!string(conf_sdlang.tags[maintag][0].attributes[subtag][0].value);
            }
          }
        }
      }
      return conf;
    }
    private auto configSettingsSDLangToAAmake(Tag conf_sdlang) {
      auto conf = sdlangToAAmake(conf_aa, conf_sdlang);
      return conf;
    }
    private auto documentMakeSDLangToAAmake(Tag document_make_sdlang) {
      auto dochead_make = sdlangToAAmake(make_aa, document_make_sdlang);
      /+
      hm = "links";
      if (!(document_make_sdlang.maybe.tags[hm].empty)) {
        /+ TODO
          stuff to fix
        +/
        // hs = "link";
        // if (!(document_make_sdlang.tags[hm][0].maybe.attributes[hs].empty)
        // && (document_make_sdlang.tags[hm][0].attributes[hs][0].value.length > 1)) {
        //   writeln(document_make_sdlang.tags[hm][0].attributes[hs][0].value);
        //   dochead_meta[hm][hs] =
        //     to!string(document_make_sdlang.tags[hm][0].attributes[hs][0].value);
        // }
      }
      +/
      // writeln(dochead_make);
      return dochead_make;
    }
    final private auto headerMakeSDLang(in string src_header) {
      scope(failure) {
        stderr.writefln(
          "%s\n%s\n%s:%s failed here:\n  src_header: %s",
          __MODULE__, __FUNCTION__,
          __FILE__, __LINE__,
          src_header,
        );
      }
      Tag sdl_root_header;
      try {
        sdl_root_header = parseSource(src_header);
      }
      catch(SDLangParseException e) {
        stderr.writeln("SDLang problem with this document header:");
        stderr.writeln(src_header);
        // Error messages of the form:
        // myFile.sdl(5:28): Error: Invalid integer suffix.
        stderr.writeln(e.msg);
      }
      debug(sdlang) {
        writeln("header SDL:");
        writeln(sdl_root_header.toSDLDocument());
      }
      return sdl_root_header;
    }
    private auto headerSDLangGet(in char[] src_header) {
      char[][] source_header_arr =
        split(cast(char[]) src_header, rgx.line_delimiter);
      char[] header_clean;
      // TODO
      foreach(header_line; source_header_arr) {
        if (!match(header_line, rgx.comments)) {
          header_clean ~= header_line ~ "\n";
          // writeln(header_line);
        }
      }
      /+ get sdlang tags +/
      auto header_sdlang=headerMakeSDLang(to!string(header_clean));
      debug(sdlang) {
        writeln("--------------");
        stdout.rawWrite( header_sdlang.toSDLDocument() );
        writeln("--------------");
        Value test = header_sdlang.tags["title"][0].values[0];
        assert(test == typeid(string));
        writeln(header_sdlang.maybe.tags["title"]);
        writeln(header_sdlang.maybe.tags["title"][0].maybe.attributes["subtitle"]);
      }
      return header_sdlang; // sdlang.ast.Tag
    }
    private auto headerSDLangToAAmake(Tag header_sdlang, string[string][string] dochead_make) {
      dochead_make = sdlangToAAmake(dochead_make, header_sdlang);
      auto dochead_meta = sdlangToAAmake(meta_aa, header_sdlang);
      /+
      /+ dochead +/
      string hm;
      string hs;
      /+ meta +/
      auto dochead_meta = meta_aa;
      hm = "title";
      if (!(header_sdlang.maybe.tags[hm].empty)) {
        /+ TODO Title REQUIRED +/
        hs = "main";
        if (!(header_sdlang.tags[hm].empty)
        && (header_sdlang.tags[hm][0].values[0].length > 1)) {
          writeln(header_sdlang.tags[hm][0].values[0]);
          dochead_meta[hm][hs] =
            to!string(header_sdlang.tags[hm][0].values[0]);
            // to!string(header_sdlang.tags[hm][0].attributes[hs][0].value);
        } else if (!(header_sdlang.tags[hm][0].maybe.attributes[hs].empty)
        && (header_sdlang.tags[hm][0].attributes[hs][0].value.length > 1)) {
          writeln(header_sdlang.tags[hm][0].attributes[hs][0].value);
          dochead_meta[hm][hs] =
            to!string(header_sdlang.tags[hm][0].attributes[hs][0].value);
        } else {
          writeln("Required header metadata Title, missing");
        }
        hs = "sub";
        if (!(header_sdlang.tags[hm][0].maybe.attributes[hs].empty)
        && (header_sdlang.tags[hm][0].attributes[hs][0].value.length > 1)) {
          writeln(header_sdlang.tags[hm][0].attributes[hs][0].value);
          dochead_meta[hm][hs] =
            to!string(header_sdlang.tags[hm][0].attributes[hs][0].value);
        } else if (!(header_sdlang.tags[hm][0].maybe.attributes["subtitle"].empty)
        && (header_sdlang.tags[hm][0].attributes["subtitle"][0].value.length > 1)) {
          writeln(header_sdlang.tags[hm][0].attributes["subtitle"][0].value);
          dochead_meta[hm][hs] =
            to!string(header_sdlang.tags[hm][0].attributes["subtitle"][0].value);
        }
        // full title composite (main + sub)
        // hs = "full";
        // dochead_meta[hm][hs] = dochead_meta[hm]["main"] ~ dochead_meta[hm]["sub"];
        hs = "language";
        if (!(header_sdlang.tags[hm][0].maybe.attributes["lang"].empty)
        && (header_sdlang.tags[hm][0].attributes["lang"][0].value.length > 1)) {
          writeln(header_sdlang.tags[hm][0].attributes["lang"][0].value);
          dochead_meta[hm][hs] =
            to!string(header_sdlang.tags[hm][0].attributes["lang"][0].value);
        } else if (!(header_sdlang.tags[hm][0].maybe.attributes[hs].empty)
        && (header_sdlang.tags[hm][0].attributes[hs][0].value.length > 1)) {
          writeln(header_sdlang.tags[hm][0].attributes[hs][0].value);
          dochead_meta[hm][hs] =
            to!string(header_sdlang.tags[hm][0].attributes[hs][0].value);
        }
      }
      hm = "creator";
      if (!(header_sdlang.maybe.tags[hm].empty)) {
        /+ Creator / Author REQUIRED +/
        /+ TODO
        - decide on representation for & deal with multiple authors;
        - author(s) full name;
        - author(s) surname & other parts
        +/
        hs = "author";
        if (!(header_sdlang.tags[hm][0].maybe.attributes[hs].empty)
        && (header_sdlang.tags[hm][0].attributes[hs][0].value.length > 1)) {
          writeln(header_sdlang.tags[hm][0].attributes[hs][0].value);
          dochead_meta[hm][hs] =
            to!string(header_sdlang.tags[hm][0].attributes[hs][0].value);
        }
      }
      hm = "links";
      if (!(header_sdlang.maybe.tags[hm].empty)) {
        /+ TODO
          stuff to fix
        +/
        // hs = "link";
        // if (!(header_sdlang.tags[hm][0].maybe.attributes[hs].empty)
        // && (header_sdlang.tags[hm][0].attributes[hs][0].value.length > 1)) {
        //   writeln(header_sdlang.tags[hm][0].attributes[hs][0].value);
        //   dochead_meta[hm][hs] =
        //     to!string(header_sdlang.tags[hm][0].attributes[hs][0].value);
        // }
      }
      hm = "publisher";
      if (!(header_sdlang.maybe.tags[hm].empty)) {
        hs = "name";
        if (!(header_sdlang.tags[hm][0].maybe.attributes[hs].empty)
        && (header_sdlang.tags[hm][0].attributes[hs][0].value.length > 1)) {
          writeln(header_sdlang.tags[hm][0].attributes[hs][0].value);
          dochead_meta[hm][hs] =
            to!string(header_sdlang.tags[hm][0].attributes[hs][0].value);
        }
      }
      }
      +/
      auto t = tuple(dochead_make, dochead_meta);
      static assert(!isTypeTuple!(t));
      return t;
    }
    private auto headerSDLangToAA(char[] header_sdlang_src, string[string][string] conf_doc_make_aa) {
      auto header_sdlang_tag = headerSDLangGet(header_sdlang_src); // sdlang.ast.Tag
      auto header_aa_tuple = headerSDLangToAAmake(header_sdlang_tag, conf_doc_make_aa);
      return header_aa_tuple;
    }
    
  }
}