1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: ml-class.lisp
6 ;;;; Purpose: Markup Language 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: ml-class.lisp,v 1.18 2002/10/16 22:56:07 kevin Exp $
16 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
18 ;;;; KMRCL users are granted the rights to distribute and use this software
19 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
20 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
21 ;;;; *************************************************************************
25 (declaim (optimize (speed 3) (safety 1)))
27 (defun ml-class-of (obj)
28 #-(or cmu sbcl) (class-of obj)
29 #+sbcl (sb-pcl:class-of obj)
30 #+cmu (pcl:class-of obj))
32 (defun ml-class-name (obj)
33 #-(or cmu sbcl) (class-name obj)
34 #+sbcl (sb-pcl:class-name obj)
35 #+cmu (pcl:class-name obj))
37 (defclass ml-class (#-(or cmu sbcl) standard-class
38 #+cmu pcl::standard-class
39 #+sbcl sb-pcl::standard-class)
40 ((title :initarg :title :type string :reader ml-std-title
42 "Print Title for class")
43 (fields :initarg :fields :reader ml-std-fields
45 "List of field lists for printing. Format is
46 ((fieldname type optional-formatter) ... )")
48 :initarg :subobjects-lists :reader ml-std-subobjects-lists
50 "List of fields that contain a list of subobjects objects.")
52 :initarg :ref-fields :type list :reader ml-std-ref-field
54 "List of fields that can be referred to by browsers.
55 Format is ((field-name field-lookup-func other-link-params) ...)")
57 ;;; The remainder of these fields are calculated one time
58 ;;; in finalize-inheritence.
59 (value-func :initform nil :type function :reader ml-std-value-func)
60 (xmlvalue-func :initform nil :type function :reader ml-std-xmlvalue-func)
61 (fmtstr-text :initform nil :type string :reader ml-std-fmtstr-text)
62 (fmtstr-html :initform nil :type string :reader ml-std-fmtstr-html)
63 (fmtstr-xml :initform nil :type string :reader ml-std-fmtstr-xml)
64 (fmtstr-text-labels :initform nil :type string :reader ml-std-fmtstr-text-labels)
65 (fmtstr-html-labels :initform nil :type string :reader ml-std-fmtstr-html-labels)
66 (fmtstr-xml-labels :initform nil :type string :reader ml-std-fmtstr-xml-labels)
67 (fmtstr-html-ref :initform nil :type string :reader ml-std-fmtstr-html-ref)
68 (fmtstr-xml-ref :initform nil :type string :reader ml-std-fmtstr-xml-ref)
69 (fmtstr-html-ref-labels :initform nil :type string :reader ml-std-fmtstr-html-ref-labels)
70 (fmtstr-xml-ref-labels :initform nil :type string :reader ml-std-fmtstr-xml-ref-labels)
72 (:default-initargs :title nil :fields nil :subobjects-lists nil :ref-fields nil)
73 (:documentation "Metaclass for Markup Language classes."))
76 (defmethod pcl:finalize-inheritance :after ((cl ml-class))
81 (defmethod sb-pcl:finalize-inheritance :after ((cl ml-class))
86 (defmethod pcl:validate-superclass ((class ml-class) (superclass pcl::standard-class))
90 (defmethod sb-pcl:validate-superclass ((class ml-class) (superclass sb-pcl::standard-class))
94 (defmethod mop:finalize-inheritance :after ((cl ml-class))
98 (defmethod clos:finalize-inheritance :after ((cl ml-class))
102 ;;(defmethod pcl::class-finalized-p ((cl ml-class))
103 ;; (and (not (null (slot-value cl 'pcl::wrapper)))
104 ;; (not (null (slot-value cl 'fmtstr-text)))))
107 ;;(defmethod sb-pcl::class-finalized-p ((cl ml-class))
108 ;; (and (not (null (slot-value cl 'sb-pcl::wrapper)))
109 ;; (not (null (slot-value cl 'fmtstr-text)))))
112 (defmethod clos:process-a-class-option ((class ml-class)
116 (error "ml-class title must have a value"))
117 (if (null (cdr value))
118 (list name (car value))
119 (list name `',value)))
122 (defmethod clos:process-a-class-option ((class ml-class)
126 (error "ml-class fields must have a value"))
127 (list name `',value))
130 (defmethod clos:process-a-class-option ((class ml-class)
131 (name (eql :ref-fields))
134 (error "ml-class ref-fields must have a value"))
135 (list name `',value))
138 (defmethod clos:process-a-class-option ((class ml-class)
139 (name (eql :subobjects-lists))
142 (error "ml-class subobjects-lists must have a value"))
143 (list name `',value))
145 ;;;; Class initialization function
147 (defun init-ml-class (cl)
148 (let ((fmtstr-text "")
151 (fmtstr-text-labels "")
152 (fmtstr-html-labels "")
153 (fmtstr-xml-labels "")
156 (fmtstr-html-ref-labels "")
157 (fmtstr-xml-ref-labels "")
161 (classname (class-name cl))
162 (package (symbol-package (ml-class-name cl)))
163 (ref-fields (slot-value cl 'ref-fields)))
164 (declare (ignore classname))
165 (dolist (f (slot-value cl 'fields))
167 (namestr (symbol-name (car f)))
168 (namestr-lower (string-downcase (symbol-name (car f))))
170 (formatter (caddr f))
172 (plain-value-func nil)
173 html-str xml-str html-label-str xml-label-str)
175 (when (or (eql type :integer) (eql type :fixnum))
176 (setq value-fmt "~d"))
178 (when (eql type :commainteger)
179 (setq value-fmt "~:d"))
181 (when (eql type :boolean)
182 (setq value-fmt "~a"))
185 (setq first-field nil)
187 (string-append fmtstr-text " ")
188 (string-append fmtstr-html " ")
189 (string-append fmtstr-xml " ")
190 (string-append fmtstr-text-labels " ")
191 (string-append fmtstr-html-labels " ")
192 (string-append fmtstr-xml-labels " ")
193 (string-append fmtstr-html-ref " ")
194 (string-append fmtstr-xml-ref " ")
195 (string-append fmtstr-html-ref-labels " ")
196 (string-append fmtstr-xml-ref-labels " ")))
198 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
199 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
200 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
201 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
203 (string-append fmtstr-text value-fmt)
204 (string-append fmtstr-html html-str)
205 (string-append fmtstr-xml xml-str)
206 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
207 (string-append fmtstr-html-labels html-label-str)
208 (string-append fmtstr-xml-labels xml-label-str)
210 (if (find name ref-fields :key #'car)
212 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
213 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
214 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
215 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
217 (string-append fmtstr-html-ref html-str)
218 (string-append fmtstr-xml-ref xml-str)
219 (string-append fmtstr-html-ref-labels html-label-str)
220 (string-append fmtstr-xml-ref-labels xml-label-str)))
223 (setq plain-value-func
224 (list `(,formatter (,(intern namestr package) x))))
225 (setq plain-value-func
226 (list `(,(intern namestr package) x))))
227 (setq value-func (append value-func plain-value-func))
229 (if (eql type :cdata)
230 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
231 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
235 (setq value-func `(lambda (x) (values ,@value-func)))
236 (setq value-func `(lambda () (values))))
237 (setq value-func (compile nil (eval value-func)))
240 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
241 (setq xmlvalue-func `(lambda () (values))))
242 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
244 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
245 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
246 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
247 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
248 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
249 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
250 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
251 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
252 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
253 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
254 (setf (slot-value cl 'value-func) value-func)
255 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
259 (defun ml-class-fmtstr-text (obj)
260 (slot-value (ml-class-of obj) 'fmtstr-text))
262 (defun ml-class-fmtstr-html (obj)
263 (slot-value (ml-class-of obj) 'fmtstr-html))
265 (defun ml-class-fmtstr-xml (obj)
266 (slot-value (ml-class-of obj) 'fmtstr-xml))
268 (defun ml-class-fmtstr-text-labels (obj)
269 (slot-value (ml-class-of obj) 'fmtstr-text-labels))
271 (defun ml-class-fmtstr-html-labels (obj)
272 (slot-value (ml-class-of obj) 'fmtstr-html-labels))
274 (defun ml-class-fmtstr-xml-labels (obj)
275 (slot-value (ml-class-of obj) 'fmtstr-xml-labels))
277 (defun ml-class-value-func (obj)
278 (slot-value (ml-class-of obj) 'value-func))
280 (defun ml-class-xmlvalue-func (obj)
281 (slot-value (ml-class-of obj) 'xmlvalue-func))
283 (eval-when (:compile-toplevel :load-toplevel :execute)
284 (defun ml-class-title (obj)
285 (awhen (slot-value (ml-class-of obj) 'title)
290 (defun ml-class-subobjects-lists (obj)
291 (slot-value (ml-class-of obj) 'subobjects-lists))
293 (defun ml-class-ref-fields (obj)
294 (slot-value (ml-class-of obj) 'ref-fields))
296 (defun ml-class-fields (obj)
297 (slot-value (ml-class-of obj) 'fields))
299 (defun ml-class-fmtstr-html-ref (obj)
300 (slot-value (ml-class-of obj) 'fmtstr-html-ref))
302 (defun ml-class-fmtstr-xml-ref (obj)
303 (slot-value (ml-class-of obj) 'fmtstr-xml-ref))
305 (defun ml-class-fmtstr-html-ref-labels (obj)
306 (slot-value (ml-class-of obj) 'fmtstr-html-ref-labels))
308 (defun ml-class-fmtstr-xml-ref-labels (obj)
309 (slot-value (ml-class-of obj) 'fmtstr-xml-ref-labels))
311 ;;; Class name functions
313 (defgeneric ml-class-stdname (x))
314 (defmethod ml-class-stdname ((name string))
315 (string-downcase (subseq name 1)))
317 (defmethod ml-class-stdname ((cl standard-object))
318 (string-downcase (subseq (ml-class-name (ml-class-of cl)) 1)))
320 ;;;; Generic Print functions
322 (defparameter *default-textformat* nil)
323 (defparameter *default-htmlformat* nil)
324 (defparameter *default-htmlrefformat* nil)
325 (defparameter *default-xmlformat* nil)
326 (defparameter *default-xmlrefformat* nil)
327 (defparameter *default-ie-xmlrefformat* nil)
328 (defparameter *default-nullformat* nil)
329 (defparameter *default-init-format?* nil)
331 (defun make-format-instance (fmt)
332 (unless *default-init-format?*
333 (setq *default-textformat* (make-instance 'textformat))
334 (setq *default-htmlformat* (make-instance 'htmlformat))
335 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
336 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
337 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
338 (setq *default-xmlformat* (make-instance 'xmlformat))
339 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
340 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
341 (setq *default-nullformat* (make-instance 'nullformat))
342 (setq *default-init-format?* t))
345 (:text *default-textformat*)
346 (:html *default-htmlformat*)
347 (:htmlref *default-htmlrefformat*)
348 (:xhtml *default-xhtmlformat*)
349 (:xhtmlref *default-xhtmlrefformat*)
350 (:xml *default-xmlformat*)
351 (:xmlref *default-xmlrefformat*)
352 (:ie-xmlref *default-ie-xmlrefformat*)
353 (:null *default-nullformat*)
354 (otherwise *default-textformat*)))
356 ;;;; Output format classes for print ml-classes
358 (defclass dataformat ()
359 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
360 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
361 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
362 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
363 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
364 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
365 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
366 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
367 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
368 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
369 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
370 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
371 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
372 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
373 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
374 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
375 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
376 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
377 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
378 (link-ref :initarg :link-ref :reader link-ref))
379 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
380 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
381 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
382 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
383 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
384 :obj-data-value-func nil :link-ref nil)
385 (:documentation "Parent for all dataformat objects"))
387 (defclass binaryformat (dataformat)
390 (defclass nullformat (dataformat)
393 (defun text-list-start-value-func (obj nitems)
394 (values (ml-class-title obj) nitems))
396 (defclass textformat (dataformat)
398 (:default-initargs :list-start-fmtstr "~a~P:~%"
399 :list-start-value-func #'text-list-start-value-func
402 :obj-data-fmtstr #'ml-class-fmtstr-text
403 :obj-data-fmtstr-labels #'ml-class-fmtstr-text-labels
404 :obj-data-end-fmtstr "~%"
405 :obj-data-value-func #'ml-class-value-func))
408 (defun class-name-of (obj)
409 (string-downcase (ml-class-name (ml-class-of obj))))
411 (defun htmlformat-list-start-value-func (x nitems)
412 (values (ml-class-title x) nitems (class-name-of x)))
414 (defclass htmlformat (textformat)
416 (:default-initargs :file-start-str "<html><body>~%"
417 :file-end-str "</body><html>~%"
419 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
420 :list-start-value-func #'htmlformat-list-start-value-func
421 :list-end-fmtstr "</ul></div>~%"
423 :list-end-value-func #'identity
425 :obj-start-fmtstr "<li>"
426 :obj-start-value-func #'identity
428 :obj-end-fmtstr "</li>~%"
429 :obj-end-value-func #'identity
431 :obj-data-fmtstr #'ml-class-fmtstr-html-labels
432 :obj-data-fmtstr-labels #'ml-class-fmtstr-html-labels
433 :obj-data-value-func #'ml-class-value-func))
435 (defclass xhtmlformat (textformat)
437 (:default-initargs :file-start-str "<html><body>~%"
438 :file-end-str "</body><html>~%"
440 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
441 :list-start-value-func #'htmlformat-list-start-value-func
442 :list-end-fmtstr "</ul></div>~%"
444 :list-end-value-func #'identity
446 :obj-start-fmtstr "<li>"
447 :obj-start-value-func #'identity
449 :obj-end-fmtstr "</li>~%"
450 :obj-end-value-func #'identity
452 :obj-data-fmtstr #'ml-class-fmtstr-html-labels
453 :obj-data-fmtstr-labels #'ml-class-fmtstr-html-labels
454 :obj-data-value-func #'ml-class-xmlvalue-func))
457 (defun xmlformat-list-end-value-func (x)
458 (format nil "~alist" (class-name-of x)))
460 (defun xmlformat-list-start-value-func (x nitems)
461 (values (format nil "~alist" (class-name-of x)) (ml-class-title x) nitems))
463 (defclass xmlformat (textformat)
465 (:default-initargs :file-start-str "" ; (std-xml-header)
467 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
468 :list-start-value-func #'xmlformat-list-start-value-func
470 :list-end-fmtstr "</~a>~%"
471 :list-end-value-func #'xmlformat-list-end-value-func
472 :obj-start-fmtstr "<~a>"
473 :obj-start-value-func #'class-name-of
475 :obj-end-fmtstr "</~a>~%"
476 :obj-end-value-func #'class-name-of
479 :obj-data-fmtstr #'ml-class-fmtstr-xml
480 :obj-data-fmtstr-labels #'ml-class-fmtstr-xml-labels
481 :obj-data-value-func #'ml-class-xmlvalue-func))
483 (defclass link-ref ()
484 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
485 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
486 (page-name :type string :initarg :page-name :accessor page-name)
487 (href-head :type string :initarg :href-head :accessor href-head)
488 (href-end :type string :initarg :href-end :accessor href-end)
489 (ampersand :type string :initarg :ampersand :accessor ampersand))
490 (:default-initargs :fmtstr nil
492 :page-name "disp-func1"
493 :href-head nil :href-end nil :ampersand nil)
494 (:documentation "Formatting for a linked reference"))
496 (defclass html-link-ref (link-ref)
498 (:default-initargs :fmtstr #'ml-class-fmtstr-html-ref
499 :fmtstr-labels #'ml-class-fmtstr-html-ref-labels
504 (defclass xhtml-link-ref (link-ref)
506 (:default-initargs :fmtstr #'ml-class-fmtstr-html-ref
507 :fmtstr-labels #'ml-class-fmtstr-html-ref-labels
512 (defclass xml-link-ref (link-ref)
514 (:default-initargs :fmtstr #'ml-class-fmtstr-xml-ref
515 :fmtstr-labels #'ml-class-fmtstr-xml-ref-labels
516 :href-head "xmllink xlink:type=\"simple\" xlink:href="
519 (:documentation "Mozilla's and W3's idea of a link with XML"))
521 (defclass ie-xml-link-ref (xml-link-ref)
523 (:default-initargs :href-head "html:a href="
525 (:documentation "Internet Explorer's idea of a link with XML"))
528 (defclass htmlrefformat (htmlformat)
530 (:default-initargs :link-ref (make-instance 'html-link-ref)))
532 (defclass xhtmlrefformat (xhtmlformat)
534 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
536 (defclass xmlrefformat (xmlformat)
538 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
540 (defclass ie-xmlrefformat (xmlformat)
542 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
545 ;;; File Start and Ends
547 (defgeneric fmt-file-start (fmt s))
548 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
550 (defmethod fmt-file-start ((fmt textformat) (s stream))
551 (aif (file-start-str fmt)
554 (defgeneric fmt-file-end (fmt s))
555 (defmethod fmt-file-end ((fmt textformat) (s stream))
556 (aif (file-end-str fmt)
559 ;;; List Start and Ends
561 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
562 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
563 (if (list-start-indent fmt)
564 (indent-spaces indent s))
565 (aif (list-start-fmtstr fmt)
568 (funcall (list-start-value-func fmt) x num-items)))))
570 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
571 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
572 (declare (ignore num-items))
573 (if (list-end-indent fmt)
574 (indent-spaces indent s))
575 (aif (list-end-fmtstr fmt)
578 (funcall (list-end-value-func fmt) x)))))
580 ;;; Object Start and Ends
582 (defgeneric fmt-obj-start (obj fmt s &optional indent))
583 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
584 (if (obj-start-indent fmt)
585 (indent-spaces indent s))
586 (aif (obj-start-fmtstr fmt)
589 (funcall (obj-start-value-func fmt) x)))))
591 (defgeneric fmt-obj-end (obj fmt s &optional indent))
592 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
593 (if (obj-end-indent fmt)
594 (indent-spaces indent s))
595 (aif (obj-end-fmtstr fmt)
598 (funcall (obj-end-value-func fmt) x)))))
602 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
603 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
604 (declare (ignore obj fieldname))
605 (format nil "~a\"~a?func=~a~akey=~a~a\""
606 (href-head ref) (make-url (page-name ref)) fieldfunc
607 (ampersand ref) fieldvalue
610 (dolist (var refvars)
611 (string-append varstr (format nil "~a~a=~a"
612 (ampersand ref) (car var) (cadr var))))
616 (defgeneric make-link-end (obj ref fieldname))
617 (defmethod make-link-end (obj (ref link-ref) fieldname)
618 (declare (ignore obj fieldname))
619 (format nil "~a" (href-end ref))
622 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
623 (defmethod fmt-obj-data (x (fmt textformat) s
624 &optional (indent 0) (label nil) (refvars nil))
625 (if (obj-data-indent fmt)
626 (indent-spaces indent s))
628 (fmt-obj-data-with-ref x fmt s label refvars)
629 (fmt-obj-data-plain x fmt s label))
630 (aif (obj-data-end-fmtstr fmt)
633 (defgeneric fmt-obj-data-plain (obj fmt s label))
634 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
637 (funcall (obj-data-fmtstr-labels fmt) x)
639 (funcall (funcall (obj-data-value-func fmt) x) x)))
640 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
642 (funcall (funcall (obj-data-value-func fmt) x) x)))))
644 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
645 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
646 (let ((refstr (make-ref-data-str x fmt label))
650 (funcall (funcall (obj-data-value-func fmt) x) x))))
652 ;; make list of reference link fields for printing to refstr template
653 (dolist (field (ml-class-ref-fields x))
655 (make-link-start x (link-ref fmt) (car field) (cadr field)
656 (nth (position (car field) (ml-class-fields x) :key #'car) field-values)
657 (append (caddr field) refvars)))
658 (link-end (make-link-end x (link-ref fmt) (car field))))
659 (push link-start refvalues)
660 (push link-end refvalues)))
661 (setq refvalues (nreverse refvalues))
663 (apply #'format s refstr refvalues)))
665 (defgeneric obj-data (obj))
666 (defmethod obj-data (x)
667 "Returns the objects data as a string. Used by common-graphics outline function"
668 (let ((fmt (make-format-instance :text)))
669 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
671 (funcall (funcall (obj-data-value-func fmt) x) x)))))
673 (defgeneric make-ref-data-str (obj fmt &optional label))
674 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
675 "Return fmt string for that contains ~a slots for reference link start and end"
676 (unless (link-ref fmt)
677 (error "fmt does not contain a link-ref"))
680 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
682 (funcall (funcall (obj-data-value-func fmt) x) x)))
683 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
684 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
687 ;;; Display method for objects
690 (defgeneric load-all-subobjects (objs))
691 (defmethod load-all-subobjects (objs)
692 "Load all subobjects if they have not already been loaded."
694 (let ((objlist (mklist objs)))
695 (dolist (obj objlist)
696 (awhen (ml-class-subobjects-lists obj) ;; access list of functions
697 (dolist (child-obj it) ;; for each child function
698 (awhen (funcall (car child-obj) obj)
699 (load-all-subobjects it))))))
702 (defgeneric output-ml-class (objs fmt strm &optional label english-only-function indent subobjects refvars))
703 (defmethod output-ml-class (objs (fmt dataformat) (strm stream)
704 &optional (label nil) (english-only-function nil)
705 (indent 0) (subobjects nil) (refvars nil))
706 "Display a single or list of ml-class instances and their subobjects"
708 (setq objs (mklist objs))
709 (let ((nobjs (length objs)))
710 (fmt-list-start (car objs) fmt strm indent nobjs)
712 (unless (and english-only-function
713 (multiple-value-bind (eng term) (funcall english-only-function obj)
714 (and term (not eng))))
715 (fmt-obj-start obj fmt strm indent)
716 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
718 (awhen (ml-class-subobjects-lists obj) ;; access list of functions
719 (dolist (child-obj it) ;; for each child function
720 (awhen (funcall (car child-obj) obj) ;; access set of child objects
721 (output-ml-class it fmt strm label
722 english-only-function
723 (1+ indent) subobjects refvars)))))
724 (fmt-obj-end obj fmt strm indent)))
725 (fmt-list-end (car objs) fmt strm indent nobjs))
728 (defun display-ml-class (objs &key (os *standard-output*) (format :text)
729 (label nil) (english-only-function nil) (subobjects nil)
730 (file-wrapper t) (refvars nil))
731 "EXPORTED Function: displays a ml-class. Simplies call to output-ml-class"
732 (let ((fmt (make-format-instance format)))
734 (fmt-file-start fmt os))
736 (output-ml-class objs fmt os label english-only-function 0 subobjects refvars))
738 (fmt-file-end fmt os)))