Source

hh-web-tags / taglibraries.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
 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
;; Copyright (c) 2010 Haphazard House LLC

;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:

;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.

;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.

(in-package :hh-web)

(export
 (list

  '*tag-library*
  '*tag-library-provider-registry*
  'create-folder-tag-library-provider
  'create-asdf-system-tag-library-provider

  'in-tag-library
  'use-tag-library
  '+tag-library

  )
 )

;;;------------------------------------------------------------------------------------
;;; Dynamic variables
;;;------------------------------------------------------------------------------------

(defvar *tag-library-provider-registry* ()
  "A list of functions such that when each is called with 1 argument (a tag name), they
  return the tag source as a stream or string"
  )

(defvar *minimum-tag-library-stale-time* 1
  "Minimum time in seconds for which a tag library must remain stale (e.g. not expire)
   before reloading from its source"
)

;;;------------------------------------------------------------------------------------
;;; Conditions
;;;------------------------------------------------------------------------------------

(define-condition library-not-found-error (error) 
  (
   (library-name
    :initarg :name
    :reader library-name
    )
   )
  (:report (lambda (condition stream)
             (format stream "Could not find tag library ~a~%."
                     (library-name condition)
		     )
	     )
	   )
  )

;;;------------------------------------------------------------------------------------
;;; Tag libraries
;;;------------------------------------------------------------------------------------

(defclass tag-library ()
  (
   (name
    :initarg :name
    :reader tag-library-name
    )
   (provider
    :initarg :provider
    :accessor tag-library-provider
    )
   (modified-time
    :initform (now)
    :accessor modified-time-of
    )
   (package
    :initform nil
    :initarg :package
    :accessor tag-library-package
    )
   (libraries
    :initform ()
    :accessor tag-libraries-used
    :documentation "A list of symbols identifying other libraries upon 
    which this one depends"
    )
   (tags
    :initform ()
    :accessor tag-library-tags
    :documentation "A list of symbols identifying tags defined by this library"
    )

   )
)

(defmethod initialize-instance :after ( (library tag-library) &key )
  (let* (
	 (pkg-name (intern (symbol-name (tag-library-name library) )
			   'cl-user
			   )
	   )
	 (pkg (or (find-package pkg-name)
		  (make-package pkg-name) 
		  )
	   )
	 )
    (setf (tag-library-package library) pkg)
    library
    )
  )

(defgeneric tag-library-stalep (library)
  (:method ( (library tag-library) )
    (timestamp> (now) 
       (timestamp+ (modified-time-of library) *minimum-tag-library-stale-time* :sec)
       )
    )
  )

(defgeneric tag-library-expiredp (library)
  (:documentation "Return if the library (or any of its dependent libraries) has epxired")
  )

;;;------------------------------------------------------------------------------------
;;; Tag library providers
;;;------------------------------------------------------------------------------------

(defclass tag-library-provider ()
  ()
  )

; -- Base functions suggested all providers implement

(defgeneric provider-tag-library-expiredp (library provider)
  (:documentation "Return t if the provider considers
   the tag library expired, otherwise nil"
		  )
  (:method ( library (provider tag-library-provider)  )
    t
    )
  )

(defgeneric load-tag-library-from-provider (library-name provider)
  (:documentation "If the provider can provide a tag library with the indicated name,
   return the tag library; otherwise, return nil"
		  )
  )

; -- Helper and framework functions

(defgeneric read-tag-library (input-stream)
  (:documentation "Read in a tag library from a stream"
		  )

  (:method ( (input-stream stream) )
    (let (
	  (*tag-library* nil)
	  (*read-eval* nil) 
	  (*package* (find-package 'cl-user) )
	  )
      ;; read tag library
      (use-package :hh-web *package*)
      (loop 
	 ;; TODO as a consequence of how this is implemented, any whitespace after last tag 
	 ;; causes an irrelevant EOF error
	 while (listen input-stream)
	 do (eval (read input-stream) )
	 )
      ;; expecting an in-tag-library call inside the file
      ;; to set the value of this variable
      *tag-library* 
      )
    )
  (:method ( (input-string string) )
    (with-input-from-string (input-stream input-string)
      (read-tag-library input-stream)
      )
    )
  )

(defgeneric load-tag-library (library-name &optional *tag-library-provider-registry*)
  (:documentation "Load the tag library from the first provider that can provide it")
  (:method (library-name &optional (*tag-library-provider-registry* *tag-library-provider-registry*) )
    (when *tag-library-provider-registry*
      (let* (
	     (provider (car *tag-library-provider-registry*) )
	     (found-tag-library (when provider
				  (load-tag-library-from-provider library-name provider ) 
				  )
	       )
	     )
	(if found-tag-library
	    (progn
	      (setf (gethash library-name *available-tag-libraries*) found-tag-library)
	      found-tag-library
	      )
	    (load-tag-library library-name (cdr *tag-library-provider-registry*) )
	    )
	)
      )
    )
  )

(defun flush-tag-library (library-name cached-library)
  "Safely a cached library, typically for reloading"
  ;; remove the library from the cache
  (remhash library-name *available-tag-libraries*)
  ;; also, delete the library's package...should make it easier to redefine it
  (when (template-package cached-library)
    (delete-package (tag-library-package cached-library))
    )
  )

(defun find-cached-tag-library (library-name)
  (let (
	(cached-library (gethash library-name *available-tag-libraries* nil) )
	)
    (if cached-library
	(if (tag-library-expiredp cached-library) 
	    (progn
	      (remhash library-name *available-tag-libraries*)
	      nil
	      )
	    cached-library
	    )
	)
    )
  )

(defmethod tag-library-expiredp ( (library tag-library) )
  (when (tag-library-stalep library)
    (or (not (slot-boundp library 'provider)) ;; in case a library fails during creation with no provider found
	(provider-tag-library-expiredp library (tag-library-provider library) )
	(find-if #'(lambda (library-name) 
		     (let (
			   (library (find-cached-tag-library library-name))
			   )
		       (if library
			   (tag-library-expiredp (find-cached-tag-library library-name) )
			   t
			   )
		       )
		     )
		 (tag-libraries-used library )
		 )
	)
    )
  )

;; -------- Folder provider ---------------------
;; 
;;  provides tag libraries in individual files of a specified 
;;  folder on the filesystem
;;

(defclass folder-tag-library-provider (tag-library-provider)
  (
   (folder
    :initarg :folder
    :accessor folder-of
    )
   )
)

(defgeneric tag-library-full-path (library-name provider)
  (:method (library-name (provider folder-tag-library-provider) )
    (let (
	   (provider-path (folder-of provider) )
					; folder-name should be an absolute path, ending / to indicate directory
	   )
      (merge-pathnames (make-pathname :name (pathname-name (string-downcase
							    (symbol-name library-name)
							    )
							   )
				      :type "lisp"
				      ) 
		       (make-pathname :host (pathname-host provider-path)
				      :directory (pathname-directory provider-path)
				      )
 		       )
      )
    )
)

(defgeneric tag-library-file-modified-time (library-name provider)
  (:method (library-name (provider folder-tag-library-provider) )
    (let (
	  (full-path (tag-library-full-path library-name provider) )
	  )
      (unix-to-timestamp (sb-posix:stat-mtime (sb-posix:stat full-path)) )
      )
    )
  )

(defun create-folder-tag-library-provider (folder)
  (make-instance 'folder-tag-library-provider :folder folder)
  )

(defun create-asdf-system-tag-library-provider (system)
  (make-instance 'folder-tag-library-provider 
		 :folder (asdf:system-relative-pathname 
					(asdf:find-system system) 
					(make-pathname :directory `(:relative "taglibraries") )
					)
		 )
  )


; -- Implementation of base tag-library-provider functions

(defmethod provider-tag-library-expiredp (library (provider folder-tag-library-provider) )
  (timestamp>  (tag-library-file-modified-time (tag-library-name library) provider)
	(modified-time-of library)
	)
  )

(defmethod load-tag-library-from-provider (library-name  (provider folder-tag-library-provider))
  (let (
	(full-path (tag-library-full-path library-name provider) )
	)
    (when (probe-file full-path)
      (let (
	    (library (read-tag-library (open full-path :direction :input) )
	      )
	    )
	;; TODO note: if there is a failure during read, then neither of the following 2
	;; statements will execute
	(setf (tag-library-provider library) provider)
	(setf (modified-time-of library) 
	      (tag-library-file-modified-time library-name provider)
	      )
	library
	)
      )
    )
  )



; -- Tag library support for rendering to html

;; generic function declared in tags.lisp, because tags need to invoke the function 
;; at point of tag declaration

(defmethod add-tag-to-library ( (tag-name symbol) &optional (*tag-library* *tag-library*))
  (if *tag-library*
      (putendnew tag-name (tag-library-tags *tag-library*) :test 'equal)
      )
  )

(defmethod expand-expression-with-libraries (expr &optional (*available-tag-libraries* *available-tag-libraries*))
  `(macrolet (
	      ,@(tag-library-tag-expansions (hash-contents *available-tag-libraries*))
	      )
     ,expr
     )
  )

(defgeneric tag-library (name)
  (:documentation "Return the library with the indicated name")
)

(defgeneric tag-library-tag-expansions (some-library)
  (:documentation "For some library, return the list of macro expansions 
   for each tag in the library")
  )

(defmethod tag-library ( (name symbol) )
  (gethash name *available-tag-libraries*)
  )

(defmethod tag-library-tag-expansions ( (some-library tag-library) )
  (mapcar (lambda (each-tag) (tag-expansion each-tag) )
	  (tag-library-tags some-library)
	  )
  )

(defmethod tag-library-tag-expansions ( (some-library (eql 'nil) ) )
  nil
  )

(defmethod tag-library-tag-expansions ( (some-library symbol) )
  (tag-library-tag-expansions (tag-library some-library))
  )

(defmethod tag-library-tag-expansions ( (some-libraries list) )
  "Gather all tag expansions for all libraries in the list"
  
  (apply 'concatenate (cons 'list 
			    (mapcar (lambda (each-library) 
				      (tag-library-tag-expansions each-library)
				      )
				    some-libraries
				    )
			    )
	 )
  )

;;;------------------------------------------------------------------------------------
;;;  Tag library language
;;;
;;;  Macros & functions used in a tag library file
;;;

(defmacro in-tag-library (library-name)
  "All following tag definitions will go into this named library"
  `(let* (
	  (library (make-instance 'tag-library :name (quote ,library-name)) )
	  (library-package (tag-library-package library) )
	  )
     (setf (gethash (quote ,library-name) *available-tag-libraries*)
	   library
	   )
     (setf *package* library-package )
     (setf *tag-library* library)

     ;; setup tag library package for use
     (use-package 'cl library-package)
     (use-package 'cl-user library-package)
     (use-package 'hh-web library-package)
     library
     )
  )

(defun import-tag-library (library-name)
  (let (
	(library (or (find-cached-tag-library library-name) 
		     (load-tag-library library-name)
		     )
	  )
	)
    (if library
	(progn
	  (use-package (tag-library-package library) *package*)
	  library
	  )
	(error 'tag-library-not-found-error :name library-name)
	)
    )
  )

(defun use-tag-library (library-name)
  "Add the named tag library to the list of libraries used by the
  active tag library"
  (let* (
	 (library (import-tag-library library-name) )
	 )
    (when library
      (when *tag-library*
	(putendnew library-name (tag-libraries-used *tag-library*) :test 'equal)
	)
      library
      )
    )
  )