-
Notifications
You must be signed in to change notification settings - Fork 9
/
doc.lisp
185 lines (166 loc) · 6.23 KB
/
doc.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
;;; doc.lisp --- extract blocky docs into orgmode format
;; Copyright (C) 2009-2011, 2012 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 :blocky)
(defun methodp (symbol)
(and (fboundp symbol)
(get 'symbol 'is-method)))
(defun document-extended-argument (entry stream)
(format stream "~A" entry))
(defun heading (level text stream)
(fresh-line stream)
(format stream "~A ~A"
(make-string level :initial-element (character "*"))
text)
(fresh-line stream))
(defun document-method (prototype method stream)
(let* ((symbol (intern (make-method-id prototype method)))
(doc (documentation symbol 'function)))
(when doc
(multiple-value-bind (arglist options prototype method)
(find-method-data prototype method)
(heading 2 (format nil "~A (method)" (make-non-keyword method))
stream)
(heading 3 "Arguments" stream)
(dolist (arg arglist)
(document-extended-argument arg stream)
(fresh-line stream))
(heading 3 "Documentation" stream)
(format stream "~A" doc)
(fresh-line stream)))))
(defun document-function (symbol stream)
(let ((doc (documentation symbol 'function)))
(when doc
(let ((type (cond
((macro-function symbol) 'macro)
((fboundp symbol) 'function))))
(heading 2 (format nil "~A (~A)" symbol (string-downcase (symbol-name type)))
stream)
(heading 3 "Arguments" stream)
(format stream "~S" (sb-introspect:function-lambda-list (fdefinition symbol)))
(heading 3 "Documentation" stream)
(format stream "~A" doc)
(fresh-line stream)))))
(defun document-variable (symbol stream)
(heading 2 (format nil "~A (variable)" symbol)
stream)
(heading 3 "Documentation" stream)
(format stream "~A" (documentation symbol 'variable))
(fresh-line stream))
(defun find-prototype-methods (prototype)
(let ((proto (find-object prototype))
(methods '()))
(labels ((extract (k v)
(when (and (symbolp v)
(fboundp v))
(push k methods))))
(maphash #'extract
(object-fields proto)))
(sort methods #'string<)))
(defun document-prototype (name stream)
(heading 1 (format nil "~A (prototype)" (subseq name (1+ (position (character ":") name))))
stream)
(let ((proto (gethash name *prototypes*)))
(when proto
(let* ((field-descriptors
(field-value :field-descriptors proto))
(parent-name (find-super-prototype-name proto))
(methods (find-prototype-methods proto)))
(when parent-name
(heading 3 (format nil "Parent name: ~A" parent-name) stream))
(let ((doc (field-value :documentation proto)))
(when (stringp doc)
(heading 2 "Documentation" stream)
(format stream "~A" doc)
(fresh-line stream)))
(when field-descriptors
(heading 2 "Fields" stream)
(dolist (d field-descriptors)
(fresh-line stream)
(destructuring-bind (name (&key documentation initform &allow-other-keys)) d
(when (and name documentation)
(format stream "*** ~A (field)" name)
(heading 4 "Documentation" stream)
(format stream "~A" documentation)
(fresh-line stream)
(when initform
(heading 4 "Initial value" stream)
(format stream "~S" initform))))))
(fresh-line stream)
;; methods
(message "Documenting ~S methods..." (length methods))
(dolist (m methods)
(document-method name m stream))
methods))))
(defun preamble-file-lines (preamble-file)
(with-open-file (file preamble-file
:direction :input
:if-does-not-exist nil)
(let* ((len (file-length file))
(string (make-string len)))
(read-sequence string file)
(split-string-on-lines string))))
;; (loop for line = (read-line file nil)
;; while line collect line)))
(defun document-package (package-name &key (stream t) preamble-file title)
(let ((package (find-package package-name))
symbols functions variables)
;; header
(when title
(format stream "#+TITLE: ~A" title)
(fresh-line stream))
(format stream "#+OPTIONS: toc:2 *:nil")
(fresh-line stream)
(do-external-symbols (symbol package)
(push symbol symbols))
;; remove method symbols and method defun symbols
(setf symbols
(sort
(remove-if #'(lambda (s)
(or (search "%" (symbol-name s))
(methodp s)))
symbols)
#'string<))
;; print preamble
(let ((preamble-lines
(when preamble-file
(preamble-file-lines preamble-file))))
(when preamble-file
(dolist (line preamble-lines)
(format stream "~A " line)
(fresh-line stream))))
;; document prototypes and their respective methods
(let (prototypes)
(loop for prototype being the hash-keys in *prototypes* do
(push prototype prototypes))
(setf prototypes
(mapcar #'(lambda (name)
(subseq name (1+ (position (character ":") name))))
prototypes))
(dolist (prototype (sort prototypes #'string<))
(let ((methods (document-prototype (concatenate 'string "BLOCKY:" prototype) stream)))
(dolist (method methods)
(setf symbols (remove (make-non-keyword method) symbols :test 'eq)))
)))
;; document syms
(heading 1 "Functions, Macros, and Variables" stream)
(dolist (sym symbols)
(if (fboundp sym)
(document-function sym stream)
(document-variable sym stream)))))
(defun document-package-to-file (package-name output-file &key preamble-file title)
(with-open-file (stream output-file :direction :output :if-exists :supersede)
(document-package package-name :title title :stream stream :preamble-file preamble-file)))
;; (document-package-to-file :blocky #P"/home/dto/ioweb/reference.org" :title "Blocky reference manual" :preamble-file #P"~/blocky/guide.org")
;;; doc.lisp ends here