Add formatting options.
This commit is contained in:
		| @@ -1,6 +1,6 @@ | |||||||
| # Package | # Package | ||||||
|  |  | ||||||
| version       = "0.1.0" | version       = "0.2.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" | ||||||
|   | |||||||
| @@ -3,9 +3,20 @@ | |||||||
|  |  | ||||||
| ## Simple command-line wrapper around the ESV API. | ## Simple command-line wrapper around the ESV API. | ||||||
|  |  | ||||||
| import std/[httpclient, json, logging, os, re, strutils, uri] | 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 = | ||||||
|  |   let rawLines = raw.splitLines --> map(it.strip) | ||||||
|  |   let wrapped = (rawLines --> | ||||||
|  |     filter(not isEmptyOrWhitespace(it.strip) and match(it, re"^\[\d+\].*")). | ||||||
|  |     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>\p> -- *" & rawLines[0] & " (ESV)*" | ||||||
|  |  | ||||||
| when isMainModule: | when isMainModule: | ||||||
|   const USAGE = """Usage: |   const USAGE = """Usage: | ||||||
|   esv_api <reference> [options] |   esv_api <reference> [options] | ||||||
| @@ -17,6 +28,9 @@ Options: | |||||||
|   --echo-args                   Echo back the arguments that were passed on the |   --echo-args                   Echo back the arguments that were passed on the | ||||||
|                                 command line for debugging purposes. |                                 command line for debugging purposes. | ||||||
|  |  | ||||||
|  |   -f, --output-format <format>  Select a specific output format. Valid values | ||||||
|  |                                 are 'raw', 'markdown', 'plain'. | ||||||
|  |  | ||||||
|   -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 | ||||||
|                                 .esv_api.cfg.json file or the ESV_API_TOKEN |                                 .esv_api.cfg.json file or the ESV_API_TOKEN | ||||||
| @@ -30,7 +44,7 @@ Options: | |||||||
|  |  | ||||||
|   try: |   try: | ||||||
|     # Parse arguments |     # Parse arguments | ||||||
|     let args = docopt(USAGE, version = "0.1.0") |     let args = docopt(USAGE, version = "0.2.0") | ||||||
|  |  | ||||||
|     if args["--debug"]: |     if args["--debug"]: | ||||||
|       consoleLogger.levelThreshold = lvlDebug |       consoleLogger.levelThreshold = lvlDebug | ||||||
| @@ -55,8 +69,14 @@ Options: | |||||||
|     debug "requesting " & urlPath |     debug "requesting " & urlPath | ||||||
|     let respJson = parseJson(http.getContent(urlPath)) |     let respJson = parseJson(http.getContent(urlPath)) | ||||||
|  |  | ||||||
|     let formattedPassages = respJson["passages"].getElems --> |     let formattedPassages = | ||||||
|       map(it.getStr.multiReplace([(re"\[(\d+)\]", "$1")])) |       case $args["--output-format"]: | ||||||
|  |       of "text": | ||||||
|  |         respJson["passages"].getElems --> | ||||||
|  |           map(it.getStr.multiReplace([(re"\[(\d+)\]", "$1")])) | ||||||
|  |       of "raw": respJson["passages"].getElems --> map(it.getStr) | ||||||
|  |       else: | ||||||
|  |         respJson["passages"].getElems --> map(formatMarkdown(it.getStr)) | ||||||
|  |  | ||||||
|     echo formattedPassages.join("\p") |     echo formattedPassages.join("\p") | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user