3 ;;;; A docstring extractor for the sbcl manual. Creates
4 ;;;; @include-ready documentation from the docstrings of exported
5 ;;;; symbols of specified packages.
7 ;;;; This software is part of the SBCL software system. SBCL is in the
8 ;;;; public domain and is provided with absolutely no warranty. See
9 ;;;; the COPYING file for more information.
11 ;;;; Written by Rudi Schlatte <rudi@constantly.at>, mangled
12 ;;;; by Nikodemus Siivola.
17 ;;;; * Method documentation untested
18 ;;;; * Method sorting, somehow
19 ;;;; * Index for macros & constants?
20 ;;;; * This is getting complicated enough that tests would be good
21 ;;;; * Nesting (currently only nested itemizations work)
22 ;;;; * doc -> internal form -> texinfo (so that non-texinfo format are also
23 ;;;; easily generated)
25 ;;;; FIXME: The description below is no longer complete. This
26 ;;;; should possibly be turned into a contrib with proper documentation.
28 ;;;; Formatting heuristics (tweaked to format SAVE-LISP-AND-DIE sanely):
30 ;;;; Formats SYMBOL as @code{symbol}, or @var{symbol} if symbol is in
31 ;;;; the argument list of the defun / defmacro.
33 ;;;; Lines starting with * or - that are followed by intented lines
34 ;;;; are marked up with @itemize.
36 ;;;; Lines containing only a SYMBOL that are followed by indented
37 ;;;; lines are marked up as @table @code, with the SYMBOL as the item.
39 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
40 (require 'sb-introspect
))
42 (defpackage :sb-texinfo
44 (:shadow
#:documentation
)
45 (:export
#:generate-includes
#:document-package
)
47 "Tools to generate TexInfo documentation from docstrings."))
49 (in-package :sb-texinfo
)
51 ;;;; various specials and parameters
53 (defvar *texinfo-output
*)
54 (defvar *texinfo-variables
*)
55 (defvar *documentation-package
*)
57 (defparameter *undocumented-packages
* '(sb-pcl sb-int sb-kernel sb-sys sb-c
))
59 (defparameter *documentation-types
*
64 ;;structure ; also handled by `type'
67 "A list of symbols accepted as second argument of `documentation'")
69 (defparameter *character-replacements
*
70 '((#\
* .
"star") (#\
/ .
"slash") (#\
+ .
"plus")
71 (#\
< .
"lt") (#\
> .
"gt"))
72 "Characters and their replacement names that `alphanumize' uses. If
73 the replacements contain any of the chars they're supposed to replace,
74 you deserve to lose.")
76 (defparameter *characters-to-drop
* '(#\\ #\
` #\')
77 "Characters that should be removed by `alphanumize'.")
79 (defparameter *texinfo-escaped-chars
* "@{}"
80 "Characters that must be escaped with #\@ for Texinfo.")
82 (defparameter *itemize-start-characters
* '(#\
* #\-
)
83 "Characters that might start an itemization in docstrings when
84 at the start of a line.")
86 (defparameter *symbol-characters
* "ABCDEFGHIJKLMNOPQRSTUVWXYZ*:-+&#'"
87 "List of characters that make up symbols in a docstring.")
89 (defparameter *symbol-delimiters
* " ,.!?;")
91 (defparameter *ordered-documentation-kinds
*
92 '(package type structure condition class macro
))
100 (nconc (flatten (car list
)) (flatten (cdr list
))))
102 (cons (car list
) nil
))
104 (cons (car list
) (flatten (cdr list
))))))
106 (defun whitespacep (char)
107 (find char
#(#\tab
#\space
#\page
)))
109 (defun setf-name-p (name)
111 (and (listp name
) (= 2 (length name
)) (eq (car name
) 'setf
))))
113 (defgeneric specializer-name
(specializer))
115 (defmethod specializer-name ((specializer eql-specializer
))
116 (list 'eql
(eql-specializer-object specializer
)))
118 (defmethod specializer-name ((specializer class
))
119 (class-name specializer
))
121 (defun ensure-class-precedence-list (class)
122 (unless (class-finalized-p class
)
123 (finalize-inheritance class
))
124 (class-precedence-list class
))
126 (defun specialized-lambda-list (method)
127 ;; courtecy of AMOP p. 61
128 (let* ((specializers (method-specializers method
))
129 (lambda-list (method-lambda-list method
))
130 (n-required (length specializers
)))
131 (append (mapcar (lambda (arg specializer
)
132 (if (eq specializer
(find-class 't
))
134 `(,arg
,(specializer-name specializer
))))
135 (subseq lambda-list
0 n-required
)
137 (subseq lambda-list n-required
))))
139 (defun string-lines (string)
140 "Lines in STRING as a vector."
141 (coerce (with-input-from-string (s string
)
142 (loop for line
= (read-line s nil nil
)
143 while line collect line
))
146 (defun indentation (line)
147 "Position of first non-SPACE character in LINE."
148 (position-if-not (lambda (c) (char= c
#\Space
)) line
))
150 (defun docstring (x doc-type
)
151 (cl:documentation x doc-type
))
153 (defun flatten-to-string (list)
154 (format nil
"~{~A~^-~}" (flatten list
)))
156 (defun alphanumize (original)
157 "Construct a string without characters like *`' that will f-star-ck
158 up filename handling. See `*character-replacements*' and
159 `*characters-to-drop*' for customization."
160 (let ((name (remove-if (lambda (x) (member x
*characters-to-drop
*))
162 (flatten-to-string original
)
164 (chars-to-replace (mapcar #'car
*character-replacements
*)))
165 (flet ((replacement-delimiter (index)
166 (cond ((or (< index
0) (>= index
(length name
))) "")
167 ((alphanumericp (char name index
)) "-")
169 (loop for index
= (position-if #'(lambda (x) (member x chars-to-replace
))
172 do
(setf name
(concatenate 'string
(subseq name
0 index
)
173 (replacement-delimiter (1- index
))
174 (cdr (assoc (aref name index
)
175 *character-replacements
*))
176 (replacement-delimiter (1+ index
))
177 (subseq name
(1+ index
))))))
180 ;;;; generating various names
182 (defgeneric name
(thing)
183 (:documentation
"Name for a documented thing. Names are either
184 symbols or lists of symbols."))
186 (defmethod name ((symbol symbol
))
189 (defmethod name ((cons cons
))
192 (defmethod name ((package package
))
193 (package-name package
))
195 (defmethod name ((method method
))
197 (generic-function-name (method-generic-function method
))
198 (method-qualifiers method
)
199 (specialized-lambda-list method
)))
201 ;;; Node names for DOCUMENTATION instances
203 (defgeneric name-using-kind
/name
(kind name doc
))
205 (defmethod name-using-kind/name
(kind (name string
) doc
)
206 (declare (ignore kind doc
))
209 (defmethod name-using-kind/name
(kind (name symbol
) doc
)
210 (declare (ignore kind
))
211 (format nil
"~A:~A" (package-name (get-package doc
)) name
))
213 (defmethod name-using-kind/name
(kind (name list
) doc
)
214 (declare (ignore kind
))
215 (assert (setf-name-p name
))
216 (format nil
"(setf ~A:~A)" (package-name (get-package doc
)) (second name
)))
218 (defmethod name-using-kind/name
((kind (eql 'method
)) name doc
)
219 (format nil
"~A~{ ~A~} ~A"
220 (name-using-kind/name nil
(first name
) doc
)
224 (defun node-name (doc)
225 "Returns TexInfo node name as a string for a DOCUMENTATION instance."
226 (let ((kind (get-kind doc
)))
227 (format nil
"~:(~A~) ~(~A~)" kind
(name-using-kind/name kind
(get-name doc
) doc
))))
229 (defun short-package-name (package)
230 (car (sort (copy-list (cons (package-name package
) (package-nicknames package
)))
233 ;;; Definition titles for DOCUMENTATION instances
235 (defgeneric title-using-kind
/name
(kind name doc
))
237 (defmethod title-using-kind/name
(kind (name string
) doc
)
238 (declare (ignore kind doc
))
241 (defmethod title-using-kind/name
(kind (name symbol
) doc
)
242 (declare (ignore kind
))
243 (format nil
"~A:~A" (short-package-name (get-package doc
)) name
))
245 (defmethod title-using-kind/name
(kind (name list
) doc
)
246 (declare (ignore kind
))
247 (assert (setf-name-p name
))
248 (format nil
"(setf ~A:~A)" (short-package-name (get-package doc
)) (second name
)))
250 (defmethod title-using-kind/name
((kind (eql 'method
)) name doc
)
251 (format nil
"~{~A ~}~A"
253 (title-using-kind/name nil
(first name
) doc
)))
255 (defun title-name (doc)
256 "Returns a string to be used as name of the definition."
257 (string-downcase (title-using-kind/name
(get-kind doc
) (get-name doc
) doc
)))
259 (defun include-pathname (doc)
260 (let* ((kind (get-kind doc
))
261 (name (nstring-downcase
262 (if (eq 'package kind
)
263 (format nil
"package-~A" (alphanumize (get-name doc
)))
264 (format nil
"~A-~A-~A"
266 ((function generic-function
) "fun")
269 (otherwise (symbol-name (get-kind doc
))))
270 (alphanumize (package-name (get-package doc
)))
271 (alphanumize (get-name doc
)))))))
272 (make-pathname :name name
:type
"texinfo")))
274 ;;;; documentation class and related methods
276 (defclass documentation
()
277 ((name :initarg
:name
:reader get-name
)
278 (kind :initarg
:kind
:reader get-kind
)
279 (string :initarg
:string
:reader get-string
)
280 (children :initarg
:children
:initform nil
:reader get-children
)
281 (package :initform
*documentation-package
* :reader get-package
)))
283 (defmethod print-object ((documentation documentation
) stream
)
284 (print-unreadable-object (documentation stream
:type t
)
285 (princ (list (get-kind documentation
) (get-name documentation
)) stream
)))
287 (defgeneric make-documentation
(x doc-type string
))
289 (defmethod make-documentation ((x package
) doc-type string
)
290 (declare (ignore doc-type
))
291 (make-instance 'documentation
296 (defmethod make-documentation (x (doc-type (eql 'function
)) string
)
297 (declare (ignore doc-type
))
298 (let* ((fdef (and (fboundp x
) (fdefinition x
)))
300 (kind (cond ((and (symbolp x
) (special-operator-p x
))
302 ((and (symbolp x
) (macro-function x
))
304 ((typep fdef
'generic-function
)
305 (assert (or (symbolp name
) (setf-name-p name
)))
308 (assert (or (symbolp name
) (setf-name-p name
)))
310 (children (when (eq kind
'generic-function
)
311 (collect-gf-documentation fdef
))))
312 (make-instance 'documentation
316 :children children
)))
318 (defmethod make-documentation ((x method
) doc-type string
)
319 (declare (ignore doc-type
))
320 (make-instance 'documentation
325 (defmethod make-documentation (x (doc-type (eql 'type
)) string
)
326 (make-instance 'documentation
329 :kind
(etypecase (find-class x nil
)
330 (structure-class 'structure
)
331 (standard-class 'class
)
332 (sb-pcl::condition-class
'condition
)
333 ((or built-in-class null
) 'type
))))
335 (defmethod make-documentation (x (doc-type (eql 'variable
)) string
)
336 (make-instance 'documentation
339 :kind
(if (constantp x
)
343 (defmethod make-documentation (x (doc-type (eql 'setf
)) string
)
344 (declare (ignore doc-type
))
345 (make-instance 'documentation
350 (defmethod make-documentation (x doc-type string
)
351 (make-instance 'documentation
356 (defun maybe-documentation (x doc-type
)
357 "Returns a DOCUMENTATION instance for X and DOC-TYPE, or NIL if
358 there is no corresponding docstring."
359 (let ((docstring (docstring x doc-type
)))
361 (make-documentation x doc-type docstring
))))
363 (defun lambda-list (doc)
365 ((package constant variable type structure class condition nil
)
368 (third (get-name doc
)))
372 ;; believe it or not, the above comment was written before CSR
373 ;; came along and obfuscated this. (2005-07-04)
374 (when (symbolp (get-name doc
))
375 (labels ((clean (x &key optional key
)
378 ((cons (member &optional
))
379 (cons (car x
) (clean (cdr x
) :optional t
)))
380 ((cons (member &key
))
381 (cons (car x
) (clean (cdr x
) :key t
)))
384 (cond (key (if (consp (caar x
))
389 (clean (cdr x
) :key key
:optional optional
)))
392 (cond ((or key optional
) (car x
))
394 (clean (cdr x
) :key key
:optional optional
))))))
395 (clean (sb-introspect:function-lambda-list
(get-name doc
))))))))
397 (defun get-string-name (x)
398 (let ((name (get-name x
)))
399 (cond ((symbolp name
)
401 ((and (consp name
) (eq 'setf
(car name
)))
402 (symbol-name (second name
)))
406 (error "Don't know which symbol to use for name ~S" name
)))))
408 (defun documentation< (x y
)
409 (let ((p1 (position (get-kind x
) *ordered-documentation-kinds
*))
410 (p2 (position (get-kind y
) *ordered-documentation-kinds
*)))
411 (if (or (not (and p1 p2
)) (= p1 p2
))
412 (string< (get-string-name x
) (get-string-name y
))
415 ;;;; turning text into texinfo
417 (defun escape-for-texinfo (string &optional downcasep
)
418 "Return STRING with characters in *TEXINFO-ESCAPED-CHARS* escaped
419 with #\@. Optionally downcase the result."
420 (let ((result (with-output-to-string (s)
421 (loop for char across string
422 when
(find char
*texinfo-escaped-chars
*)
423 do
(write-char #\
@ s
)
424 do
(write-char char s
)))))
425 (if downcasep
(nstring-downcase result
) result
)))
427 (defun empty-p (line-number lines
)
428 (and (< -
1 line-number
(length lines
))
429 (not (indentation (svref lines line-number
)))))
433 (defvar *not-symbols
* '("ANSI" "CLHS"))
435 (defun locate-symbols (line)
436 "Return a list of index pairs of symbol-like parts of LINE."
437 ;; This would be a good application for a regex ...
439 (flet ((grab (start end
)
440 (unless (member (subseq line start end
) '("ANSI" "CLHS"))
441 (push (list start end
) result
))))
446 ;; symbol at end of line
447 (when (and begin
(or (> i
(1+ begin
))
448 (not (member (char line begin
) '(#\A
#\I
)))))
452 ((and begin
(find (char line i
) *symbol-delimiters
*))
453 ;; symbol end; remember it if it's not "A" or "I"
454 (when (or (> i
(1+ begin
)) (not (member (char line begin
) '(#\A
#\I
))))
458 ((and begin
(not (find (char line i
) *symbol-characters
*)))
459 ;; Not a symbol: abort
461 ((and maybe-begin
(not begin
) (find (char line i
) *symbol-characters
*))
462 ;; potential symbol begin at this position
465 ((find (char line i
) *symbol-delimiters
*)
466 ;; potential symbol begin after this position
467 (setf maybe-begin t
))
469 ;; Not reading a symbol, not at potential start of symbol
470 (setf maybe-begin nil
)))))))
472 (defun texinfo-line (line)
473 "Format symbols in LINE texinfo-style: either as code or as
474 variables if the symbol in question is contained in symbols
475 *TEXINFO-VARIABLES*."
476 (with-output-to-string (result)
478 (dolist (symbol/index
(locate-symbols line
))
479 (write-string (subseq line last
(first symbol
/index
)) result
)
480 (let ((symbol-name (apply #'subseq line symbol
/index
)))
481 (format result
(if (member symbol-name
*texinfo-variables
*
485 (string-downcase symbol-name
)))
486 (setf last
(second symbol
/index
)))
487 (write-string (subseq line last
) result
))))
491 (defun lisp-section-p (line line-number lines
)
492 "Returns T if the given LINE looks like start of lisp code --
493 ie. if it starts with whitespace followed by a paren or
494 semicolon, and the previous line is empty"
495 (let ((offset (indentation line
)))
498 (find (find-if-not #'whitespacep line
) "(;")
499 (empty-p (1- line-number
) lines
))))
501 (defun collect-lisp-section (lines line-number
)
502 (let ((lisp (loop for index
= line-number then
(1+ index
)
503 for line
= (and (< index
(length lines
)) (svref lines index
))
504 while
(indentation line
)
506 (values (length lisp
) `("@lisp" ,@lisp
"@end lisp"))))
508 ;;; itemized sections
510 (defun maybe-itemize-offset (line)
511 "Return NIL or the indentation offset if LINE looks like it starts
512 an item in an itemization."
513 (let* ((offset (indentation line
))
514 (char (when offset
(char line offset
))))
516 (member char
*itemize-start-characters
* :test
#'char
=)
517 (char= #\Space
(find-if-not (lambda (c) (char= c char
))
521 (defun collect-maybe-itemized-section (lines starting-line
)
522 ;; Return index of next line to be processed outside
523 (let ((this-offset (maybe-itemize-offset (svref lines starting-line
)))
526 (loop for line-number from starting-line below
(length lines
)
527 for line
= (svref lines line-number
)
528 for indentation
= (indentation line
)
529 for offset
= (maybe-itemize-offset line
)
532 ;; empty line -- inserts paragraph.
534 (incf lines-consumed
))
535 ((and offset
(> indentation this-offset
))
536 ;; nested itemization -- handle recursively
537 ;; FIXME: tables in itemizations go wrong
538 (multiple-value-bind (sub-lines-consumed sub-itemization
)
539 (collect-maybe-itemized-section lines line-number
)
540 (when sub-lines-consumed
541 (incf line-number
(1- sub-lines-consumed
)) ; +1 on next loop
542 (incf lines-consumed sub-lines-consumed
)
543 (setf result
(nconc (nreverse sub-itemization
) result
)))))
544 ((and offset
(= indentation this-offset
))
546 (push (format nil
"@item ~A"
547 (texinfo-line (subseq line
(1+ offset
))))
549 (incf lines-consumed
))
550 ((and (not offset
) (> indentation this-offset
))
551 ;; continued item from previous line
552 (push (texinfo-line line
) result
)
553 (incf lines-consumed
))
555 ;; end of itemization
557 ;; a single-line itemization isn't.
558 (if (> (count-if (lambda (line) (> (length line
) 0)) result
) 1)
559 (values lines-consumed
`("@itemize" ,@(reverse result
) "@end itemize"))
564 (defun tabulation-body-p (offset line-number lines
)
565 (when (< line-number
(length lines
))
566 (let ((offset2 (indentation (svref lines line-number
))))
567 (and offset2
(< offset offset2
)))))
569 (defun tabulation-p (offset line-number lines direction
)
570 (let ((step (ecase direction
571 (:backwards
(1- line-number
))
572 (:forwards
(1+ line-number
)))))
573 (when (and (plusp line-number
) (< line-number
(length lines
)))
574 (and (eql offset
(indentation (svref lines line-number
)))
575 (or (when (eq direction
:backwards
)
576 (empty-p step lines
))
577 (tabulation-p offset step lines direction
)
578 (tabulation-body-p offset step lines
))))))
580 (defun maybe-table-offset (line-number lines
)
581 "Return NIL or the indentation offset if LINE looks like it starts
582 an item in a tabulation. Ie, if it is (1) indented, (2) preceded by an
583 empty line, another tabulation label, or a tabulation body, (3) and
584 followed another tabulation label or a tabulation body."
585 (let* ((line (svref lines line-number
))
586 (offset (indentation line
))
587 (prev (1- line-number
))
588 (next (1+ line-number
)))
589 (when (and offset
(plusp offset
))
590 (and (or (empty-p prev lines
)
591 (tabulation-body-p offset prev lines
)
592 (tabulation-p offset prev lines
:backwards
))
593 (or (tabulation-body-p offset next lines
)
594 (tabulation-p offset next lines
:forwards
))
597 ;;; FIXME: This and itemization are very similar: could they share
598 ;;; some code, mayhap?
600 (defun collect-maybe-table-section (lines starting-line
)
601 ;; Return index of next line to be processed outside
602 (let ((this-offset (maybe-table-offset starting-line lines
))
605 (loop for line-number from starting-line below
(length lines
)
606 for line
= (svref lines line-number
)
607 for indentation
= (indentation line
)
608 for offset
= (maybe-table-offset line-number lines
)
611 ;; empty line -- inserts paragraph.
613 (incf lines-consumed
))
614 ((and offset
(= indentation this-offset
))
615 ;; start of new item, or continuation of previous item
616 (if (and result
(search "@item" (car result
) :test
#'char
=))
617 (push (format nil
"@itemx ~A" (texinfo-line line
))
621 (push (format nil
"@item ~A" (texinfo-line line
))
623 (incf lines-consumed
))
624 ((> indentation this-offset
)
625 ;; continued item from previous line
626 (push (texinfo-line line
) result
)
627 (incf lines-consumed
))
629 ;; end of itemization
631 ;; a single-line table isn't.
632 (if (> (count-if (lambda (line) (> (length line
) 0)) result
) 1)
633 (values lines-consumed
634 `("" "@table @emph" ,@(reverse result
) "@end table" ""))
639 (defmacro with-maybe-section
(index &rest forms
)
640 `(multiple-value-bind (count collected
) (progn ,@forms
)
642 (dolist (line collected
)
643 (write-line line
*texinfo-output
*))
644 (incf ,index
(1- count
)))))
646 (defun write-texinfo-string (string &optional lambda-list
)
647 "Try to guess as much formatting for a raw docstring as possible."
648 (let ((*texinfo-variables
* (flatten lambda-list
))
649 (lines (string-lines (escape-for-texinfo string nil
))))
650 (loop for line-number from
0 below
(length lines
)
651 for line
= (svref lines line-number
)
653 ((with-maybe-section line-number
654 (and (lisp-section-p line line-number lines
)
655 (collect-lisp-section lines line-number
))))
656 ((with-maybe-section line-number
657 (and (maybe-itemize-offset line
)
658 (collect-maybe-itemized-section lines line-number
))))
659 ((with-maybe-section line-number
660 (and (maybe-table-offset line-number lines
)
661 (collect-maybe-table-section lines line-number
))))
663 (write-line (texinfo-line line
) *texinfo-output
*))))))
665 ;;;; texinfo formatting tools
667 (defun hide-superclass-p (class-name super-name
)
668 (let ((super-package (symbol-package super-name
)))
670 ;; KLUDGE: We assume that we don't want to advertise internal
671 ;; classes in CP-lists, unless the symbol we're documenting is
673 (and (member super-package
#.
'(mapcar #'find-package
*undocumented-packages
*))
674 (not (eq super-package
(symbol-package class-name
))))
675 ;; KLUDGE: We don't generally want to advertise SIMPLE-ERROR or
676 ;; SIMPLE-CONDITION in the CPLs of conditions that inherit them
677 ;; simply as a matter of convenience. The assumption here is that
678 ;; the inheritance is incidental unless the name of the condition
679 ;; begins with SIMPLE-.
680 (and (member super-name
'(simple-error simple-condition
))
681 (let ((prefix "SIMPLE-"))
682 (mismatch prefix
(string class-name
) :end2
(length prefix
)))
683 t
; don't return number from MISMATCH
686 (defun hide-slot-p (symbol slot
)
687 ;; FIXME: There is no pricipal reason to avoid the slot docs fo
688 ;; structures and conditions, but their DOCUMENTATION T doesn't
689 ;; currently work with them the way we'd like.
690 (not (and (typep (find-class symbol nil
) 'standard-class
)
691 (docstring slot t
))))
693 (defun texinfo-anchor (doc)
694 (format *texinfo-output
* "@anchor{~A}~%" (node-name doc
)))
696 ;;; KLUDGE: &AUX *PRINT-PRETTY* here means "no linebreaks please"
697 (defun texinfo-begin (doc &aux
*print-pretty
*)
698 (let ((kind (get-kind doc
)))
699 (format *texinfo-output
* "@~A {~:(~A~)} ~(~A~@[ ~{~A~^ ~}~]~)~%"
701 ((package constant variable
)
703 ((structure class condition type
)
707 (map 'string
(lambda (char) (if (eql char
#\-
) #\Space char
)) (string kind
))
709 ;; &foo would be amusingly bold in the pdf thanks to TeX/Texinfo
710 ;; interactions,so we escape the ampersand -- amusingly for TeX.
711 ;; sbcl.texinfo defines macros that expand @&key and friends to &key.
712 (mapcar (lambda (name)
713 (if (member name lambda-list-keywords
)
714 (format nil
"@~A" name
)
716 (lambda-list doc
)))))
718 (defun texinfo-index (doc)
719 (let ((title (title-name doc
)))
721 ((structure type class condition
)
722 (format *texinfo-output
* "@tindex ~A~%" title
))
724 (format *texinfo-output
* "@vindex ~A~%" title
))
725 ((compiler-macro function method-combination macro generic-function
)
726 (format *texinfo-output
* "@findex ~A~%" title
)))))
728 (defun texinfo-inferred-body (doc)
729 (when (member (get-kind doc
) '(class structure condition
))
730 (let ((name (get-name doc
)))
731 ;; class precedence list
732 (format *texinfo-output
* "Class precedence list: @code{~(~{@lw{~A}~^, ~}~)}~%~%"
733 (remove-if (lambda (class) (hide-superclass-p name class
))
734 (mapcar #'class-name
(ensure-class-precedence-list (find-class name
)))))
736 (let ((slots (remove-if (lambda (slot) (hide-slot-p name slot
))
737 (class-direct-slots (find-class name
)))))
739 (format *texinfo-output
* "Slots:~%@itemize~%")
741 (format *texinfo-output
*
742 "@item ~(@code{~A}~#[~:; --- ~]~
743 ~:{~2*~@[~2:*~A~P: ~{@code{@w{~S}}~^, ~}~]~:^; ~}~)~%~%"
744 (slot-definition-name slot
)
748 (lambda (name things
)
750 (list name
(length things
) things
)))
751 '("initarg" "reader" "writer")
753 (slot-definition-initargs slot
)
754 (slot-definition-readers slot
)
755 (slot-definition-writers slot
)))))
756 ;; FIXME: Would be neater to handler as children
757 (write-texinfo-string (docstring slot t
)))
758 (format *texinfo-output
* "@end itemize~%~%"))))))
760 (defun texinfo-body (doc)
761 (write-texinfo-string (get-string doc
)))
763 (defun texinfo-end (doc)
764 (write-line (case (get-kind doc
)
765 ((package variable constant
) "@end defvr")
766 ((structure type class condition
) "@end deftp")
770 (defun write-texinfo (doc)
771 "Writes TexInfo for a DOCUMENTATION instance to *TEXINFO-OUTPUT*."
775 (texinfo-inferred-body doc
)
778 ;; FIXME: Children should be sorted one way or another
779 (mapc #'write-texinfo
(get-children doc
)))
783 (defun collect-gf-documentation (gf)
784 "Collects method documentation for the generic function GF"
785 (loop for method in
(generic-function-methods gf
)
786 for doc
= (maybe-documentation method t
)
790 (defun collect-name-documentation (name)
791 (loop for type in
*documentation-types
*
792 for doc
= (maybe-documentation name type
)
796 (defun collect-symbol-documentation (symbol)
797 "Collects all docs for a SYMBOL and (SETF SYMBOL), returns a list of
798 the form DOC instances. See `*documentation-types*' for the possible
800 (nconc (collect-name-documentation symbol
)
801 (collect-name-documentation (list 'setf symbol
))))
803 (defun collect-documentation (package)
804 "Collects all documentation for all external symbols of the given
805 package, as well as for the package itself."
806 (let* ((*documentation-package
* (find-package package
))
808 (check-type package package
)
809 (do-external-symbols (symbol package
)
810 (setf docs
(nconc (collect-symbol-documentation symbol
) docs
)))
811 (let ((doc (maybe-documentation *documentation-package
* t
)))
816 (defmacro with-texinfo-file
(pathname &body forms
)
817 `(with-open-file (*texinfo-output
* ,pathname
819 :if-does-not-exist
:create
820 :if-exists
:supersede
)
823 (defun generate-includes (directory &rest packages
)
824 "Create files in `directory' containing Texinfo markup of all
825 docstrings of each exported symbol in `packages'. `directory' is
826 created if necessary. If you supply a namestring that doesn't end in a
827 slash, you lose. The generated files are of the form
828 \"<doc-type>_<packagename>_<symbol-name>.texinfo\" and can be included
829 via @include statements. Texinfo syntax-significant characters are
830 escaped in symbol names, but if a docstring contains invalid Texinfo
832 (handler-bind ((warning #'muffle-warning
))
833 (let ((directory (merge-pathnames (pathname directory
))))
834 (ensure-directories-exist directory
)
835 (dolist (package packages
)
836 (dolist (doc (collect-documentation (find-package package
)))
837 (with-texinfo-file (merge-pathnames (include-pathname doc
) directory
)
838 (write-texinfo doc
))))
841 (defun document-package (package &optional filename
)
842 "Create a file containing all available documentation for the
843 exported symbols of `package' in Texinfo format. If `filename' is not
844 supplied, a file \"<packagename>.texinfo\" is generated.
846 The definitions can be referenced using Texinfo statements like
847 @ref{<doc-type>_<packagename>_<symbol-name>.texinfo}. Texinfo
848 syntax-significant characters are escaped in symbol names, but if a
849 docstring contains invalid Texinfo markup, you lose."
850 (handler-bind ((warning #'muffle-warning
))
851 (let* ((package (find-package package
))
852 (filename (or filename
(make-pathname
853 :name
(string-downcase (package-name package
))
855 (docs (sort (collect-documentation package
) #'documentation
<)))
856 (with-texinfo-file filename
858 (write-texinfo doc
)))