aboutsummaryrefslogtreecommitdiffhomepage
path: root/org/meta_read_source_files.org
blob: 9569ee4ff58445089f35c38e884b3d86bf36f0c4 (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
#+TITLE:       sdp markup source raw
#+AUTHOR:      Ralph Amissah
#+EMAIL:       [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+DESCRIPTION: documents - structuring, publishing in multiple formats & search
#+KEYWORDS
#+LANGUAGE:    en
#+STARTUP:     indent content
#+OPTIONS:     H:3 num:nil toc:t \n:nil @:t ::t |:t ^:nil _:nil -:t f:t *:t <:t
#+OPTIONS:     TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc
#+OPTIONS:     author:nil email:nil creator:nil timestamp:nil
#+PROPERTY:    header-args :padline no :exports code :noweb yes
#+EXPORT_SELECT_TAGS:  export
#+EXPORT_EXCLUDE_TAGS: noexport
#+FILETAGS:            :sdp:rel:meta:read:file
#+TAGS: assert(a) class(c) debug(d) mixin(m) sdp(s) tangle(T) template(t) WEB(W) noexport(n)

[[./sdp.org][sdp]]  [[./][org/]]
* imports

#+name: imports_std
#+BEGIN_SRC d
import
  sdp.meta,
  std.file,
  std.path;
#+END_SRC

* 1. get _config file_ (read in)            :module:sdp:meta_read_config_files:
** 0. module template

#+BEGIN_SRC d :tangle ../src/sdp/meta/read_config_files.d
/++
  read configuration files<BR>
  - read config files<BR>
  meta_config_files.d
+/
module sdp.meta.read_config_files;
<<meta_config_file_in>>
<<meta_config_file_sdl>>
<<meta_config_file_hub>>
#+END_SRC

** 1. read config file, (conf.sdl & sisu_document_make)        :file:config:

#+name: meta_config_file_in
#+BEGIN_SRC d
static template configIn() {
  <<imports_std>>
  final string configIn(C,E)(C conf_sdl, E env) {
    /+ FIX clean up conf paths ↓ +/
    string sisudoc_conf_pwd = chainPath(to!string(env["pwd"]), "sisudoc/conf").array;
    string sisudoc_conf_pwd_a = chainPath(to!string(env["pwd"]), "conf").array;
    string sisudoc_conf_pwd_b = chainPath(to!string(env["pwd"]), "../conf").array;
    string sisudoc_conf_pwd_c = chainPath(to!string(env["pwd"]), "../../conf").array;
    string sisudoc_conf_pwd_d = chainPath(to!string(env["pwd"]), "../../../conf").array;
    /+ FIX clean up conf paths ↑
       (compare pwd to doc path location, and build config path)
    +/
    string dot_pwd = chainPath(to!string(env["pwd"]), ".sisu").array;
    string underscore_pwd = chainPath(to!string(env["pwd"]), "_sisu").array;
    string dot_home = chainPath(to!string(env["home"]), ".sisu").array;
    string[] possible_config_path_locations = [
      sisudoc_conf_pwd,
      sisudoc_conf_pwd_a,
      sisudoc_conf_pwd_b,
      sisudoc_conf_pwd_c,
      sisudoc_conf_pwd_d,
      dot_pwd,
      underscore_pwd,
      dot_home,
      "/etc/sisu"
    ];
    string config_file_str;
    foreach(pth; possible_config_path_locations) {
      auto conf_file = format(
        "%s/%s",
        pth,
        conf_sdl,
      );
      if (config_file_str.length > 0) {
        break;
      }
      try {
        if (exists(conf_file)) {
          debug(configfile) {
            writeln(conf_file);
          }
          config_file_str = conf_file.readText;
          break;
        }
      }
      catch (ErrnoException ex) {
      }
      catch (FileException ex) {
      }
    }
    return config_file_str;
  }
}
#+END_SRC

** 2. config files get sdlang root tag
*** 0. template
#+name: meta_config_file_sdl
#+BEGIN_SRC d
/+

+/
static template ConfigSDLang() {
  import sdlang;
  <<imports_std>>
  <<meta_config_file_sdlang>>
}
#+END_SRC

*** config file get sdl root tag                         :file:conf:sdlang:

#+name: meta_config_file_sdlang
#+BEGIN_SRC d
auto ConfigSDLang(string configuration, string conf_sdl_filename) {
  Tag sdl_root_conf;
  try {
    sdl_root_conf = parseSource(configuration);
  }
  catch(ParseException e) {
    stderr.writeln("SDLang problem with content for ", conf_sdl_filename);
    stderr.writeln(e.msg);
  }
  return sdl_root_conf;
}
#+END_SRC

** 3. config file (conf.sdl & sisu_document_make)          :file:config:hub:
*** 0. template

#+name: meta_config_file_hub
#+BEGIN_SRC d
/+
+/
static template configRead() {
  <<imports_std>>
  <<meta_config_file_import_sdlang>>
  <<meta_config_file_sdlang_hub>>
}
#+END_SRC

*** config hub import sdlang
#+name: meta_config_file_import_mixin
#+BEGIN_SRC d
import sdlang;
#+END_SRC

*** config hub function

Read in sdl config file name given, and return sdl root

#+name: meta_config_file_sdlang_hub
#+BEGIN_SRC d
final auto configRead(C,E)(C conf_sdl, E env) {
  auto configuration = configIn!()(conf_sdl, env);
  auto sdl_root = ConfigSDLang!()(configuration, conf_sdl);
  return sdl_root;
}
#+END_SRC

* 2. get _markup source_, read file         :module:sdp:meta_read_source_files:
** 0. module template

#+BEGIN_SRC d :tangle ../src/sdp/meta/read_source_files.d
/++
  module meta_read_source_files;<BR>
  - open markup files<BR>
  - if master file scan for addional files to import/insert
+/
module sdp.meta.read_source_files;
static template SiSUrawMarkupContent() {
  import
    sdp.meta.rgx;
  <<imports_std>>
  mixin SiSUrgxInit;
  static auto rgx = Rgx();
  auto rawsrc = RawMarkupContent();
  auto SiSUrawMarkupContent(Fn)(Fn fn_src) {
    auto _0_header_1_body_content_2_insert_filelist_tuple =
      rawsrc.sourceContentSplitIntoHeaderAndBody(rawsrc.sourceContent(fn_src), fn_src);
    return _0_header_1_body_content_2_insert_filelist_tuple;
  }
  struct RawMarkupContent {
    final sourceContent(in string fn_src) {
      auto raw = MarkupRawUnit();
      auto source_txt_str =
        raw.markupSourceReadIn(fn_src);
      return source_txt_str;
    }
    final auto sourceContentSplitIntoHeaderAndBody(in string source_txt_str, in string fn_src="") {
      auto raw = MarkupRawUnit();
      string[] insert_file_list;
      auto t =
        raw.markupSourceHeaderContentRawLineTupleArray(source_txt_str);
      auto header_raw = t[0];
      auto sourcefile_body_content = t[1];
      if (fn_src.match(rgx.src_fn_master)) { // filename with path needed if master file (.ssm) not otherwise
        auto ins = Inserts();
        auto tu =
          ins.scan_master_src_for_insert_files_and_import_content(sourcefile_body_content, fn_src);
        static assert(!isTypeTuple!(tu));
        sourcefile_body_content = tu[0];
        insert_file_list = tu[1].dup;
      }
      t = tuple(
        header_raw,
        sourcefile_body_content,
        insert_file_list
      );
      static assert(t.length==3);
      return t;
    }
  }
  struct MarkupRawUnit {
    import std.file;
    <<meta_markup_source_raw_read_file_source_string>>
    <<meta_markup_source_raw_doc_header_and_content_split>>
    <<meta_markup_source_raw_source_line_array>>
    <<meta_markup_source_raw_read_in_file>>
    <<meta_markup_source_raw_tuple_of_header_and_body>>
    <<meta_markup_source_raw_get_insert_source_line_array>>
  }
  struct Inserts {
    import sdp.meta.defaults;
    auto scan_subdoc_source(
      char[][] markup_sourcefile_insert_content,
      string fn_src
    ) {
      mixin SiSUrgxInitFlags;
      <<meta_inserts_scan>>
      foreach (line; markup_sourcefile_insert_content) {
        <<meta_inserts_scan_loop>>
      } // end src subdoc (inserts) loop
      <<meta_inserts_scan_post>>
    }
    auto scan_master_src_for_insert_files_and_import_content(
      char[][] sourcefile_body_content,
      string fn_src
    ) {
      mixin SiSUrgxInitFlags;
      <<meta_master_doc_scan_for_insert_filenames>>
      foreach (line; sourcefile_body_content) {
        <<meta_master_doc_scan_for_insert_filenames_loop>>
      } // end src doc loop
      <<meta_master_doc_scan_for_insert_filenames_post>>
    }
  }
}
#+END_SRC

** get markup source, read file                              :source:markup:
*** [#A] read file, source string                                  :string:

#+name: meta_markup_source_raw_read_file_source_string
#+BEGIN_SRC d
final private string readInMarkupSource(in char[] fn_src) {
  enforce(
    exists(fn_src)!=0,
    "file not found"
  );
  string source_txt_str;
  try {
    if (exists(fn_src)) {
      source_txt_str = fn_src.readText;
    }
  }
  catch (ErrnoException ex) {
  }
  catch (UTFException ex) {
    // Handle validation errors
  }
  catch (FileException ex) {
    // Handle errors
  }
  std.utf.validate(source_txt_str);
  return source_txt_str;
}
#+END_SRC

**** notes
source_txt_str = readText(fn_src); // ok

catch (ErrnoException ex) {
}

*** [#A] document header & content, array.length == 2               :array:

here you split document header and body, an array.length == 2
split is on first match of level A~ (which is required)

#+name: meta_markup_source_raw_doc_header_and_content_split
#+BEGIN_SRC d
final private char[][] header0Content1(in string src_text) {
  /+ split string on _first_ match of "^:?A~\s" into [header, content] array/tuple +/
  char[][] header_and_content;
  auto m = (cast(char[]) src_text).matchFirst(rgx.heading_a);
  header_and_content ~= m.pre;
  header_and_content ~= m.hit ~ m.post;
  assert(header_and_content.length == 2,
    "document markup is broken, header body split == "
    ~ header_and_content.length.to!string
    ~ "; (header / body array split should == 2 (split is on level A~))"
  );
  return header_and_content;
}
#+END_SRC

*** source line array                                               :array:

#+name: meta_markup_source_raw_source_line_array
#+BEGIN_SRC d
final private char[][] markupSourceLineArray(in char[] src_text) {
  char[][] source_line_arr =
    (cast(char[]) src_text).split(rgx.newline_eol_strip_preceding);
  return source_line_arr;
}
#+END_SRC

*** source content raw line array                                   :array:
- used for regular .sst files; master .ssm files and; .ssi inserts
- regex is passed for relevant enforce match

**** read in file

#+name: meta_markup_source_raw_read_in_file
#+BEGIN_SRC d
auto markupSourceReadIn(in string fn_src) {
  static auto rgx = Rgx();
  enforce(
    fn_src.match(rgx.src_pth),
    "not a sisu markup filename"
  );
  auto source_txt_str = readInMarkupSource(fn_src);
  return source_txt_str;
}
#+END_SRC

**** tuple header and body content

#+name: meta_markup_source_raw_tuple_of_header_and_body
#+BEGIN_SRC d
auto markupSourceHeaderContentRawLineTupleArray(in string source_txt_str) {
  string[] file_insert_list = [];
  auto hc = header0Content1(source_txt_str);
  auto header = hc[0];
  char[] source_txt = hc[1];
  auto source_line_arr = markupSourceLineArray(source_txt);
  auto t = tuple(
    header,
    source_line_arr,
    file_insert_list
  );
  return t;
}
#+END_SRC

**** get insert source line array

#+name: meta_markup_source_raw_get_insert_source_line_array
#+BEGIN_SRC d
final char[][] getInsertMarkupSourceContentRawLineArray(
  in char[] fn_src_insert,
  Regex!(char) rgx_file
) {
  enforce(
    fn_src_insert.match(rgx_file),
    "not a sisu markup filename"
  );
  auto source_txt_str = readInMarkupSource(fn_src_insert);
  auto source_line_arr = markupSourceLineArray(source_txt_str);
  return source_line_arr;
}
#+END_SRC

** get markup source, master file & inserts             :masterfile:inserts:
[[./sdp.org][sdp]]  [[./][org/]]

*** scan inserts (sub-document) source                    :scan_insert_src:
**** scan subdoc source

#+name: meta_inserts_scan
#+BEGIN_SRC d
char[][] contents_insert;
auto type1 = flags_type_init;
auto fn_pth_full = fn_src.match(rgx.src_pth);
auto markup_src_file_path = fn_pth_full.captures[1];
#+END_SRC

**** loop insert (sub-document)

#+name: meta_inserts_scan_loop
#+BEGIN_SRC d
if (type1["curly_code"] == 1) {
  type1["header_make"] = 0;
  type1["header_meta"] = 0;
  if (line.matchFirst(rgx.block_curly_code_close)) {
    type1["curly_code"] = 0;
  }
  contents_insert ~= line;
} else if (line.matchFirst(rgx.block_curly_code_open)) {
  type1["curly_code"] = 1;
  type1["header_make"] = 0;
  type1["header_meta"] = 0;
  contents_insert ~= line;
} else if (type1["tic_code"] == 1) {
  type1["header_make"] = 0;
  type1["header_meta"] = 0;
  if (line.matchFirst(rgx.block_tic_close)) {
    type1["tic_code"] = 0;
  }
  contents_insert ~= line;
} else if (line.matchFirst(rgx.block_tic_code_open)) {
  type1["tic_code"] = 1;
  type1["header_make"] = 0;
  type1["header_meta"] = 0;
  contents_insert ~= line;
} else if (
  (type1["header_make"] == 1)
  && line.matchFirst(rgx.native_header_sub)
) {
    type1["header_make"] = 1;
    type1["header_meta"] = 0;
} else if (
  (type1["header_meta"] == 1)
  && line.matchFirst(rgx.native_header_sub)
) {
    type1["header_meta"] = 1;
    type1["header_make"] = 0;
} else if (auto m = line.match(rgx.insert_src_fn_ssi_or_sst)) {
  type1["header_make"] = 0;
  type1["header_meta"] = 0;
  auto insert_fn = m.captures[2];
  auto insert_sub_pth = m.captures[1];
  auto fn_src_insert =
    chainPath(markup_src_file_path, insert_sub_pth ~ insert_fn).array;
  auto raw = MarkupRawUnit();
  auto markup_sourcesubfile_insert_content =
    raw.getInsertMarkupSourceContentRawLineArray(fn_src_insert, rgx.src_fn_find_inserts);
  debug(insert_file) {
    tell_l("red", line);
    tell_l("red", fn_src_insert);
    tell_l("fuchsia", "ERROR");
    writeln(
      "  length contents insert array: ",
      markup_sourcesubfile_insert_content.length
    );
  }
  auto ins = Inserts();
  /+
    1. load file,
    2. read lines;
    3. scan lines,
    4. if filename insert, and insert filename
    5.   repeat 1
    6. else
    7.   add line to new array;
  +/
} else {
  type1["header_make"] = 0;
  type1["header_meta"] = 0;
  contents_insert ~= line;
}
#+END_SRC

**** post loop

#+name: meta_inserts_scan_post
#+BEGIN_SRC d
return contents_insert;
#+END_SRC

*** scan document source                                         :scan_src:
**** scan doc source

#+name: meta_master_doc_scan_for_insert_filenames
#+BEGIN_SRC d
char[][] contents;
auto type = flags_type_init;
auto fn_pth_full = fn_src.match(rgx.src_pth);
auto markup_src_file_path = fn_pth_full.captures[1];
string[] insert_file_list =[];
#+END_SRC

**** include inserts: _loop master_ scan for inserts (insert documents)

#+name: meta_master_doc_scan_for_insert_filenames_loop
#+BEGIN_SRC d
if (type["curly_code"] == 1) {
  if (line.matchFirst(rgx.block_curly_code_close)) {
    type["curly_code"] = 0;
  }
  contents ~= line;
} else if (line.matchFirst(rgx.block_curly_code_open)) {
  type["curly_code"] = 1;
  contents ~= line;
} else if (type["tic_code"] == 1) {
  if (line.matchFirst(rgx.block_tic_close)) {
    type["tic_code"] = 0;
  }
  contents ~= line;
} else if (line.matchFirst(rgx.block_tic_code_open)) {
  type["tic_code"] = 1;
  contents ~= line;
} else if (auto m = line.match(rgx.insert_src_fn_ssi_or_sst)) {
  auto insert_fn = m.captures[2];
  auto insert_sub_pth = m.captures[1];
  auto fn_src_insert =
    chainPath(markup_src_file_path, insert_sub_pth ~ insert_fn).array;
    insert_file_list ~= to!string(fn_src_insert);
  auto raw = MarkupRawUnit();
  /+ TODO +/
  if (auto ma = line.match(rgx.src_fn_text)) {
    /+ .sst when inserted, not used: headers and heading level ^:?A~ so remove +/
    writeln(__LINE__); writeln(ma);
  }
  auto markup_sourcefile_insert_content =
    raw.getInsertMarkupSourceContentRawLineArray(fn_src_insert, rgx.src_fn_find_inserts);
  debug(insert_file) {
    tell_l("red", line);
    tell_l("red", fn_src_insert);
    writeln(
      "  length contents insert array: ",
      markup_sourcefile_insert_content.length
    );
  }
  auto ins = Inserts();
  auto contents_insert = ins.scan_subdoc_source(
    markup_sourcefile_insert_content,
    to!string(fn_src_insert)
  );
  contents ~= contents_insert;
  /+
    1. load file,
    2. read lines;
    3. scan lines,
    4. if filename insert, and insert filename
    5.   repeat 1
    6. else
    7.   add line to new array;
  +/
} else {
  contents ~= line;
}
#+END_SRC

**** post loop

#+name: meta_master_doc_scan_for_insert_filenames_post
#+BEGIN_SRC d
debug(insert_file) {
  writeln(__LINE__);
  writeln(contents.length);
}
auto t = tuple(
  contents,
  insert_file_list
);
return t;
#+END_SRC