/++
module ao_read_source_files;
- open markup files
- if master file scan for addional files to import/insert
+/
template SiSUmarkupRaw() {
private import
ao_rgx;
private import
std.algorithm,
std.array,
std.container,
std.exception,
std.stdio,
std.file,
std.path,
std.range,
std.regex,
std.string,
std.traits,
std.typecons,
std.uni,
std.utf,
std.conv : to;
mixin SiSUrgxInit;
auto rgx = Rgx();
auto rawsrc = MarkupRaw();
auto SiSUmarkupRaw(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 MarkupRaw {
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 (match(fn_src, 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 {
private import std.file;
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 = readText(fn_src);
}
}
catch (ErrnoException ex) {
}
catch (UTFException ex) {
// Handle validation errors
}
catch (FileException ex) {
// Handle errors
}
std.utf.validate(source_txt_str);
return source_txt_str;
}
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 = matchFirst(cast(char[]) src_text, 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 == " ~ to!string(header_and_content.length) ~
"; (header / body array split should == 2 (split is on level A~))"
);
return header_and_content;
}
final private char[][] markupSourceLineArray(in char[] src_text) {
char[][] source_line_arr =
split(cast(char[]) src_text, rgx.newline_eol_strip_preceeding);
return source_line_arr;
}
auto markupSourceReadIn(in string fn_src) {
auto rgx = Rgx();
enforce(
match(fn_src, rgx.src_pth),
"not a sisu markup filename"
);
auto source_txt_str = readInMarkupSource(fn_src);
return source_txt_str;
}
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;
}
final char[][] getInsertMarkupSourceContentRawLineArray(
in char[] fn_src,
Regex!(char) rgx_file
) {
enforce(
match(fn_src, rgx_file),
"not a sisu markup filename"
);
auto source_txt_str = readInMarkupSource(fn_src);
auto source_line_arr = markupSourceLineArray(source_txt_str);
return source_line_arr;
}
}
struct Inserts {
private import ao_defaults; // ao_defaults.d
auto scan_subdoc_source(
char[][] markup_sourcefile_insert_content,
string fn_src
) {
mixin SiSUrgxInitFlags;
char[][] contents_insert;
auto type1 = flags_type_init;
auto fn_pth_full = match(fn_src, rgx.src_pth);
auto markup_src_file_path = fn_pth_full.captures[1];
foreach (line; markup_sourcefile_insert_content) {
if (type1["curly_code"] == 1) {
type1["header_make"] = 0;
type1["header_meta"] = 0;
if (matchFirst(line, rgx.block_curly_code_close)) {
type1["curly_code"] = 0;
}
contents_insert ~= line;
} else if (matchFirst(line, 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 (matchFirst(line, rgx.block_tic_close)) {
type1["tic_code"] = 0;
}
contents_insert ~= line;
} else if (matchFirst(line, 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)
&& matchFirst(line, rgx.native_header_sub)
) {
type1["header_make"] = 1;
type1["header_meta"] = 0;
} else if (
(type1["header_meta"] == 1)
&& matchFirst(line, rgx.native_header_sub)
) {
type1["header_meta"] = 1;
type1["header_make"] = 0;
} else if (auto m = match(line, 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) { // 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 subdoc (inserts) loop
return contents_insert;
}
auto scan_master_src_for_insert_files_and_import_content(
char[][] sourcefile_body_content,
string fn_src
) {
mixin SiSUrgxInitFlags;
char[][] contents;
auto type = flags_type_init;
auto fn_pth_full = match(fn_src, rgx.src_pth);
auto markup_src_file_path = fn_pth_full.captures[1];
string[] insert_file_list =[];
foreach (line; sourcefile_body_content) {
if (type["curly_code"] == 1) {
if (matchFirst(line, rgx.block_curly_code_close)) {
type["curly_code"] = 0;
}
contents ~= line;
} else if (matchFirst(line, rgx.block_curly_code_open)) {
type["curly_code"] = 1;
contents ~= line;
} else if (type["tic_code"] == 1) {
if (matchFirst(line, rgx.block_tic_close)) {
type["tic_code"] = 0;
}
contents ~= line;
} else if (matchFirst(line, rgx.block_tic_code_open)) {
type["tic_code"] = 1;
contents ~= line;
} else if (auto m = match(line, 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 = match(line, 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) { // 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 doc loop
debug(insert) { // insert file
writeln(__LINE__);
writeln(contents.length);
}
auto t = tuple(
contents,
insert_file_list
);
return t;
}
}
}