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;
const enum ESVAPI_URL = "https://api.esv.org/v3/passage";
const string[] ESVAPI_BIBLE_BOOKS = [
const string[] BIBLE_BOOKS = [
// Old Testament
"Genesis",
"Exodus",
@ -44,12 +44,12 @@ const string[] ESVAPI_BIBLE_BOOKS = [
"Joshua",
"Judges",
"Ruth",
"1_Samuel",
"2_Samuel",
"1_Kings",
"2_Kings",
"1_Chronicles",
"2_Chronicles",
"1 Samuel",
"2 Samuel",
"1 Kings",
"2 Kings",
"1 Chronicles",
"2 Chronicles",
"Ezra",
"Nehemiah",
"Esther",
@ -58,7 +58,7 @@ const string[] ESVAPI_BIBLE_BOOKS = [
"Psalms", // <- both are valid
"Proverbs",
"Ecclesiastes",
"Song_of_Solomon",
"Song of Solomon",
"Isaiah",
"Jeremiah",
"Lamentations",
@ -83,25 +83,25 @@ const string[] ESVAPI_BIBLE_BOOKS = [
"John",
"Acts",
"Romans",
"1_Corinthians",
"2_Corinthians",
"1 Corinthians",
"2 Corinthians",
"Galatians",
"Ephesians",
"Philippians",
"Colossians",
"1_Thessalonians",
"2_Thessalonians",
"1_Timothy",
"2_Timothy",
"1 Thessalonians",
"2 Thessalonians",
"1 Timothy",
"2 Timothy",
"Titus",
"Philemon",
"Hebrews",
"James",
"1_Peter",
"2_Peter",
"1_John",
"2_John",
"3_John",
"1 Peter",
"2 Peter",
"1 John",
"2 John",
"3 John",
"Jude",
"Revelation"
];
@ -182,7 +182,7 @@ class EsvAPI
*/
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())
return true;
@ -228,7 +228,7 @@ class EsvAPI
throw new EsvException("Invalid verse format");
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);
string response;
request.onProgress = onProgress;
@ -257,7 +257,7 @@ class EsvAPI
if (!validateVerse(verse))
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);
ubyte[] response;
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.path : baseName, expandTilde, isValidPath;
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.string : splitLines;
@ -165,7 +165,7 @@ key = " ~ DEFAULT_APIKEY ~ "
// Initialise API object and validate the book and verse
EsvAPI esv = new EsvAPI(apiKey);
if (!esv.validateBook(args[1]))
if (!esv.validateBook(args[1].extractBook()))
panic("book '" ~ args[1] ~ "' does not exist");
if (!esv.validateVerse(args[2]))
panic("invalid verse format '" ~ args[2] ~ "'");
@ -209,9 +209,9 @@ key = " ~ DEFAULT_APIKEY ~ "
try {
esv.opts.boolOpts["include_" ~ key] =
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'");
}
);
@ -234,7 +234,7 @@ key = " ~ DEFAULT_APIKEY ~ "
if (optNoPassageReferences) esv.opts.boolOpts["include_passage_references"] = false;
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;
foreach (string line; verses.splitLines())
++lines;
@ -253,9 +253,9 @@ key = " ~ DEFAULT_APIKEY ~ "
catch (ProcessException e) {
if (!e.msg.matchFirst(regex("^Executable file not found")).empty) {
panic(e.msg
.matchFirst(": (.+)$")[0]
.replaceFirst(regex("^: "), "")
~ ": command not found"
.matchFirst(": (.+)$")[0]
.replaceFirst(regex("^: "), "")
~ ": command not found"
);
}
}
@ -265,12 +265,17 @@ key = " ~ DEFAULT_APIKEY ~ "
return 0;
}
string extractOpt(GetOptException e) @safe
string extractOpt(in GetOptException e) @safe
{
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();
catch (ConvException e) {