-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcl-livedocs.lisp
305 lines (272 loc) · 12.8 KB
/
cl-livedocs.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
;; An info document generated dynamically from Common Lisp packages exported definitions.
;; TODO: parse docstrings and format Texinfo rich text. For example, transform symbols in upper case to links to the object, if present in the package.
(require :def-properties (asdf:system-relative-pathname :cl-livedocs "def-properties.lisp"))
(in-package #:cl-livedocs)
(defclass lisp-info-document (webinfo:info-document)
((nodes :initarg :nodes
:accessor nodes
:initform nil))
(:documentation "An info document generated dynamically from Common Lisp packages exported definitions."))
(defmethod webinfo::find-node ((doc lisp-info-document) node-name)
(labels ((childs (doc-or-node)
(if (typep doc-or-node 'webinfo::info-document)
(nodes doc-or-node)
(webinfo::children doc-or-node)))
(tree-find (node node-name)
(or (find node-name (childs node) :key 'webinfo::node-name :test 'string=)
(loop for child in (childs node)
for found-node := (tree-find child node-name)
while (not found-node)
finally (return found-node)))))
(tree-find doc node-name)))
(defun collect-package-info (&optional (package *package*))
(let (infos)
(do-external-symbols (symbol package)
(push
(def-properties:symbol-properties symbol t)
infos))
(apply #'append infos)))
(defmethod webinfo::all-nodes ((doc lisp-info-document))
(labels ((descendants (node)
(append (webinfo::children node)
(apply #'append (mapcar #'descendants (webinfo::children node))))))
(loop for node in (nodes doc)
appending (descendants node))))
;; Info nodes
(defun make-info-document-for-package (package)
(let ((doc (make-instance 'lisp-info-document
:name (hunchentoot:url-encode (package-name package))
:title (package-name package)))
(top-node (make-instance 'webinfo::sexp-info-node
:name "Top"
:title (package-name package)))
(variables-node (make-instance 'webinfo::sexp-info-node
:name "Variables"
:title "Variables"
:description (format nil "Dictionary of all external variables in ~a package" package)))
(macros-node (make-instance 'webinfo::sexp-info-node
:name "Macros"
:title "Macros"
:description (format nil "Dictionary of all external macros in ~a package" package)))
(functions-node (make-instance 'webinfo::sexp-info-node
:name "Functions"
:title "Functions"
:description (format nil "Dictionary of all external functions in ~a package" package)))
(classes-node (make-instance 'webinfo::sexp-info-node
:name "Classes"
:title "Classes"
:description (format nil "Dictionary of all external classes in ~a package" package)))
(index-node (make-instance 'webinfo::sexp-info-node
:name "Index"
:title "Index"
:description "Definitions index"))
(package-info (collect-package-info package)))
;; Build top node
(flet ((menu-entry (node)
`(:|menuentry| ()
(:|menunode| ()
,(webinfo::node-name node))
(:|menudescription| ()
(:|pre| ,(webinfo::description node))))))
(setf (webinfo::contents top-node)
`(:|chapter| ()
(:|sectiontitle| ()
,(format nil "Package reference: ~a" (package-name package)))
,@(awhen (documentation package t)
(webinfo::text->sexp it))
(:|menu| ()
,@(loop for node in (list variables-node
macros-node
functions-node
classes-node
index-node)
collect (menu-entry node))))))
(push top-node (nodes doc))
;; Build definition nodes
(setf (webinfo::node-up variables-node) "Top")
(setf (webinfo::contents variables-node)
`(:|chapter| ()
(:|sectiontitle| ()
"Variables")
,@(loop for info in (remove-if-not
(lambda (info)
(eql (aget info :type) :variable))
package-info)
collect (lispinfo->sexp info))))
(push variables-node (webinfo::children top-node))
(setf (webinfo::node-up macros-node) "Top")
(setf (webinfo::contents macros-node)
`(:|chapter| ()
(:|sectiontitle| ()
"Macros")
,@(loop for info in (remove-if-not
(lambda (info)
(eql (aget info :type) :macro))
package-info)
collect (lispinfo->sexp info))))
(push macros-node (webinfo::children top-node))
(setf (webinfo::node-up functions-node) "Top")
(setf (webinfo::contents functions-node)
`(:|chapter| ()
(:|sectiontitle| ()
"Functions")
,@(loop for info in (remove-if-not
(lambda (info)
(member (aget info :type) '(:function :generic-function)))
package-info)
collect (lispinfo->sexp info))))
(push functions-node (webinfo::children top-node))
(setf (webinfo::node-up classes-node) "Top")
(setf (webinfo::contents classes-node)
`(:|chapter| ()
(:|sectiontitle| ()
"Classes")
,@(loop for info in (remove-if-not
(lambda (info)
(member (aget info :type) '(:class)))
package-info)
collect (lispinfo->sexp info))))
(push classes-node (webinfo::children top-node))
;; Build index node
(setf (webinfo::node-up index-node) "Top")
(setf (webinfo::contents index-node)
`(:|chapter| ()
(:|sectiontitle| () "Index")
(:|printindex| (:|value| "vr"))
(:|printindex| (:|value| "fn"))
(:|printindex| (:|value| "tp"))
(:|printindex| (:|value| "cp"))))
(push index-node (webinfo::children top-node))
(setf (webinfo::children top-node) (nreverse (webinfo::children top-node)))
(setf (webinfo::node-next top-node) "Variables")
(setf (webinfo::node-next variables-node) "Macros")
(setf (webinfo::node-prev macros-node) "Variables")
(setf (webinfo::node-next macros-node) "Functions")
(setf (webinfo::node-prev functions-node) "Macros")
(setf (webinfo::node-next functions-node) "Classes")
(setf (webinfo::node-prev classes-node) "Functions")
(setf (webinfo::node-next classes-node) "Index")
(setf (webinfo::node-prev index-node) "Classes")
(initialize-lisp-document-indexes doc package-info)
doc))
(defun initialize-lisp-document-indexes (doc package-info)
(setf (webinfo::indexes doc)
(list (cons :vr (loop for info in package-info
when (member (aget info :type) '(:variable))
collect (cons (princ-to-string (aget info :name))
(webinfo::find-node doc "Variables"))))
(cons :fn
(append
(loop for info in package-info
when (member (aget info :type) '(:macro))
collect (cons (princ-to-string (aget info :name))
(webinfo::find-node doc "Macros")))
(loop for info in package-info
when (member (aget info :type) '(:function :generic-function))
collect (cons (princ-to-string (aget info :name))
(webinfo::find-node doc "Functions")))
))
(cons :tp (loop for info in package-info
when (member (aget info :type) '(:class))
collect (cons (princ-to-string (aget info :name))
(webinfo::find-node doc "Classes")))))))
(defun lispinfo->sexp (info)
(ecase (aget info :type)
(:variable
`(:|defvr| ()
(:|definitionterm| ()
(:|indexterm| (:index "vr")
,(aget info :name))
(:|defcategory| () "Variable")
(:|defvariable| () ,(aget info :name)))
(:|definitionitem| ()
,@(or (aand (aget info :documentation)
(webinfo::text->sexp it))
`((:|para| () ""))))
(:|vindex| ()
(:|indexterm| ()
,(princ-to-string (aget info :name))))))
(:function
`(:|deffn| ()
(:|definitionterm| ()
(:|indexterm| (:index "fn")
,(aget info :name))
(:|defcategory| () "Function")
(:|deffunction| () ,(aget info :name))
(:|defparam| () ,(aget info :args)))
(:|definitionitem| ()
,@(or (aand (aget info :documentation)
(webinfo::text->sexp it))
`((:|para| () ""))))
(:|findex| ()
(:|indexterm| ()
,(princ-to-string (aget info :name))))))
(:generic-function
`(:|deffn| ()
(:|definitionterm| ()
(:|indexterm| (:index "fn")
,(aget info :name))
(:|defcategory| () "Generic function")
(:|deffunction| () ,(aget info :name))
(:|defparam| () ,(aget info :args)))
(:|definitionitem| ()
,@(or (aand (aget info :documentation)
(webinfo::text->sexp it))
`((:|para| () ""))))
(:|findex| ()
(:|indexterm| ()
,(princ-to-string (aget info :name))))))
(:class
`(:|deftp| ()
(:|definitionterm| ()
(:|indexterm| (:index "tp")
,(aget info :name))
(:|defcategory| () "Class")
(:|defdatatype| () ,(aget info :name)))
(:|definitionitem| ()
,@(or (aand (aget info :documentation)
(webinfo::text->sexp it))
`((:|para| () ""))))
(:|tindex| ()
(:|indexterm| ()
,(princ-to-string (aget info :name))))))
(:macro
`(:|deffn| ()
(:|definitionterm| ()
(:|indexterm| (:index "fn")
,(aget info :name))
(:|defcategory| () "Macro")
(:|deffunction| () ,(aget info :name))
(:|defparam| () ,(aget info :args)))
(:|definitionitem| ()
,@(or (aand (aget info :documentation)
(webinfo::text->sexp it))
`((:|para| () ""))))
(:|findex| ()
(:|indexterm| ()
,(princ-to-string (aget info :name))))))))
(defclass livedocs-info-repository (webinfo::dir-info-repository webinfo::indexable-info-repository)
())
(defmethod initialize-instance ((repo livedocs-info-repository) &rest initargs)
(declare (ignore initargs))
(setf (webinfo::dir repo)
(mapcar 'make-info-document-for-package
(remove "COMMON-LISP" (sort (list-all-packages) 'string< :key 'package-name)
:test 'equalp
:key 'package-name)))
(call-next-method))
(defun start (&rest args &key fulltext-search &allow-other-keys)
(let ((acceptor
(apply #'webinfo:start-webinfo
:info-repository
(make-instance 'livedocs-info-repository
:search-index (when fulltext-search
(webinfo::make-memory-search-index)))
:app-settings (list (cons :theme (make-instance 'webinfo::nav-theme)))
(let ((acceptor-args (copy-list args)))
(remf acceptor-args :fulltext-search)
acceptor-args))))
(format t "------------------------------------------------------------------
Common Lisp live documentation is here: http://localhost:~a~%
------------------------------------------------------------------~%"
(hunchentoot:acceptor-port acceptor))))