Compare commits

...

3 Commits
0.2.2 ... main

3 changed files with 63 additions and 12 deletions

1
.tool-versions Normal file
View File

@ -0,0 +1 @@
nim 1.6.20

View File

@ -1,6 +1,6 @@
# Package
version = "0.2.2"
version = "0.3.0"
author = "Jonathan Bernard"
description = "Simple Nim CLI wrapper around the ESV API (api.esv.org)"
license = "MIT"
@ -11,7 +11,7 @@ bin = @["esv_api"]
# Dependencies
requires "nim >= 1.6.10"
requires @["docopt", "zero_functional"]
requires @["docopt", "nimquery", "zero_functional"]
# dependencies from git.jdb-software.com/jdb/nim-packages.git
requires @["cliutils"]

View File

@ -7,15 +7,60 @@ import std/[httpclient, json, logging, os, re, strutils, uri, wordwrap]
import cliutils, docopt, zero_functional
proc formatMarkdown(raw: string): string =
let rawLines = raw.splitLines
let wrapped = (raw.splitLines -->
filter(match(it, re"^\s+(\[\d+\]|\w).*")).
map(it.strip.multiReplace([(re"\((\d+)\)", ""), (re"\[(\d+)\]", "**$1**")])).
map(wrapWords(it, maxLineWidth = 74, newLine = "\p"))).
join("\p")
var reference = ""
var inVerse = false
var verseLines = newSeq[string]()
result = (wrapped.splitLines --> map("> " & it)).join("\p") &
"\p>\p> -- *" & rawLines[0].strip & " (ESV)*"
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(it.multiReplace([(re"\((\d+)\)", ""), (re"\[(\d+)\]", "**$1**")])).
map(wrapWords(it, maxLineWidth = 74, newLine = "\p"))).join("\p")
result = (wrapped.splitLines --> map("> " & it)).
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:
const USAGE = """Usage:
@ -29,7 +74,7 @@ Options:
command line for debugging purposes.
-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
default this will be read either from the
@ -44,7 +89,7 @@ Options:
try:
# Parse arguments
let args = docopt(USAGE, version = "0.2.2")
let args = docopt(USAGE, version = "0.3.0")
if args["--debug"]:
consoleLogger.levelThreshold = lvlDebug
@ -71,6 +116,11 @@ Options:
let formattedPassages =
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":
respJson["passages"].getElems -->
map(it.getStr.multiReplace([(re"\[(\d+)\]", "$1")]))