diff options
author | Case Duckworth | 2024-06-15 21:17:03 -0500 |
---|---|---|
committer | Case Duckworth | 2024-06-15 21:17:03 -0500 |
commit | 703e9e93087d32364087a0ebc9e315869b70ff7c (patch) | |
tree | de5cfdd1a687dbe68686929497e870fad5f28800 /lib/emit.scm | |
parent | Write executable (diff) | |
download | jimmy-703e9e93087d32364087a0ebc9e315869b70ff7c.tar.gz jimmy-703e9e93087d32364087a0ebc9e315869b70ff7c.zip |
Update things
Diffstat (limited to 'lib/emit.scm')
-rw-r--r-- | lib/emit.scm | 96 |
1 files changed, 69 insertions, 27 deletions
diff --git a/lib/emit.scm b/lib/emit.scm index 2a8ab97..546ec5c 100644 --- a/lib/emit.scm +++ b/lib/emit.scm | |||
@@ -2,8 +2,10 @@ | |||
2 | 2 | ||
3 | (import scheme (chicken base) | 3 | (import scheme (chicken base) |
4 | (chicken format) | 4 | (chicken format) |
5 | (chicken io) | ||
5 | (chicken irregex) | 6 | (chicken irregex) |
6 | (chicken port) | 7 | (chicken port) |
8 | (chicken process) | ||
7 | (chicken string) | 9 | (chicken string) |
8 | (only utf8-srfi-13 string-join) | 10 | (only utf8-srfi-13 string-join) |
9 | (jimmy util)) | 11 | (jimmy util)) |
@@ -15,9 +17,14 @@ | |||
15 | (with-output-to-string | 17 | (with-output-to-string |
16 | (lambda () (emit doc)))) | 18 | (lambda () (emit doc)))) |
17 | 19 | ||
20 | ;;; Change these for different output types | ||
21 | |||
22 | (define-public output-type | ||
23 | (make-parameter 'gemini)) | ||
24 | |||
18 | (define-public formats | 25 | (define-public formats |
19 | (make-parameter | 26 | (make-parameter |
20 | ;; (TYPE (line . LINE-FMT) (stanza . STANZA-FMT) (inline . INLINE-FMT)) | 27 | ;; (EL (line . LINE-FMT) (stanza . STANZA-FMT) (inline . INLINE-FMT)) |
21 | '((para (line . "~A") | 28 | '((para (line . "~A") |
22 | (stanza . "~A~%~%")) | 29 | (stanza . "~A~%~%")) |
23 | (verb (line . "~A~%") | 30 | (verb (line . "~A~%") |
@@ -36,35 +43,60 @@ | |||
36 | (hdr3 (line . "### ~A~%") | 43 | (hdr3 (line . "### ~A~%") |
37 | (stanza . "~A~%"))))) | 44 | (stanza . "~A~%"))))) |
38 | 45 | ||
46 | (define-public set-formats! formats) | ||
47 | |||
39 | (define-public filters | 48 | (define-public filters |
40 | (make-parameter | 49 | (make-parameter |
41 | ;; (TYPE (line . LINE-FILTER) (stanza . STANZA-FILTER)) | 50 | ;; (EL (line . LINE-FILTER) (stanza . STANZA-FILTER)) |
42 | ;; line-filter : (lambda (list-of-strs) ...) -> list-of-strs (for format) | 51 | ;; line-filter : (lambda (list-of-strs) ...) -> list-of-strs (for format) |
43 | ;; stanza-filter : (lambda (list-of-strs) ...) -> str | 52 | ;; stanza-filter : (lambda (list-of-strs) ...) -> str |
44 | `((verb (line . ,identity) | 53 | `((verb |
45 | (stanza . ,join-lines)) | 54 | (stanza . ,(o ensure-newline (cut string-join <> "\n")))) |
46 | (default | 55 | (default |
47 | (line . ,identity) | 56 | (line . ,identity) |
48 | (stanza . ,flush-lines-left))))) | 57 | (stanza . ,flush-lines-left))))) |
49 | 58 | ||
59 | (define-public set-filters! filters) | ||
60 | |||
61 | ;;; Implementation | ||
62 | |||
50 | (define (format-line line el) | 63 | (define (format-line line el) |
51 | (cond | 64 | (cond |
52 | ((string? (car line)) ; regular stanza line | 65 | ((string? (car line)) ; regular stanza line |
53 | (sprintf* (get-format el 'line) | 66 | (format/filter el 'line line)) |
54 | ((get-filter el 'line) line))) | ||
55 | ((symbol? (car line)) ; inline element | 67 | ((symbol? (car line)) ; inline element |
56 | (sprintf* (get-format (car line) 'inline) | 68 | (format/filter (car line) '(inline . line) (cdr line))) |
57 | ((get-filter (car line) 'line) (cdr line)))) | ||
58 | (else (error "Malformed line" line)))) | 69 | (else (error "Malformed line" line)))) |
59 | 70 | ||
60 | (define (format-stanza stanza) | 71 | (define (format-stanza stanza) |
61 | (let* ((type (car stanza)) | 72 | (let* ((el (caar stanza)) |
62 | (data (cdr stanza)) | ||
63 | (text (map (lambda (ln) | 73 | (text (map (lambda (ln) |
64 | (format-line ln type)) | 74 | (format-line ln el)) |
65 | data))) | 75 | (cdr stanza)))) |
66 | (sprintf (get-format type 'stanza) | 76 | (case el |
67 | ((get-filter type 'stanza) text)))) | 77 | ((verb) (format-verb stanza)) |
78 | (else | ||
79 | (format/filter el 'stanza text))))) | ||
80 | |||
81 | (define (format-verb stanza) | ||
82 | (let ((el (car stanza)) | ||
83 | (text (apply append (cdr stanza)))) | ||
84 | (with-output-to-string | ||
85 | (lambda () | ||
86 | (cond | ||
87 | ((and (pair? (cdr el)) | ||
88 | (equal? (cadr el) "|")) | ||
89 | ;; special case: pipe to an external process | ||
90 | (let ((cmdline (cddr el))) | ||
91 | (if (find-command (car cmdline) #;<TODO:JIMMY_PATH>) | ||
92 | (receive (in out pid) | ||
93 | (process (car cmdline) (cdr cmdline) | ||
94 | `(("JIMMY_OUTPUT" . ,(->string (output-type))))) | ||
95 | (display (ensure-newline text) out) | ||
96 | (read-string #f in))))) | ||
97 | (else ; verbatim baby | ||
98 | (printf (get-format 'verb 'stanza) | ||
99 | ((get-filter 'verb 'stanza) text)))))))) | ||
68 | 100 | ||
69 | ;;; Utilities | 101 | ;;; Utilities |
70 | 102 | ||
@@ -77,18 +109,28 @@ | |||
77 | (define (get-format el scope) | 109 | (define (get-format el scope) |
78 | (or (get-from (formats) el scope) | 110 | (or (get-from (formats) el scope) |
79 | "")) | 111 | "")) |
112 | |||
80 | (define (get-filter el scope) (get-from (filters) el scope)) | 113 | (define (get-filter el scope) (get-from (filters) el scope)) |
81 | 114 | ||
82 | (define (sprintf* fmt lis) | 115 | (define (format/filter el scope text) |
83 | (let loop ((num (length (irregex-extract "~[aA]" fmt))) | 116 | (define (sprintf* fmt lis) |
84 | (lis lis) | 117 | (let loop ((num (length (irregex-extract "~[aA]" fmt))) |
85 | (out '())) | 118 | (lis (if (list? lis) lis (list lis))) |
86 | (cond | 119 | (out '())) |
87 | ((null? lis) | 120 | (cond |
88 | (apply sprintf fmt (reverse out))) | 121 | ((null? lis) |
89 | ((= 1 num) | 122 | (apply sprintf fmt (reverse out))) |
90 | (loop 0 '() (cons (string-join lis) out))) | 123 | ((= 1 num) |
91 | (else | 124 | (loop 0 '() (cons (string-join lis) out))) |
92 | (loop (- num 1) | 125 | (else |
93 | (cdr lis) | 126 | (loop (- num 1) |
94 | (cons (car lis) out)))))) | 127 | (cdr lis) |
128 | (cons (car lis) out)))))) | ||
129 | |||
130 | (define-values (format-scope filter-scope) | ||
131 | (if (pair? scope) | ||
132 | (values (car scope) (cdr scope)) | ||
133 | (values scope scope))) | ||
134 | |||
135 | (sprintf* (get-format el format-scope) | ||
136 | ((get-filter el filter-scope) text))) | ||