From d61236952f19c1a1107a11cf11874104c0fe1a9b Mon Sep 17 00:00:00 2001
From: Ralph Amissah <ralph.amissah@gmail.com>
Date: Thu, 14 Nov 2019 11:16:09 -0500
Subject: 0.8.3 (if exists) get site config & output path

- read once & read early:
  - once per batch processing
  - early (needed by metadata --harvest)
---
 src/doc_reform/source/paths_source.d      | 153 ++++++++++++++++++------------
 src/doc_reform/source/read_config_files.d |  51 ++++++----
 src/doc_reform/source/read_source_files.d |   6 +-
 3 files changed, 124 insertions(+), 86 deletions(-)

(limited to 'src/doc_reform/source')

diff --git a/src/doc_reform/source/paths_source.d b/src/doc_reform/source/paths_source.d
index f291fb5..e0b844a 100644
--- a/src/doc_reform/source/paths_source.d
+++ b/src/doc_reform/source/paths_source.d
@@ -163,6 +163,9 @@ template PathMatters() {
           string pod_name_with_path() {
             return (is_pod) ? _manifested.pod_manifest_path : "";
           }
+          string pods_collection_root_path() {
+            return (is_pod) ? _manifested.pods_collection_root_path : "";
+          }
           string pod_name() {
             return pod_name_with_path.baseName;
           }
@@ -247,6 +250,9 @@ template PathMatters() {
           string absolute_path_to_src() {
             return (_env["pwd"].chainPath(path_and_fn)).dirName.array;
           }
+          string path_to_doc_root_path_to_lang_and_filename() {
+            return _env["pwd"].chainPath(path_and_fn).array;
+          }
           string base_dir() {
             string _dir;
             if (
@@ -359,7 +365,17 @@ template PathMatters() {
         return SRC_();
       }
       auto output() {
-        auto _opt_actions = _opt_actions;
+        /+
+          - command line if output path set
+          - config file if found and set set
+            - search for and if exists read config
+              - default paths to config related to:
+                - source markup path;
+                - current dir;
+                - home dir
+              - get output path if set
+          - (program) default within current directory?
+        +/
         auto _env = _env;
         struct Out_ {
           auto path() {
@@ -393,8 +409,9 @@ template ConfigFilePaths() {
   mixin DocReformRgxInit;
   static auto rgx = Rgx();
   auto ConfigFilePaths(M,E)(
-    M   _manifested,
-    E   _env,
+    M      _manifested,
+    E      _env,
+    string _cli_config_path_set = ""
   ) {
     struct ConfFilePaths {
       string config_filename_document() {
@@ -410,37 +427,43 @@ template ConfigFilePaths() {
             /+ config local site (file system only, not in pod) +/
             /+ return paths +/
             string[] _possible_config_path_locations;
-            if (_manifested.src.is_pod) {
-              /+ config document in pod +/
-              string _dr_doc_conf_pod;
-              string _dr_doc_conf_pod_text;
-              _dr_doc_conf_pod = asNormalizedPath(chainPath(
-                to!string(_env["pwd"]),
-                _manifested.pod.manifest_path ~ "/conf"
-              )).array;
-              _dr_doc_conf_pod_text = asNormalizedPath(chainPath(
-                to!string(_env["pwd"]),
-                _manifested.pod.manifest_path ~ "/media/text/" ~ _manifested.src.lng ~ "/conf"
-              )).array;
-              /+ return paths +/
-              _possible_config_path_locations = [
-                _dr_doc_conf_pod_text,
-                _dr_doc_conf_pod,
-              ];
+            if (_cli_config_path_set.empty) {
+              if (_manifested.src.is_pod) {
+                /+ config document in pod +/
+                string _dr_doc_conf_pod;
+                string _dr_doc_conf_pod_text;
+                _dr_doc_conf_pod = asNormalizedPath(chainPath(
+                  to!string(_env["pwd"]),
+                  _manifested.pod.manifest_path ~ "/conf"
+                )).array;
+                _dr_doc_conf_pod_text = asNormalizedPath(chainPath(
+                  to!string(_env["pwd"]),
+                  _manifested.pod.manifest_path ~ "/media/text/" ~ _manifested.src.lng ~ "/conf"
+                )).array;
+                /+ return paths +/
+                _possible_config_path_locations = [
+                  _dr_doc_conf_pod_text,
+                  _dr_doc_conf_pod,
+                ];
+              } else {
+                /+ config document (& or local site) on filesystem +/
+                string _dr_doc_conf_pwd   = ((chainPath(to!string(_env["pwd"]), "dr_doc/conf")).asNormalizedPath).array; // think about
+                string _dr_doc_conf_pwd_a = ((chainPath(to!string(_env["pwd"]), "conf")).asNormalizedPath).array;
+                string _dr_doc_conf_pwd_b = ((chainPath(to!string(_env["pwd"]), "../conf")).asNormalizedPath).array;
+                string _dr_doc_conf_pwd_c = ((chainPath(to!string(_env["pwd"]), "../../conf")).asNormalizedPath).array;
+                string _dr_doc_conf_pwd_d = ((chainPath(to!string(_env["pwd"]), "../../../conf")).asNormalizedPath).array;
+                /+ return paths +/
+                _possible_config_path_locations = [
+                  _dr_doc_conf_pwd,
+                  _dr_doc_conf_pwd_a,
+                  _dr_doc_conf_pwd_b,
+                  _dr_doc_conf_pwd_c,
+                  _dr_doc_conf_pwd_d,
+                ];
+              }
             } else {
-              /+ config document (& or local site) on filesystem +/
-              string _dr_doc_conf_pwd   = ((chainPath(to!string(_env["pwd"]), "dr_doc/conf")).asNormalizedPath).array; // think about
-              string _dr_doc_conf_pwd_a = ((chainPath(to!string(_env["pwd"]), "conf")).asNormalizedPath).array;
-              string _dr_doc_conf_pwd_b = ((chainPath(to!string(_env["pwd"]), "../conf")).asNormalizedPath).array;
-              string _dr_doc_conf_pwd_c = ((chainPath(to!string(_env["pwd"]), "../../conf")).asNormalizedPath).array;
-              string _dr_doc_conf_pwd_d = ((chainPath(to!string(_env["pwd"]), "../../../conf")).asNormalizedPath).array;
-              /+ return paths +/
               _possible_config_path_locations = [
-                _dr_doc_conf_pwd,
-                _dr_doc_conf_pwd_a,
-                _dr_doc_conf_pwd_b,
-                _dr_doc_conf_pwd_c,
-                _dr_doc_conf_pwd_d,
+                _cli_config_path_set
               ];
             }
             /+ FIX clean up conf paths ↑
@@ -451,39 +474,45 @@ template ConfigFilePaths() {
           string[] config_local_site() {
             /+ FIX clean up conf paths ↓ +/
             /+ config local site (file system only, not in pod) +/
-            string _dot_pwd        = ((chainPath(to!string(_env["pwd"]), ".dr")).asNormalizedPath).array;
-            string _underscore_pwd = ((chainPath(to!string(_env["pwd"]), "_dr")).asNormalizedPath).array;
-            string _dot_home       = ((chainPath(to!string(_env["home"]), ".dr")).asNormalizedPath).array;
-            /+ return paths +/
             string[] _possible_config_path_locations;
-            if (_manifested.src.is_pod) {
-              string _collection_root_a = ((chainPath(to!string(_manifested.pod.collection_root.to!string), ".dr")).asNormalizedPath).array;
-              string _collection_root_b = ((chainPath(to!string(_manifested.pod.collection_root.to!string), "_dr")).asNormalizedPath).array;
-              _possible_config_path_locations = [
-                _dot_pwd,
-                _underscore_pwd,
-                _dot_home,
-                "/etc/dr",
-                _collection_root_a, // set priority higher?
-                _collection_root_b // set priority higher?
-              ];
+            if (_cli_config_path_set.empty) {
+              string _dot_pwd        = ((chainPath(to!string(_env["pwd"]), ".dr")).asNormalizedPath).array;
+              string _underscore_pwd = ((chainPath(to!string(_env["pwd"]), "_dr")).asNormalizedPath).array;
+              string _dot_home       = ((chainPath(to!string(_env["home"]), ".dr")).asNormalizedPath).array;
+              /+ return paths +/
+              if (_manifested.src.is_pod) {
+                string _collection_root_a = ((chainPath(to!string(_manifested.pod.collection_root.to!string), ".dr")).asNormalizedPath).array;
+                string _collection_root_b = ((chainPath(to!string(_manifested.pod.collection_root.to!string), "_dr")).asNormalizedPath).array;
+                _possible_config_path_locations = [
+                  _dot_pwd,
+                  _underscore_pwd,
+                  _dot_home,
+                  "/etc/dr",
+                  _collection_root_a, // set priority higher?
+                  _collection_root_b  // set priority higher?
+                ];
+              } else {
+                /+ config document (& or local site) on filesystem +/
+                string _dr_doc_conf_pwd   = ((chainPath(to!string(_env["pwd"]), "dr_doc/conf")).asNormalizedPath).array;
+                string _dr_doc_conf_pwd_a = ((chainPath(to!string(_env["pwd"]), "conf")).asNormalizedPath).array;
+                string _dr_doc_conf_pwd_b = ((chainPath(to!string(_env["pwd"]), "../conf")).asNormalizedPath).array;
+                string _dr_doc_conf_pwd_c = ((chainPath(to!string(_env["pwd"]), "../../conf")).asNormalizedPath).array;
+                string _dr_doc_conf_pwd_d = ((chainPath(to!string(_env["pwd"]), "../../../conf")).asNormalizedPath).array;
+                _possible_config_path_locations = [
+                  _dr_doc_conf_pwd,
+                  _dr_doc_conf_pwd_a,
+                  _dr_doc_conf_pwd_b,
+                  _dr_doc_conf_pwd_c,
+                  _dr_doc_conf_pwd_d,
+                  _dot_pwd,
+                  _underscore_pwd,
+                  _dot_home,
+                  "/etc/dr"
+                ];
+              }
             } else {
-              /+ config document (& or local site) on filesystem +/
-              string _dr_doc_conf_pwd   = ((chainPath(to!string(_env["pwd"]), "dr_doc/conf")).asNormalizedPath).array;
-              string _dr_doc_conf_pwd_a = ((chainPath(to!string(_env["pwd"]), "conf")).asNormalizedPath).array;
-              string _dr_doc_conf_pwd_b = ((chainPath(to!string(_env["pwd"]), "../conf")).asNormalizedPath).array;
-              string _dr_doc_conf_pwd_c = ((chainPath(to!string(_env["pwd"]), "../../conf")).asNormalizedPath).array;
-              string _dr_doc_conf_pwd_d = ((chainPath(to!string(_env["pwd"]), "../../../conf")).asNormalizedPath).array;
               _possible_config_path_locations = [
-                _dr_doc_conf_pwd,
-                _dr_doc_conf_pwd_a,
-                _dr_doc_conf_pwd_b,
-                _dr_doc_conf_pwd_c,
-                _dr_doc_conf_pwd_d,
-                _dot_pwd,
-                _underscore_pwd,
-                _dot_home,
-                "/etc/dr"
+                _cli_config_path_set
               ];
             }
             /+ FIX clean up conf paths ↑
diff --git a/src/doc_reform/source/read_config_files.d b/src/doc_reform/source/read_config_files.d
index 4827fff..2b3e4fb 100644
--- a/src/doc_reform/source/read_config_files.d
+++ b/src/doc_reform/source/read_config_files.d
@@ -14,30 +14,42 @@ static template readConfigSite() {
     std.file,
     std.path;
   mixin DocReformRgxInit;
-  final auto readConfigSite(M,E)(M _manifested, E _env) {
+  final auto readConfigSite(M,E)(
+    M      _manifested,
+    E      _env,
+    string _cli_config_path_set = ""
+  ) {
     static auto rgx = Rgx();
     string config_file_str;
     string conf_filename = "NONE";
-    auto _conf_file_details = ConfigFilePaths!()(_manifested, _env);
+    auto _conf_file_details = ConfigFilePaths!()(_manifested, _env, _cli_config_path_set);
     string[] possible_config_path_locations = _conf_file_details.possible_config_path_locations.config_local_site;
     foreach(conf_fn; [_conf_file_details.config_filename_site]) {
       foreach(pth; possible_config_path_locations) {
-        char[] conf_file = ((chainPath(pth.to!string, conf_fn)).asNormalizedPath).array;
+        char[] conf_file;
         conf_filename = conf_fn;
-        if (config_file_str.length > 0) {
-          // conf_filename = conf_fn;
-          break;
-        }
-        try {
-          if (exists(conf_file)) {
-            debug(io) {
-              writeln("WARNING (io debug) in config file found: ", conf_file);
+        if (exists(pth)) {
+          auto f_attrib = pth.getLinkAttributes;
+          if (
+            possible_config_path_locations.length == 1
+            && f_attrib.attrIsFile
+          ) {
+            conf_file = pth.to!(char[]);
+            conf_filename = pth.baseName;
+          } else if (f_attrib.attrIsDir) {
+            conf_file = ((chainPath(pth.to!string, conf_fn)).asNormalizedPath).array;
+            conf_filename = conf_fn;
+          }
+          try {
+            if (exists(conf_file)) {
+              if (conf_file.getLinkAttributes.attrIsFile) {
+                config_file_str = conf_file.readText;
+                break;
+              }
             }
-            config_file_str = conf_file.readText;
-            break;
+          } catch (ErrnoException ex) {
+          } catch (FileException ex) {
           }
-        } catch (ErrnoException ex) {
-        } catch (FileException ex) {
         }
       }
       if (config_file_str.length > 0) { break; }
@@ -80,16 +92,14 @@ static template readConfigDoc() {
         auto conf_file = ((chainPath(pth.to!string, conf_fn)).asNormalizedPath).array;
         conf_filename = conf_fn;
         if (config_file_str.length > 0) {
-          // conf_filename = conf_fn;
           break;
         }
         try {
           if (exists(conf_file)) {
-            debug(io) {
-              writeln("WARNING (io debug) in config file found: ", conf_file);
+            if (conf_file.getLinkAttributes.attrIsFile) {
+              config_file_str = conf_file.readText;
+              break;
             }
-            config_file_str = conf_file.readText;
-            break;
           }
         } catch (ErrnoException ex) {
         } catch (FileException ex) {
@@ -121,7 +131,6 @@ static template configReadSiteYAML() {
     doc_reform.source.paths_source,
     std.file,
     std.path;
-  import dyaml;
   final YAMLDocument configReadSiteYAML(M,E)(M _manifested, E _env) {
     string _configuration = configReadInSiteYAML!()(_manifested, _env);
     auto _conf_file_details = ConfigFilePaths!()(_manifested, _env);
diff --git a/src/doc_reform/source/read_source_files.d b/src/doc_reform/source/read_source_files.d
index 05f6630..f4de5fe 100644
--- a/src/doc_reform/source/read_source_files.d
+++ b/src/doc_reform/source/read_source_files.d
@@ -85,10 +85,10 @@ static template DocReformRawMarkupContent() {
       string source_txt_str;
       try {
         if (exists(fn_src)) {
-          debug(io) {
-            writeln("in src, markup source file found: ", fn_src);
+          if (fn_src.getLinkAttributes.attrIsFile) {
+            source_txt_str = fn_src.readText;
+          } else {
           }
-          source_txt_str = fn_src.readText;
         }
       } catch (ErrnoException ex) {
       } catch (UTFException ex) {
-- 
cgit v1.2.3