Add --split option to split over several lines.

This commit is contained in:
Jonathan Bernard 2022-12-08 18:35:06 -06:00
parent c998c5c48c
commit 4d6fa44bc3
3 changed files with 22 additions and 9 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "1.0.0"
version = "1.0.1"
author = "Jonathan Bernard"
description = "Utility for managing data-uris: transforming data to and from, etc."
license = "GPL-3.0-only"
@ -15,6 +15,7 @@ requires @[
"nim >= 1.4.4",
"docopt 0.6.8",
"filetype",
"zero_functional",
"https://git.jdb-software.com/jdb/update-nim-package-version"
]

View File

@ -1,13 +1,13 @@
## Data URI Utilities
import base64, docopt, filetype, logging, nre, os
from strutils import isEmptyOrWhitespace
import std/[base64, logging, os, sequtils, strutils, wordwrap]
import std/nre except toSeq
import docopt, filetype, zero_functional
include "data_uripkg/version.nim"
import data_uripkg/version
let dataUriPattern = re"^data:([^;,]+)?(;base64)?,(.+)$"
proc encodeAsDataUri*(value: string, mimeType: string = ""): string =
let actualMimeType =
if mimeType.isEmptyOrWhitespace: filetype.match(cast[seq[byte]](value)).mime.value
@ -48,6 +48,10 @@ Options:
-t, --type <mimeType> Manually set the MIME type rather than trying to
infer it from the file extension. This only applies
when encoding files to data-uris.
-s, --split <length> Split the output across multiple lines with a maximum
of <length> characters on each line. By default the
output is contiguous, all on one line.
"""
logging.addHandler(newConsoleLogger())
@ -73,12 +77,20 @@ Options:
try:
if args["encode"]:
let binaryData = readAll(fileIn)
write(fileOut, encodeAsDataUri(binaryData))
let encodedData = encodeAsDataUri(binaryData)
if args["--split"]:
write(fileOut, encodedData.wrapWords(parseInt($args["--split"])))
else: write(fileOut, encodedData)
if args["decode"]:
let dataUri = readAll(fileIn)
write(fileOut, decodeDataUri(dataUri))
let dataToDecode = (dataUri.splitLines.toSeq -->
map(it.replace(re"\s+", ""))).
join("")
let decodedData = decodeDataUri(dataToDecode)
write(fileOut, decodedData)
except: raise getCurrentException()
finally:
close(fileIn)
close(fileOut)

View File

@ -1 +1 @@
const DATA_URI_VERSION* = "1.0.0"
const DATA_URI_VERSION* = "1.0.1"