1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes colors colors.constants
4 combinators combinators.smart definitions definitions.icons effects
5 fry generic hashtables help.stylesheet help.topics io io.styles
6 kernel make math namespaces parser present prettyprint
7 prettyprint.stylesheet quotations see sequences sets slots
8 sorting splitting strings vectors vocabs vocabs.loader words
10 FROM: prettyprint.sections => with-pprint ;
11 FROM: namespaces => set ;
14 PREDICATE: simple-element < array
15 [ t ] [ first word? not ] if-empty ;
22 : last-span? ( -- ? ) last-element get span eq? ;
23 : last-block? ( -- ? ) last-element get block eq? ;
24 : last-blank-line? ( -- ? ) last-element get blank-line eq? ;
31 : ($blank-line) ( -- )
32 nl nl blank-line last-element set ;
35 last-block? [ nl ] when
39 GENERIC: print-element ( element -- )
41 M: simple-element print-element [ print-element ] each ;
42 M: string print-element [ write ] ($span) ;
43 M: array print-element unclip execute( arg -- ) ;
44 M: word print-element { } swap execute( arg -- ) ;
45 M: f print-element drop ;
47 : print-element* ( element style -- )
48 [ print-element ] with-style ;
50 : with-default-style ( quot -- )
51 default-span-style get [
52 default-block-style get swap with-nesting
55 : print-content ( element -- )
56 [ print-element ] with-default-style ;
58 : ($block) ( quot -- )
62 block last-element set ; inline
66 : $snippet ( children -- )
67 [ snippet-style get print-element* ] ($span) ;
72 : $emphasis ( children -- )
73 [ emphasis-style get print-element* ] ($span) ;
75 : $strong ( children -- )
76 [ strong-style get print-element* ] ($span) ;
78 : $url ( children -- )
80 dup first href associate url-style get assoc-union
85 drop nl last-element get [ nl ] when
86 blank-line last-element set ;
89 : ($heading) ( children quot -- )
92 : $heading ( element -- )
93 [ heading-style get print-element* ] ($heading) ;
95 : $subheading ( element -- )
96 [ strong-style get print-element* ] ($heading) ;
98 : ($code-style) ( presentation -- hash )
99 presented associate code-style get assoc-union ;
101 : ($code) ( presentation quot -- )
103 code-char-style get [
105 [ ($code-style) ] dip with-nesting
109 : $code ( element -- )
110 "\n" join dup <input> [ write ] ($code) ;
112 : $syntax ( element -- ) "Syntax" $heading $code ;
114 : $description ( element -- )
115 "Word description" $heading print-element ;
117 : $class-description ( element -- )
118 "Class description" $heading print-element ;
120 : $error-description ( element -- )
121 "Error description" $heading print-element ;
123 : $var-description ( element -- )
124 "Variable description" $heading print-element ;
126 : $contract ( element -- )
127 "Generic word contract" $heading print-element ;
129 : $examples ( element -- )
130 "Examples" $heading print-element ;
132 : $example ( element -- )
133 1 cut* [ "\n" join ] bi@ over <input> [
134 [ print ] [ output-style get format ] bi*
137 : $unchecked-example ( element -- )
138 #! help-lint ignores these.
141 : $markup-example ( element -- )
142 first dup unparse " print-element" append 1array $code
145 : $warning ( element -- )
149 "Warning" $heading print-element
153 : $deprecated ( element -- )
155 deprecated-style get [
157 "This word is deprecated" $heading print-element
162 : $image ( element -- )
163 [ first write-image ] ($span) ;
165 : <$image> ( path -- element )
166 1array \ $image prefix ;
172 : write-link ( string object -- )
173 link-style get [ write-object ] with-style ;
175 : link-icon ( topic -- )
176 definition-icon 1array $image ;
178 : link-text ( topic -- )
179 [ article-name ] keep write-link ;
181 GENERIC: link-long-text ( topic -- )
183 M: topic link-long-text
184 [ article-title ] keep write-link ;
186 GENERIC: link-effect? ( word -- ? )
188 M: parsing-word link-effect? drop f ;
189 M: symbol link-effect? drop f ;
190 M: word link-effect? drop t ;
192 : $effect ( effect -- )
193 effect>string stack-effect-style get format ;
195 M: word link-long-text
196 dup presented associate [
197 [ article-name link-style get format ]
200 bl stack-effect $effect
205 : >topic ( obj -- topic ) dup topic? [ >link ] unless ;
207 : topic-span ( topic quot -- ) [ >topic ] dip ($span) ; inline
211 : ($link) ( topic -- ) [ link-text ] topic-span ;
212 : $link ( element -- ) first ($link) ;
214 : ($long-link) ( topic -- ) [ link-long-text ] topic-span ;
215 : $long-link ( element -- ) first ($long-link) ;
217 : ($pretty-link) ( topic -- )
218 [ [ link-icon ] [ drop bl ] [ link-text ] tri ] topic-span ;
219 : $pretty-link ( element -- ) first ($pretty-link) ;
221 : ($long-pretty-link) ( topic -- )
222 [ [ link-icon ] [ drop bl ] [ link-long-text ] tri ] topic-span ;
224 : <$pretty-link> ( definition -- element )
225 1array \ $pretty-link prefix ;
227 : ($subsection) ( element quot -- )
229 subsection-style get [ call ] with-style
232 : $subsection* ( topic -- )
234 [ ($long-pretty-link) ] with-scope
237 : $subsections ( children -- )
238 [ $subsection* ] each ($blank-line) ;
240 : $subsection ( element -- )
243 : ($vocab-link) ( text vocab -- )
244 >vocab-link write-link ;
246 : $vocab-subsection ( element -- )
248 first2 dup vocab-help
249 [ 2nip ($long-pretty-link) ]
250 [ [ >vocab-link link-icon bl ] [ ($vocab-link) ] bi ]
254 : $vocab-link ( element -- )
255 first dup vocab-name swap ($vocab-link) ;
257 : $vocabulary ( element -- )
259 "Vocabulary" $heading nl dup ($vocab-link)
262 : (textual-list) ( seq quot sep -- )
263 '[ _ print-element ] swap interleave ; inline
265 : textual-list ( seq quot -- )
266 ", " (textual-list) ; inline
268 : $links ( topics -- )
269 [ [ ($link) ] textual-list ] ($span) ;
271 : $vocab-links ( vocabs -- )
272 [ vocab ] map $links ;
274 : $breadcrumbs ( topics -- )
275 [ [ ($link) ] " > " (textual-list) ] ($span) ;
277 : $see-also ( topics -- )
278 "See also" $heading $links ;
280 : related-words ( seq -- )
281 dup '[ _ "related" set-word-prop ] each ;
283 : $related ( element -- )
284 first dup "related" word-prop remove
285 [ $see-also ] unless-empty ;
287 : ($grid) ( style quot -- )
289 table-content-style get [
290 swap [ last-element off call ] tabular-output
294 : $list ( element -- )
298 bullet get write-cell
299 [ print-element ] with-cell
304 : $table ( element -- )
308 [ [ print-element ] with-cell ] each
313 : a/an ( str -- str )
314 [ first ] [ length ] bi 1 =
315 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
317 GENERIC: ($instance) ( element -- )
320 dup name>> a/an write bl ($link) ;
322 M: string ($instance)
328 : $instance ( element -- ) first ($instance) ;
332 { 1 [ first ($instance) ] }
333 { 2 [ first2 [ ($instance) " or " print-element ] [ ($instance) ] bi* ] }
337 [ [ ($instance) ", " print-element ] each ]
338 [ "or " print-element ($instance) ]
343 : $maybe ( element -- )
346 : $quotation ( element -- )
347 { "a " { $link quotation } " with stack effect " } print-element
350 : values-row ( seq -- seq )
351 unclip \ $snippet swap present 2array
352 swap dup first word? [ \ $instance prefix ] when 2array ;
354 : $values ( element -- )
355 "Inputs and outputs" $heading
356 [ values-row ] map $table ;
358 : $side-effects ( element -- )
359 "Side effects" $heading "Modifies " print-element
360 [ $snippet ] textual-list ;
362 : $errors ( element -- )
363 "Errors" $heading print-element ;
365 : $notes ( element -- )
366 "Notes" $heading print-element ;
368 : ($see) ( word quot -- )
370 code-char-style get [
371 code-style get swap with-nesting
375 : $see ( element -- ) first [ see* ] ($see) ;
377 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
379 : $definition ( element -- )
380 "Definition" $heading $see ;
382 : $methods ( element -- )
388 : $value ( object -- )
389 "Variable value" $heading
390 "Current value in global namespace:" print-element
391 first dup [ pprint-short ] ($code) ;
393 : $curious ( element -- )
394 "For the curious..." $heading print-element ;
396 : $references ( element -- )
397 "References" $heading
398 unclip print-element [ \ $link swap ] { } map>assoc $list ;
400 : $shuffle ( element -- )
402 "Shuffle word. Rearranges the top of the datastack as indicated in the stack effect pattern." $description ;
404 : $complex-shuffle ( element -- )
406 "Shuffle word. Rearranges the top of the datastack as indicated in the stack effect pattern." $description
407 { "The data flow represented by this shuffle word can be more clearly expressed using " { $link "locals" } "." } $deprecated ;
409 : $low-level-note ( children -- )
411 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
413 : $values-x/y ( children -- )
414 drop { { "x" number } { "y" number } } $values ;
416 : $parsing-note ( children -- )
418 "This word should only be called from parsing words."
421 : $io-error ( children -- )
423 "Throws an error if the I/O operation fails." $errors ;
425 : $prettyprinting-note ( children -- )
427 "This word should only be called from inside the "
428 { $link with-pprint } " combinator."
431 GENERIC: elements* ( elt-type element -- )
433 M: simple-element elements*
434 [ elements* ] with each ;
436 M: object elements* 2drop ;
439 [ dup first \ $markup-example eq? [ 2drop ] [ [ elements* ] with each ] if ]
440 [ [ first eq? ] keep swap [ , ] [ drop ] if ] 2bi ;
442 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
444 : collect-elements ( element seq -- elements )
445 swap '[ _ elements [ rest ] map concat ] map concat prune ;
447 : <$link> ( topic -- element )
448 1array \ $link prefix ;
450 : <$snippet> ( str -- element )
451 1array \ $snippet prefix ;
453 : $definition-icons ( element -- )
455 icons get >alist sort-keys
456 [ [ <$link> ] [ definition-icon-path <$image> ] bi* swap ] assoc-map
457 { "" "Definition class" } prefix