Initial commit with initial data URI conversion implementation.
This commit is contained in:
commit
4add35fb5e
20
data_uri.nimble
Normal file
20
data_uri.nimble
Normal file
@ -0,0 +1,20 @@
|
||||
# Package
|
||||
|
||||
version = "0.1.0"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Utility for managing data-uris: transforming data to and from, etc."
|
||||
license = "GPL-3.0-only"
|
||||
srcDir = "src"
|
||||
installExt = @["nim"]
|
||||
bin = @["data_uri"]
|
||||
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires @[
|
||||
"nim >= 1.4.4",
|
||||
"docopt 0.6.8",
|
||||
"filetype",
|
||||
|
||||
"https://git.jdb-labs.com/jdb/update-nim-package-version"
|
||||
]
|
BIN
src/.data_uri.nim.swp
Normal file
BIN
src/.data_uri.nim.swp
Normal file
Binary file not shown.
88
src/data_uri.nim
Normal file
88
src/data_uri.nim
Normal file
@ -0,0 +1,88 @@
|
||||
## Data URI Utilities
|
||||
|
||||
import base64, docopt, filetype, logging, nre, os
|
||||
from strutils import isEmptyOrWhitespace
|
||||
|
||||
include "data_uripkg/version.nim"
|
||||
|
||||
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
|
||||
else: mimeType
|
||||
|
||||
let encoded = encode(value)
|
||||
|
||||
return "data:" & actualMimeType & ";base64," & encoded
|
||||
|
||||
|
||||
proc decodeDataUri*(dataUri: string): string =
|
||||
let parseMatch = dataUri.match(dataUriPattern)
|
||||
|
||||
if not parseMatch.isSome:
|
||||
raise newException(Exception, "input does not look like a valid data URI")
|
||||
|
||||
let encodedData = parseMatch.get.captures[2]
|
||||
return decode(encodedData)
|
||||
|
||||
when isMainModule:
|
||||
|
||||
try:
|
||||
let doc = """
|
||||
Usage:
|
||||
data_uri encode [options]
|
||||
data_uri decode [options]
|
||||
|
||||
Options:
|
||||
|
||||
-h, --help Print this usage information.
|
||||
|
||||
-i, --input <inFile> Read input from inFile rather than from stdin (the
|
||||
default).
|
||||
|
||||
-o, --output <outFile> Write output to outFile rather than to stdout (the
|
||||
default).
|
||||
|
||||
-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.
|
||||
"""
|
||||
|
||||
logging.addHandler(newConsoleLogger())
|
||||
|
||||
# Parse arguments
|
||||
let args = docopt(doc, version = DATA_URI_VERSION)
|
||||
|
||||
if args["--help"]:
|
||||
stderr.writeLine(doc)
|
||||
quit()
|
||||
|
||||
if args["--input"] and not fileExists($args["--input"]):
|
||||
raise newException(Exception, "'" & $args["--input"] & "' is not a valid file.'")
|
||||
|
||||
let fileIn =
|
||||
if args["--input"]: open($args["--input"])
|
||||
else: stdin
|
||||
|
||||
let fileOut =
|
||||
if args["--output"]: open($args["--output"], fmWrite)
|
||||
else: stdout
|
||||
|
||||
try:
|
||||
if args["encode"]:
|
||||
let binaryData = readAll(fileIn)
|
||||
write(fileOut, encodeAsDataUri(binaryData))
|
||||
|
||||
if args["decode"]:
|
||||
let dataUri = readAll(fileIn)
|
||||
write(fileOut, decodeDataUri(dataUri))
|
||||
|
||||
finally:
|
||||
close(fileIn)
|
||||
close(fileOut)
|
||||
|
||||
except:
|
||||
fatal "data_uri: " & getCurrentExceptionMsg()
|
||||
quit(QuitFailure)
|
1
src/data_uripkg/version.nim
Executable file
1
src/data_uripkg/version.nim
Executable file
@ -0,0 +1 @@
|
||||
const DATA_URI_VERSION* = "0.1.0"
|
1
tests/config.nims
Normal file
1
tests/config.nims
Normal file
@ -0,0 +1 @@
|
||||
switch("path", "$projectDir/../src")
|
12
tests/test1.nim
Normal file
12
tests/test1.nim
Normal file
@ -0,0 +1,12 @@
|
||||
# This is just an example to get you started. You may wish to put all of your
|
||||
# tests into a single file, or separate them into multiple `test1`, `test2`
|
||||
# etc. files (better names are recommended, just make sure the name starts with
|
||||
# the letter 't').
|
||||
#
|
||||
# To run these tests, simply execute `nimble test`.
|
||||
|
||||
import unittest
|
||||
|
||||
#import data_uripkg/submodule
|
||||
#test "correct welcome":
|
||||
# check getWelcomeMessage() == "Hello, World!"
|
Loading…
x
Reference in New Issue
Block a user