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.4 2002/11/23 22:19:17 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 (declare (ignore rest))
218 (let* ((name (name field))
219 (print-formatter (print-formatter field))
220 (type (ho-type field))
221 (reference (reference field))
222 (namestr (symbol-name name))
223 (namestr-lower (string-downcase (symbol-name name)))
225 (plain-value-func nil)
226 html-str xml-str html-label-str xml-label-str)
227 (unless (subobject field)
228 (when (or (eql type :integer) (eql type :fixnum))
229 (setq value-fmt "~d"))
231 (when (eql type :boolean)
232 (setq value-fmt "~a"))
235 (setq first-field nil)
237 (string-append fmtstr-text " ")
238 (string-append fmtstr-html " ")
239 (string-append fmtstr-xml " ")
240 (string-append fmtstr-text-labels " ")
241 (string-append fmtstr-html-labels " ")
242 (string-append fmtstr-xml-labels " ")
243 (string-append fmtstr-html-ref " ")
244 (string-append fmtstr-xml-ref " ")
245 (string-append fmtstr-html-ref-labels " ")
246 (string-append fmtstr-xml-ref-labels " ")))
248 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
249 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
250 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
251 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
253 (string-append fmtstr-text value-fmt)
254 (string-append fmtstr-html html-str)
255 (string-append fmtstr-xml xml-str)
256 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
257 (string-append fmtstr-html-labels html-label-str)
258 (string-append fmtstr-xml-labels xml-label-str)
262 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
263 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
264 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
265 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
267 (string-append fmtstr-html-ref html-str)
268 (string-append fmtstr-xml-ref xml-str)
269 (string-append fmtstr-html-ref-labels html-label-str)
270 (string-append fmtstr-xml-ref-labels xml-label-str)))
273 (setq plain-value-func
274 (list `(,print-formatter (,(intern namestr package) x))))
275 (setq plain-value-func
276 (list `(,(intern namestr package) x))))
277 (setq value-func (append value-func plain-value-func))
279 (if (eql type :cdata)
280 (setq xml-value-func (append xml-value-func (list `(xml-cdata ,@plain-value-func))))
281 (setq xml-value-func (append xml-value-func plain-value-func)))
285 (setq value-func `(lambda (x) (values ,@value-func)))
286 (setq value-func `(lambda () (values))))
289 (setq xml-value-func `(lambda (x) (values ,@xml-value-func)))
290 (setq xml-value-func `(lambda () (values))))
292 (setf (slot-value meta 'fmtstr-text) fmtstr-text)
293 (setf (slot-value meta 'fmtstr-html) fmtstr-html)
294 (setf (slot-value meta 'fmtstr-xml) fmtstr-xml)
295 (setf (slot-value meta 'fmtstr-text-labels) fmtstr-text-labels)
296 (setf (slot-value meta 'fmtstr-html-labels) fmtstr-html-labels)
297 (setf (slot-value meta 'fmtstr-xml-labels) fmtstr-xml-labels)
298 (setf (slot-value meta 'fmtstr-html-ref) fmtstr-html-ref)
299 (setf (slot-value meta 'fmtstr-xml-ref) fmtstr-xml-ref)
300 (setf (slot-value meta 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
301 (setf (slot-value meta 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
302 (setf (slot-value meta 'value-func) value-func)
303 (setf (slot-value meta 'xml-value-func) xml-value-func))
306 (defgeneric ho-title (obj) )
307 (defgeneric ho-name (obj) )
308 (defgeneric ho-value-func (obj) )
309 (defgeneric ho-xml-value-func (obj) )
310 (defgeneric ho-fmtstr-text (obj) )
311 (defgeneric ho-fmtstr-html (obj) )
312 (defgeneric ho-fmtstr-xml (obj) )
313 (defgeneric ho-fmtstr-text-labels (obj) )
314 (defgeneric ho-fmtstr-html-labels (obj) )
315 (defgeneric ho-fmtstr-xml-labels (obj) )
316 (defgeneric ho-fmtstr-html-ref (obj) )
317 (defgeneric ho-fmtstr-xml-ref (obj) )
318 (defgeneric ho-fmtstr-html-ref-labels (obj) )
319 (defgeneric ho-fmtstr-xml-ref-labels (obj) )
320 (defgeneric ho-fields (obj) )
321 (defgeneric ho-references (obj) )
322 (defgeneric ho-subobjects (obj) )
324 (defmacro define-hyperobject (name parents fields &rest meta-fields)
325 (let* ((meta (make-instance 'hyperobject-meta))
326 (cl-fields (process-hyper-fields fields meta))
327 (title (process-title name meta-fields))
328 (documentation (process-documentation meta-fields))
329 (value-func (gensym))
330 (xml-value-func (gensym)))
331 (init-hyperobject-class name meta)
333 (eval-when (:compile-toplevel :load-toplevel :execute)
334 (defclass ,name ,(append parents (list 'hyperobject)) ,cl-fields
335 ,@(and documentation (list (list :documentation documentation)))))
336 (let ((,value-func (compile nil (eval (slot-value ,meta 'value-func))))
337 (,xml-value-func (compile nil (eval (slot-value ,meta 'xml-value-func)))))
338 (defmethod ho-title ((obj ,name))
340 (defmethod ho-name ((obj ,name))
341 ,(string-downcase (symbol-name name)))
342 (defmethod ho-fields ((obj ,name))
343 ',(slot-value meta 'fields))
344 (defmethod ho-references ((obj ,name))
345 ',(slot-value meta 'references))
346 (defmethod ho-subobjects ((obj ,name))
347 ',(slot-value meta 'subobjects))
348 (defmethod ho-value-func ((obj ,name))
350 (defmethod ho-xml-value-func ((obj ,name))
352 (defmethod ho-fmtstr-text ((obj ,name))
353 ,(slot-value meta 'fmtstr-text))
354 (defmethod ho-fmtstr-html ((obj ,name))
355 ,(slot-value meta 'fmtstr-html))
356 (defmethod ho-fmtstr-xml ((obj ,name))
357 ,(slot-value meta 'fmtstr-xml))
358 (defmethod ho-fmtstr-text-labels ((obj ,name))
359 ,(slot-value meta 'fmtstr-text-labels))
360 (defmethod ho-fmtstr-html-labels ((obj ,name))
361 ,(slot-value meta 'fmtstr-html-labels))
362 (defmethod ho-fmtstr-xml-labels ((obj ,name))
363 ,(slot-value meta 'fmtstr-xml-labels))
364 (defmethod ho-fmtstr-html-ref ((obj ,name))
365 ,(slot-value meta 'fmtstr-html-ref))
366 (defmethod ho-fmtstr-xml-ref ((obj ,name))
367 ,(slot-value meta 'fmtstr-xml-ref))
368 (defmethod ho-fmtstr-html-ref-labels ((obj ,name))
369 ,(slot-value meta 'fmtstr-html-ref-labels))
370 (defmethod ho-fmtstr-xml-ref-labels ((obj ,name))
371 ,(slot-value meta '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 hoes
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 (ho-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 #'ho-fmtstr-text
459 :obj-data-fmtstr-labels #'ho-fmtstr-text-labels
460 :obj-data-end-fmtstr "~%"
461 :obj-data-value-func #'ho-value-func))
464 (defun htmlformat-list-start-value-func (x nitems)
465 (values (ho-title x) nitems (ho-name 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 #'ho-fmtstr-html-labels
485 :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
486 :obj-data-value-func #'ho-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 #'ho-fmtstr-html-labels
506 :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
507 :obj-data-value-func #'ho-xml-value-func))
510 (defun xmlformat-list-end-value-func (x)
511 (format nil "~alist" (ho-name x)))
513 (defun xmlformat-list-start-value-func (x nitems)
514 (values (format nil "~alist" (ho-name x)) (ho-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 #'ho-name
528 :obj-end-fmtstr "</~a>~%"
529 :obj-end-value-func #'ho-name
532 :obj-data-fmtstr #'ho-fmtstr-xml
533 :obj-data-fmtstr-labels #'ho-fmtstr-xml-labels
534 :obj-data-value-func #'ho-xml-value-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 #'ho-fmtstr-html-ref
552 :fmtstr-labels #'ho-fmtstr-html-ref-labels
557 (defclass xhtml-link-ref (link-ref)
559 (:default-initargs :fmtstr #'ho-fmtstr-html-ref
560 :fmtstr-labels #'ho-fmtstr-html-ref-labels
565 (defclass xml-link-ref (link-ref)
567 (:default-initargs :fmtstr #'ho-fmtstr-xml-ref
568 :fmtstr-labels #'ho-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 (ho-references x))
708 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
709 (nth (position (name ref) (ho-fields x)
710 :key #'(lambda (x) (name x)))
712 (append (link-parameters ref) refvars)))
713 (link-end (make-link-end x (link-ref fmt) (name ref))))
714 (push link-start refvalues)
715 (push link-end refvalues)))
716 (setq refvalues (nreverse refvalues))
718 (apply #'format s refstr refvalues)))
720 (defgeneric obj-data (obj))
721 (defmethod obj-data (x)
722 "Returns the objects data as a string. Used by common-graphics outline function"
723 (let ((fmt (make-format-instance :text)))
724 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
726 (funcall (funcall (obj-data-value-func fmt) x) x)))))
728 (defgeneric make-ref-data-str (obj fmt &optional label))
729 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
730 "Return fmt string for that contains ~a slots for reference link start and end"
731 (unless (link-ref fmt)
732 (error "fmt does not contain a link-ref"))
735 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
737 (funcall (funcall (obj-data-value-func fmt) x) x)))
738 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
739 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
742 ;;; Display method for objects
745 (defgeneric load-all-subobjects (objs))
746 (defmethod load-all-subobjects (objs)
747 "Load all subobjects if they have not already been loaded."
749 (let ((objlist (mklist objs)))
750 (dolist (obj objlist)
751 (awhen (ho-subobjects obj) ;; access list of functions
752 (dolist (subobj it) ;; for each child function
753 (awhen (funcall (reader subobj) obj)
754 (load-all-subobjects it))))))
757 (defgeneric print-hyperobject-class (objs fmt strm
758 &optional label english-only-function
759 indent subobjects refvars))
761 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream)
762 &optional (label nil) (indent 0)
763 (english-only-function nil)
764 (subobjects nil) (refvars nil))
765 "Display a single or list of hyperobject-class instances and their subobjects"
767 (setq objs (mklist objs))
768 (let ((nobjs (length objs)))
769 (fmt-list-start (car objs) fmt strm indent nobjs)
771 (unless (and english-only-function
772 (multiple-value-bind (eng term) (funcall english-only-function obj)
773 (and term (not eng))))
774 (fmt-obj-start obj fmt strm indent)
775 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
777 (awhen (ho-subobjects obj) ;; access list of functions
778 (dolist (subobj it) ;; for each child function
779 (awhen (funcall (reader subobj) obj) ;; access set of child objects
780 (print-hyperobject-class it fmt strm label
781 (1+ indent) english-only-function
782 subobjects refvars)))))
783 (fmt-obj-end obj fmt strm indent)))
784 (fmt-list-end (car objs) fmt strm indent nobjs))
789 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
790 (label nil) (english-only-function nil)
791 (subobjects nil) (file-wrapper t) (refvars nil))
792 "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
793 (let ((fmt (make-format-instance format)))
795 (fmt-file-start fmt os))
797 (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
799 (fmt-file-end fmt os)))
803 (defmethod print-object ((obj hyperobject) (s stream))
804 (print-unreadable-object (obj s :type t :identity t)
805 (let ((fmt (make-instance 'hyperobject::textformat)))
807 s (funcall (hyperobject::obj-data-fmtstr fmt) obj)
809 (funcall (funcall (hyperobject::obj-data-value-func fmt) obj) obj))))))