-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtransform.lisp
45 lines (35 loc) · 1.45 KB
/
transform.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(in-package #:org.shirakumo.staple)
(defvar *pathname-type-type-map* ())
(defun pathname-type->type (type &optional errorp)
(loop for (result . types) in *pathname-type-type-map*
do (when (find type types :test #'string-equal)
(return result))
finally (when errorp (error "Unknown pathname-type ~s." type))))
(defun (setf pathname-type->type) (types type)
(check-type type keyword)
(check-type types list)
(setf *pathname-type-type-map*
(remove type *pathname-type-type-map* :key #'car))
(when types
(setf *pathname-type-type-map*
(list* (list* type types)
*pathname-type-type-map*)))
types)
(defgeneric compile-source (source type))
(defmethod compile-source (source (type string))
(compile-source source (pathname-type->type type T)))
(defmethod compile-source ((source pathname) type)
(compile-source (read-file source) type))
(defmethod compile-source ((source pathname) (type (eql T)))
(compile-source source (pathname-type source)))
(defmacro define-source-compiler ((type &rest pathname-types) (input) &body body)
(check-type type keyword)
`(progn
,@(when pathname-types `((setf (pathname-type->type ,type) ',pathname-types)))
(defmethod compile-source ((,input string) (type (eql ,type)))
,@body)
',type))
(define-source-compiler (:html "htm" "html" "xhtml") (input)
(plump:parse input))
(define-source-compiler (:text "txt" "text") (input)
input)