| 1 |
;;; |
| 2 |
;;; text.template - simple text templating |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2016-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; This module just uses string-interpolate feature to apply |
| 35 |
;; runtime-provided text (template). Most of the code is for |
| 36 |
;; easy-to-use environment manipulation. |
| 37 |
|
| 38 |
(define-module text.template |
| 39 |
(use gauche.dictionary) |
| 40 |
(use file.util) |
| 41 |
(export make-template-environment |
| 42 |
expand-template-string |
| 43 |
expand-template-file)) |
| 44 |
(select-module text.template) |
| 45 |
|
| 46 |
(define-class <template-environment> () |
| 47 |
(;; all slots are private |
| 48 |
(module :init-keyword :module))) |
| 49 |
|
| 50 |
(define (make-template-environment :key |
| 51 |
(extends '(gauche)) |
| 52 |
(imports '()) |
| 53 |
(bindings #f)) ; can be dict, or kv-list |
| 54 |
(make <template-environment> |
| 55 |
:module (setup-template-environment extends imports bindings))) |
| 56 |
|
| 57 |
(define (setup-template-environment extends imports bindings) |
| 58 |
(rlet1 m (make-module #f) |
| 59 |
(dolist [i imports] |
| 60 |
(if (list? i) |
| 61 |
(eval `(import ,@i) m) |
| 62 |
(eval `(import ,i) m))) |
| 63 |
(cond |
| 64 |
[(is-a? bindings <dictionary>) |
| 65 |
(dict-for-each |
| 66 |
bindings |
| 67 |
(^[k v] |
| 68 |
(unless (symbol? k) |
| 69 |
(error "Symbol required for binding table key, but got:" k)) |
| 70 |
(eval `(define ,k ',v) m)))] |
| 71 |
[(list? bindings) |
| 72 |
(do-plist ([k v] bindings) |
| 73 |
(unless (symbol? k) |
| 74 |
(error "Symbol required for binding table key, but got:" k)) |
| 75 |
(eval `(define ,k ',v) m))] |
| 76 |
[(not bindings)] |
| 77 |
[else (error "Invalid bindings: a dictionary or kv-list expected, but got" |
| 78 |
bindings)]) |
| 79 |
(eval `(extend ,@extends) m))) |
| 80 |
|
| 81 |
(define (expand-template-string text env) |
| 82 |
(assume-type env <template-environment>) |
| 83 |
(eval (string-interpolate text) (~ env'module))) |
| 84 |
|
| 85 |
(define (expand-template-file filename env :optional (paths '())) |
| 86 |
(assume-type env <template-environment>) |
| 87 |
(if-let1 file (if (or (null? paths) (absolute-path? filename)) |
| 88 |
filename |
| 89 |
(any (^p (let1 f (build-path p filename) |
| 90 |
(and (file-exists? f) f))) |
| 91 |
paths)) |
| 92 |
(expand-template-string (file->string file) env) |
| 93 |
(errorf "Couldn't find template file ~s in ~s" filename paths))) |