about summary refs log tree commit diff stats
path: root/lib/wrap.scm
blob: f801029f102494e5dfdab4b7ee3d4fb6ed37e029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(declare (module (jimmy wrap)))

(import scheme (chicken base)
        (jimmy emit)
        (jimmy util)
        (only (chicken io) read-string)
        (only (chicken port) with-output-to-string)
        (only (chicken string) string-translate* string-intersperse))

;; templates are strings with variables interpolated with "{{variables}}"

(define-public (wrap document template)
  (let* ((meta (map (lambda (el)
                      (cons (string-append "{{" (car el) "}}")
                            (string-intersperse (cdr el) " ")))
                    (alist-walk document 'meta)))
         (body (cons "{{body}}" (emit-string document))))
    (string-translate* template (cons body meta))))

(define-public (wrap-with document file)
  (wrap document (with-input-from-file file read-string)))