From 815e669310f5e73d13cc121bd7f6cdaec5b6ec0d Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 26 May 2024 22:49:44 -0500 Subject: Updates! I totally forgot to actually commit things for a while, so uh Updates!!! --- src/emit.scm | 61 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 24 deletions(-) (limited to 'src/emit.scm') diff --git a/src/emit.scm b/src/emit.scm index aa36eb5..d6fe19e 100644 --- a/src/emit.scm +++ b/src/emit.scm @@ -34,18 +34,6 @@ (line . "~A") (block . "~A~%~%")))) -(define (string-join ss #!optional sep) - (if (string? ss) ss - (string-intersperse ss (or sep " ")))) - -(define (char->tag char beg end) - (lambda (str) - (irregex-replace/all `(: ($ (or bos space)) - ,char ($ (+ (~ ,char))) ,char - ($ (or space eos))) - str - 1 beg 2 end 3))) - (define-public filters `((para (line . ,(o (char->tag "*" "" "") @@ -58,16 +46,27 @@ (line . ,(lambda (ln) (let ((ws (cond ((list? ln) ln) ((string? ln) (string-split ln))))) - (list (car ws) (string-join (cdr ws))))))) + (let ((x (list (car ws) (string-join (cdr ws))))) + x))))) (default - (line . ,list) + (line . ,(lambda (x) (print x) (if (list? x) x (list x)))) (block . ,identity)))) +(define (string-join ss #!optional sep) + (if (string? ss) ss + (string-intersperse ss (or sep " ")))) + +(define (char->tag char beg end) + (lambda (str) + (irregex-replace/all `(: ($ (or bos space)) + ,char ($ (+ (~ ,char))) ,char + ($ (or space eos))) + str + 1 beg 2 end 3))) + (define (get-from from type subtype) (or (alist-walk from type subtype) - (if (eq? subtype 'inline) - (alist-walk from type 'list) - (lambda _ '(""))))) + (get-from from 'default subtype))) (define (get-format type subtype) (get-from formats type subtype)) (define (get-filter type subtype) (get-from filters type subtype)) @@ -76,19 +75,19 @@ (cond ;; if LINE is a string, wrap it in a list ((string? line) - (set! line (list line))) + (sprintf (get-format type 'line) + ((get-filter type 'line) line))) ;; if it's a list of strings, join them together and filter them ((and (list? line) (string? (car line))) - (set! line ((get-filter type 'line) line))) + (sprintf (get-format type 'line) + (apply string-append ((get-filter type 'line) line)))) ;; if the car of LINE is a symbol, it's an inline thing. ((and (list? line) (symbol? (car line))) - (set! line (format-line (get-format (car line) 'inline) - ((get-filter (car line) 'line) (cdr line)) - type))) - (else (error "Malformed line" line))) - (apply sprintf fmt line)) + (sprintf* (get-format (car line) 'inline) + ((get-filter (car line) 'line) (cdr line)))) + (else (error "Malformed line" line)))) (define (format-block block) (if (assq (car block) formats) @@ -107,3 +106,17 @@ (sprintf (get-format type 'block) ((get-filter type 'block) text))) "")) + +(define (sprintf* fmt lis) + (let loop ((num (length (irregex-extract "~[aA]" fmt))) + (lis lis) + (out '())) + (cond + ((null? lis) + (apply sprintf fmt (reverse out))) + ((= 1 num) + (loop 0 '() (cons (string-join lis) out))) + (else + (loop (- num 1) + (cdr lis) + (cons (car lis) out)))))) -- cgit 1.4.1-21-gabe81