Add reading format, rework plain format.

This commit is contained in:
Jonathan Bernard 2023-09-17 06:57:52 -05:00
parent a0c17bcad9
commit 8422199d7b
2 changed files with 38 additions and 3 deletions

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.2.3" version = "0.3.0"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Simple Nim CLI wrapper around the ESV API (api.esv.org)" description = "Simple Nim CLI wrapper around the ESV API (api.esv.org)"
license = "MIT" license = "MIT"

View File

@ -32,6 +32,36 @@ proc formatMarkdown(raw: string): string =
result = (wrapped.splitLines --> map("> " & it)). result = (wrapped.splitLines --> map("> " & it)).
join("\p") & "\p> -- *" & reference & " (ESV)*" join("\p") & "\p> -- *" & reference & " (ESV)*"
proc formatPlain(raw: string, keepVerseNumbers = true): string =
var reference = ""
var inVerse = false
var verseLines = newSeq[string]()
for line in raw.splitLines:
if reference.len == 0: reference = line.strip
if inVerse:
if line.startsWith("Footnotes"): inVerse = false
elif line.isEmptyOrWhitespace and verseLines[^1] != "":
verseLines.add("")
elif not line.match(re"^\s+[^\s]"): continue
elif line.match(re"$(.*)\(ESV\)$"): verseLines.add(line[0 ..< ^5])
else: verseLines.add(line)
elif line.match(re"^\s+\[\d+\]"):
inVerse = true
verseLines.add(line)
let wrapped = (verseLines -->
map(if it.len > 90: it.strip else: it & " ").
map(
if keepVerseNumbers:
it.multiReplace([(re"\((\d+)\)", ""), (re"\[(\d+)\]", "$1")])
else:
it.multiReplace([(re"\((\d+)\)", ""), (re"\[(\d+)\]", "")])).
map(wrapWords(it, maxLineWidth = 74, newLine = "\p"))).join("\p")
result = (wrapped.splitLines --> map(it)).
join("\p") & "\p " & reference & " (ESV)"
when isMainModule: when isMainModule:
const USAGE = """Usage: const USAGE = """Usage:
esv_api <reference> [options] esv_api <reference> [options]
@ -44,7 +74,7 @@ Options:
command line for debugging purposes. command line for debugging purposes.
-f, --output-format <format> Select a specific output format. Valid values -f, --output-format <format> Select a specific output format. Valid values
are 'raw', 'markdown', 'plain'. are 'raw', 'markdown', 'plain', 'reading'.
-t, --esv-api-token <token> Provide the API token on the command line. By -t, --esv-api-token <token> Provide the API token on the command line. By
default this will be read either from the default this will be read either from the
@ -59,7 +89,7 @@ Options:
try: try:
# Parse arguments # Parse arguments
let args = docopt(USAGE, version = "0.2.3") let args = docopt(USAGE, version = "0.3.0")
if args["--debug"]: if args["--debug"]:
consoleLogger.levelThreshold = lvlDebug consoleLogger.levelThreshold = lvlDebug
@ -86,6 +116,11 @@ Options:
let formattedPassages = let formattedPassages =
case $args["--output-format"]: case $args["--output-format"]:
of "plain":
respJson["passages"].getElems --> map(formatPlain(it.getStr))
of "reading":
respJson["passages"].getElems -->
map(formatPlain(it.getStr, keepVerseNumbers = false))
of "text": of "text":
respJson["passages"].getElems --> respJson["passages"].getElems -->
map(it.getStr.multiReplace([(re"\[(\d+)\]", "$1")])) map(it.getStr.multiReplace([(re"\[(\d+)\]", "$1")]))