migrate to initial.d over dini

initial.d is my very own INI parser, see
  <https://git.sr.ht/~jeremy/initial>
This commit is contained in:
Jeremy Baxter 2024-02-13 09:31:56 +13:00
parent de9e18f06e
commit 2e93c891fd
10 changed files with 509 additions and 1642 deletions

51
esv.d
View file

@ -32,9 +32,10 @@ import std.regex : regex, matchFirst, replaceAll, replaceFirst;
import std.stdio : writef, writeln, writefln, stderr;
import std.string : splitLines;
import initial;
import config;
import esvapi;
import dini;
enum VERSION = "0.2.0";
@ -92,7 +93,7 @@ run(string[] args)
{
string apiKey;
string configPath;
Ini iniData;
INIUnit ini;
ESVApi esv;
/* Parse command-line options */
@ -177,17 +178,14 @@ key = %s
"(DEFAULT_APIKEY));
}
}
iniData = Ini.Parse(configPath);
readINIFile(ini, configPath);
} catch (FileException e) {
/* filesystem syscall errors */
throw new Exception(e.msg);
}
try {
apiKey = iniData["api"].getKey("key");
} catch (IniException e) {
apiKey = "";
}
enforce(apiKey != "",
apiKey = ini["api"].key("key");
enforce(apiKey != null,
"API key not present in configuration file; cannot proceed");
esv = new ESVApi(apiKey);
@ -223,38 +221,23 @@ key = %s
return true;
}
esv.extraParameters = iniData["api"].getKey("parameters");
string
returnValid(string def, string val) @safe
{
return val == "" ? def : val;
}
esv.extraParameters = ini["api"].key("parameters", "");
/* Get [passage] keys */
foreach (string key; ["footnotes", "headings", "passage-references", "verse-numbers"]) {
try {
try
esv.opts.b["include-" ~ key] =
returnValid("true", iniData["passage"].getKey(key)).to!bool();
} catch (ConvException e) {
throw new Exception(format!
"%s: key '%s' of section 'passage' is not a boolean value (must be either 'true' or 'false')"
(configPath, key)
);
} catch (IniException e) {} // just do nothing; use the default settings
ini["passage"].keyAs!bool(key, "true");
catch (INITypeException e)
throw new Exception(configPath ~ ": " ~ e.msg);
}
/* Get line-length ([passage]) */
try {
esv.opts.i["line-length"] =
returnValid("0", iniData["passage"].getKey("line-length")).to!int();
ini["passage"].keyAs!int("line-length", "0");
} catch (INITypeException e) {
throw new Exception(configPath ~ ": " ~ e.msg);
}
catch (ConvException e) {
throw new Exception(
format!"%s: illegal value '%s' -- must be an integer"(
configPath,
iniData["passage"].getKey("line-length"))
);
} catch (IniException e) {} // just do nothing; use the default setting
if (fFlag) esv.opts.b["include-footnotes"] = true;
if (hFlag) esv.opts.b["include-headings"] = true;
@ -279,5 +262,7 @@ extractOpt(in GetOptException e) @safe
private string
parseBook(in string book) @safe
{
return book.replaceAll(regex("[-_]"), " ");
import std.string : tr;
return book.tr("-_", " ");
}