1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays definitions generic io kernel assocs
4 hashtables namespaces make parser prettyprint sequences strings
5 io.styles vectors words math sorting splitting classes slots fry
6 sets vocabs help.stylesheet help.topics vocabs.loader quotations
10 PREDICATE: simple-element < array
11 [ t ] [ first word? not ] if-empty ;
17 : last-span? ( -- ? ) last-element get span eq? ;
18 : last-block? ( -- ? ) last-element get block eq? ;
21 last-block? [ nl ] when
25 GENERIC: print-element ( element -- )
27 M: simple-element print-element [ print-element ] each ;
28 M: string print-element [ write ] ($span) ;
29 M: array print-element unclip execute( arg -- ) ;
30 M: word print-element { } swap execute( arg -- ) ;
31 M: f print-element drop ;
33 : print-element* ( element style -- )
34 [ print-element ] with-style ;
36 : with-default-style ( quot -- )
37 default-span-style get [
39 default-block-style get swap with-nesting
42 : print-content ( element -- )
43 [ print-element ] with-default-style ;
45 : ($block) ( quot -- )
46 last-element get [ nl ] when
49 block last-element set ; inline
53 : $snippet ( children -- )
54 [ snippet-style get print-element* ] ($span) ;
59 : $emphasis ( children -- )
60 [ emphasis-style get print-element* ] ($span) ;
62 : $strong ( children -- )
63 [ strong-style get print-element* ] ($span) ;
65 : $url ( children -- )
67 dup first href associate url-style get assoc-union
75 : ($heading) ( children quot -- )
76 last-element get [ nl ] when ($block) ; inline
78 : $heading ( element -- )
79 [ heading-style get print-element* ] ($heading) ;
81 : $subheading ( element -- )
82 [ strong-style get print-element* ] ($heading) ;
84 : ($code-style) ( presentation -- hash )
85 presented associate code-style get assoc-union ;
87 : ($code) ( presentation quot -- )
91 [ ($code-style) ] dip with-nesting
95 : $code ( element -- )
96 "\n" join dup <input> [ write ] ($code) ;
98 : $syntax ( element -- ) "Syntax" $heading $code ;
100 : $description ( element -- )
101 "Word description" $heading print-element ;
103 : $class-description ( element -- )
104 "Class description" $heading print-element ;
106 : $error-description ( element -- )
107 "Error description" $heading print-element ;
109 : $var-description ( element -- )
110 "Variable description" $heading print-element ;
112 : $contract ( element -- )
113 "Generic word contract" $heading print-element ;
115 : $examples ( element -- )
116 "Examples" $heading print-element ;
118 : $example ( element -- )
119 1 cut* swap "\n" join dup <input> [
120 input-style get format nl print-element
123 : $unchecked-example ( element -- )
124 #! help-lint ignores these.
127 : $markup-example ( element -- )
128 first dup unparse " print-element" append 1array $code
131 : $warning ( element -- )
135 "Warning" $heading print-element
140 : $image ( element -- )
141 [ [ "" ] dip first image associate format ] ($span) ;
144 : write-link ( string object -- )
145 link-style get [ write-object ] with-style ;
147 : ($link) ( article -- )
148 [ [ article-name ] [ >link ] bi write-link ] ($span) ;
150 : $link ( element -- )
153 : ($definition-link) ( word -- )
154 [ article-name ] keep write-link ;
156 : $definition-link ( element -- )
157 first ($definition-link) ;
159 : ($long-link) ( object -- )
160 [ article-title ] [ >link ] bi write-link ;
162 : $long-link ( object -- )
165 : ($subsection) ( element quot -- )
167 subsection-style get [
173 : $subsection ( element -- )
174 [ first ($long-link) ] ($subsection) ;
176 : ($vocab-link) ( text vocab -- )
177 >vocab-link write-link ;
179 : $vocab-subsection ( element -- )
181 first2 dup vocab-help dup [
188 : $vocab-link ( element -- )
189 first dup vocab-name swap ($vocab-link) ;
191 : $vocabulary ( element -- )
193 "Vocabulary" $heading nl dup ($vocab-link)
196 : textual-list ( seq quot -- )
197 [ ", " print-element ] swap interleave ; inline
199 : $links ( topics -- )
200 [ [ ($link) ] textual-list ] ($span) ;
202 : $vocab-links ( vocabs -- )
203 [ vocab ] map $links ;
205 : $see-also ( topics -- )
206 "See also" $heading $links ;
208 : related-words ( seq -- )
209 dup '[ _ "related" set-word-prop ] each ;
211 : $related ( element -- )
212 first dup "related" word-prop remove
213 [ $see-also ] unless-empty ;
215 : ($grid) ( style quot -- )
217 table-content-style get [
218 swap [ last-element off call ] tabular-output
222 : $list ( element -- )
226 bullet get write-cell
227 [ print-element ] with-cell
232 : $table ( element -- )
236 [ [ print-element ] with-cell ] each
241 : a/an ( str -- str )
242 [ first ] [ length ] bi 1 =
243 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
245 GENERIC: ($instance) ( element -- )
248 dup name>> a/an write bl ($link) ;
250 M: string ($instance)
251 dup a/an write bl $snippet ;
256 : $instance ( element -- ) first ($instance) ;
260 { 1 [ first ($instance) ] }
261 { 2 [ first2 [ ($instance) " or " print-element ] [ ($instance) ] bi* ] }
265 [ [ ($instance) ", " print-element ] each ]
266 [ "or " print-element ($instance) ]
271 : $maybe ( element -- )
274 : $quotation ( element -- )
275 { "a " { $link quotation } " with stack effect " } print-element
278 : values-row ( seq -- seq )
279 unclip \ $snippet swap ?word-name 2array
280 swap dup first word? [ \ $instance prefix ] when 2array ;
282 : $values ( element -- )
283 "Inputs and outputs" $heading
284 [ values-row ] map $table ;
286 : $side-effects ( element -- )
287 "Side effects" $heading "Modifies " print-element
288 [ $snippet ] textual-list ;
290 : $errors ( element -- )
291 "Errors" $heading print-element ;
293 : $notes ( element -- )
294 "Notes" $heading print-element ;
296 : ($see) ( word quot -- )
299 code-style get swap with-nesting
303 : $see ( element -- ) first [ see ] ($see) ;
305 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
307 : $definition ( element -- )
308 "Definition" $heading $see ;
310 : $methods ( element -- )
316 : $value ( object -- )
317 "Variable value" $heading
318 "Current value in global namespace:" print-element
319 first dup [ pprint-short ] ($code) ;
321 : $curious ( element -- )
322 "For the curious..." $heading print-element ;
324 : $references ( element -- )
325 "References" $heading
326 unclip print-element [ \ $link swap ] { } map>assoc $list ;
328 : $shuffle ( element -- )
330 "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
332 : $low-level-note ( children -- )
334 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
336 : $values-x/y ( children -- )
337 drop { { "x" number } { "y" number } } $values ;
339 : $parsing-note ( children -- )
341 "This word should only be called from parsing words."
344 : $io-error ( children -- )
346 "Throws an error if the I/O operation fails." $errors ;
348 : $prettyprinting-note ( children -- )
350 "This word should only be called from inside the "
351 { $link with-pprint } " combinator."
354 GENERIC: elements* ( elt-type element -- )
356 M: simple-element elements*
357 [ elements* ] with each ;
359 M: object elements* 2drop ;
362 [ [ elements* ] with each ] 2keep
363 [ first eq? ] keep swap [ , ] [ drop ] if ;
365 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
367 : collect-elements ( element seq -- elements )
368 swap '[ _ elements [ rest ] map concat ] map concat prune ;
370 : <$link> ( topic -- element )
371 1array \ $link prefix ;
373 : <$snippet> ( str -- element )
374 1array \ $snippet prefix ;