summary refs log tree commit diff stats
path: root/boudin.util.scm
diff options
context:
space:
mode:
Diffstat (limited to 'boudin.util.scm')
-rw-r--r--boudin.util.scm68
1 files changed, 68 insertions, 0 deletions
diff --git a/boudin.util.scm b/boudin.util.scm new file mode 100644 index 0000000..2683730 --- /dev/null +++ b/boudin.util.scm
@@ -0,0 +1,68 @@
1(declare (module (boudin util))
2 (export assoc-ref
3 slurp
4 wrap-paragraphs
5 edisplay
6 eprint))
7
8(import (srfi 1)
9 (srfi 152))
10
11(define (edisplay x)
12 (parameterize ((current-output-port (current-error-port)))
13 (display x)))
14
15(define (eprint . xs)
16 (parameterize ((current-output-port (current-error-port)))
17 (for-each display xs)
18 (newline)))
19
20(define (assoc-ref k alist)
21 (let ((k/v (and (pair? alist)
22 (assoc k alist))))
23 (if k/v (cdr k/v) #f)))
24
25(define (wrap-paragraphs text)
26 (let loop ((ps (map string-trim (*split-paragraphs text)))
27 (acc '()))
28 (cond
29 ((null? ps)
30 (apply string-append (reverse acc)))
31 ((zero? (string-length (car ps)))
32 (loop (cdr ps) acc))
33 ((eq? #\< (string-ref (car ps) 0))
34 (loop (cdr ps) (cons (car ps) acc)))
35 (else
36 (loop (cdr ps)
37 (cons (string-append "<p>" (car ps) "</p>\n")
38 acc))))))
39
40(define (*split-paragraphs text)
41 (define (*end-buf buf acc)
42 (cons (apply string-append (reverse buf)) acc))
43 (let loop ((ls (string-split text "\n"))
44 (buf '())
45 (acc '()))
46 (cond
47 ((and (null? ls)
48 (null? buf))
49 (reverse acc))
50 ((null? ls)
51 (loop '() '() (*end-buf buf acc)))
52 ((zero? (string-length (car ls)))
53 (loop (cdr ls) '() (*end-buf buf acc)))
54 (else
55 (loop (cdr ls)
56 (cons (string-append (car ls) "\n") buf)
57 acc)))))
58
59(define slurp
60 (case-lambda
61 (() (slurp (current-input-port)))
62 ((port)
63 (let loop ((ch (read-char port))
64 (acc '()))
65 (if (eof-object? ch)
66 (list->string (reverse acc))
67 (loop (read-char port)
68 (cons ch acc)))))))