#+TITLE: sdp header extract #+AUTHOR: Ralph Amissah #+EMAIL: ralph.amissah@gmail.com #+STARTUP: indent #+LANGUAGE: en #+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:ao: #+TAGS: assert(a) class(c) debug(d) mixin(m) sdp(s) tangle(T) template(t) WEB(W) noexport(n) [[./sdp.org][sdp]] [[./][org/]] * 0. Header Hub :module:sdp:ao_conf_make_meta: ** module template #+BEGIN_SRC d :tangle ../src/sdp/ao_conf_make_meta.d /++ extract native/orig header return associative array
the header is passed as text (lopped off top of a sisu markup file until the required first heading ^A~), determine whether is a native header or sdlang one with a regex check if whether it contains the "native header" required tag/field @title: then process accordingly as a "native header" or "sdlang header" converting the metadata and make instructions to a common json format used by program internally. Moved to associative array. +/ module sdp.ao_conf_make_meta; template SiSUheaderExtractHub() { private import std.exception, std.regex, std.stdio, std.traits, std.typecons, std.utf, std.conv : to; private import sdlang; private import sdp.ao_rgx, sdp.ao_conf_make_meta_native, sdp.ao_conf_make_meta_sdlang; mixin SiSUrgxInit; mixin SiSUheaderExtractNative; mixin SiSUheaderExtractSDLang; auto rgx = Rgx(); auto SiSUheaderExtractHub(Src, DocMake)( Src header_src, DocMake conf_doc_make_aa ) { debug(asserts){ static assert(is(typeof(header_src) == char[])); static assert(is(typeof(conf_doc_make_aa) == string[string][string])); } auto head_native = HeaderDocMetadataAndMakeNativeToAA(); auto head_sdlang = HeaderExtractSDL(); auto header_make_and_meta_tuple = (header_src.match(rgx.native_header_meta_title)) ? (head_native.headerNativeToAA(header_src)) : (head_sdlang.headerSDLangToAA(header_src, conf_doc_make_aa)); static assert(!isTypeTuple!(header_make_and_meta_tuple)); static assert(header_make_and_meta_tuple.length==2); return header_make_and_meta_tuple; } } #+END_SRC * A. header sdlang (extract) :module:sdp:ao_conf_make_meta_sdlang: ** module template ** extract sdlang header #+BEGIN_SRC d :tangle ../src/sdp/ao_conf_make_meta_sdlang.d /++ sdlang headers
extract sdlang header return sdlang +/ module sdp.ao_conf_make_meta_sdlang; template SiSUheaderExtractSDLang() { 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 HeaderExtractSDL { mixin SiSUregisters; mixin SiSUrgxInit; auto rgx = Rgx(); <> } } #+END_SRC ** sdlang to associative array #+name: ao_conf_make_meta_sdl #+BEGIN_SRC d private auto sdlangToAAmake(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 (!(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 > 0)) { debug(headersdlang) { 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; } #+END_SRC ** conf settings sdlang #+name: ao_conf_make_meta_sdl #+BEGIN_SRC d private auto configSettingsSDLangToAAmake(Tag)(Tag conf_sdlang) { auto conf = sdlangToAAmake(conf_aa, conf_sdlang); return conf; } #+END_SRC ** conf make sdlang #+name: ao_conf_make_meta_sdl #+BEGIN_SRC d private auto documentMakeSDLangToAAmake(Tag)(Tag document_make_sdlang) { auto dochead_make = sdlangToAAmake(make_aa, document_make_sdlang); return dochead_make; } #+END_SRC ** header :header:sdl: *** sdlang header parse and extract root Tag :sdlang:root:tag: #+name: ao_conf_make_meta_sdl #+BEGIN_SRC d 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(sdl_root_header.toSDLDocument()); } return sdl_root_header; } #+END_SRC *** sdlang header get :sdlang:get:src: #+name: ao_conf_make_meta_sdl #+BEGIN_SRC d 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)); 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 } #+END_SRC *** sdlang header to associative array make sdlTag in :sdlang:aa: #+name: ao_conf_make_meta_sdl #+BEGIN_SRC d private auto headerSDLangToAAmake(Tag,Ma)(Tag header_sdlang, Ma dochead_make) { debug(asserts){ static assert(is(typeof(dochead_make) == string[string][string])); } dochead_make = sdlangToAAmake(dochead_make, header_sdlang); auto dochead_meta = sdlangToAAmake(meta_aa, header_sdlang); if (dochead_meta["title"]["main"].empty) { dochead_meta["title"]["main"] = to!string(header_sdlang.maybe.tags["title"][0].values[0]); } 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; } #+END_SRC *** hub: get sdlang header and convert to associative array :hub:sdlang:aa: #+name: ao_conf_make_meta_sdl #+BEGIN_SRC d 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; } #+END_SRC * B. header native :module:sdp:ao_conf_make_meta_native: ** Header Native :header:native:markup_header_extract_native: #+BEGIN_SRC d :tangle ../src/sdp/ao_conf_make_meta_native.d /++ native headers using
@title:
:subtitle:
type tags
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; <> } } #+END_SRC ** native header document metadata in associative array :aa: #+name: ao_markup_header_extract_native #+BEGIN_SRC d 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; } #+END_SRC ** native header extract to string object :string: #+name: ao_markup_header_extract_native #+BEGIN_SRC d 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; } #+END_SRC ** native header reset states :reset: #+name: ao_markup_header_extract_native #+BEGIN_SRC d 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"); } #+END_SRC ** hub: native header start :hub: #+name: ao_markup_header_extract_native #+BEGIN_SRC d 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; } #+END_SRC