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.getopt : getopt, GetOptException;
|
||||||
import std.path : baseName, expandTilde;
|
import std.path : baseName, expandTilde;
|
||||||
import std.process : environment;
|
import std.process : environment;
|
||||||
|
import std.regex : regex, matchAll, replaceFirst;
|
||||||
import std.stdio : writeln, writefln;
|
import std.stdio : writeln, writefln;
|
||||||
|
import std.string : tr;
|
||||||
|
|
||||||
import esvapi;
|
import esvapi;
|
||||||
import initial;
|
import initial;
|
||||||
|
@ -36,8 +38,28 @@ import cf = config;
|
||||||
|
|
||||||
string cFlag; /* config path */
|
string cFlag; /* config path */
|
||||||
bool eFlag; /* exact matches */
|
bool eFlag; /* exact matches */
|
||||||
|
bool mFlag; /* machine readable */
|
||||||
bool VFlag; /* show version */
|
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
|
int
|
||||||
main(string[] args)
|
main(string[] args)
|
||||||
{
|
{
|
||||||
|
@ -59,6 +81,7 @@ main(string[] args)
|
||||||
config.caseSensitive,
|
config.caseSensitive,
|
||||||
"c", &cFlag,
|
"c", &cFlag,
|
||||||
"e", &eFlag,
|
"e", &eFlag,
|
||||||
|
"m", &mFlag,
|
||||||
"V", &VFlag,
|
"V", &VFlag,
|
||||||
);
|
);
|
||||||
} catch (GetOptException e) {
|
} catch (GetOptException e) {
|
||||||
|
@ -71,7 +94,7 @@ main(string[] args)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length < 2) {
|
if (args.length < 2) {
|
||||||
stderr.writefln("usage: %s [-eV] [-c config] query",
|
stderr.writefln("usage: %s [-emV] [-c config] query",
|
||||||
baseName(args[0]));
|
baseName(args[0]));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -110,7 +133,9 @@ main(string[] args)
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
writeln(esv.searchFormat(query));
|
writeln(mFlag
|
||||||
|
? esv.searchFormat(query, &machineReadableFmt)
|
||||||
|
: esv.searchFormat(query));
|
||||||
catch (ESVException)
|
catch (ESVException)
|
||||||
die("no results");
|
die("no results");
|
||||||
catch (CurlException e)
|
catch (CurlException e)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue