1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: HYPEROBJECT -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: hyperobject.lisp
6 ;;;; Purpose: Hyper Object (Plain - no Metaclass)
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Nov 2002
10 ;;;; This is a rewrite of hyperobjec't to avoid using metaclasses.
12 ;;;; $Id: hyperobject.lisp,v 1.5 2002/11/22 10:49:24 kevin Exp $
14 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
16 ;;;; *************************************************************************
18 (in-package :hyperobject)
21 (eval-when (:compile-toplevel :execute)
22 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
27 (defclass hyperobject ()
30 (defclass hyperobject-meta ()
31 ((fields :initform nil :type list)
32 (subobjects :initform nil :type list)
33 (references :initform nil :type list)
34 (value-func :initform nil :type function)
35 (xml-value-func :initform nil :type function)
36 (fmtstr-text :initform nil :type string)
37 (fmtstr-html :initform nil :type string)
38 (fmtstr-xml :initform nil :type string)
39 (fmtstr-text-labels :initform nil :type string)
40 (fmtstr-html-labels :initform nil :type string)
41 (fmtstr-xml-labels :initform nil :type string)
42 (fmtstr-html-ref :initform nil :type string)
43 (fmtstr-xml-ref :initform nil :type string)
44 (fmtstr-html-ref-labels :initform nil :type string)
45 (fmtstr-xml-ref-labels :initform nil :type string)
47 (:documentation "Class holding meta information for hyperobjects"))
50 ((name :type symbol :initform nil :initarg :name :reader name)
51 (format-func :initform nil :initarg :format-func :reader format-func)
52 (cl-type :initform nil :reader cl-type)
53 (ho-type :initform nil :reader ho-type)
54 (subobject :initform nil :reader subobject)
55 (reference :initform nil :reader reference)
59 (defmethod print-object ((obj field) (s stream))
60 (print-unreadable-object (obj s :type t :identity t)
61 (format s "~S" (name obj))))
63 (defclass subobject ()
64 ((name :type symbol :initform nil :initarg :name :reader name)
65 (reader :type function :initform nil :initarg :reader :reader reader)))
67 (defmethod print-object ((obj subobject) (s stream))
68 (print-unreadable-object (obj s :type t :identity t)
69 (format s "~S" (name obj))))
71 (defclass reference ()
72 ((name :type symbol :initform nil :initarg :name :reader name)
73 (lookup :type function :initform nil :initarg :lookup :reader lookup)
74 (link-parameters :type list :initform nil :initarg :link-parameters
75 :reader link-parameters)))
77 (defmethod print-object ((obj reference) (s stream))
78 (print-unreadable-object (obj s :type t :identity t)
79 (format s "~S" (name obj))))
81 (defun remove-keys (key-names args)
82 (loop for ( name val ) on args by #'cddr
83 unless (member (symbol-name name) key-names
84 :key #'symbol-name :test 'equal)
85 append (list name val)))
87 (defun convert-ho-type (ho-type)
88 (check-type ho-type symbol)
89 (case (intern (symbol-name ho-type) (symbol-name :keyword))
105 (defun process-hyper-fields (raw-fields meta)
110 (loop for field in raw-fields
113 (name &rest rest &key
114 ;; the following list of keywords is reproduced below in the
115 ;; remove-keys form. important to keep them in sync
116 type reader writer accessor initform format-func initarg
119 &allow-other-keys) field
120 (let ((other-args (remove-keys
121 '(type reader writer accessor initform format-func
122 initarg reference subobject)
124 (field-obj (make-instance 'field :name name))
126 (declare (ignore other-args))
127 (push field-obj fields)
128 (loop for (k v) in `((:type ,type) (:reader ,reader) (:writer ,writer)
129 (:accessor ,accessor) (:initform ,initform)
130 (:format-func ,format-func) (:initarg ,initarg)
131 (:subobject ,subobject) (:reference ,reference))
138 (intern (symbol-name name)
139 (symbol-name :keyword))))
143 (push (list :accessor v) kv)))
146 (setf (slot-value field-obj 'format-func) v)))
149 (push (list :writer v) kv)))
152 (push (list :reader v) kv)
153 (push (list :reader name) kv)))
156 (setf (slot-value field-obj 'ho-type) v)
157 (setf (slot-value field-obj 'cl-type) (convert-ho-type v))
158 (push (list :type (cl-type field-obj)) kv)))
161 (let ((subobj (make-instance
162 'subobject :name name
167 (setf (slot-value field-obj 'subobject) subobj)
168 (push subobj subobjects))))
171 (let ((ref (make-instance 'reference :name name :lookup v)))
172 (setf (slot-value field-obj 'reference) ref)
173 (push ref references))))
177 (loop for (k v) in kv
180 (setf (slot-value meta 'fields) (nreverse fields))
181 (setf (slot-value meta 'references) (nreverse references))
182 (setf (slot-value meta 'subobjects) (nreverse subobjects))
186 (defun process-title (name meta)
187 (let ((title (cadr (assoc :title meta))))
192 (symbol-name name))))
194 (defun process-documentation (meta)
195 (let ((doc (cadr (assoc :title meta))))
196 (if (and doc (symbolp doc))
200 (defun fmt-comma-integer (i)
201 (format nil "~:d" i))
203 ;;;; Class initialization function
204 (defun init-hyperobject-class (name meta)
205 (let ((fmtstr-text "")
208 (fmtstr-text-labels "")
209 (fmtstr-html-labels "")
210 (fmtstr-xml-labels "")
213 (fmtstr-html-ref-labels "")
214 (fmtstr-xml-ref-labels "")
218 (package (symbol-package name)))
219 (dolist (field (slot-value meta 'fields))
220 (declare (ignore rest))
221 (let* ((name (name field))
222 (format-func (format-func field))
223 (type (ho-type field))
224 (reference (reference field))
225 (namestr (symbol-name name))
226 (namestr-lower (string-downcase (symbol-name name)))
228 (plain-value-func nil)
229 html-str xml-str html-label-str xml-label-str)
230 (unless (subobject field)
231 (when (or (eql type :integer) (eql type :fixnum))
232 (setq value-fmt "~d"))
234 (when (eql type :boolean)
235 (setq value-fmt "~a"))
238 (setq first-field nil)
240 (string-append fmtstr-text " ")
241 (string-append fmtstr-html " ")
242 (string-append fmtstr-xml " ")
243 (string-append fmtstr-text-labels " ")
244 (string-append fmtstr-html-labels " ")
245 (string-append fmtstr-xml-labels " ")
246 (string-append fmtstr-html-ref " ")
247 (string-append fmtstr-xml-ref " ")
248 (string-append fmtstr-html-ref-labels " ")
249 (string-append fmtstr-xml-ref-labels " ")))
251 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
252 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
253 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
254 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
256 (string-append fmtstr-text value-fmt)
257 (string-append fmtstr-html html-str)
258 (string-append fmtstr-xml xml-str)
259 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
260 (string-append fmtstr-html-labels html-label-str)
261 (string-append fmtstr-xml-labels xml-label-str)
265 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
266 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
267 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
268 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
270 (string-append fmtstr-html-ref html-str)
271 (string-append fmtstr-xml-ref xml-str)
272 (string-append fmtstr-html-ref-labels html-label-str)
273 (string-append fmtstr-xml-ref-labels xml-label-str)))
276 (setq plain-value-func
277 (list `(,format-func (,(intern namestr package) x))))
278 (setq plain-value-func
279 (list `(,(intern namestr package) x))))
280 (setq value-func (append value-func plain-value-func))
282 (if (eql type :cdata)
283 (setq xml-value-func (append xml-value-func (list `(xml-cdata ,@plain-value-func))))
284 (setq xml-value-func (append xml-value-func plain-value-func)))
288 (setq value-func `(lambda (x) (values ,@value-func)))
289 (setq value-func `(lambda () (values))))
292 (setq xml-value-func `(lambda (x) (values ,@xml-value-func)))
293 (setq xml-value-func `(lambda () (values))))
295 (setf (slot-value meta 'fmtstr-text) fmtstr-text)
296 (setf (slot-value meta 'fmtstr-html) fmtstr-html)
297 (setf (slot-value meta 'fmtstr-xml) fmtstr-xml)
298 (setf (slot-value meta 'fmtstr-text-labels) fmtstr-text-labels)
299 (setf (slot-value meta 'fmtstr-html-labels) fmtstr-html-labels)
300 (setf (slot-value meta 'fmtstr-xml-labels) fmtstr-xml-labels)
301 (setf (slot-value meta 'fmtstr-html-ref) fmtstr-html-ref)
302 (setf (slot-value meta 'fmtstr-xml-ref) fmtstr-xml-ref)
303 (setf (slot-value meta 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
304 (setf (slot-value meta 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
305 (setf (slot-value meta 'value-func) value-func)
306 (setf (slot-value meta 'xml-value-func) xml-value-func))
309 (defgeneric ho-title (obj) )
310 (defgeneric ho-name (obj) )
311 (defgeneric ho-value-func (obj) )
312 (defgeneric ho-xml-value-func (obj) )
313 (defgeneric ho-fmtstr-text (obj) )
314 (defgeneric ho-fmtstr-html (obj) )
315 (defgeneric ho-fmtstr-xml (obj) )
316 (defgeneric ho-fmtstr-text-labels (obj) )
317 (defgeneric ho-fmtstr-html-labels (obj) )
318 (defgeneric ho-fmtstr-xml-labels (obj) )
319 (defgeneric ho-fmtstr-html-ref (obj) )
320 (defgeneric ho-fmtstr-xml-ref (obj) )
321 (defgeneric ho-fmtstr-html-ref-labels (obj) )
322 (defgeneric ho-fmtstr-xml-ref-labels (obj) )
323 (defgeneric ho-fields (obj) )
324 (defgeneric ho-references (obj) )
325 (defgeneric ho-subobjects (obj) )
327 (defmacro define-hyperobject (name parents fields &rest meta-fields)
328 (let* ((meta (make-instance 'hyperobject-meta))
329 (cl-fields (process-hyper-fields fields meta))
330 (title (process-title name meta-fields))
331 (documentation (process-documentation meta-fields))
332 (value-func (gensym))
333 (xml-value-func (gensym)))
334 (init-hyperobject-class name meta)
336 (eval-when (:compile-toplevel :load-toplevel :execute)
337 (defclass ,name ,(append parents (list 'hyperobject)) ,cl-fields
338 ,@(and documentation (list (list :documentation documentation)))))
339 (let ((,value-func (compile nil (eval (slot-value ,meta 'value-func))))
340 (,xml-value-func (compile nil (eval (slot-value ,meta 'xml-value-func)))))
341 (defmethod ho-title ((obj ,name))
343 (defmethod ho-name ((obj ,name))
344 ,(string-downcase (symbol-name name)))
345 (defmethod ho-fields ((obj ,name))
346 ',(slot-value meta 'fields))
347 (defmethod ho-references ((obj ,name))
348 ',(slot-value meta 'references))
349 (defmethod ho-subobjects ((obj ,name))
350 ',(slot-value meta 'subobjects))
351 (defmethod ho-value-func ((obj ,name))
353 (defmethod ho-xml-value-func ((obj ,name))
355 (defmethod ho-fmtstr-text ((obj ,name))
356 ,(slot-value meta 'fmtstr-text))
357 (defmethod ho-fmtstr-html ((obj ,name))
358 ,(slot-value meta 'fmtstr-html))
359 (defmethod ho-fmtstr-xml ((obj ,name))
360 ,(slot-value meta 'fmtstr-xml))
361 (defmethod ho-fmtstr-text-labels ((obj ,name))
362 ,(slot-value meta 'fmtstr-text-labels))
363 (defmethod ho-fmtstr-html-labels ((obj ,name))
364 ,(slot-value meta 'fmtstr-html-labels))
365 (defmethod ho-fmtstr-xml-labels ((obj ,name))
366 ,(slot-value meta 'fmtstr-xml-labels))
367 (defmethod ho-fmtstr-html-ref ((obj ,name))
368 ,(slot-value meta 'fmtstr-html-ref))
369 (defmethod ho-fmtstr-xml-ref ((obj ,name))
370 ,(slot-value meta 'fmtstr-xml-ref))
371 (defmethod ho-fmtstr-html-ref-labels ((obj ,name))
372 ,(slot-value meta 'fmtstr-html-ref-labels))
373 (defmethod ho-fmtstr-xml-ref-labels ((obj ,name))
374 ,(slot-value meta 'fmtstr-xml-ref-labels))
377 ;;;; Generic Print functions
379 (defparameter *default-textformat* nil)
380 (defparameter *default-htmlformat* nil)
381 (defparameter *default-htmlrefformat* nil)
382 (defparameter *default-xhtmlformat* nil)
383 (defparameter *default-xhtmlrefformat* nil)
384 (defparameter *default-xmlformat* nil)
385 (defparameter *default-xmlrefformat* nil)
386 (defparameter *default-ie-xmlrefformat* nil)
387 (defparameter *default-nullformat* nil)
388 (defparameter *default-init-format?* nil)
390 (defun make-format-instance (fmt)
391 (unless *default-init-format?*
392 (setq *default-textformat* (make-instance 'textformat))
393 (setq *default-htmlformat* (make-instance 'htmlformat))
394 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
395 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
396 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
397 (setq *default-xmlformat* (make-instance 'xmlformat))
398 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
399 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
400 (setq *default-nullformat* (make-instance 'nullformat))
401 (setq *default-init-format?* t))
404 (:text *default-textformat*)
405 (:html *default-htmlformat*)
406 (:htmlref *default-htmlrefformat*)
407 (:xhtml *default-xhtmlformat*)
408 (:xhtmlref *default-xhtmlrefformat*)
409 (:xml *default-xmlformat*)
410 (:xmlref *default-xmlrefformat*)
411 (:ie-xmlref *default-ie-xmlrefformat*)
412 (:null *default-nullformat*)
413 (otherwise *default-textformat*)))
415 ;;;; Output format classes for print hoes
417 (defclass dataformat ()
418 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
419 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
420 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
421 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
422 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
423 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
424 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
425 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
426 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
427 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
428 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
429 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
430 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
431 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
432 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
433 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
434 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
435 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
436 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
437 (link-ref :initarg :link-ref :reader link-ref))
438 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
439 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
440 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
441 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
442 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
443 :obj-data-value-func nil :link-ref nil)
444 (:documentation "Parent for all dataformat objects"))
446 (defclass binaryformat (dataformat)
449 (defclass nullformat (dataformat)
452 (defun text-list-start-value-func (obj nitems)
453 (values (ho-title obj) nitems))
455 (defclass textformat (dataformat)
457 (:default-initargs :list-start-fmtstr "~a~P:~%"
458 :list-start-value-func #'text-list-start-value-func
461 :obj-data-fmtstr #'ho-fmtstr-text
462 :obj-data-fmtstr-labels #'ho-fmtstr-text-labels
463 :obj-data-end-fmtstr "~%"
464 :obj-data-value-func #'ho-value-func))
467 (defun htmlformat-list-start-value-func (x nitems)
468 (values (ho-title x) nitems (ho-name 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 #'ho-fmtstr-html-labels
488 :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
489 :obj-data-value-func #'ho-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 #'ho-fmtstr-html-labels
509 :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
510 :obj-data-value-func #'ho-xml-value-func))
513 (defun xmlformat-list-end-value-func (x)
514 (format nil "~alist" (ho-name x)))
516 (defun xmlformat-list-start-value-func (x nitems)
517 (values (format nil "~alist" (ho-name x)) (ho-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 #'ho-name
531 :obj-end-fmtstr "</~a>~%"
532 :obj-end-value-func #'ho-name
535 :obj-data-fmtstr #'ho-fmtstr-xml
536 :obj-data-fmtstr-labels #'ho-fmtstr-xml-labels
537 :obj-data-value-func #'ho-xml-value-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 #'ho-fmtstr-html-ref
555 :fmtstr-labels #'ho-fmtstr-html-ref-labels
560 (defclass xhtml-link-ref (link-ref)
562 (:default-initargs :fmtstr #'ho-fmtstr-html-ref
563 :fmtstr-labels #'ho-fmtstr-html-ref-labels
568 (defclass xml-link-ref (link-ref)
570 (:default-initargs :fmtstr #'ho-fmtstr-xml-ref
571 :fmtstr-labels #'ho-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 (ho-references x))
711 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
712 (nth (position (name ref) (ho-fields x)
713 :key #'(lambda (x) (name x)))
715 (append (link-parameters ref) refvars)))
716 (link-end (make-link-end x (link-ref fmt) (name ref))))
717 (push link-start refvalues)
718 (push link-end refvalues)))
719 (setq refvalues (nreverse refvalues))
721 (apply #'format s refstr refvalues)))
723 (defgeneric obj-data (obj))
724 (defmethod obj-data (x)
725 "Returns the objects data as a string. Used by common-graphics outline function"
726 (let ((fmt (make-format-instance :text)))
727 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
729 (funcall (funcall (obj-data-value-func fmt) x) x)))))
731 (defgeneric make-ref-data-str (obj fmt &optional label))
732 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
733 "Return fmt string for that contains ~a slots for reference link start and end"
734 (unless (link-ref fmt)
735 (error "fmt does not contain a link-ref"))
738 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
740 (funcall (funcall (obj-data-value-func fmt) x) x)))
741 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
742 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
745 ;;; Display method for objects
748 (defgeneric load-all-subobjects (objs))
749 (defmethod load-all-subobjects (objs)
750 "Load all subobjects if they have not already been loaded."
752 (let ((objlist (mklist objs)))
753 (dolist (obj objlist)
754 (awhen (ho-subobjects obj) ;; access list of functions
755 (dolist (subobj it) ;; for each child function
756 (awhen (funcall (reader subobj) obj)
757 (load-all-subobjects it))))))
760 (defgeneric print-hyperobject-class (objs fmt strm
761 &optional label english-only-function
762 indent subobjects refvars))
764 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream)
765 &optional (label nil) (indent 0)
766 (english-only-function nil)
767 (subobjects nil) (refvars nil))
768 "Display a single or list of hyperobject-class instances and their subobjects"
770 (setq objs (mklist objs))
771 (let ((nobjs (length objs)))
772 (fmt-list-start (car objs) fmt strm indent nobjs)
774 (unless (and english-only-function
775 (multiple-value-bind (eng term) (funcall english-only-function obj)
776 (and term (not eng))))
777 (fmt-obj-start obj fmt strm indent)
778 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
780 (awhen (ho-subobjects obj) ;; access list of functions
781 (dolist (subobj it) ;; for each child function
782 (awhen (funcall (reader subobj) obj) ;; access set of child objects
783 (print-hyperobject-class it fmt strm label
784 (1+ indent) english-only-function
785 subobjects refvars)))))
786 (fmt-obj-end obj fmt strm indent)))
787 (fmt-list-end (car objs) fmt strm indent nobjs))
792 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
793 (label nil) (english-only-function nil)
794 (subobjects nil) (file-wrapper t) (refvars nil))
795 "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
796 (let ((fmt (make-format-instance format)))
798 (fmt-file-start fmt os))
800 (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
802 (fmt-file-end fmt os)))
806 (defmethod print-object ((obj hyperobject) (s stream))
807 (print-unreadable-object (obj s :type t :identity t)
808 (let ((fmt (make-instance 'hyperobject::textformat)))
810 s (funcall (hyperobject::obj-data-fmtstr fmt) obj)
812 (funcall (funcall (hyperobject::obj-data-value-func fmt) obj) obj))))))