1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: hyperobject.lisp
6 ;;;; Purpose: Hyper Object Metaclass
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; This metaclass as functions to classes to allow display
11 ;;;; in Text, HTML, and XML formats. This includes hyperlinking
12 ;;;; capability and sub-objects.
14 ;;;; $Id: hyperobject.lisp,v 1.9 2002/11/22 19:48:49 kevin Exp $
16 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
18 ;;;; *************************************************************************
20 (in-package :hyperobject)
22 (eval-when (:compile-toplevel :execute)
23 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
28 `(mop::class-slots mop::slot-definition-name mop:finalize-inheritance
29 mop::standard-direct-slot-definition mop::standard-effective-slot-definition
30 mop:direct-slot-definition-class mop:compute-effective-slot-definition
31 excl::compute-effective-slot-definition-initargs)
33 `(clos:class-slots clos::slot-definition-name clos:finalize-inheritance
34 clos::standard-direct-slot-definition clos::standard-effective-slot-definition
35 clos:direct-slot-definition-class clos:compute-effective-slot-definition
36 clos::compute-effective-slot-definition-initargs)
38 `(sb-pcl:class-of sb-pcl:class-name sb-pcl:class-slots sb-pcl::standard-class
39 sb-pcl::slot-definition-name sb-pcl:finalize-inheritance
40 sb-pcl::standard-direct-slot-definition
41 sb-pcl::standard-effective-slot-definition sb-pcl::validate-superclass
42 sb-pcl:direct-slot-definition-class sb-pcl:compute-effective-slot-definition
43 sb-pcl::compute-effective-slot-definition-initargs)
45 `(pcl:class-of pcl:class-name pcl:class-slots pcl::standard-class
46 pcl::slot-definition-name pcl:finalize-inheritance
47 pcl::standard-direct-slot-definition pcl::standard-effective-slot-definition
48 pcl::validate-superclass pcl:direct-slot-definition-class
49 pcl:compute-effective-slot-definition
50 pcl::compute-effective-slot-definition-initargs)
56 (defclass hyperobject-dsd (standard-direct-slot-definition)
57 ((ho-type :initarg :ho-type :initform nil :accessor dsd-ho-type)
58 (format-func :initarg :format-func :initform nil :accessor dsd-format-func)
59 (subobject :initarg :subobject :initform nil :accessor dsd-subobject)
60 (reference :initarg :reference :initform nil :accessor dsd-reference)
63 (defclass hyperobject-esd (standard-effective-slot-definition)
64 ((ho-type :initarg :ho-type :initform nil :accessor esd-ho-type)
65 (format-func :initarg :format-func :initform nil :accessor esd-format-func)
66 (subobject :initarg :subobject :initform nil :accessor esd-subobject)
67 (reference :initarg :reference :initform nil :accessor esd-reference)
72 (defclass hyperobject-class (standard-class)
73 ((title :initarg :title :type string :initform nil
74 :documentation "Print Title for class")
75 (print-slots :initarg :print-slots :type list :initform nil
76 :documentation "List of slots to print")
78 :initarg :subobjects :initform nil
79 :documentation "List of fields that contain a list of subobjects objects.")
81 :initarg :references :type list :initform nil
83 "List of fields that can be referred to by browsers.
84 Format is ((field-name field-lookup-func other-link-params) ...)")
86 ;;; The remainder of these fields are calculated one time
87 ;;; in finalize-inheritence.
88 (value-func :initform nil :type function)
89 (xmlvalue-func :initform nil :type function)
90 (fmtstr-text :initform nil :type string)
91 (fmtstr-html :initform nil :type string)
92 (fmtstr-xml :initform nil :type string)
93 (fmtstr-text-labels :initform nil :type string)
94 (fmtstr-html-labels :initform nil :type string)
95 (fmtstr-xml-labels :initform nil :type string)
96 (fmtstr-html-ref :initform nil :type string)
97 (fmtstr-xml-ref :initform nil :type string)
98 (fmtstr-html-ref-labels :initform nil :type string)
99 (fmtstr-xml-ref-labels :initform nil :type string)
101 (:documentation "Metaclass for Markup Language classes."))
103 (defclass subobject ()
104 ((name :type symbol :initform nil :initarg :name :reader name)
105 (reader :type function :initform nil :initarg :reader :reader reader)))
107 (defmethod print-object ((obj subobject) (s stream))
108 (print-unreadable-object (obj s :type t :identity t)
109 (format s "~S" (name obj))))
111 (defclass reference ()
112 ((name :type symbol :initform nil :initarg :name :reader name)
113 (lookup :type function :initform nil :initarg :lookup :reader lookup)
114 (link-parameters :type list :initform nil :initarg :link-parameters
115 :reader link-parameters)))
117 (defmethod print-object ((obj reference) (s stream))
118 (print-unreadable-object (obj s :type t :identity t)
119 (format s "~S" (name obj))))
122 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
125 (defmethod finalize-inheritance :after ((cl hyperobject-class))
126 (init-hyperobject-class cl))
129 (defmethod direct-slot-definition-class ((cl hyperobject-class)
131 (find-class 'hyperobject-dsd))
136 (defmethod clos:process-a-class-option ((class hyperobject-class)
140 (error "hyperobject-class title must have a value"))
141 (if (null (cdr value))
142 (list name (car value))
143 (list name `',value)))
146 (defmethod clos:process-a-class-option ((class hyperobject-class)
147 (name (eql :print-slots))
149 (if (null (cdr value))
150 (list name (car value))
151 (list name `',value)))
153 (defmethod compute-effective-slot-definition :around
154 ((cl hyperobject-class) #+(or allegro lispworks) slot dsds)
155 (declare (ignorable slot))
156 (let* ((dsd (car dsds))
157 (ho-type (slot-value dsd 'type)))
158 (setf (slot-value dsd 'ho-type) ho-type)
159 (setf (slot-value dsd 'type) (convert-ho-type ho-type))
160 (let ((ia (compute-effective-slot-definition-initargs
161 cl #+lispworks slot dsds)))
163 #'make-instance 'hyperobject-esd
165 :format-func (slot-value dsd 'format-func)
166 :subobject (slot-value dsd 'subobject)
167 :reference (slot-value dsd 'reference)
171 (defun convert-ho-type (ho-type)
172 (check-type ho-type symbol)
173 (case (intern (symbol-name ho-type) (symbol-name :keyword))
189 ;;;; Class initialization function
191 (defun find-slot-by-name (cl name)
192 (find name (class-slots cl) :key #'slot-definition-name))
194 (defun init-hyperobject-class (cl)
195 (let ((fmtstr-text "")
198 (fmtstr-text-labels "")
199 (fmtstr-html-labels "")
200 (fmtstr-xml-labels "")
203 (fmtstr-html-ref-labels "")
204 (fmtstr-xml-ref-labels "")
208 (classname (class-name cl))
209 (package (symbol-package (class-name cl)))
212 (declare (ignore classname))
213 (dolist (slot (class-slots cl))
214 (when (slot-value slot 'subobject)
215 (push (make-instance 'subobject :name (slot-definition-name slot)
216 :reader (if (eq t (esd-subobject slot))
217 (slot-definition-name slot)
218 (esd-subobject slot)))
220 (setf (slot-value cl 'subobjects) subobjects)
221 (dolist (slot-name (slot-value cl 'print-slots))
222 (let ((slot (find-slot-by-name cl slot-name)))
224 (error "Slot ~A is not found in class ~S" slot-name cl))
225 (let ((name (slot-definition-name slot))
226 (namestr (symbol-name (slot-definition-name slot)))
227 (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
228 (type (slot-value slot 'ho-type))
229 (formatter (slot-value slot 'format-func))
231 (plain-value-func nil)
232 html-str xml-str html-label-str xml-label-str)
234 (when (or (eql type :integer) (eql type :fixnum))
235 (setq value-fmt "~d"))
237 (when (eql type :commainteger)
238 (setq value-fmt "~:d"))
240 (when (eql type :boolean)
241 (setq value-fmt "~a"))
244 (setq first-field nil)
246 (string-append fmtstr-text " ")
247 (string-append fmtstr-html " ")
248 (string-append fmtstr-xml " ")
249 (string-append fmtstr-text-labels " ")
250 (string-append fmtstr-html-labels " ")
251 (string-append fmtstr-xml-labels " ")
252 (string-append fmtstr-html-ref " ")
253 (string-append fmtstr-xml-ref " ")
254 (string-append fmtstr-html-ref-labels " ")
255 (string-append fmtstr-xml-ref-labels " ")))
257 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
258 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
259 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
260 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
262 (string-append fmtstr-text value-fmt)
263 (string-append fmtstr-html html-str)
264 (string-append fmtstr-xml xml-str)
265 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
266 (string-append fmtstr-html-labels html-label-str)
267 (string-append fmtstr-xml-labels xml-label-str)
269 (if (esd-reference slot)
271 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
272 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
273 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
274 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>")
275 (push (make-instance 'reference :name name :lookup (esd-reference slot))
278 (string-append fmtstr-html-ref html-str)
279 (string-append fmtstr-xml-ref xml-str)
280 (string-append fmtstr-html-ref-labels html-label-str)
281 (string-append fmtstr-xml-ref-labels xml-label-str)))
284 (setq plain-value-func
285 (list `(,formatter (,(intern namestr package) x))))
286 (setq plain-value-func
287 (list `(,(intern namestr package) x))))
288 (setq value-func (append value-func plain-value-func))
290 (if (eql type :cdata)
291 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
292 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
295 (setf (slot-value cl 'references) references)
298 (setq value-func `(lambda (x) (values ,@value-func)))
299 (setq value-func `(lambda () (values))))
300 (setq value-func (compile nil (eval value-func)))
303 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
304 (setq xmlvalue-func `(lambda () (values))))
305 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
307 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
308 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
309 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
310 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
311 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
312 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
313 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
314 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
315 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
316 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
317 (setf (slot-value cl 'value-func) value-func)
318 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
322 (defun hyperobject-class-fmtstr-text (obj)
323 (slot-value (class-of obj) 'fmtstr-text))
325 (defun hyperobject-class-fmtstr-html (obj)
326 (slot-value (class-of obj) 'fmtstr-html))
328 (defun hyperobject-class-fmtstr-xml (obj)
329 (slot-value (class-of obj) 'fmtstr-xml))
331 (defun hyperobject-class-fmtstr-text-labels (obj)
332 (slot-value (class-of obj) 'fmtstr-text-labels))
334 (defun hyperobject-class-fmtstr-html-labels (obj)
335 (slot-value (class-of obj) 'fmtstr-html-labels))
337 (defun hyperobject-class-fmtstr-xml-labels (obj)
338 (slot-value (class-of obj) 'fmtstr-xml-labels))
340 (defun hyperobject-class-value-func (obj)
341 (slot-value (class-of obj) 'value-func))
343 (defun hyperobject-class-xmlvalue-func (obj)
344 (slot-value (class-of obj) 'xmlvalue-func))
346 (eval-when (:compile-toplevel :load-toplevel :execute)
347 (defun hyperobject-class-title (obj)
348 (awhen (slot-value (class-of obj) 'title)
353 (defun hyperobject-class-subobjects (obj)
354 (slot-value (class-of obj) 'subobjects))
356 (defun hyperobject-class-references (obj)
357 (slot-value (class-of obj) 'references))
359 (defun hyperobject-class-fields (obj)
360 (class-slots (class-of obj)))
362 (defun hyperobject-class-fmtstr-html-ref (obj)
363 (slot-value (class-of obj) 'fmtstr-html-ref))
365 (defun hyperobject-class-fmtstr-xml-ref (obj)
366 (slot-value (class-of obj) 'fmtstr-xml-ref))
368 (defun hyperobject-class-fmtstr-html-ref-labels (obj)
369 (slot-value (class-of obj) 'fmtstr-html-ref-labels))
371 (defun hyperobject-class-fmtstr-xml-ref-labels (obj)
372 (slot-value (class-of obj) 'fmtstr-xml-ref-labels))
374 ;;;; Generic Print functions
376 (defparameter *default-textformat* nil)
377 (defparameter *default-htmlformat* nil)
378 (defparameter *default-htmlrefformat* nil)
379 (defparameter *default-xhtmlformat* nil)
380 (defparameter *default-xhtmlrefformat* nil)
381 (defparameter *default-xmlformat* nil)
382 (defparameter *default-xmlrefformat* nil)
383 (defparameter *default-ie-xmlrefformat* nil)
384 (defparameter *default-nullformat* nil)
385 (defparameter *default-init-format?* nil)
387 (defun make-format-instance (fmt)
388 (unless *default-init-format?*
389 (setq *default-textformat* (make-instance 'textformat))
390 (setq *default-htmlformat* (make-instance 'htmlformat))
391 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
392 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
393 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
394 (setq *default-xmlformat* (make-instance 'xmlformat))
395 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
396 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
397 (setq *default-nullformat* (make-instance 'nullformat))
398 (setq *default-init-format?* t))
401 (:text *default-textformat*)
402 (:html *default-htmlformat*)
403 (:htmlref *default-htmlrefformat*)
404 (:xhtml *default-xhtmlformat*)
405 (:xhtmlref *default-xhtmlrefformat*)
406 (:xml *default-xmlformat*)
407 (:xmlref *default-xmlrefformat*)
408 (:ie-xmlref *default-ie-xmlrefformat*)
409 (:null *default-nullformat*)
410 (otherwise *default-textformat*)))
412 ;;;; Output format classes for print hyperobject-classes
414 (defclass dataformat ()
415 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
416 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
417 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
418 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
419 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
420 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
421 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
422 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
423 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
424 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
425 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
426 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
427 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
428 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
429 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
430 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
431 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
432 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
433 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
434 (link-ref :initarg :link-ref :reader link-ref))
435 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
436 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
437 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
438 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
439 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
440 :obj-data-value-func nil :link-ref nil)
441 (:documentation "Parent for all dataformat objects"))
443 (defclass binaryformat (dataformat)
446 (defclass nullformat (dataformat)
449 (defun text-list-start-value-func (obj nitems)
450 (values (hyperobject-class-title obj) nitems))
452 (defclass textformat (dataformat)
454 (:default-initargs :list-start-fmtstr "~a~P:~%"
455 :list-start-value-func #'text-list-start-value-func
458 :obj-data-fmtstr #'hyperobject-class-fmtstr-text
459 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-text-labels
460 :obj-data-end-fmtstr "~%"
461 :obj-data-value-func #'hyperobject-class-value-func))
464 (defun class-name-of (obj)
465 (string-downcase (class-name (class-of obj))))
467 (defun htmlformat-list-start-value-func (x nitems)
468 (values (hyperobject-class-title x) nitems (class-name-of x)))
470 (defclass htmlformat (textformat)
472 (:default-initargs :file-start-str "<html><body>~%"
473 :file-end-str "</body><html>~%"
475 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
476 :list-start-value-func #'htmlformat-list-start-value-func
477 :list-end-fmtstr "</ul></div>~%"
479 :list-end-value-func #'identity
481 :obj-start-fmtstr "<li>"
482 :obj-start-value-func #'identity
484 :obj-end-fmtstr "</li>~%"
485 :obj-end-value-func #'identity
487 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
488 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
489 :obj-data-value-func #'hyperobject-class-value-func))
491 (defclass xhtmlformat (textformat)
493 (:default-initargs :file-start-str "<html><body>~%"
494 :file-end-str "</body><html>~%"
496 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
497 :list-start-value-func #'htmlformat-list-start-value-func
498 :list-end-fmtstr "</ul></div>~%"
500 :list-end-value-func #'identity
502 :obj-start-fmtstr "<li>"
503 :obj-start-value-func #'identity
505 :obj-end-fmtstr "</li>~%"
506 :obj-end-value-func #'identity
508 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
509 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
510 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
513 (defun xmlformat-list-end-value-func (x)
514 (format nil "~alist" (class-name-of x)))
516 (defun xmlformat-list-start-value-func (x nitems)
517 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-title x) nitems))
519 (defclass xmlformat (textformat)
521 (:default-initargs :file-start-str "" ; (std-xml-header)
523 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
524 :list-start-value-func #'xmlformat-list-start-value-func
526 :list-end-fmtstr "</~a>~%"
527 :list-end-value-func #'xmlformat-list-end-value-func
528 :obj-start-fmtstr "<~a>"
529 :obj-start-value-func #'class-name-of
531 :obj-end-fmtstr "</~a>~%"
532 :obj-end-value-func #'class-name-of
535 :obj-data-fmtstr #'hyperobject-class-fmtstr-xml
536 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-xml-labels
537 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
539 (defclass link-ref ()
540 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
541 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
542 (page-name :type string :initarg :page-name :accessor page-name)
543 (href-head :type string :initarg :href-head :accessor href-head)
544 (href-end :type string :initarg :href-end :accessor href-end)
545 (ampersand :type string :initarg :ampersand :accessor ampersand))
546 (:default-initargs :fmtstr nil
548 :page-name "disp-func1"
549 :href-head nil :href-end nil :ampersand nil)
550 (:documentation "Formatting for a linked reference"))
552 (defclass html-link-ref (link-ref)
554 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
555 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
560 (defclass xhtml-link-ref (link-ref)
562 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
563 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
568 (defclass xml-link-ref (link-ref)
570 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-xml-ref
571 :fmtstr-labels #'hyperobject-class-fmtstr-xml-ref-labels
572 :href-head "xmllink xlink:type=\"simple\" xlink:href="
575 (:documentation "Mozilla's and W3's idea of a link with XML"))
577 (defclass ie-xml-link-ref (xml-link-ref)
579 (:default-initargs :href-head "html:a href="
581 (:documentation "Internet Explorer's idea of a link with XML"))
584 (defclass htmlrefformat (htmlformat)
586 (:default-initargs :link-ref (make-instance 'html-link-ref)))
588 (defclass xhtmlrefformat (xhtmlformat)
590 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
592 (defclass xmlrefformat (xmlformat)
594 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
596 (defclass ie-xmlrefformat (xmlformat)
598 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
601 ;;; File Start and Ends
603 (defgeneric fmt-file-start (fmt s))
604 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
606 (defmethod fmt-file-start ((fmt textformat) (s stream))
607 (aif (file-start-str fmt)
610 (defgeneric fmt-file-end (fmt s))
611 (defmethod fmt-file-end ((fmt textformat) (s stream))
612 (aif (file-end-str fmt)
615 ;;; List Start and Ends
617 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
618 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
619 (if (list-start-indent fmt)
620 (indent-spaces indent s))
621 (aif (list-start-fmtstr fmt)
624 (funcall (list-start-value-func fmt) x num-items)))))
626 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
627 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
628 (declare (ignore num-items))
629 (if (list-end-indent fmt)
630 (indent-spaces indent s))
631 (aif (list-end-fmtstr fmt)
634 (funcall (list-end-value-func fmt) x)))))
636 ;;; Object Start and Ends
638 (defgeneric fmt-obj-start (obj fmt s &optional indent))
639 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
640 (if (obj-start-indent fmt)
641 (indent-spaces indent s))
642 (aif (obj-start-fmtstr fmt)
645 (funcall (obj-start-value-func fmt) x)))))
647 (defgeneric fmt-obj-end (obj fmt s &optional indent))
648 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
649 (if (obj-end-indent fmt)
650 (indent-spaces indent s))
651 (aif (obj-end-fmtstr fmt)
654 (funcall (obj-end-value-func fmt) x)))))
658 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
659 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
660 (declare (ignore obj fieldname))
661 (format nil "~a\"~a?func=~a~akey=~a~a\""
662 (href-head ref) (make-url (page-name ref)) fieldfunc
663 (ampersand ref) fieldvalue
666 (dolist (var refvars)
667 (string-append varstr (format nil "~a~a=~a"
668 (ampersand ref) (car var) (cadr var))))
672 (defgeneric make-link-end (obj ref fieldname))
673 (defmethod make-link-end (obj (ref link-ref) fieldname)
674 (declare (ignore obj fieldname))
675 (format nil "~a" (href-end ref))
678 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
679 (defmethod fmt-obj-data (x (fmt textformat) s
680 &optional (indent 0) (label nil) (refvars nil))
681 (if (obj-data-indent fmt)
682 (indent-spaces indent s))
684 (fmt-obj-data-with-ref x fmt s label refvars)
685 (fmt-obj-data-plain x fmt s label))
686 (aif (obj-data-end-fmtstr fmt)
689 (defgeneric fmt-obj-data-plain (obj fmt s label))
690 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
693 (funcall (obj-data-fmtstr-labels fmt) x)
695 (funcall (funcall (obj-data-value-func fmt) x) x)))
696 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
698 (funcall (funcall (obj-data-value-func fmt) x) x)))))
700 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
701 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
702 (let ((refstr (make-ref-data-str x fmt label))
706 (funcall (funcall (obj-data-value-func fmt) x) x))))
708 ;; make list of reference link fields for printing to refstr template
709 (dolist (ref (hyperobject-class-references x))
711 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
712 (nth (position (name ref)
713 (hyperobject-class-fields x)
715 (slot-definition-name x)))
717 (append (link-parameters ref) refvars)))
718 (link-end (make-link-end x (link-ref fmt) (name ref))))
719 (push link-start refvalues)
720 (push link-end refvalues)))
721 (setq refvalues (nreverse refvalues))
723 (apply #'format s refstr refvalues)))
725 (defgeneric obj-data (obj))
726 (defmethod obj-data (x)
727 "Returns the objects data as a string. Used by common-graphics outline function"
728 (let ((fmt (make-format-instance :text)))
729 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
731 (funcall (funcall (obj-data-value-func fmt) x) x)))))
733 (defgeneric make-ref-data-str (obj fmt &optional label))
734 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
735 "Return fmt string for that contains ~a slots for reference link start and end"
736 (unless (link-ref fmt)
737 (error "fmt does not contain a link-ref"))
740 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
742 (funcall (funcall (obj-data-value-func fmt) x) x)))
743 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
744 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
747 ;;; Display method for objects
750 (defgeneric load-all-subobjects (objs))
751 (defmethod load-all-subobjects (objs)
752 "Load all subobjects if they have not already been loaded."
754 (let ((objlist (mklist objs)))
755 (dolist (obj objlist)
756 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
757 (dolist (child-obj it) ;; for each child function
758 (awhen (funcall (reader child-obj) obj)
759 (load-all-subobjects it))))))
762 (defgeneric print-hyperobject-class (objs fmt strm
763 &optional label english-only-function
764 indent subobjects refvars))
766 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream)
767 &optional (label nil) (indent 0)
768 (english-only-function nil)
769 (subobjects nil) (refvars nil))
770 "Display a single or list of hyperobject-class instances and their subobjects"
772 (setq objs (mklist objs))
773 (let ((nobjs (length objs)))
774 (fmt-list-start (car objs) fmt strm indent nobjs)
776 (unless (and english-only-function
777 (multiple-value-bind (eng term) (funcall english-only-function obj)
778 (and term (not eng))))
779 (fmt-obj-start obj fmt strm indent)
780 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
782 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
783 (dolist (child-obj it) ;; for each child function
784 (awhen (funcall (reader child-obj) obj) ;; access set of child objects
785 (print-hyperobject-class it fmt strm label
786 (1+ indent) english-only-function
787 subobjects refvars)))))
788 (fmt-obj-end obj fmt strm indent)))
789 (fmt-list-end (car objs) fmt strm indent nobjs))
794 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
795 (label nil) (english-only-function nil)
796 (subobjects nil) (file-wrapper t) (refvars nil))
797 "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
798 (let ((fmt (make-format-instance format)))
800 (fmt-file-start fmt os))
802 (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
804 (fmt-file-end fmt os)))
808 (defclass hyperobject ()
810 (:metaclass hyperobject-class))
813 (defmethod print-object ((obj hyperobject) (s stream))
814 (print-unreadable-object (obj s :type t :identity t)
815 (let ((fmt (make-instance 'hyperobject::textformat)))
817 s (funcall (obj-data-fmtstr fmt) obj)
819 (funcall (funcall (obj-data-value-func fmt) obj) obj))))))