source: main/templ.lisp

Last change on this file was main,1, checked in by David Owen <dsowen@…>, 18 years ago

First version.

File size: 2.9 KB
Line 
1#|
2Copyright (C) 2008  David Owen <dsowen@fugue88.ws>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU Lesser Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU Lesser Public License for more details.
13
14You should have received a copy of the GNU Lesser Public License
15along with this program.  If not, see <http://www.gnu.org/licenses/>.
16|#
17
18(defpackage #:dso-simple-template-templ
19  (:use #:cl)
20  (:import-from #:dso-parse #:defgrammar)
21  (:export #:template #:read-template-from-string #:read-template
22           #:load-template))
23
24(in-package #:dso-simple-template-templ)
25
26
27
28(defun flatten-chars (tree)
29  (flet ((f (tree)
30           (destructuring-bind (x) tree
31             (aref x 0))))
32    (coerce (mapcar #'f tree) 'string)))
33
34(defun flatten-expression-chars (tree)
35  (flet ((f (tree)
36           (destructuring-bind (x) tree
37             (aref x 0))))
38    (destructuring-bind (x) tree
39      (coerce (mapcar #'f x) 'string))))
40
41(defgrammar ()
42  (expression-start #\{)
43  (expression-finish #\})
44  (escaped-expression-start ({} 2 2 expression-start)
45                            :filter (lambda (x) '("{"))
46                            :cclass t)
47  #|(escaped-expression-finish ({} 2 2 expression-finish)
48                             :filter (lambda (x) '("}"))
49                             :cclass t)|#
50  (newline #\Newline
51           :filter (lambda (x) '("
52"))
53           :cclass t)
54  (expression-char (/ #|escaped-expression-finish|# ((! expression-finish) (^ ".")))
55                   :cclass t)
56  (expression ((= expression-start) (+ expression-char) (= expression-finish))
57              :filter flatten-expression-chars)
58  (body-char (/ escaped-expression-start ((! expression-start) (^ ".")) newline)
59             :cclass t)
60  (body-text (+ body-char) :filter flatten-chars)
61  (simple-template (+ (/ body-text expression))))
62
63
64
65(defclass template ()
66  ((parts :type list :initarg :parts :reader parts)))
67
68(defun read-template-from-string (s)
69  (multiple-value-bind (ok tail tree) (simple-template s)
70    (unless (and ok (string= tail ""))
71      (error "Can't parse the template."))
72    (make-instance 'template :parts (cdr tree))))
73
74(defun gulp-stream (stream)
75  (let ((s-out (make-string-output-stream))
76        (buffer (make-sequence 'string 4096)))
77    (do ((n #1=(read-sequence buffer stream) #1#))
78        ((= n 0))
79      (write-sequence buffer s-out :end n))
80    (get-output-stream-string s-out)))
81
82(defun read-template (stream)
83  (read-template-from-string (gulp-stream stream)))
84
85(defun load-template (filename)
86  (with-open-file (stream filename)
87    (read-template stream)))
Note: See TracBrowser for help on using the repository browser.