Skip to content

Instantly share code, notes, and snippets.

@miraneko
Last active May 18, 2025 09:59
Show Gist options
  • Save miraneko/950806847a322a3409df13dc25ddff84 to your computer and use it in GitHub Desktop.
Save miraneko/950806847a322a3409df13dc25ddff84 to your computer and use it in GitHub Desktop.
mkdatauri - a simple tool to make data: URIs, rewritten in scheme
;; Copyright (c) 2025, Mira Ciel Nekomimi-Sireneva
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
;;
;; 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;; SPDX-License-Identifier: BSD-2-Clause
;; this program uses CHICKEN 5 with eggs `args`, `uri-common`, and `base64`
(import (chicken process-context)
(chicken port)
(chicken io)
args
uri-common
base64)
(define (usage)
(with-output-to-port (current-error-port)
(lambda ()
(print "Usage: " (car (argv)) " [options...]")
(newline)
(display (args:usage opts))))
(exit 1))
(define opts
(list (args:make-option (t type) (#:required "TYPE") "Set the content type")
(args:make-option (c charset) (#:required "CHARSET") "Set the character set")
(args:make-option (u utf8) #:none "Set the character set to UTF-8")
(args:make-option (b base64) #:none "Use base64 instead of percent encoding")
(args:make-option (a auto) #:none "Use optimal encoding")
(args:make-option (n no-newline) #:none "Suppress newline")
(args:make-option (v V version) #:none "Display version"
(print "mkdatauri 2.0.0")
(exit))
(args:make-option (h help) #:none "Display this text"
(usage))))
(receive (options operands) (args:parse (command-line-arguments) opts)
(let* ((input (read-string))
(content-type (alist-ref 'type options))
(charset (or (alist-ref 'charset options)
(and (alist-ref 'utf8 options) "UTF-8")))
(no-newline? (alist-ref 'no-newline options))
(percent-encoded (uri-encode-string input))
(base64-encoded (base64-encode input))
(base64? (or (alist-ref 'base64 options)
(and (alist-ref 'auto options)
(< (+ 7 (string-length base64-encoded))
(string-length percent-encoded))))))
(display (string-append "data:"
(or content-type "")
(if charset (string-append ";charset=" charset)
"")
(if base64? (string-append ";base64," base64-encoded)
(string-append "," percent-encoded))))
(when (not no-newline?)
(newline))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment