From 6149ba565e1f811057960805035db182ccf76dca Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 8 Jun 2023 21:21:32 -0500 Subject: Initial commit --- chicken-heredoc.scm | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 chicken-heredoc.scm diff --git a/chicken-heredoc.scm b/chicken-heredoc.scm new file mode 100755 index 0000000..e0e55ad --- /dev/null +++ b/chicken-heredoc.scm @@ -0,0 +1,60 @@ +#!/bin/sh +#| -*- scheme -*- +exec csi -ss "$0" "$@" +heredocs for chicken +idea-https://git.sr.ht/~evhan/beaker/tree/master/item/aux/generate-wiki-page.scm +|# + +(import (only (chicken irregex) + irregex-replace/all + irregex-search) + (only (chicken io) + read-string) + (only (chicken port) + make-concatenated-port) + (only (chicken random) + pseudo-random-real) + (only (chicken time) + current-seconds)) + +(define filestr + (make-parameter #f)) + +(define def-end + (make-parameter "&def-end")) + +(define (randomizer #!optional str) + (let ((base (number->string (+ (current-seconds) + (pseudo-random-real))))) + (if (and str (irregex-search base str)) + (randomizer str) + base))) + +(define-syntax def + (syntax-rules () + ((def var val) + ;; I'm pretty sure this only works in CHICKEN. + (begin (set! var val) + (def-end))))) + +(define (heredoc-expand file) + (let* ((delim (string-append "END-" (randomizer))) + (template (make-concatenated-port + (open-input-string (string-append "#<#" delim "\n")) + (open-input-file file) + (open-input-string (string-append "\n" delim "\n")))) + (expanded (open-output-string)) + (str (begin + (display (eval (read template)) expanded) + (get-output-string expanded)))) + (irregex-replace/all `(seq ,(def-end) (* "\n")) + str + ""))) + +(define (main args) + (for-each (lambda (f) + ;; Is this overkill? Maybe.... + (parameterize ((filestr (with-input-from-file f read-string))) + (parameterize ((def-end (randomizer (filestr)))) + (display (heredoc-expand f))))) + args)) -- cgit 1.4.1-21-gabe81