Refactor config parsing code in main.d, rename esv.d -> esvapi.d, rename class EsvAPI to ESVApi, makefile changes

This commit is contained in:
Jeremy Baxter 2023-06-03 18:52:24 +12:00
parent 7f61d7bfa6
commit 5dc2a12f1e
3 changed files with 24 additions and 30 deletions

View file

@ -2,9 +2,9 @@ IMPORT = import
PREFIX = /usr/local
MANPREFIX = /usr/share/man
DC = ldc2
CFLAGS = -O -I${IMPORT} -w
OBJS = main.o esv.o ini.o
DC = ldc2
CFLAGS = -O -I${IMPORT} -release -w
OBJS = main.o esvapi.o ini.o
all: esv
@ -12,17 +12,17 @@ esv: ${OBJS}
${DC} ${CFLAGS} -of$@ ${OBJS}
# main executable
main.o: main.d esv.o
main.o: main.d esvapi.o
${DC} ${CFLAGS} -c main.d -of$@
esv.o: esv.d
${DC} ${CFLAGS} -c esv.d -of$@
esvapi.o: esvapi.d
${DC} ${CFLAGS} -c esvapi.d -of$@
ini.o: ${IMPORT}/dini/*.d
${DC} ${CFLAGS} -c ${IMPORT}/dini/*.d -of$@
clean:
rm -f ${PROG} ${OBJS}
rm -f esv ${OBJS}
install: esv
install -m755 esv ${DESTDIR}${PREFIX}/bin/esv

View file

@ -18,6 +18,8 @@
* along with esv. If not, see <http://www.gnu.org/licenses/>.
*/
module esvapi;
import std.algorithm : filter, map;
import std.array : appender;
import std.ascii : isAlphaNum;
@ -106,12 +108,12 @@ const string[] BIBLE_BOOKS = [
"Revelation"
];
class EsvAPI
class ESVApi
{
private string _key;
private string _url;
private string _mode;
EsvAPIOptions opts;
ESVApiOptions opts;
string extraParameters;
int delegate(size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) onProgress;
string tmpDir;
@ -312,7 +314,7 @@ class EsvAPI
}
}
struct EsvAPIOptions
struct ESVApiOptions
{
bool[string] boolOpts;
int[string] intOpts;
@ -337,7 +339,7 @@ struct EsvAPIOptions
intOpts["indent_declares"] = 40;
intOpts["indent_psalm_doxology"] = 30;
intOpts["line_length"] = 0;
indent_using = "space";
indent_using = "space";
}
}

30
main.d
View file

@ -20,6 +20,7 @@
import std.conv : to, ConvException;
import std.file : exists, write, FileException;
import std.format : format;
import std.getopt : getopt, GetOptException, config;
import std.path : baseName, expandTilde, isValidPath;
import std.process : environment, executeShell;
@ -27,7 +28,7 @@ import std.regex : regex, matchFirst, replaceAll, replaceFirst;
import std.stdio : writef, writeln, writefln, stderr;
import std.string : splitLines;
import esv;
import esvapi;
import dini;
enum VERSION = "0.2.0";
@ -164,7 +165,7 @@ key = " ~ DEFAULT_APIKEY ~ "
panic("API key not present in configuration file; cannot proceed");
// Initialise API object and validate the book and verse
EsvAPI esv = new EsvAPI(apiKey);
ESVApi esv = new ESVApi(apiKey);
if (!esv.validateBook(args[1].extractBook()))
panic("book '" ~ args[1] ~ "' does not exist");
if (!esv.validateVerse(args[2]))
@ -208,17 +209,17 @@ key = " ~ DEFAULT_APIKEY ~ "
foreach (string key; ["footnotes", "headings", "passage_references", "verse_numbers"]) {
try {
esv.opts.boolOpts["include_" ~ key] =
returnValid("true", iniData["passage"].getKey(key)).catchConvException(
(in ConvException ex, in char[] str)
{
panic(configPath ~ ": value '" ~ cast(string)str ~
"' is not convertible to a boolean value; must be either 'true' or 'false'");
}
);
returnValid("true", iniData["passage"].getKey(key)).to!bool();
} catch (ConvException e) {
panic(
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
}
// Get line_length ([passage])
try esv.opts.intOpts["line_length"] = returnValid("0", iniData["passage"].getKey("line_length")).to!int();
try esv.opts.intOpts["line_length"] = returnValid("0", iniData["passage"].getKey("line_length")).to!int();
catch (ConvException e) {
panic(configPath ~ ": value '" ~ iniData["passage"].getKey("line_length")
~ "' is not convertible to an integer value; must be a non-decimal number");
@ -274,12 +275,3 @@ 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) {
catchNet(e, sb);
return false;
}
}