Rename ESVAPI_BIBLE_BOOKS to BIBLE_BOOKS and make it use spaces for book names, instead of underscores.

This commit is contained in:
Jeremy Baxter 2023-05-11 18:59:13 +12:00
parent 64fca717c6
commit 7f61d7bfa6
2 changed files with 37 additions and 32 deletions

44
esv.d
View file

@ -34,7 +34,7 @@ import std.utf : toUTF8;
import std.net.curl; import std.net.curl;
const enum ESVAPI_URL = "https://api.esv.org/v3/passage"; const enum ESVAPI_URL = "https://api.esv.org/v3/passage";
const string[] ESVAPI_BIBLE_BOOKS = [ const string[] BIBLE_BOOKS = [
// Old Testament // Old Testament
"Genesis", "Genesis",
"Exodus", "Exodus",
@ -44,12 +44,12 @@ const string[] ESVAPI_BIBLE_BOOKS = [
"Joshua", "Joshua",
"Judges", "Judges",
"Ruth", "Ruth",
"1_Samuel", "1 Samuel",
"2_Samuel", "2 Samuel",
"1_Kings", "1 Kings",
"2_Kings", "2 Kings",
"1_Chronicles", "1 Chronicles",
"2_Chronicles", "2 Chronicles",
"Ezra", "Ezra",
"Nehemiah", "Nehemiah",
"Esther", "Esther",
@ -58,7 +58,7 @@ const string[] ESVAPI_BIBLE_BOOKS = [
"Psalms", // <- both are valid "Psalms", // <- both are valid
"Proverbs", "Proverbs",
"Ecclesiastes", "Ecclesiastes",
"Song_of_Solomon", "Song of Solomon",
"Isaiah", "Isaiah",
"Jeremiah", "Jeremiah",
"Lamentations", "Lamentations",
@ -83,25 +83,25 @@ const string[] ESVAPI_BIBLE_BOOKS = [
"John", "John",
"Acts", "Acts",
"Romans", "Romans",
"1_Corinthians", "1 Corinthians",
"2_Corinthians", "2 Corinthians",
"Galatians", "Galatians",
"Ephesians", "Ephesians",
"Philippians", "Philippians",
"Colossians", "Colossians",
"1_Thessalonians", "1 Thessalonians",
"2_Thessalonians", "2 Thessalonians",
"1_Timothy", "1 Timothy",
"2_Timothy", "2 Timothy",
"Titus", "Titus",
"Philemon", "Philemon",
"Hebrews", "Hebrews",
"James", "James",
"1_Peter", "1 Peter",
"2_Peter", "2 Peter",
"1_John", "1 John",
"2_John", "2 John",
"3_John", "3 John",
"Jude", "Jude",
"Revelation" "Revelation"
]; ];
@ -182,7 +182,7 @@ class EsvAPI
*/ */
final bool validateBook(in char[] book) const nothrow @safe final bool validateBook(in char[] book) const nothrow @safe
{ {
foreach (string b; ESVAPI_BIBLE_BOOKS) foreach (string b; BIBLE_BOOKS)
{ {
if (book.capitalize() == b.capitalize()) if (book.capitalize() == b.capitalize())
return true; return true;
@ -228,7 +228,7 @@ class EsvAPI
throw new EsvException("Invalid verse format"); throw new EsvException("Invalid verse format");
string apiURL = format!"%s/%s/?q=%s+%s%s%s"(_url, _mode, string apiURL = format!"%s/%s/?q=%s+%s%s%s"(_url, _mode,
book.capitalize().replaceAll(regex("_"), "+"), verse, assembleParameters(), extraParameters); book.capitalize().replaceAll(regex(" "), "+"), verse, assembleParameters(), extraParameters);
auto request = HTTP(apiURL); auto request = HTTP(apiURL);
string response; string response;
request.onProgress = onProgress; request.onProgress = onProgress;
@ -257,7 +257,7 @@ class EsvAPI
if (!validateVerse(verse)) if (!validateVerse(verse))
throw new EsvException("Invalid verse format"); throw new EsvException("Invalid verse format");
string apiURL = format!"%s/audio/?q=%s+%s"(_url, book.capitalize().replaceAll(regex("_"), "+"), verse); string apiURL = format!"%s/audio/?q=%s+%s"(_url, book.capitalize().replaceAll(regex(" "), "+"), verse);
auto request = HTTP(apiURL); auto request = HTTP(apiURL);
ubyte[] response; ubyte[] response;
request.onProgress = onProgress; request.onProgress = onProgress;

25
main.d
View file

@ -23,7 +23,7 @@ import std.file : exists, write, FileException;
import std.getopt : getopt, GetOptException, config; import std.getopt : getopt, GetOptException, config;
import std.path : baseName, expandTilde, isValidPath; import std.path : baseName, expandTilde, isValidPath;
import std.process : environment, executeShell; import std.process : environment, executeShell;
import std.regex : regex, matchFirst, replaceFirst; import std.regex : regex, matchFirst, replaceAll, replaceFirst;
import std.stdio : writef, writeln, writefln, stderr; import std.stdio : writef, writeln, writefln, stderr;
import std.string : splitLines; import std.string : splitLines;
@ -165,7 +165,7 @@ key = " ~ DEFAULT_APIKEY ~ "
// Initialise API object and validate the book and verse // Initialise API object and validate the book and verse
EsvAPI esv = new EsvAPI(apiKey); EsvAPI esv = new EsvAPI(apiKey);
if (!esv.validateBook(args[1])) if (!esv.validateBook(args[1].extractBook()))
panic("book '" ~ args[1] ~ "' does not exist"); panic("book '" ~ args[1] ~ "' does not exist");
if (!esv.validateVerse(args[2])) if (!esv.validateVerse(args[2]))
panic("invalid verse format '" ~ args[2] ~ "'"); panic("invalid verse format '" ~ args[2] ~ "'");
@ -209,9 +209,9 @@ key = " ~ DEFAULT_APIKEY ~ "
try { try {
esv.opts.boolOpts["include_" ~ key] = esv.opts.boolOpts["include_" ~ key] =
returnValid("true", iniData["passage"].getKey(key)).catchConvException( returnValid("true", iniData["passage"].getKey(key)).catchConvException(
(ConvException ex, string str) (in ConvException ex, in char[] str)
{ {
panic(configPath ~ ": value '" ~ str ~ panic(configPath ~ ": value '" ~ cast(string)str ~
"' is not convertible to a boolean value; must be either 'true' or 'false'"); "' is not convertible to a boolean value; must be either 'true' or 'false'");
} }
); );
@ -234,7 +234,7 @@ key = " ~ DEFAULT_APIKEY ~ "
if (optNoPassageReferences) esv.opts.boolOpts["include_passage_references"] = false; if (optNoPassageReferences) esv.opts.boolOpts["include_passage_references"] = false;
if (optLineLength != 0) esv.opts.intOpts ["line_length"] = optLineLength; if (optLineLength != 0) esv.opts.intOpts ["line_length"] = optLineLength;
string verses = esv.getVerses(args[1], args[2]); string verses = esv.getVerses(args[1].extractBook(), args[2]);
int lines; int lines;
foreach (string line; verses.splitLines()) foreach (string line; verses.splitLines())
++lines; ++lines;
@ -253,9 +253,9 @@ key = " ~ DEFAULT_APIKEY ~ "
catch (ProcessException e) { catch (ProcessException e) {
if (!e.msg.matchFirst(regex("^Executable file not found")).empty) { if (!e.msg.matchFirst(regex("^Executable file not found")).empty) {
panic(e.msg panic(e.msg
.matchFirst(": (.+)$")[0] .matchFirst(": (.+)$")[0]
.replaceFirst(regex("^: "), "") .replaceFirst(regex("^: "), "")
~ ": command not found" ~ ": command not found"
); );
} }
} }
@ -265,12 +265,17 @@ key = " ~ DEFAULT_APIKEY ~ "
return 0; return 0;
} }
string extractOpt(GetOptException e) @safe string extractOpt(in GetOptException e) @safe
{ {
return e.msg.matchFirst("-.")[0]; return e.msg.matchFirst("-.")[0];
} }
bool catchConvException(string sb, void delegate(ConvException ex, string str) catchNet) string extractBook(in string book) @safe
{
return book.replaceAll(regex("[-_]"), " ");
}
bool catchConvException(in char[] sb, void delegate(in ConvException ex, in char[] str) @system catchNet)
{ {
try return sb.to!bool(); try return sb.to!bool();
catch (ConvException e) { catch (ConvException e) {