-
Notifications
You must be signed in to change notification settings - Fork 6
/
ldoc.lisp
190 lines (173 loc) · 6.36 KB
/
ldoc.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
;;; ldoc.lisp --- extract and format documentation from lisp files
;; Copyright (C) 2009 David O'Toole
;; Author: David O'Toole <[email protected]>
;; Keywords: lisp, tools
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(in-package :xe2)
;; todo show parent name if any
(defun clon-prototype-p (form)
(when (symbolp form)
(let* ((name (symbol-name form))
(len (length name)))
(and (string= "=" (subseq name 0 1))
(string= "=" (subseq name (- len 1) len))))))
(defun clon-method-p (form)
(when (symbolp form)
(let* ((delimiter ">>")
(name (symbol-name form))
(len (length name))
(delimiter-pos (search delimiter name)))
(when (numberp delimiter-pos)
(values (subseq name 0 delimiter-pos)
(subseq name (+ 2 delimiter-pos)))))))
(defun clon-parent-name (form)
(when (and (symbolp form) (boundp form))
(assert (symbol-value form))
(let ((parent (object-parent (symbol-value form))))
(when parent
(object-name parent)))))
(defun remove-delimiters (form)
(let* ((name (symbol-name form))
(len (length name)))
(subseq name 1 (- len 1))))
(defun document-symbol (symbol stream)
"Documentation string."
(let* ((type (if (clon-prototype-p symbol)
'variable
(if (fboundp symbol)
(if (macro-function symbol)
'function
'function)
'variable)))
(type-name (if (clon-method-p symbol)
"method" (if (clon-prototype-p symbol)
"prototype"
(if (fboundp symbol)
(if (macro-function symbol)
"macro" "function")
"variable"))))
(doc (if (clon-prototype-p symbol)
(field-value :documentation (symbol-value symbol))
(documentation symbol type)))
(name (if (clon-prototype-p symbol)
(remove-delimiters symbol)
(if (clon-method-p symbol)
(multiple-value-bind (method-name prototype-name) (clon-method-p symbol)
(format nil "~A [~A]" method-name prototype-name))
(symbol-name symbol))))
(args (when (fboundp symbol) (sb-introspect:function-lambda-list (fdefinition symbol)))))
(format stream "** ~A (~A)" name type-name)
(fresh-line stream)
(when args
(format stream "*** Arguments")
(fresh-line stream)
(format stream "~A" (if (clon-method-p symbol)
(cdr args) args))
(fresh-line stream))
(when doc
(format stream "*** Documentation")
(fresh-line stream)
(format stream "~A" doc))
(fresh-line stream)))
(defun do-heading (name stream)
(fresh-line stream)
(format stream "* ~A" name)
(fresh-line stream))
(defun document-package (package-name stream &optional preamble-file)
(let (syms protos methods proto-hashes preamble-lines)
(when preamble-file
(setf preamble-lines (with-open-file (file preamble-file
:direction :input
:if-does-not-exist nil)
(loop for line = (read-line file nil)
while line collect line))))
(do-external-symbols (sym package-name)
(when (< 3 (length (symbol-name sym)))
(push sym syms)))
(setf syms (sort syms #'string<))
;; print preamble
(dolist (line preamble-lines)
(format stream "~A" line)
(fresh-line stream))
;; sort symbols
(setf syms (remove-if #'(lambda (s)
(when (clon-prototype-p s)
(push s protos)))
syms))
(setf syms (remove-if #'(lambda (s)
(when (clon-method-p s)
(push s methods)))
syms))
;; document prototypes
(setf protos (nreverse protos))
(dolist (p protos)
(let (pile
(field-descriptors (when (and (clon-prototype-p p) (boundp p))
(field-value :field-descriptors (symbol-value p))))
(parent-name (clon-parent-name p)))
(dolist (m methods)
(multiple-value-bind (method-name proto-name)
(clon-method-p m)
(fresh-line t)
(when (string= proto-name (remove-delimiters p))
(push m pile))))
(setf pile (sort pile #'(lambda (s z)
(multiple-value-bind (method-name1 proto-name1)
(clon-method-p s)
(multiple-value-bind (method-name2 proto-name2)
(clon-method-p z)
(string> method-name1 method-name2))))))
(when pile
(do-heading (format nil "~A (prototype)" (symbol-name p)) stream)
(when parent-name
(fresh-line stream)
(format stream "** Parent prototype")
(fresh-line stream)
(format stream ": ~A" parent-name)
(fresh-line stream))
(let ((doc (field-value :documentation (symbol-value p))))
(when (stringp doc)
(format stream "** Documentation")
(fresh-line stream)
(format stream "~A" doc)
(fresh-line stream)))
(when field-descriptors
(format stream "*** Fields")
(fresh-line stream)
(dolist (d field-descriptors)
(fresh-line stream)
(destructuring-bind (name (&key documentation initform &allow-other-keys)) d
(when name (format stream "**** ~A (field)" name))
(when documentation
(fresh-line stream)
(format stream "***** Documentation")
(fresh-line stream)
(format stream "~A" documentation)
(fresh-line stream)
(when initform
(format stream "***** Initialization form")
(fresh-line stream)
(format stream ": ~S" initform))))))
(fresh-line stream)
(dolist (proto (reverse pile))
(document-symbol proto stream)))))
;; document syms
(do-heading "Symbols" stream)
(dolist (sym syms)
(document-symbol sym stream))))
(defun document-package-to-file (package-name output-file &optional preamble-file)
(with-open-file (stream output-file :direction :output :if-exists :supersede)
(document-package package-name stream preamble-file)))
;; (document-package :clon t)
;; (document-package-to-file :xe2 #P"/home/dto/notebook/xe2-reference.org" #P"/home/dto/xe2/doc-preamble.org")
;; (document-package :xe2 t #P"/home/dto/xe2/doc-preamble.org")
;;; ldoc.lisp ends here