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.11 2002/11/23 22:19:17 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 (print-formatter :initarg :print-formatter :initform nil :accessor dsd-print-formatter)
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 (print-formatter :initarg :print-formatter :initform nil :accessor esd-print-formatter)
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 :print-formatter (slot-value dsd 'print-formatter)
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 (print-formatter (slot-value slot 'print-formatter))
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 :boolean)
238 (setq value-fmt "~a"))
241 (setq first-field nil)
243 (string-append fmtstr-text " ")
244 (string-append fmtstr-html " ")
245 (string-append fmtstr-xml " ")
246 (string-append fmtstr-text-labels " ")
247 (string-append fmtstr-html-labels " ")
248 (string-append fmtstr-xml-labels " ")
249 (string-append fmtstr-html-ref " ")
250 (string-append fmtstr-xml-ref " ")
251 (string-append fmtstr-html-ref-labels " ")
252 (string-append fmtstr-xml-ref-labels " ")))
254 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
255 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
256 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
257 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
259 (string-append fmtstr-text value-fmt)
260 (string-append fmtstr-html html-str)
261 (string-append fmtstr-xml xml-str)
262 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
263 (string-append fmtstr-html-labels html-label-str)
264 (string-append fmtstr-xml-labels xml-label-str)
266 (if (esd-reference slot)
268 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
269 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
270 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
271 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>")
272 (push (make-instance 'reference :name name :lookup (esd-reference slot))
275 (string-append fmtstr-html-ref html-str)
276 (string-append fmtstr-xml-ref xml-str)
277 (string-append fmtstr-html-ref-labels html-label-str)
278 (string-append fmtstr-xml-ref-labels xml-label-str)))
281 (setq plain-value-func
282 (list `(,print-formatter (,(intern namestr package) x))))
283 (setq plain-value-func
284 (list `(,(intern namestr package) x))))
285 (setq value-func (append value-func plain-value-func))
287 (if (eql type :cdata)
288 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
289 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
292 (setf (slot-value cl 'references) references)
295 (setq value-func `(lambda (x) (values ,@value-func)))
296 (setq value-func `(lambda () (values))))
297 (setq value-func (compile nil (eval value-func)))
300 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
301 (setq xmlvalue-func `(lambda () (values))))
302 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
304 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
305 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
306 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
307 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
308 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
309 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
310 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
311 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
312 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
313 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
314 (setf (slot-value cl 'value-func) value-func)
315 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
319 (defun hyperobject-class-fmtstr-text (obj)
320 (slot-value (class-of obj) 'fmtstr-text))
322 (defun hyperobject-class-fmtstr-html (obj)
323 (slot-value (class-of obj) 'fmtstr-html))
325 (defun hyperobject-class-fmtstr-xml (obj)
326 (slot-value (class-of obj) 'fmtstr-xml))
328 (defun hyperobject-class-fmtstr-text-labels (obj)
329 (slot-value (class-of obj) 'fmtstr-text-labels))
331 (defun hyperobject-class-fmtstr-html-labels (obj)
332 (slot-value (class-of obj) 'fmtstr-html-labels))
334 (defun hyperobject-class-fmtstr-xml-labels (obj)
335 (slot-value (class-of obj) 'fmtstr-xml-labels))
337 (defun hyperobject-class-value-func (obj)
338 (slot-value (class-of obj) 'value-func))
340 (defun hyperobject-class-xmlvalue-func (obj)
341 (slot-value (class-of obj) 'xmlvalue-func))
343 (eval-when (:compile-toplevel :load-toplevel :execute)
344 (defun hyperobject-class-title (obj)
345 (awhen (slot-value (class-of obj) 'title)
350 (defun hyperobject-class-subobjects (obj)
351 (slot-value (class-of obj) 'subobjects))
353 (defun hyperobject-class-references (obj)
354 (slot-value (class-of obj) 'references))
356 (defun hyperobject-class-fields (obj)
357 (class-slots (class-of obj)))
359 (defun hyperobject-class-fmtstr-html-ref (obj)
360 (slot-value (class-of obj) 'fmtstr-html-ref))
362 (defun hyperobject-class-fmtstr-xml-ref (obj)
363 (slot-value (class-of obj) 'fmtstr-xml-ref))
365 (defun hyperobject-class-fmtstr-html-ref-labels (obj)
366 (slot-value (class-of obj) 'fmtstr-html-ref-labels))
368 (defun hyperobject-class-fmtstr-xml-ref-labels (obj)
369 (slot-value (class-of obj) 'fmtstr-xml-ref-labels))
371 ;;;; Generic Print functions
373 (defparameter *default-textformat* nil)
374 (defparameter *default-htmlformat* nil)
375 (defparameter *default-htmlrefformat* nil)
376 (defparameter *default-xhtmlformat* nil)
377 (defparameter *default-xhtmlrefformat* nil)
378 (defparameter *default-xmlformat* nil)
379 (defparameter *default-xmlrefformat* nil)
380 (defparameter *default-ie-xmlrefformat* nil)
381 (defparameter *default-nullformat* nil)
382 (defparameter *default-init-format?* nil)
384 (defun make-format-instance (fmt)
385 (unless *default-init-format?*
386 (setq *default-textformat* (make-instance 'textformat))
387 (setq *default-htmlformat* (make-instance 'htmlformat))
388 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
389 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
390 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
391 (setq *default-xmlformat* (make-instance 'xmlformat))
392 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
393 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
394 (setq *default-nullformat* (make-instance 'nullformat))
395 (setq *default-init-format?* t))
398 (:text *default-textformat*)
399 (:html *default-htmlformat*)
400 (:htmlref *default-htmlrefformat*)
401 (:xhtml *default-xhtmlformat*)
402 (:xhtmlref *default-xhtmlrefformat*)
403 (:xml *default-xmlformat*)
404 (:xmlref *default-xmlrefformat*)
405 (:ie-xmlref *default-ie-xmlrefformat*)
406 (:null *default-nullformat*)
407 (otherwise *default-textformat*)))
409 ;;;; Output format classes for print hyperobject-classes
411 (defclass dataformat ()
412 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
413 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
414 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
415 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
416 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
417 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
418 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
419 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
420 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
421 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
422 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
423 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
424 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
425 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
426 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
427 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
428 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
429 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
430 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
431 (link-ref :initarg :link-ref :reader link-ref))
432 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
433 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
434 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
435 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
436 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
437 :obj-data-value-func nil :link-ref nil)
438 (:documentation "Parent for all dataformat objects"))
440 (defclass binaryformat (dataformat)
443 (defclass nullformat (dataformat)
446 (defun text-list-start-value-func (obj nitems)
447 (values (hyperobject-class-title obj) nitems))
449 (defclass textformat (dataformat)
451 (:default-initargs :list-start-fmtstr "~a~P:~%"
452 :list-start-value-func #'text-list-start-value-func
455 :obj-data-fmtstr #'hyperobject-class-fmtstr-text
456 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-text-labels
457 :obj-data-end-fmtstr "~%"
458 :obj-data-value-func #'hyperobject-class-value-func))
461 (defun class-name-of (obj)
462 (string-downcase (class-name (class-of obj))))
464 (defun htmlformat-list-start-value-func (x nitems)
465 (values (hyperobject-class-title x) nitems (class-name-of x)))
467 (defclass htmlformat (textformat)
469 (:default-initargs :file-start-str "<html><body>~%"
470 :file-end-str "</body><html>~%"
472 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
473 :list-start-value-func #'htmlformat-list-start-value-func
474 :list-end-fmtstr "</ul></div>~%"
476 :list-end-value-func #'identity
478 :obj-start-fmtstr "<li>"
479 :obj-start-value-func #'identity
481 :obj-end-fmtstr "</li>~%"
482 :obj-end-value-func #'identity
484 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
485 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
486 :obj-data-value-func #'hyperobject-class-value-func))
488 (defclass xhtmlformat (textformat)
490 (:default-initargs :file-start-str "<html><body>~%"
491 :file-end-str "</body><html>~%"
493 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
494 :list-start-value-func #'htmlformat-list-start-value-func
495 :list-end-fmtstr "</ul></div>~%"
497 :list-end-value-func #'identity
499 :obj-start-fmtstr "<li>"
500 :obj-start-value-func #'identity
502 :obj-end-fmtstr "</li>~%"
503 :obj-end-value-func #'identity
505 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
506 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
507 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
510 (defun xmlformat-list-end-value-func (x)
511 (format nil "~alist" (class-name-of x)))
513 (defun xmlformat-list-start-value-func (x nitems)
514 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-title x) nitems))
516 (defclass xmlformat (textformat)
518 (:default-initargs :file-start-str "" ; (std-xml-header)
520 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
521 :list-start-value-func #'xmlformat-list-start-value-func
523 :list-end-fmtstr "</~a>~%"
524 :list-end-value-func #'xmlformat-list-end-value-func
525 :obj-start-fmtstr "<~a>"
526 :obj-start-value-func #'class-name-of
528 :obj-end-fmtstr "</~a>~%"
529 :obj-end-value-func #'class-name-of
532 :obj-data-fmtstr #'hyperobject-class-fmtstr-xml
533 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-xml-labels
534 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
536 (defclass link-ref ()
537 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
538 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
539 (page-name :type string :initarg :page-name :accessor page-name)
540 (href-head :type string :initarg :href-head :accessor href-head)
541 (href-end :type string :initarg :href-end :accessor href-end)
542 (ampersand :type string :initarg :ampersand :accessor ampersand))
543 (:default-initargs :fmtstr nil
545 :page-name "disp-func1"
546 :href-head nil :href-end nil :ampersand nil)
547 (:documentation "Formatting for a linked reference"))
549 (defclass html-link-ref (link-ref)
551 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
552 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
557 (defclass xhtml-link-ref (link-ref)
559 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
560 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
565 (defclass xml-link-ref (link-ref)
567 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-xml-ref
568 :fmtstr-labels #'hyperobject-class-fmtstr-xml-ref-labels
569 :href-head "xmllink xlink:type=\"simple\" xlink:href="
572 (:documentation "Mozilla's and W3's idea of a link with XML"))
574 (defclass ie-xml-link-ref (xml-link-ref)
576 (:default-initargs :href-head "html:a href="
578 (:documentation "Internet Explorer's idea of a link with XML"))
581 (defclass htmlrefformat (htmlformat)
583 (:default-initargs :link-ref (make-instance 'html-link-ref)))
585 (defclass xhtmlrefformat (xhtmlformat)
587 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
589 (defclass xmlrefformat (xmlformat)
591 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
593 (defclass ie-xmlrefformat (xmlformat)
595 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
598 ;;; File Start and Ends
600 (defgeneric fmt-file-start (fmt s))
601 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
603 (defmethod fmt-file-start ((fmt textformat) (s stream))
604 (aif (file-start-str fmt)
607 (defgeneric fmt-file-end (fmt s))
608 (defmethod fmt-file-end ((fmt textformat) (s stream))
609 (aif (file-end-str fmt)
612 ;;; List Start and Ends
614 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
615 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
616 (if (list-start-indent fmt)
617 (indent-spaces indent s))
618 (aif (list-start-fmtstr fmt)
621 (funcall (list-start-value-func fmt) x num-items)))))
623 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
624 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
625 (declare (ignore num-items))
626 (if (list-end-indent fmt)
627 (indent-spaces indent s))
628 (aif (list-end-fmtstr fmt)
631 (funcall (list-end-value-func fmt) x)))))
633 ;;; Object Start and Ends
635 (defgeneric fmt-obj-start (obj fmt s &optional indent))
636 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
637 (if (obj-start-indent fmt)
638 (indent-spaces indent s))
639 (aif (obj-start-fmtstr fmt)
642 (funcall (obj-start-value-func fmt) x)))))
644 (defgeneric fmt-obj-end (obj fmt s &optional indent))
645 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
646 (if (obj-end-indent fmt)
647 (indent-spaces indent s))
648 (aif (obj-end-fmtstr fmt)
651 (funcall (obj-end-value-func fmt) x)))))
655 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
656 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
657 (declare (ignore obj fieldname))
658 (format nil "~a\"~a?func=~a~akey=~a~a\""
659 (href-head ref) (make-url (page-name ref)) fieldfunc
660 (ampersand ref) fieldvalue
663 (dolist (var refvars)
664 (string-append varstr (format nil "~a~a=~a"
665 (ampersand ref) (car var) (cadr var))))
669 (defgeneric make-link-end (obj ref fieldname))
670 (defmethod make-link-end (obj (ref link-ref) fieldname)
671 (declare (ignore obj fieldname))
672 (format nil "~a" (href-end ref))
675 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
676 (defmethod fmt-obj-data (x (fmt textformat) s
677 &optional (indent 0) (label nil) (refvars nil))
678 (if (obj-data-indent fmt)
679 (indent-spaces indent s))
681 (fmt-obj-data-with-ref x fmt s label refvars)
682 (fmt-obj-data-plain x fmt s label))
683 (aif (obj-data-end-fmtstr fmt)
686 (defgeneric fmt-obj-data-plain (obj fmt s label))
687 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
690 (funcall (obj-data-fmtstr-labels fmt) x)
692 (funcall (funcall (obj-data-value-func fmt) x) x)))
693 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
695 (funcall (funcall (obj-data-value-func fmt) x) x)))))
697 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
698 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
699 (let ((refstr (make-ref-data-str x fmt label))
703 (funcall (funcall (obj-data-value-func fmt) x) x))))
705 ;; make list of reference link fields for printing to refstr template
706 (dolist (ref (hyperobject-class-references x))
708 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
709 (nth (position (name ref)
710 (hyperobject-class-fields x)
712 (slot-definition-name x)))
714 (append (link-parameters ref) refvars)))
715 (link-end (make-link-end x (link-ref fmt) (name ref))))
716 (push link-start refvalues)
717 (push link-end refvalues)))
718 (setq refvalues (nreverse refvalues))
720 (apply #'format s refstr refvalues)))
722 (defgeneric obj-data (obj))
723 (defmethod obj-data (x)
724 "Returns the objects data as a string. Used by common-graphics outline function"
725 (let ((fmt (make-format-instance :text)))
726 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
728 (funcall (funcall (obj-data-value-func fmt) x) x)))))
730 (defgeneric make-ref-data-str (obj fmt &optional label))
731 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
732 "Return fmt string for that contains ~a slots for reference link start and end"
733 (unless (link-ref fmt)
734 (error "fmt does not contain a link-ref"))
737 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
739 (funcall (funcall (obj-data-value-func fmt) x) x)))
740 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
741 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
744 ;;; Display method for objects
747 (defgeneric load-all-subobjects (objs))
748 (defmethod load-all-subobjects (objs)
749 "Load all subobjects if they have not already been loaded."
751 (let ((objlist (mklist objs)))
752 (dolist (obj objlist)
753 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
754 (dolist (child-obj it) ;; for each child function
755 (awhen (funcall (reader child-obj) obj)
756 (load-all-subobjects it))))))
759 (defgeneric print-hyperobject-class (objs fmt strm
760 &optional label english-only-function
761 indent subobjects refvars))
763 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream)
764 &optional (label nil) (indent 0)
765 (english-only-function nil)
766 (subobjects nil) (refvars nil))
767 "Display a single or list of hyperobject-class instances and their subobjects"
769 (setq objs (mklist objs))
770 (let ((nobjs (length objs)))
771 (fmt-list-start (car objs) fmt strm indent nobjs)
773 (unless (and english-only-function
774 (multiple-value-bind (eng term) (funcall english-only-function obj)
775 (and term (not eng))))
776 (fmt-obj-start obj fmt strm indent)
777 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
779 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
780 (dolist (child-obj it) ;; for each child function
781 (awhen (funcall (reader child-obj) obj) ;; access set of child objects
782 (print-hyperobject-class it fmt strm label
783 (1+ indent) english-only-function
784 subobjects refvars)))))
785 (fmt-obj-end obj fmt strm indent)))
786 (fmt-list-end (car objs) fmt strm indent nobjs))
791 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
792 (label nil) (english-only-function nil)
793 (subobjects nil) (file-wrapper t) (refvars nil))
794 "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
795 (let ((fmt (make-format-instance format)))
797 (fmt-file-start fmt os))
799 (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
801 (fmt-file-end fmt os)))
805 (defclass hyperobject ()
807 (:metaclass hyperobject-class))
810 (defmethod print-object ((obj hyperobject) (s stream))
811 (print-unreadable-object (obj s :type t :identity t)
812 (let ((fmt (make-instance 'hyperobject::textformat)))
814 s (funcall (obj-data-fmtstr fmt) obj)
816 (funcall (funcall (obj-data-value-func fmt) obj) obj))))))