esvsearch: implement machine-readable output format
This commit is contained in:
parent
4564b88e42
commit
67171b71dc
1 changed files with 27 additions and 2 deletions
29
esvsearch.d
29
esvsearch.d
|
@ -24,7 +24,9 @@ import std.file : FileException;
|
|||
import std.getopt : getopt, GetOptException;
|
||||
import std.path : baseName, expandTilde;
|
||||
import std.process : environment;
|
||||
import std.regex : regex, matchAll, replaceFirst;
|
||||
import std.stdio : writeln, writefln;
|
||||
import std.string : tr;
|
||||
|
||||
import esvapi;
|
||||
import initial;
|
||||
|
@ -36,8 +38,28 @@ import cf = config;
|
|||
|
||||
string cFlag; /* config path */
|
||||
bool eFlag; /* exact matches */
|
||||
bool mFlag; /* machine readable */
|
||||
bool VFlag; /* show version */
|
||||
|
||||
string
|
||||
machineReadableFmt(string reference, string content)
|
||||
{
|
||||
/* match the start of the reference against bibleBooks
|
||||
* to identify what book it's from, so we can replace
|
||||
* spaces in the book name with underscores :-) */
|
||||
foreach (string book; bibleBooks) {
|
||||
auto match = reference.matchAll(regex("^(" ~ book ~ ") \\d"));
|
||||
if (!match.empty) {
|
||||
assert(match.captures[1] == book
|
||||
&& bookValid(match.captures[1]));
|
||||
reference = reference.replaceFirst(
|
||||
regex('^' ~ book), book.tr(" ", "_"));
|
||||
}
|
||||
}
|
||||
|
||||
return reference ~ " / " ~ content ~ "\n";
|
||||
}
|
||||
|
||||
int
|
||||
main(string[] args)
|
||||
{
|
||||
|
@ -59,6 +81,7 @@ main(string[] args)
|
|||
config.caseSensitive,
|
||||
"c", &cFlag,
|
||||
"e", &eFlag,
|
||||
"m", &mFlag,
|
||||
"V", &VFlag,
|
||||
);
|
||||
} catch (GetOptException e) {
|
||||
|
@ -71,7 +94,7 @@ main(string[] args)
|
|||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
stderr.writefln("usage: %s [-eV] [-c config] query",
|
||||
stderr.writefln("usage: %s [-emV] [-c config] query",
|
||||
baseName(args[0]));
|
||||
return 1;
|
||||
}
|
||||
|
@ -110,7 +133,9 @@ main(string[] args)
|
|||
}
|
||||
|
||||
try
|
||||
writeln(esv.searchFormat(query));
|
||||
writeln(mFlag
|
||||
? esv.searchFormat(query, &machineReadableFmt)
|
||||
: esv.searchFormat(query));
|
||||
catch (ESVException)
|
||||
die("no results");
|
||||
catch (CurlException e)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue