]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/vocabs/browser/browser.factor
Merge branch 'master' into new_ui
[factor.git] / basis / tools / vocabs / browser / browser.factor
1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes classes.builtin
4 classes.intersection classes.mixin classes.predicate
5 classes.singleton classes.tuple classes.union combinators
6 definitions effects fry generic help help.markup help.stylesheet
7 help.topics io io.files io.pathnames io.styles kernel macros
8 make namespaces prettyprint sequences sets sorting summary
9 tools.vocabs vocabs vocabs.loader words words.symbol ;
10 IN: tools.vocabs.browser
11
12 : vocab-status-string ( vocab -- string )
13     {
14         { [ dup vocab not ] [ drop "" ] }
15         { [ dup vocab-main ] [ drop "[Runnable]" ] }
16         [ drop "[Loaded]" ]
17     } cond ;
18
19 : vocab-row ( vocab -- row )
20     [ <$link> ] [ vocab-status-string ] [ vocab-summary ] tri
21     3array ;
22
23 : vocab-headings ( -- headings )
24     {
25         { $strong "Vocabulary" }
26         { $strong "State" }
27         { $strong "Summary" }
28     } ;
29
30 : root-heading ( root -- )
31     [ "Children from " prepend ] [ "Children" ] if*
32     $heading ;
33
34 : $vocabs ( seq -- )
35     [ vocab-row ] map vocab-headings prefix $table ;
36
37 : $vocab-roots ( assoc -- )
38     [
39         [ drop ] [ [ root-heading ] [ $vocabs ] bi* ] if-empty
40     ] assoc-each ;
41
42 TUPLE: vocab-tag name ;
43
44 INSTANCE: vocab-tag topic
45
46 C: <vocab-tag> vocab-tag
47
48 : $tags ( seq -- ) [ <vocab-tag> ] map $links ;
49
50 TUPLE: vocab-author name ;
51
52 INSTANCE: vocab-author topic
53
54 C: <vocab-author> vocab-author
55
56 : $authors ( seq -- ) [ <vocab-author> ] map $links ;
57
58 : describe-help ( vocab -- )
59     [
60         dup vocab-help
61         [ "Documentation" $heading ($link) ]
62         [ "Summary" $heading vocab-summary print-element ]
63         ?if
64     ] unless-empty ;
65
66 : describe-children ( vocab -- )
67     vocab-name all-child-vocabs $vocab-roots ;
68
69 : describe-files ( vocab -- )
70     vocab-files [ <pathname> ] map [
71         "Files" $heading
72         [
73             snippet-style get [
74                 code-style get [
75                     stack.
76                 ] with-nesting
77             ] with-style
78         ] ($block)
79     ] unless-empty ;
80
81 : describe-tuple-classes ( classes -- )
82     [
83         "Tuple classes" $subheading
84         [
85             [ <$link> ]
86             [ superclass <$link> ]
87             [ "slots" word-prop [ name>> ] map " " join <$snippet> ]
88             tri 3array
89         ] map
90         { { $strong "Class" } { $strong "Superclass" } { $strong "Slots" } } prefix
91         $table
92     ] unless-empty ;
93
94 : describe-predicate-classes ( classes -- )
95     [
96         "Predicate classes" $subheading
97         [
98             [ <$link> ]
99             [ superclass <$link> ]
100             bi 2array
101         ] map
102         { { $strong "Class" } { $strong "Superclass" } } prefix
103         $table
104     ] unless-empty ;
105
106 : (describe-classes) ( classes heading -- )
107     '[
108         _ $subheading
109         [ <$link> 1array ] map $table
110     ] unless-empty ;
111
112 : describe-builtin-classes ( classes -- )
113     "Builtin classes" (describe-classes) ;
114
115 : describe-singleton-classes ( classes -- )
116     "Singleton classes" (describe-classes) ;
117
118 : describe-mixin-classes ( classes -- )
119     "Mixin classes" (describe-classes) ;
120
121 : describe-union-classes ( classes -- )
122     "Union classes" (describe-classes) ;
123
124 : describe-intersection-classes ( classes -- )
125     "Intersection classes" (describe-classes) ;
126
127 : describe-classes ( classes -- )
128     [ builtin-class? ] partition
129     [ tuple-class? ] partition
130     [ singleton-class? ] partition
131     [ predicate-class? ] partition
132     [ mixin-class? ] partition
133     [ union-class? ] partition
134     [ intersection-class? ] filter
135     {
136         [ describe-builtin-classes ]
137         [ describe-tuple-classes ]
138         [ describe-singleton-classes ]
139         [ describe-predicate-classes ]
140         [ describe-mixin-classes ]
141         [ describe-union-classes ]
142         [ describe-intersection-classes ]
143     } spread ;
144
145 : word-syntax ( word -- string/f )
146     \ $syntax swap word-help elements dup length 1 =
147     [ first second ] [ drop f ] if ;
148
149 : describe-parsing ( words -- )
150     [
151         "Parsing words" $subheading
152         [
153             [ <$link> ]
154             [ word-syntax dup [ <$snippet> ] when ]
155             bi 2array
156         ] map
157         { { $strong "Word" } { $strong "Syntax" } } prefix
158         $table
159     ] unless-empty ;
160
161 : words-table ( words -- )
162     [
163         [ <$link> ]
164         [ stack-effect dup [ effect>string <$snippet> ] when ]
165         bi 2array
166     ] map
167     { { $strong "Word" } { $strong "Stack effect" } } prefix
168     $table ;
169
170 : (describe-words) ( words heading -- )
171     '[ _ $subheading words-table ] unless-empty ;
172
173 : describe-generics ( words -- )
174     "Generic words" (describe-words) ;
175
176 : describe-macros ( words -- )
177     "Macro words" (describe-words) ;
178
179 : describe-primitives ( words -- )
180     "Primitives" (describe-words) ;
181
182 : describe-compounds ( words -- )
183     "Ordinary words" (describe-words) ;
184
185 : describe-predicates ( words -- )
186     "Class predicate words" (describe-words) ;
187
188 : describe-symbols ( words -- )
189     [
190         "Symbol words" $subheading
191         [ <$link> 1array ] map $table
192     ] unless-empty ;
193
194 : $words ( words -- )
195     [
196         "Words" $heading
197
198         natural-sort
199         [ [ class? ] filter describe-classes ]
200         [
201             [ [ class? ] [ symbol? ] bi and not ] filter
202             [ parsing-word? ] partition
203             [ generic? ] partition
204             [ macro? ] partition
205             [ symbol? ] partition
206             [ primitive? ] partition
207             [ predicate? ] partition swap
208             {
209                 [ describe-parsing ]
210                 [ describe-generics ]
211                 [ describe-macros ]
212                 [ describe-symbols ]
213                 [ describe-primitives ]
214                 [ describe-compounds ]
215                 [ describe-predicates ]
216             } spread
217         ] bi
218     ] unless-empty ;
219
220 : words. ( vocab -- )
221     last-element off
222     words $words ;
223
224 : describe-metadata ( vocab -- )
225     [
226         [ vocab-tags [ "Tags:" swap \ $tags prefix 2array , ] unless-empty ]
227         [ vocab-authors [ "Authors:" swap \ $authors prefix 2array , ] unless-empty ]
228         bi
229     ] { } make
230     [ "Meta-data" $heading $table ] unless-empty ;
231
232 : $vocab ( element -- )
233     first {
234         [ describe-help ]
235         [ describe-metadata ]
236         [ words $words ]
237         [ describe-files ]
238         [ describe-children ]
239     } cleave ;
240
241 : keyed-vocabs ( str quot -- seq )
242     all-vocabs [
243         swap [
244             [ [ 2dup ] dip swap call member? ] filter
245         ] dip swap
246     ] assoc-map 2nip ; inline
247
248 : tagged ( tag -- assoc )
249     [ vocab-tags ] keyed-vocabs ;
250
251 : authored ( author -- assoc )
252     [ vocab-authors ] keyed-vocabs ;
253
254 : $tagged-vocabs ( element -- )
255     first tagged $vocab-roots ;
256
257 : $authored-vocabs ( element -- )
258     first authored $vocab-roots ;
259
260 : $all-tags ( element -- )
261     drop "Tags" $heading all-tags $tags ;
262
263 : $all-authors ( element -- )
264     drop "Authors" $heading all-authors $authors ;
265
266 INSTANCE: vocab topic
267
268 INSTANCE: vocab-link topic
269
270 M: vocab-spec article-title vocab-name " vocabulary" append ;
271
272 M: vocab-spec article-name vocab-name ;
273
274 M: vocab-spec article-content
275     vocab-name \ $vocab swap 2array ;
276
277 M: vocab-spec article-parent drop "vocab-index" ;
278
279 M: vocab-tag >link ;
280
281 M: vocab-tag article-title
282     name>> "Vocabularies tagged ``" "''" surround ;
283
284 M: vocab-tag article-name name>> ;
285
286 M: vocab-tag article-content
287     \ $tagged-vocabs swap name>> 2array ;
288
289 M: vocab-tag article-parent drop "vocab-index" ;
290
291 M: vocab-tag summary article-title ;
292
293 M: vocab-author >link ;
294
295 M: vocab-author article-title
296     name>> "Vocabularies by " prepend ;
297
298 M: vocab-author article-name name>> ;
299
300 M: vocab-author article-content
301     \ $authored-vocabs swap name>> 2array ;
302
303 M: vocab-author article-parent drop "vocab-index" ;
304
305 M: vocab-author summary article-title ;