/
mapper.lisp
213 lines (163 loc) · 7.5 KB
/
mapper.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
(uiop:define-package #:40ants-doc-full/commondoc/mapper
(:use #:cl)
(:import-from #:common-doc)
(:import-from #:common-doc.ops)
(:import-from #:40ants-doc-full/commondoc/bullet)
(:import-from #:40ants-doc/object-package
#:object-package)
(:export
#:map-nodes
#:node-supports-children
#:with-node-package))
(in-package #:40ants-doc-full/commondoc/mapper)
(defvar *on-going-down* nil
"A list of callbacks to be called inside MAP-NODES.")
(defvar *on-going-up* nil
"A list of callbacks to be called inside MAP-NODES.")
(defvar *inside-title* nil
"Will be set to T when mapper goes down into a section title.
This can be useful if we don't want to do something
inside titles.")
(defvar *inside-link* nil
"Will be set to T when mapper goes down into a link
This can be useful if we don't want to do something
inside web link.")
(defun do-nothing (node)
(declare (ignore node))
(values))
(defun call-with-node-package (func)
(let ((packages-stack nil))
(flet ((set-package (node)
(let ((package (or (object-package node)
*package*)))
(push *package* packages-stack)
(setf *package* package)))
(reset-package (node)
(declare (ignore node))
(setf *package*
(pop packages-stack))))
(let ((*on-going-down* (list* #'set-package *on-going-down*))
(*on-going-up* (list* #'reset-package *on-going-up*))
(*package* *package*))
(funcall func)))))
(defmacro with-node-package (&body body)
"This macro tracks current documentation piece's package and sets *package* accordingly."
`(call-with-node-package (lambda () ,@body)))
(defvar *path*)
(defun call-with-node-path (func)
(flet ((collect-section (node)
(when (or (typep node 'common-doc:section)
(typep node '40ants-doc-full/commondoc/bullet::bullet))
(push node *path*)))
(pop-section (node)
(when (or (typep node 'common-doc:section)
(typep node '40ants-doc-full/commondoc/bullet::bullet))
(pop *path*))))
(let ((*on-going-down* (list* #'collect-section *on-going-down*))
(*on-going-up* (list* #'pop-section *on-going-up*))
(*path* nil))
(funcall func))))
(defun current-path ()
"Returns a list of section titles (strings)."
(unless (boundp '*path*)
(error "Function CURRENT-PATH should be called inside WITH-NODE-PATH macro."))
(loop for item in (reverse *path*)
for title = (typecase item
(common-doc:section
(common-doc.ops:collect-all-text
(common-doc:title item)))
(40ants-doc-full/commondoc/bullet::bullet
;; To always render a package specified version
;; of symbol names, we need to set keyword package here
(let ((*package* (find-package :keyword)))
(40ants-doc-full/commondoc/bullet::bullet-name item))))
collect title))
(defmacro with-node-path (&body body)
"This macro tracks sections and subsections providing a CURRENT-PATH function.
Useful for referencing a problematic node in a warning, because such path
makes it easier to locate the node having an issue."
`(call-with-node-path (lambda () ,@body)))
(defun process-node-with-children (node func &key
(on-going-down #'do-nothing)
(on-going-up #'do-nothing))
(let* ((result (funcall func node))
(children (when (node-supports-children result)
(common-doc:children result))))
(when (node-supports-children result)
(loop for callback in *on-going-down*
do (funcall callback result))
(funcall on-going-down result)
(setf (common-doc:children result)
(etypecase children
(list (loop for child in (common-doc:children result)
collect (map-nodes child func
:on-going-up on-going-up
:on-going-down on-going-down)))
;; Sometimes (children) contains an object like
;; COMMON-DOC:UNORDERED-LIST
(common-doc:document-node
(map-nodes children func
:on-going-up on-going-up
:on-going-down on-going-down))))
(funcall on-going-up result)
(loop for callback in *on-going-up*
do (funcall callback result)))
(values result)))
(defgeneric node-supports-children (node)
(:documentation "We have to use this function because some common-doc node types
supporting COMMON-DOC:CHILDREN do not share a common type.")
(:method (node)
nil)
(:method ((node common-doc:base-list))
t)
(:method ((node common-doc:document))
t)
(:method ((node common-doc:content-node))
t))
(defgeneric map-nodes (node func &key)
(:documentation "Recursively replaces or modifies a CommonDoc NODE with results of the FUNC call.")
(:method (node func &key
(on-going-down #'do-nothing)
(on-going-up #'do-nothing))
(if (node-supports-children node)
(process-node-with-children node func
:on-going-down on-going-down
:on-going-up on-going-up)
(funcall func node)))
(:method ((node common-doc:base-list) func &key
(on-going-down #'do-nothing)
(on-going-up #'do-nothing))
(process-node-with-children node func
:on-going-down on-going-down
:on-going-up on-going-up))
(:method ((node common-doc:document) func &key
(on-going-down #'do-nothing)
(on-going-up #'do-nothing))
(process-node-with-children node func
:on-going-down on-going-down
:on-going-up on-going-up))
(:method ((node common-doc:content-node) func &key
(on-going-down #'do-nothing)
(on-going-up #'do-nothing))
(process-node-with-children node func
:on-going-down on-going-down
:on-going-up on-going-up))
(:method ((node common-doc:link) func &key
(on-going-down #'do-nothing)
(on-going-up #'do-nothing))
(declare (ignore on-going-down on-going-up))
(let ((*inside-link* t))
(call-next-method)))
(:method ((node common-doc:section) func &key (on-going-down #'do-nothing)
(on-going-up #'do-nothing))
(let* ((result (call-next-method))
(*inside-title* t))
(loop for callback in *on-going-down*
do (funcall callback result))
(funcall on-going-down result)
(setf (common-doc:title node)
(mapcar func (common-doc:title node)))
(funcall on-going-up result)
(loop for callback in *on-going-up*
do (funcall callback result))
result)))