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
|
/++
sdlang headers<BR>
extract sdlang header return sdlang
+/
module sdp.ao.conf_make_meta_sdlang;
template SiSUheaderExtractSDLang() {
import
std.exception,
std.regex,
std.stdio,
std.string,
std.traits,
std.typecons,
std.utf,
std.conv : to;
import
sdp.ao.defaults,
sdp.ao.rgx;
struct HeaderExtractSDL {
mixin SiSUregisters;
mixin SiSUrgxInit;
auto rgx = Rgx();
private auto sdlangToAAheaderMakeMeta(C,Tag)(C conf, Tag conf_sdlang) {
debug(asserts){
static assert(is(typeof(conf) == string[string][string]));
}
foreach (maintag, subtags; conf) {
foreach (subtag, content; subtags) {
if (maintag in conf_sdlang.maybe.tags) {
Tag _maintag = conf_sdlang.getTag(maintag);
if ((subtag in _maintag.maybe.tags)
&& (_maintag.getTagValues(subtag).length > 0)) {
debug(headersdlang) {
writeln(__LINE__, ": ", maintag, ":", subtag, ": ", _maintag.getTagValues(subtag)[0]);
}
if (_maintag.getTagValues(subtag).length == 1) {
conf[maintag][subtag] =
(_maintag.getTagValues(subtag)[0]).to!string;
} else if (_maintag.getTagValues(subtag).length > 1) {
foreach (st; _maintag.getTagValues(subtag)) {
conf[maintag][subtag] ~=
st.to!string ~ ";";
}
}
} else if ((subtag in _maintag.maybe.attributes)
&& (_maintag.maybe.attributes[subtag][0].value.length > 0)) {
debug(headersdlang) {
writeln(__LINE__, ": ", maintag, ":", subtag, ": ", conf_sdlang.tags[maintag][0].attributes[subtag][0].value);
}
conf[maintag][subtag] =
(conf_sdlang.tags[maintag][0].attributes[subtag][0].value).to!string;
}
}
}
}
return conf;
}
private auto configSettingsSDLangToAAmake(Tag)(Tag conf_sdlang) {
auto conf = sdlangToAAheaderMakeMeta(conf_aa_empty, conf_sdlang);
return conf;
}
private auto documentMakeSDLangToAAmake(Tag)(Tag document_make_sdlang) {
auto dochead_make = sdlangToAAheaderMakeMeta(conf_aa_empty, document_make_sdlang);
return dochead_make;
}
final private auto headerMakeSDLang(Hs)(Hs src_header) {
debug(asserts){
static assert(is(typeof(src_header) == string));
}
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(ParseException 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(__LINE__, ": ", sdl_root_header.toSDLDocument());
writeln(__LINE__, ": ", sdl_root_header.maybe.namespaces);
writeln("header make sdlang: ", sdl_root_header.toSDLDocument());
writeln(__LINE__, ": ", sdl_root_header.getTagValues("title"));
writeln(__LINE__, ": ", sdl_root_header.getTagValues("creator"));
Tag creator = sdl_root_header.getTag("creator");
if (creator !is null) {
if ("author" in creator.maybe.tags) {
writeln(__LINE__, ": ", creator.getTagValues("author"));
} else if ("author" in creator.maybe.attributes) {
writeln(__LINE__, ": ", creator.maybe.attributes["author"][0].value);
}
}
}
return sdl_root_header;
}
private auto headerSDLangGet(Hs)(Hs src_header) {
debug(asserts){
static assert(is(typeof(src_header) == char[]));
}
char[][] source_header_arr =
(cast(char[]) src_header).split(rgx.newline_eol_delimiter);
char[] header_clean;
foreach(header_line; source_header_arr) {
if (!match(header_line, rgx.comments)) {
header_clean ~= header_line ~ "\n";
}
}
/+ get sdlang tags +/
auto header_sdlang=headerMakeSDLang(to!string(header_clean));
return header_sdlang; // sdlang.ast.Tag
}
private auto headerSDLangToAAmake(Tag,Ma)(Tag header_sdlang, Ma dochead_make) {
debug(asserts){
static assert(is(typeof(dochead_make) == string[string][string]));
}
dochead_make = sdlangToAAheaderMakeMeta(dochead_make, header_sdlang);
auto dochead_meta = sdlangToAAheaderMakeMeta(meta_aa_empty, header_sdlang);
if (dochead_meta["title"]["main"].empty) {
{
Tag _maintag = header_sdlang.getTag("title");
if (_maintag !is null) {
if ("main" in _maintag.maybe.tags) {
dochead_meta["title"]["main"] =
to!string(_maintag.getTagValues("main"));
} else if ("main" !in _maintag.maybe.attributes) {
writeln(__LINE__, ": ", _maintag.values[0]);
dochead_meta["title"]["main"] =
(_maintag.values[0]).to!string; // test that this exists
}
}
}
}
if (!(dochead_meta["title"]["subtitle"].empty)
&& (dochead_meta["title"]["sub"].empty)) {
dochead_meta["title"]["sub"] ~= dochead_meta["title"]["subtitle"];
}
dochead_meta["title"].remove("subtitle");
if (dochead_meta["title"]["sub"].empty) {
dochead_meta["title"]["full"] ~= dochead_meta["title"]["main"];
} else {
dochead_meta["title"]["full"] ~= format(
"%s - %s",
dochead_meta["title"]["main"],
dochead_meta["title"]["sub"],
);
}
dochead_meta["creator"]["author_raw"] = dochead_meta["creator"]["author"];
string[] authors_arr;
auto authors_raw_arr = dochead_meta["creator"]["author"].split(rgx.arr_delimiter);
foreach (author_raw; authors_raw_arr) {
authors_arr ~= author_raw.replace(rgx.raw_author_munge, "$2 $1");
}
dochead_meta["creator"]["author"] = join(authors_arr, ", ").chomp.chomp;
auto t = tuple(dochead_make, dochead_meta);
static assert(t.length==2);
return t;
}
private auto headerSDLangToAA(Hs,Ma)(Hs header_sdlang_src, Ma conf_doc_make_aa) {
debug(asserts){
static assert(is(typeof(header_sdlang_src) == char[]));
static assert(is(typeof(conf_doc_make_aa) == string[string][string]));
}
auto header_sdlang_tag = headerSDLangGet(header_sdlang_src);
auto header_aa_tuple = headerSDLangToAAmake(header_sdlang_tag, conf_doc_make_aa);
return header_aa_tuple;
}
}
}
|