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 # Package
version = "1.0.0" version = "1.0.1"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Utility for managing data-uris: transforming data to and from, etc." description = "Utility for managing data-uris: transforming data to and from, etc."
license = "GPL-3.0-only" license = "GPL-3.0-only"
@ -15,9 +15,10 @@ requires @[
"nim >= 1.4.4", "nim >= 1.4.4",
"docopt 0.6.8", "docopt 0.6.8",
"filetype", "filetype",
"zero_functional",
"https://git.jdb-software.com/jdb/update-nim-package-version" "https://git.jdb-software.com/jdb/update-nim-package-version"
] ]
task updateVersion, "Update the version of this package.": task updateVersion, "Update the version of this package.":
exec "update_nim_package_version data_uri 'src/data_uripkg/version.nim'" exec "update_nim_package_version data_uri 'src/data_uripkg/version.nim'"

View File

@ -1,13 +1,13 @@
## Data URI Utilities ## Data URI Utilities
import base64, docopt, filetype, logging, nre, os import std/[base64, logging, os, sequtils, strutils, wordwrap]
from strutils import isEmptyOrWhitespace import std/nre except toSeq
import docopt, filetype, zero_functional
include "data_uripkg/version.nim" import data_uripkg/version
let dataUriPattern = re"^data:([^;,]+)?(;base64)?,(.+)$" let dataUriPattern = re"^data:([^;,]+)?(;base64)?,(.+)$"
proc encodeAsDataUri*(value: string, mimeType: string = ""): string = proc encodeAsDataUri*(value: string, mimeType: string = ""): string =
let actualMimeType = let actualMimeType =
if mimeType.isEmptyOrWhitespace: filetype.match(cast[seq[byte]](value)).mime.value 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 -t, --type <mimeType> Manually set the MIME type rather than trying to
infer it from the file extension. This only applies infer it from the file extension. This only applies
when encoding files to data-uris. 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()) logging.addHandler(newConsoleLogger())
@ -73,12 +77,20 @@ Options:
try: try:
if args["encode"]: if args["encode"]:
let binaryData = readAll(fileIn) 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"]: if args["decode"]:
let dataUri = readAll(fileIn) 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: finally:
close(fileIn) close(fileIn)
close(fileOut) close(fileOut)

View File

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