Commit 2d8adda1549b8154c7c8b59d3231095a639aae78

Thomas de Grivel 2013-12-21T20:19:06

Refactor extensions as symbols. New MIME-TYPE generic function for assets and extensions. Parse mime.types files like those from apache or nginx and try to parse these files if they exist at run time.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
diff --git a/asset.lisp b/asset.lisp
index 0366ef4..b2f08e3 100644
--- a/asset.lisp
+++ b/asset.lisp
@@ -29,7 +29,7 @@
 	       :type string)
    (source-ext :initarg :source-ext
 	       :reader asset-source-ext
-	       :type keyword)
+	       :type extension)
    (sources :initarg :sources
 	    :reader asset-sources
 	    :type list)))
@@ -48,19 +48,22 @@
 (defmethod asset-ext ((asset asset))
   (asset-source-ext asset))
 
+(defmethod mime-type ((asset asset))
+  (mime-type (asset-ext asset)))
+
 (defmethod asset-url ((asset asset))
   (expand-uri nil *assets-url-template*
 	      :name (asset-name asset)
-	      :ext (string-downcase (asset-ext asset))))
+	      :ext (subseq (string-downcase (asset-ext asset)) 1)))
 
 (defmethod asset-path ((asset asset))
   (expand-uri nil *assets-path-template*
 	      :name (asset-name asset)
-	      :ext (string-downcase (asset-ext asset))))
+	      :ext (subseq (string-downcase (asset-ext asset)) 1)))
 
 (defmethod asset-source-path ((asset asset))
   (with-slots (name source-dir source-ext) asset
-    (str source-dir name (when source-ext ".") source-ext)))
+    (str source-dir name source-ext)))
 
 (defmethod print-object ((asset asset) stream)
   (print-unreadable-object (asset stream :type t)
@@ -112,7 +115,7 @@
 
 (defun extension-asset-class (extension
 			      &optional (class (find-class 'asset)))
-  (declare (type symbol extension)
+  (declare (type extension extension)
 	   (type class class))
   (when extension
     (labels ((matching-asset-class (c)
diff --git a/css.lisp b/css.lisp
index af4a0d2..523b46b 100644
--- a/css.lisp
+++ b/css.lisp
@@ -23,10 +23,10 @@
 (defclass css-asset (preprocessed-asset) ())
 
 (defmethod asset-ext ((asset css-asset))
-  (extension #:css))
+  '.css)
 
 (defmethod asset-class-extensions ((class (eql 'css-asset)))
-  (extensions #:css #:less))
+  '(.css .less))
 
 (defmethod asset-include ((output stream)
 			  (context (eql :html))
diff --git a/extensions.lisp b/extensions.lisp
index bc67668..fc28dbb 100644
--- a/extensions.lisp
+++ b/extensions.lisp
@@ -20,20 +20,17 @@
 
 ;;  Extensions package
 
-(let ((assets.extensions (defpackage :assets.extensions)))
+(defun intern-extension (name)
+  (let ((sym (intern (string-upcase (if (char= #\. (char name 0))
+					name
+					(concatenate 'string "." name)))
+		     :L>ext)))
+    (export sym :L>ext)
+    sym))
 
-  (do-symbols (sym assets.extensions)
-    (unintern sym assets.extensions))
+(defun extension-p (thing)
+  (and (symbolp thing)
+       (eq #.(find-package :L>ext) (symbol-package thing))))
 
-  (defun intern-extension (thing)
-    (when thing
-      (intern (string-upcase thing) assets.extensions))))
-
-(defmacro extension (thing)
-  (or `',(intern-extension thing)
-      `(intern-extension ,thing)))
-
-(defmacro extensions (&rest things)
-  `(list ,@(mapcar (lambda (thing)
-		     `(extension ,thing))
-		   things)))
+(deftype extension ()
+  `(and symbol (satisfies extension-p)))
diff --git a/find.lisp b/find.lisp
index c07fca0..939c0c9 100644
--- a/find.lisp
+++ b/find.lisp
@@ -46,12 +46,11 @@
 			assets)
   (let ((absolute-dir (truename dir))
 	(assets assets))
-    (dolist (path (directory (str dir name (when ext ".") ext)))
+    (dolist (path (directory (str dir name ext)))
       (let* ((name.ext (enough-namestring (truename path) absolute-dir))
 	     (name (if ext
 		       (subseq name.ext 0 (- (length name.ext)
-					     (length (string ext))
-					     1))
+					     (length (string ext))))
 		       name.ext)))
 	(unless (find-in-assets type dir name ext assets)
 	  (push (make-instance type
@@ -79,7 +78,8 @@
 			(name string)
 			(ext null)
 			assets)
-  (find-assets type dir name (asset-class-extensions type) assets))
+  (when (setq ext (asset-class-extensions type))
+    (find-assets type dir name ext assets)))
 
 ;;    Loop through dirs
 
@@ -124,7 +124,10 @@
     (if (eq assets new-assets)
 	(with-asset-spec spec (name ext)
 	  (find-assets (or class (extension-asset-class ext))
-		       nil name nil assets))
+		       nil name ext assets)
+	  (when (eq assets new-assets)
+	    (find-assets (or class (extension-asset-class ext))
+			 nil name nil assets)))
 	new-assets)))
 
 (defun find-assets-from-specs (specs &optional class assets)
diff --git a/font.lisp b/font.lisp
index 62a8761..f207a65 100644
--- a/font.lisp
+++ b/font.lisp
@@ -23,4 +23,4 @@
 (defclass font-asset (asset) ())
 
 (defmethod asset-class-extensions ((class (eql 'font-asset)))
-  (extensions #:eot #:ttf #:woff))
+  '(.eot .ttf .woff))
diff --git a/html.lisp b/html.lisp
index 099b589..5d0eea6 100644
--- a/html.lisp
+++ b/html.lisp
@@ -37,43 +37,44 @@
   (declare (optimize (safety 0) (debug 0) (speed 3))
 	   (type fixnum start end)
 	   (type string string))
-  (labels ((print-raw (raw i)
-	     (declare (type fixnum raw i))
-	     (when (< raw i)
-	       (write-string string stream :start raw :end i)))
-	   (chr (i)
-	     (typecase string
-	       (simple-array  (char string i))
-	       (simple-string (char string i))
-	       (string        (char string i))))
-	   (skip (raw i)
-	     (declare (type fixnum raw i))
-	     (cond ((= i end) (print-raw raw i))
-		   (t (let* ((char (chr i))
-			     (entity (char-entity char)))
-			(cond (entity (print-raw raw i)
-				      (write-string entity stream)
-				      (incf i)
-				      (skip i i))
-			      (t (skip raw (1+ i))))))))
-	   (stream-or-string (i)
-	     (declare (type fixnum i))
-	     (cond ((= i end) (if (and (= 0 start)
-				       (= end (length string)))
-				  string
-				  (subseq string start end)))
-		   (t (let* ((char (chr i))
-			     (entity (char-entity char)))
-			(cond (entity (with-output-to-string (out)
-					(setq stream out)
-					(print-raw start i)
+  (unless (emptyp string)
+    (labels ((print-raw (raw i)
+	       (declare (type fixnum raw i))
+	       (when (< raw i)
+		 (write-string string stream :start raw :end i)))
+	     (chr (i)
+	       (typecase string
+		 (simple-array  (char string i))
+		 (simple-string (char string i))
+		 (string        (char string i))))
+	     (skip (raw i)
+	       (declare (type fixnum raw i))
+	       (cond ((= i end) (print-raw raw i))
+		     (t (let* ((char (chr i))
+			       (entity (char-entity char)))
+			  (cond (entity (print-raw raw i)
 					(write-string entity stream)
 					(incf i)
-					(skip i i)))
-			      (t (stream-or-string (1+ i)))))))))
-    (if stream
-	(skip start start)
-	(stream-or-string start))))
+					(skip i i))
+				(t (skip raw (1+ i))))))))
+	     (stream-or-string (i)
+	       (declare (type fixnum i))
+	       (cond ((= i end) (if (and (= 0 start)
+					 (= end (length string)))
+				    string
+				    (subseq string start end)))
+		     (t (let* ((char (chr i))
+			       (entity (char-entity char)))
+			  (cond (entity (with-output-to-string (out)
+					  (setq stream out)
+					  (print-raw start i)
+					  (write-string entity stream)
+					  (incf i)
+					  (skip i i)))
+				(t (stream-or-string (1+ i)))))))))
+      (if stream
+	  (skip start start)
+	  (stream-or-string start)))))
 
 (define-compiler-macro quote-html (&whole whole string &key stream
 					  (start 0)
diff --git a/image.lisp b/image.lisp
index 23d0302..44cb4fd 100644
--- a/image.lisp
+++ b/image.lisp
@@ -23,7 +23,7 @@
 (defclass image-asset (asset) ())
 
 (defmethod asset-class-extensions ((class (eql 'image-asset)))
-  (extensions #:gif #:ico #:jpeg #:jpg #:png #:svg #:svgz))
+  '(.gif .ico .jpeg .jpg .png .svg .svgz .wbmp))
 
 (defmethod asset-include ((output stream)
 			  (context (eql :html))
diff --git a/js.lisp b/js.lisp
index bd9a96a..8a98009 100644
--- a/js.lisp
+++ b/js.lisp
@@ -23,10 +23,10 @@
 (defclass js-asset (preprocessed-asset) ())
 
 (defmethod asset-ext ((asset js-asset))
-  (extension #:js))
+  '.js)
 
 (defmethod asset-class-extensions ((class (eql 'js-asset)))
-  (extensions #:js))
+  '(.js))
 
 (defmethod asset-include ((output stream)
 			  (context (eql :html))
diff --git a/json.lisp b/json.lisp
new file mode 100644
index 0000000..3bdfaa7
--- /dev/null
+++ b/json.lisp
@@ -0,0 +1,35 @@
+;;
+;;  LowH Triangle Assets  -  Asset pipeline
+;;
+;;  Copyright 2012 Thomas de Grivel <billitch@gmail.com>
+;;
+;;  Permission to use, copy, modify, and distribute this software for any
+;;  purpose with or without fee is hereby granted, provided that the above
+;;  copyright notice and this permission notice appear in all copies.
+;;
+;;  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+;;  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+;;  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+;;  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+;;  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+;;  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+;;  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;;
+
+(in-package :lowh.triangle.assets)
+
+;;    JSON
+
+(defclass json-asset (js-asset) ())
+
+(defmethod asset-ext ((asset json-asset))
+  '.json)
+
+(defmethod asset-class-extensions ((class (eql 'json-asset)))
+  '(.json))
+
+(defmethod asset-include ((output stream)
+			  (context (eql :html))
+			  (asset json-asset)
+			  &key &allow-other-keys)
+  (error "Don't know how to include JSON in HTML."))
diff --git a/lowh.triangle.assets.asd b/lowh.triangle.assets.asd
index e2e7a68..b6ac349 100644
--- a/lowh.triangle.assets.asd
+++ b/lowh.triangle.assets.asd
@@ -35,12 +35,13 @@
    (:file "extensions" :depends-on ("package"))
    (:file "html"       :depends-on ("package"))
    (:file "lib"        :depends-on ("package"))
+   (:file "mime-types" :depends-on ("extensions"))
    (:file "generate"   :depends-on ("lib"))
-   (:file "asset"      :depends-on ("config" "extensions" "lib"))
+   (:file "asset"      :depends-on ("config" "mime-types" "lib"))
    (:file "find"       :depends-on ("asset"))
    (:file "font"       :depends-on ("asset"))
    (:file "precompile" :depends-on ("find"))
    (:file "preprocess" :depends-on ("find"))
    (:file "css"        :depends-on ("preprocess" "html"))
-   (:file "image"      :depends-on ("preprocess" "html"))
+   (:file "image"      :depends-on ("asset" "html"))
    (:file "js"         :depends-on ("preprocess" "html"))))
diff --git a/mime-types.lisp b/mime-types.lisp
new file mode 100644
index 0000000..379239d
--- /dev/null
+++ b/mime-types.lisp
@@ -0,0 +1,56 @@
+;;
+;;  LowH Triangle Assets  -  Asset pipeline
+;;
+;;  Copyright 2012 Thomas de Grivel <billitch@gmail.com>
+;;
+;;  Permission to use, copy, modify, and distribute this software for any
+;;  purpose with or without fee is hereby granted, provided that the above
+;;  copyright notice and this permission notice appear in all copies.
+;;
+;;  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+;;  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+;;  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+;;  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+;;  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+;;  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+;;  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;;
+
+(in-package :lowh.triangle.assets)
+
+(defgeneric mime-type (object))
+
+(defvar *mime-type/extension*
+  (make-hash-table :test 'eq)
+  "Hash table mapping extension symbols to mime type keywords.")
+
+(defmethod mime-type ((ext symbol))
+  (if (extension-p ext)
+      (gethash ext *mime-type/extension*)
+      (call-next-method)))
+
+(defmethod (setf mime-type) (value (ext symbol))
+  (if (extension-p ext)
+      (setf (gethash ext *mime-type/extension*) value)
+      (call-next-method)))
+
+;;  Parser for mime.types file
+
+(defun read-mime.types (input)
+  (regex-lines "^\\s*([a-zA-Z0-9_.+-]+/[a-zA-Z0-9_.+-]+)(\\s+[a-zA-Z0-9]+)+\\s*;?"
+	       input
+	       :match (lambda (match type extensions)
+			(declare (ignore match))
+			(setq type (intern (string-upcase type) :keyword))
+			(dolist (ext (cdr (cl-ppcre:split "\\s+" extensions)))
+			  (setf (mime-type (intern-extension ext)) type)))))
+
+(find-if (lambda (path)
+	   (when (probe-file path)
+	     (read-mime.types path)
+	     t))
+	 '(#P"mime.types"
+	   #P"conf/mime.types"
+	   #P"/etc/nginx/mime.types"
+	   #P"/var/www/conf/mime.types"
+	   #P"/etc/apache/mime.types"))
diff --git a/package.lisp b/package.lisp
index faec8d8..c34322d 100644
--- a/package.lisp
+++ b/package.lisp
@@ -18,9 +18,17 @@
 
 (in-package :cl-user)
 
+(defpackage :lowh.triangle.extensions
+  (:nicknames :L>ext)
+  #.(let (symbols)
+      (when (find-package :L>ext)
+	(do-external-symbols (s :L>ext)
+	  (push s symbols)))
+      `(:export ,@symbols)))
+
 (defpackage :lowh.triangle.assets
   (:nicknames :L>assets)
-  (:use :cl :alexandria :L>files :L>uri)
+  (:use :cl :alexandria :L>ext :L>files :L>uri)
   (:export
    ;;  Config
    #:*debug*
@@ -47,7 +55,9 @@
    #:find-assets-from-spec
    #:find-assets-from-specs
    #:locate-precompiled-assets
+   #:mime-type
    ;;  Rendering
+   #:quote-html
    #:process-asset
    #:asset-include
    ;;  Precompile