about summary refs log tree commit diff stats
path: root/src/wrap.scm
blob: 0ed88681641872680ca6c8118a4b8abe8444ac49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(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*))

;; 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}}"
                     (with-output-to-string
                       (lambda () (emit document))))))
    (string-translate* template (cons body meta))))

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