*** empty log message ***
[bpt/guile.git] / ice-9 / documentation.scm
1 ;;;; Copyright (C) 2000,2001 Free Software Foundation, Inc.
2 ;;;;
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
7 ;;;;
8 ;;;; This program is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;;;; GNU General Public License for more details.
12 ;;;;
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
17 ;;;;
18
19 ;;; Commentary:
20
21 ;; * This module exports:
22 ;;
23 ;; file-commentary -- a procedure that returns a file's "commentary"
24 ;;
25 ;; documentation-files -- a search-list of files using the Guile
26 ;; Documentation Format Version 2.
27 ;;
28 ;; search-documentation-files -- a procedure that takes NAME (a symbol)
29 ;; and searches `documentation-files' for
30 ;; associated documentation. optional
31 ;; arg FILES is a list of filenames to use
32 ;; instead of `documentation-files'.
33 ;;
34 ;; object-documentation -- a procedure that returns its arg's docstring
35 ;;
36 ;; * Guile Documentation Format
37 ;;
38 ;; Here is the complete and authoritative documentation for the Guile
39 ;; Documentation Format Version 2:
40 ;;
41 ;; HEADER
42 ;; ^LPROC1
43 ;; DOCUMENTATION1
44 ;;
45 ;; ^LPROC2
46 ;; DOCUMENTATION2
47 ;;
48 ;; ^L...
49 ;;
50 ;; The HEADER is completely ignored. The "^L" are formfeeds. PROC1, PROC2
51 ;; and so on are symbols that name the element documented. DOCUMENTATION1,
52 ;; DOCUMENTATION2 and so on are the related documentation, w/o any further
53 ;; formatting. Note that there are two newlines before the next formfeed;
54 ;; these are discarded when the documentation is read in.
55 ;;
56 ;; (Version 1, corresponding to guile-1.4 and prior, is documented as being
57 ;; not documented anywhere except by this embarrassingly circular comment.)
58 ;;
59 ;; * File Commentary
60 ;;
61 ;; A file's commentary is the body of text found between comments
62 ;; ;;; Commentary:
63 ;; and
64 ;; ;;; Code:
65 ;; both of which must be at the beginning of the line. In the result string,
66 ;; semicolons at the beginning of each line are discarded.
67 ;;
68 ;; You can specify to `file-commentary' alternate begin and end strings, and
69 ;; scrub procedure. Use #t to get default values. For example:
70 ;;
71 ;; (file-commentary "documentation.scm")
72 ;; You should see this text!
73 ;;
74 ;; (file-commentary "documentation.scm" "^;;; Code:" "ends here$")
75 ;; You should see the rest of this file.
76 ;;
77 ;; (file-commentary "documentation.scm" #t #t string-upcase)
78 ;; You should see this text very loudly (note semicolons untouched).
79
80 ;;; Code:
81
82 (define-module (ice-9 documentation)
83 :use-module (ice-9 rdelim)
84 :export (file-commentary
85 documentation-files search-documentation-files
86 object-documentation)
87 :autoload (ice-9 regex) (match:suffix)
88 :no-backtrace)
89
90 \f
91 ;;
92 ;; commentary extraction
93 ;;
94 (define default-in-line-re (make-regexp "^;;; Commentary:"))
95 (define default-after-line-re (make-regexp "^;;; Code:"))
96 (define default-scrub (let ((dirt (make-regexp "^;+")))
97 (lambda (line)
98 (let ((m (regexp-exec dirt line)))
99 (if m (match:suffix m) line)))))
100
101 (define (file-commentary filename . cust) ; (IN-LINE-RE AFTER-LINE-RE SCRUB)
102 ;; fixme: might be cleaner to use optargs here...
103 (let ((in-line-re (if (> 1 (length cust))
104 default-in-line-re
105 (let ((v (car cust)))
106 (cond ((regexp? v) v)
107 ((string? v) (make-regexp v))
108 (else default-in-line-re)))))
109 (after-line-re (if (> 2 (length cust))
110 default-after-line-re
111 (let ((v (cadr cust)))
112 (cond ((regexp? v) v)
113 ((string? v) (make-regexp v))
114 (else default-after-line-re)))))
115 (scrub (if (> 3 (length cust))
116 default-scrub
117 (let ((v (caddr cust)))
118 (cond ((procedure? v) v)
119 (else default-scrub)))))
120 (port (open-input-file filename)))
121 (let loop ((line (read-delimited "\n" port))
122 (doc "")
123 (parse-state 'before))
124 (if (or (eof-object? line) (eq? 'after parse-state))
125 doc
126 (let ((new-state
127 (cond ((regexp-exec in-line-re line) 'in)
128 ((regexp-exec after-line-re line) 'after)
129 (else parse-state))))
130 (if (eq? 'after new-state)
131 doc
132 (loop (read-delimited "\n" port)
133 (if (and (eq? 'in new-state) (eq? 'in parse-state))
134 (string-append doc (scrub line) "\n")
135 doc)
136 new-state)))))))
137
138 \f
139
140 ;;
141 ;; documentation-files is the list of places to look for documentation
142 ;;
143 (define documentation-files
144 (map (lambda (vicinity)
145 (in-vicinity (vicinity) "guile-procedures.txt"))
146 (list %library-dir
147 %package-data-dir
148 %site-dir
149 (lambda () "."))))
150
151 (define entry-delimiter "\f")
152
153 (define (find-documentation-in-file name file)
154 (and (file-exists? file)
155 (let ((port (open-input-file file))
156 (name (symbol->string name)))
157 (let ((len (string-length name)))
158 (read-delimited entry-delimiter port) ;skip to first entry
159 (let loop ((entry (read-delimited entry-delimiter port)))
160 (cond ((eof-object? entry) #f)
161 ;; match?
162 ((and ;; large enough?
163 (>= (string-length entry) len)
164 ;; matching name?
165 (string=? (substring entry 0 len) name)
166 ;; terminated?
167 (memq (string-ref entry len) '(#\newline)))
168 ;; cut away name tag and extra surrounding newlines
169 (substring entry (+ len 2) (- (string-length entry) 2)))
170 (else (loop (read-delimited entry-delimiter port)))))))))
171
172 (define (search-documentation-files name . files)
173 (or-map (lambda (file)
174 (find-documentation-in-file name file))
175 (cond ((null? files) documentation-files)
176 (else files))))
177
178 ;; helper until the procedure documentation property is cleaned up
179 (define (proc-doc proc)
180 (or (procedure-documentation proc)
181 (procedure-property proc 'documentation)))
182
183 (define (object-documentation object)
184 "Return the docstring for OBJECT.
185 OBJECT can be a procedure, macro or any object that has its
186 `documentation' property set."
187 (or (and (procedure? object)
188 (proc-doc object))
189 (and (macro? object)
190 (let ((transformer (macro-transformer object)))
191 (and transformer
192 (proc-doc transformer))))
193 (object-property object 'documentation)
194 (and (procedure? object)
195 (not (closure? object))
196 (procedure-name object)
197 (let ((docstring (search-documentation-files
198 (procedure-name object))))
199 (if docstring
200 (set-procedure-property! object 'documentation docstring))
201 docstring))))
202
203 ;;; documentation.scm ends here