#!/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))