diff --git a/esv_api.nimble b/esv_api.nimble index 954c66f..e8ac241 100644 --- a/esv_api.nimble +++ b/esv_api.nimble @@ -1,6 +1,6 @@ # Package -version = "0.2.2" +version = "0.2.3" author = "Jonathan Bernard" description = "Simple Nim CLI wrapper around the ESV API (api.esv.org)" license = "MIT" diff --git a/src/esv_api.nim b/src/esv_api.nim index f0301b1..6e49f5c 100644 --- a/src/esv_api.nim +++ b/src/esv_api.nim @@ -7,15 +7,30 @@ 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)*" when isMainModule: const USAGE = """Usage: @@ -44,7 +59,7 @@ Options: try: # Parse arguments - let args = docopt(USAGE, version = "0.2.2") + let args = docopt(USAGE, version = "0.2.3") if args["--debug"]: consoleLogger.levelThreshold = lvlDebug