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