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-no-mop.lisp,v 1.5 2002/11/25 02:10:38 kevin Exp $
14 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
16 ;;;; *************************************************************************
18 (in-package :hyperobject-no-mop)
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 (print-formatter :initform nil :initarg :print-formatter :reader print-formatter)
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 print-formatter initarg
119 &allow-other-keys) field
120 (let ((other-args (remove-keys
121 '(type reader writer accessor initform print-formatter
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 (:print-formatter ,print-formatter) (: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 'print-formatter) 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 ;;;; Class initialization function
201 (defun init-hyperobject-class (name meta)
202 (let ((fmtstr-text "")
205 (fmtstr-text-labels "")
206 (fmtstr-html-labels "")
207 (fmtstr-xml-labels "")
210 (fmtstr-html-ref-labels "")
211 (fmtstr-xml-ref-labels "")
215 (package (symbol-package name)))
216 (dolist (field (slot-value meta 'fields))
217 (let* ((name (name field))
218 (print-formatter (print-formatter field))
219 (type (ho-type field))
220 (reference (reference field))
221 (namestr (symbol-name name))
222 (namestr-lower (string-downcase (symbol-name name)))
224 (plain-value-func nil)
225 html-str xml-str html-label-str xml-label-str)
226 (unless (subobject field)
227 (when (or (eql type :integer) (eql type :fixnum))
228 (setq value-fmt "~d"))
230 (when (eql type :boolean)
231 (setq value-fmt "~a"))
234 (setq first-field nil)
236 (string-append fmtstr-text " ")
237 (string-append fmtstr-html " ")
238 (string-append fmtstr-xml " ")
239 (string-append fmtstr-text-labels " ")
240 (string-append fmtstr-html-labels " ")
241 (string-append fmtstr-xml-labels " ")
242 (string-append fmtstr-html-ref " ")
243 (string-append fmtstr-xml-ref " ")
244 (string-append fmtstr-html-ref-labels " ")
245 (string-append fmtstr-xml-ref-labels " ")))
247 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
248 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
249 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
250 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
252 (string-append fmtstr-text value-fmt)
253 (string-append fmtstr-html html-str)
254 (string-append fmtstr-xml xml-str)
255 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
256 (string-append fmtstr-html-labels html-label-str)
257 (string-append fmtstr-xml-labels xml-label-str)
261 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
262 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
263 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
264 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
266 (string-append fmtstr-html-ref html-str)
267 (string-append fmtstr-xml-ref xml-str)
268 (string-append fmtstr-html-ref-labels html-label-str)
269 (string-append fmtstr-xml-ref-labels xml-label-str)))
272 (setq plain-value-func
273 (list `(,print-formatter (,(intern namestr package) x))))
274 (setq plain-value-func
275 (list `(,(intern namestr package) x))))
276 (setq value-func (append value-func plain-value-func))
278 (if (eql type :cdata)
279 (setq xml-value-func (append xml-value-func (list `(xml-cdata ,@plain-value-func))))
280 (setq xml-value-func (append xml-value-func plain-value-func)))
284 (setq value-func `(lambda (x) (values ,@value-func)))
285 (setq value-func `(lambda () (values))))
288 (setq xml-value-func `(lambda (x) (values ,@xml-value-func)))
289 (setq xml-value-func `(lambda () (values))))
291 (setf (slot-value meta 'fmtstr-text) fmtstr-text)
292 (setf (slot-value meta 'fmtstr-html) fmtstr-html)
293 (setf (slot-value meta 'fmtstr-xml) fmtstr-xml)
294 (setf (slot-value meta 'fmtstr-text-labels) fmtstr-text-labels)
295 (setf (slot-value meta 'fmtstr-html-labels) fmtstr-html-labels)
296 (setf (slot-value meta 'fmtstr-xml-labels) fmtstr-xml-labels)
297 (setf (slot-value meta 'fmtstr-html-ref) fmtstr-html-ref)
298 (setf (slot-value meta 'fmtstr-xml-ref) fmtstr-xml-ref)
299 (setf (slot-value meta 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
300 (setf (slot-value meta 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
301 (setf (slot-value meta 'value-func) value-func)
302 (setf (slot-value meta 'xml-value-func) xml-value-func))
305 (defgeneric ho-title (obj) )
306 (defgeneric ho-name (obj) )
307 (defgeneric ho-value-func (obj) )
308 (defgeneric ho-xml-value-func (obj) )
309 (defgeneric ho-fmtstr-text (obj) )
310 (defgeneric ho-fmtstr-html (obj) )
311 (defgeneric ho-fmtstr-xml (obj) )
312 (defgeneric ho-fmtstr-text-labels (obj) )
313 (defgeneric ho-fmtstr-html-labels (obj) )
314 (defgeneric ho-fmtstr-xml-labels (obj) )
315 (defgeneric ho-fmtstr-html-ref (obj) )
316 (defgeneric ho-fmtstr-xml-ref (obj) )
317 (defgeneric ho-fmtstr-html-ref-labels (obj) )
318 (defgeneric ho-fmtstr-xml-ref-labels (obj) )
319 (defgeneric ho-fields (obj) )
320 (defgeneric ho-references (obj) )
321 (defgeneric ho-subobjects (obj) )
323 (defmacro define-hyperobject (name parents fields &rest meta-fields)
324 (let* ((meta (make-instance 'hyperobject-meta))
325 (cl-fields (process-hyper-fields fields meta))
326 (title (process-title name meta-fields))
327 (documentation (process-documentation meta-fields))
328 (value-func (gensym))
329 (xml-value-func (gensym)))
330 (init-hyperobject-class name meta)
332 (eval-when (:compile-toplevel :load-toplevel :execute)
333 (defclass ,name ,(append parents (list 'hyperobject)) ,cl-fields
334 ,@(and documentation (list (list :documentation documentation)))))
335 (let ((,value-func (compile nil (eval (slot-value ,meta 'value-func))))
336 (,xml-value-func (compile nil (eval (slot-value ,meta 'xml-value-func)))))
337 (defmethod ho-title ((obj ,name))
339 (defmethod ho-name ((obj ,name))
340 ,(string-downcase (symbol-name name)))
341 (defmethod ho-fields ((obj ,name))
342 ',(slot-value meta 'fields))
343 (defmethod ho-references ((obj ,name))
344 ',(slot-value meta 'references))
345 (defmethod ho-subobjects ((obj ,name))
346 ',(slot-value meta 'subobjects))
347 (defmethod ho-value-func ((obj ,name))
349 (defmethod ho-xml-value-func ((obj ,name))
351 (defmethod ho-fmtstr-text ((obj ,name))
352 ,(slot-value meta 'fmtstr-text))
353 (defmethod ho-fmtstr-html ((obj ,name))
354 ,(slot-value meta 'fmtstr-html))
355 (defmethod ho-fmtstr-xml ((obj ,name))
356 ,(slot-value meta 'fmtstr-xml))
357 (defmethod ho-fmtstr-text-labels ((obj ,name))
358 ,(slot-value meta 'fmtstr-text-labels))
359 (defmethod ho-fmtstr-html-labels ((obj ,name))
360 ,(slot-value meta 'fmtstr-html-labels))
361 (defmethod ho-fmtstr-xml-labels ((obj ,name))
362 ,(slot-value meta 'fmtstr-xml-labels))
363 (defmethod ho-fmtstr-html-ref ((obj ,name))
364 ,(slot-value meta 'fmtstr-html-ref))
365 (defmethod ho-fmtstr-xml-ref ((obj ,name))
366 ,(slot-value meta 'fmtstr-xml-ref))
367 (defmethod ho-fmtstr-html-ref-labels ((obj ,name))
368 ,(slot-value meta 'fmtstr-html-ref-labels))
369 (defmethod ho-fmtstr-xml-ref-labels ((obj ,name))
370 ,(slot-value meta 'fmtstr-xml-ref-labels))
373 ;;;; Generic Print functions
375 (defparameter *default-textformat* nil)
376 (defparameter *default-htmlformat* nil)
377 (defparameter *default-htmlrefformat* nil)
378 (defparameter *default-xhtmlformat* nil)
379 (defparameter *default-xhtmlrefformat* nil)
380 (defparameter *default-xmlformat* nil)
381 (defparameter *default-xmlrefformat* nil)
382 (defparameter *default-ie-xmlrefformat* nil)
383 (defparameter *default-nullformat* nil)
384 (defparameter *default-init-format?* nil)
386 (defun make-format-instance (fmt)
387 (unless *default-init-format?*
388 (setq *default-textformat* (make-instance 'textformat))
389 (setq *default-htmlformat* (make-instance 'htmlformat))
390 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
391 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
392 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
393 (setq *default-xmlformat* (make-instance 'xmlformat))
394 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
395 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
396 (setq *default-nullformat* (make-instance 'nullformat))
397 (setq *default-init-format?* t))
400 (:text *default-textformat*)
401 (:html *default-htmlformat*)
402 (:htmlref *default-htmlrefformat*)
403 (:xhtml *default-xhtmlformat*)
404 (:xhtmlref *default-xhtmlrefformat*)
405 (:xml *default-xmlformat*)
406 (:xmlref *default-xmlrefformat*)
407 (:ie-xmlref *default-ie-xmlrefformat*)
408 (:null *default-nullformat*)
409 (otherwise *default-textformat*)))
411 ;;;; Output format classes for print hoes
413 (defclass dataformat ()
414 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
415 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
416 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
417 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
418 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
419 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
420 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
421 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
422 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
423 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
424 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
425 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
426 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
427 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
428 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
429 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
430 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
431 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
432 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
433 (link-ref :initarg :link-ref :reader link-ref))
434 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
435 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
436 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
437 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
438 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
439 :obj-data-value-func nil :link-ref nil)
440 (:documentation "Parent for all dataformat objects"))
442 (defclass binaryformat (dataformat)
445 (defclass nullformat (dataformat)
448 (defun text-list-start-value-func (obj nitems)
449 (values (ho-title obj) nitems))
451 (defclass textformat (dataformat)
453 (:default-initargs :list-start-fmtstr "~a~P:~%"
454 :list-start-value-func #'text-list-start-value-func
457 :obj-data-fmtstr #'ho-fmtstr-text
458 :obj-data-fmtstr-labels #'ho-fmtstr-text-labels
459 :obj-data-end-fmtstr "~%"
460 :obj-data-value-func #'ho-value-func))
463 (defun htmlformat-list-start-value-func (x nitems)
464 (values (ho-title x) nitems (ho-name x)))
466 (defclass htmlformat (textformat)
468 (:default-initargs :file-start-str "<html><body>~%"
469 :file-end-str "</body><html>~%"
471 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
472 :list-start-value-func #'htmlformat-list-start-value-func
473 :list-end-fmtstr "</ul></div>~%"
475 :list-end-value-func #'identity
477 :obj-start-fmtstr "<li>"
478 :obj-start-value-func #'identity
480 :obj-end-fmtstr "</li>~%"
481 :obj-end-value-func #'identity
483 :obj-data-fmtstr #'ho-fmtstr-html-labels
484 :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
485 :obj-data-value-func #'ho-value-func))
487 (defclass xhtmlformat (textformat)
489 (:default-initargs :file-start-str "<html><body>~%"
490 :file-end-str "</body><html>~%"
492 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
493 :list-start-value-func #'htmlformat-list-start-value-func
494 :list-end-fmtstr "</ul></div>~%"
496 :list-end-value-func #'identity
498 :obj-start-fmtstr "<li>"
499 :obj-start-value-func #'identity
501 :obj-end-fmtstr "</li>~%"
502 :obj-end-value-func #'identity
504 :obj-data-fmtstr #'ho-fmtstr-html-labels
505 :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
506 :obj-data-value-func #'ho-xml-value-func))
509 (defun xmlformat-list-end-value-func (x)
510 (format nil "~alist" (ho-name x)))
512 (defun xmlformat-list-start-value-func (x nitems)
513 (values (format nil "~alist" (ho-name x)) (ho-title x) nitems))
515 (defclass xmlformat (textformat)
517 (:default-initargs :file-start-str "" ; (std-xml-header)
519 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
520 :list-start-value-func #'xmlformat-list-start-value-func
522 :list-end-fmtstr "</~a>~%"
523 :list-end-value-func #'xmlformat-list-end-value-func
524 :obj-start-fmtstr "<~a>"
525 :obj-start-value-func #'ho-name
527 :obj-end-fmtstr "</~a>~%"
528 :obj-end-value-func #'ho-name
531 :obj-data-fmtstr #'ho-fmtstr-xml
532 :obj-data-fmtstr-labels #'ho-fmtstr-xml-labels
533 :obj-data-value-func #'ho-xml-value-func))
535 (defclass link-ref ()
536 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
537 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
538 (page-name :type string :initarg :page-name :accessor page-name)
539 (href-head :type string :initarg :href-head :accessor href-head)
540 (href-end :type string :initarg :href-end :accessor href-end)
541 (ampersand :type string :initarg :ampersand :accessor ampersand))
542 (:default-initargs :fmtstr nil
544 :page-name "disp-func1"
545 :href-head nil :href-end nil :ampersand nil)
546 (:documentation "Formatting for a linked reference"))
548 (defclass html-link-ref (link-ref)
550 (:default-initargs :fmtstr #'ho-fmtstr-html-ref
551 :fmtstr-labels #'ho-fmtstr-html-ref-labels
556 (defclass xhtml-link-ref (link-ref)
558 (:default-initargs :fmtstr #'ho-fmtstr-html-ref
559 :fmtstr-labels #'ho-fmtstr-html-ref-labels
564 (defclass xml-link-ref (link-ref)
566 (:default-initargs :fmtstr #'ho-fmtstr-xml-ref
567 :fmtstr-labels #'ho-fmtstr-xml-ref-labels
568 :href-head "xmllink xlink:type=\"simple\" xlink:href="
571 (:documentation "Mozilla's and W3's idea of a link with XML"))
573 (defclass ie-xml-link-ref (xml-link-ref)
575 (:default-initargs :href-head "html:a href="
577 (:documentation "Internet Explorer's idea of a link with XML"))
580 (defclass htmlrefformat (htmlformat)
582 (:default-initargs :link-ref (make-instance 'html-link-ref)))
584 (defclass xhtmlrefformat (xhtmlformat)
586 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
588 (defclass xmlrefformat (xmlformat)
590 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
592 (defclass ie-xmlrefformat (xmlformat)
594 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
597 ;;; File Start and Ends
599 (defgeneric fmt-file-start (fmt s))
600 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
602 (defmethod fmt-file-start ((fmt textformat) (s stream))
603 (aif (file-start-str fmt)
606 (defgeneric fmt-file-end (fmt s))
607 (defmethod fmt-file-end ((fmt textformat) (s stream))
608 (aif (file-end-str fmt)
611 ;;; List Start and Ends
613 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
614 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
615 (if (list-start-indent fmt)
616 (indent-spaces indent s))
617 (aif (list-start-fmtstr fmt)
620 (funcall (list-start-value-func fmt) x num-items)))))
622 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
623 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
624 (declare (ignore num-items))
625 (if (list-end-indent fmt)
626 (indent-spaces indent s))
627 (aif (list-end-fmtstr fmt)
630 (funcall (list-end-value-func fmt) x)))))
632 ;;; Object Start and Ends
634 (defgeneric fmt-obj-start (obj fmt s &optional indent))
635 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
636 (if (obj-start-indent fmt)
637 (indent-spaces indent s))
638 (aif (obj-start-fmtstr fmt)
641 (funcall (obj-start-value-func fmt) x)))))
643 (defgeneric fmt-obj-end (obj fmt s &optional indent))
644 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
645 (if (obj-end-indent fmt)
646 (indent-spaces indent s))
647 (aif (obj-end-fmtstr fmt)
650 (funcall (obj-end-value-func fmt) x)))))
654 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
655 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
656 (declare (ignore obj fieldname))
657 (format nil "~a\"~a?func=~a~akey=~a~a\""
658 (href-head ref) (make-url (page-name ref)) fieldfunc
659 (ampersand ref) fieldvalue
662 (dolist (var refvars)
663 (string-append varstr (format nil "~a~a=~a"
664 (ampersand ref) (car var) (cadr var))))
668 (defgeneric make-link-end (obj ref fieldname))
669 (defmethod make-link-end (obj (ref link-ref) fieldname)
670 (declare (ignore obj fieldname))
671 (format nil "~a" (href-end ref))
674 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
675 (defmethod fmt-obj-data (x (fmt textformat) s
676 &optional (indent 0) (label nil) (refvars nil))
677 (if (obj-data-indent fmt)
678 (indent-spaces indent s))
680 (fmt-obj-data-with-ref x fmt s label refvars)
681 (fmt-obj-data-plain x fmt s label))
682 (aif (obj-data-end-fmtstr fmt)
685 (defgeneric fmt-obj-data-plain (obj fmt s label))
686 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
689 (funcall (obj-data-fmtstr-labels fmt) x)
691 (funcall (funcall (obj-data-value-func fmt) x) x)))
692 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
694 (funcall (funcall (obj-data-value-func fmt) x) x)))))
696 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
697 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
698 (let ((refstr (make-ref-data-str x fmt label))
702 (funcall (funcall (obj-data-value-func fmt) x) x))))
704 ;; make list of reference link fields for printing to refstr template
705 (dolist (ref (ho-references x))
707 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
708 (nth (position (name ref) (ho-fields x)
709 :key #'(lambda (x) (name x)))
711 (append (link-parameters ref) refvars)))
712 (link-end (make-link-end x (link-ref fmt) (name ref))))
713 (push link-start refvalues)
714 (push link-end refvalues)))
715 (setq refvalues (nreverse refvalues))
717 (apply #'format s refstr refvalues)))
719 (defgeneric obj-data (obj))
720 (defmethod obj-data (x)
721 "Returns the objects data as a string. Used by common-graphics outline function"
722 (let ((fmt (make-format-instance :text)))
723 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
725 (funcall (funcall (obj-data-value-func fmt) x) x)))))
727 (defgeneric make-ref-data-str (obj fmt &optional label))
728 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
729 "Return fmt string for that contains ~a slots for reference link start and end"
730 (unless (link-ref fmt)
731 (error "fmt does not contain a link-ref"))
734 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
736 (funcall (funcall (obj-data-value-func fmt) x) x)))
737 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
738 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
741 ;;; Display method for objects
744 (defgeneric load-all-subobjects (objs))
745 (defmethod load-all-subobjects (objs)
746 "Load all subobjects if they have not already been loaded."
748 (let ((objlist (mklist objs)))
749 (dolist (obj objlist)
750 (awhen (ho-subobjects obj) ;; access list of functions
751 (dolist (subobj it) ;; for each child function
752 (awhen (funcall (reader subobj) obj)
753 (load-all-subobjects it))))))
756 (defgeneric print-hyperobject-class (objs fmt strm
757 &optional label english-only-function
758 indent subobjects refvars))
760 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream)
761 &optional (label nil) (indent 0)
762 (english-only-function nil)
763 (subobjects nil) (refvars nil))
764 "Display a single or list of hyperobject-class instances and their subobjects"
766 (setq objs (mklist objs))
767 (let ((nobjs (length objs)))
768 (fmt-list-start (car objs) fmt strm indent nobjs)
770 (unless (and english-only-function
771 (multiple-value-bind (eng term) (funcall english-only-function obj)
772 (and term (not eng))))
773 (fmt-obj-start obj fmt strm indent)
774 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
776 (awhen (ho-subobjects obj) ;; access list of functions
777 (dolist (subobj it) ;; for each child function
778 (awhen (funcall (reader subobj) obj) ;; access set of child objects
779 (print-hyperobject-class it fmt strm label
780 (1+ indent) english-only-function
781 subobjects refvars)))))
782 (fmt-obj-end obj fmt strm indent)))
783 (fmt-list-end (car objs) fmt strm indent nobjs))
788 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
789 (label nil) (english-only-function nil)
790 (subobjects nil) (file-wrapper t) (refvars nil))
791 "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
792 (let ((fmt (make-format-instance format)))
794 (fmt-file-start fmt os))
796 (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
798 (fmt-file-end fmt os)))
802 (defmethod print-object ((obj hyperobject) (s stream))
803 (print-unreadable-object (obj s :type t :identity t)
804 (let ((fmt (make-instance 'hyperobject::textformat)))
806 s (funcall (hyperobject::obj-data-fmtstr fmt) obj)
808 (funcall (funcall (hyperobject::obj-data-value-func fmt) obj) obj))))))