3 Commits
0.2.0 ... 0.2.3

2 changed files with 26 additions and 11 deletions

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.2.0" version = "0.2.3"
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

@ -7,15 +7,30 @@ import std/[httpclient, json, logging, os, re, strutils, uri, wordwrap]
import cliutils, docopt, zero_functional import cliutils, docopt, zero_functional
proc formatMarkdown(raw: string): string = proc formatMarkdown(raw: string): string =
let rawLines = raw.splitLines --> map(it.strip) var reference = ""
let wrapped = (rawLines --> var inVerse = false
filter(not isEmptyOrWhitespace(it.strip) and match(it, re"^\[\d+\].*")). var verseLines = newSeq[string]()
map(it.multiReplace([(re"\((\d+)\)", ""), (re"\[(\d+)\]", "**$1**")])).
map(wrapWords(it, maxLineWidth = 74, newLine = "\p"))).
join("\p")
result = (wrapped.splitLines --> map("> " & it)).join("\p") & for line in raw.splitLines:
"\p>\p> -- *" & rawLines[0] & " (ESV)*" 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)*"
when isMainModule: when isMainModule:
const USAGE = """Usage: const USAGE = """Usage:
@ -44,7 +59,7 @@ Options:
try: try:
# Parse arguments # Parse arguments
let args = docopt(USAGE, version = "0.2.0") let args = docopt(USAGE, version = "0.2.3")
if args["--debug"]: if args["--debug"]:
consoleLogger.levelThreshold = lvlDebug consoleLogger.levelThreshold = lvlDebug
@ -78,7 +93,7 @@ Options:
else: else:
respJson["passages"].getElems --> map(formatMarkdown(it.getStr)) respJson["passages"].getElems --> map(formatMarkdown(it.getStr))
echo formattedPassages.join("\p") echo formattedPassages.join("\p\p")
except CatchableError: except CatchableError:
fatal getCurrentExceptionMsg() fatal getCurrentExceptionMsg()