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 ;
18 : last-span? ( -- ? ) last-element get span eq? ;
19 : last-block? ( -- ? ) last-element get block eq? ;
22 last-block? [ nl ] when
26 GENERIC: print-element ( element -- )
28 M: simple-element print-element [ print-element ] each ;
29 M: string print-element [ write ] ($span) ;
30 M: array print-element unclip execute( arg -- ) ;
31 M: word print-element { } swap execute( arg -- ) ;
32 M: f print-element drop ;
34 : print-element* ( element style -- )
35 [ print-element ] with-style ;
37 : with-default-style ( quot -- )
38 default-span-style get [
40 default-block-style get swap with-nesting
43 : print-content ( element -- )
44 [ print-element ] with-default-style ;
46 : ($block) ( quot -- )
47 last-element get { f table } member? [ nl ] unless
50 block last-element set ; inline
54 : $snippet ( children -- )
55 [ snippet-style get print-element* ] ($span) ;
60 : $emphasis ( children -- )
61 [ emphasis-style get print-element* ] ($span) ;
63 : $strong ( children -- )
64 [ strong-style get print-element* ] ($span) ;
66 : $url ( children -- )
68 dup first href associate url-style get assoc-union
76 : ($heading) ( children quot -- )
77 last-element get [ nl ] when ($block) ; inline
79 : $heading ( element -- )
80 [ heading-style get print-element* ] ($heading) ;
82 : $subheading ( element -- )
83 [ strong-style get print-element* ] ($heading) ;
85 : ($code-style) ( presentation -- hash )
86 presented associate code-style get assoc-union ;
88 : ($code) ( presentation quot -- )
92 [ ($code-style) ] dip with-nesting
96 : $code ( element -- )
97 "\n" join dup <input> [ write ] ($code) ;
99 : $syntax ( element -- ) "Syntax" $heading $code ;
101 : $description ( element -- )
102 "Word description" $heading print-element ;
104 : $class-description ( element -- )
105 "Class description" $heading print-element ;
107 : $error-description ( element -- )
108 "Error description" $heading print-element ;
110 : $var-description ( element -- )
111 "Variable description" $heading print-element ;
113 : $contract ( element -- )
114 "Generic word contract" $heading print-element ;
116 : $examples ( element -- )
117 "Examples" $heading print-element ;
119 : $example ( element -- )
120 1 cut* swap "\n" join dup <input> [
121 input-style get format nl print-element
124 : $unchecked-example ( element -- )
125 #! help-lint ignores these.
128 : $markup-example ( element -- )
129 first dup unparse " print-element" append 1array $code
132 : $warning ( element -- )
136 "Warning" $heading print-element
141 : $image ( element -- )
142 [ [ "" ] dip first image associate format ] ($span) ;
145 : write-link ( string object -- )
146 link-style get [ write-object ] with-style ;
148 : ($link) ( article -- )
149 [ [ article-name ] [ >link ] bi write-link ] ($span) ;
151 : $link ( element -- )
154 : ($definition-link) ( word -- )
155 [ article-name ] keep write-link ;
157 : $definition-link ( element -- )
158 first ($definition-link) ;
160 : ($long-link) ( object -- )
161 [ article-title ] [ >link ] bi write-link ;
163 : $long-link ( object -- )
166 : ($subsection) ( element quot -- )
168 subsection-style get [
174 : $subsection ( element -- )
175 [ first ($long-link) ] ($subsection) ;
177 : ($vocab-link) ( text vocab -- )
178 >vocab-link write-link ;
180 : $vocab-subsection ( element -- )
182 first2 dup vocab-help dup [
189 : $vocab-link ( element -- )
190 first dup vocab-name swap ($vocab-link) ;
192 : $vocabulary ( element -- )
194 "Vocabulary" $heading nl dup ($vocab-link)
197 : textual-list ( seq quot -- )
198 [ ", " print-element ] swap interleave ; inline
200 : $links ( topics -- )
201 [ [ ($link) ] textual-list ] ($span) ;
203 : $vocab-links ( vocabs -- )
204 [ vocab ] map $links ;
206 : $see-also ( topics -- )
207 "See also" $heading $links ;
209 : related-words ( seq -- )
210 dup '[ _ "related" set-word-prop ] each ;
212 : $related ( element -- )
213 first dup "related" word-prop remove
214 [ $see-also ] unless-empty ;
216 : ($grid) ( style quot -- )
218 table-content-style get [
219 swap [ last-element off call ] tabular-output
221 ] ($block) table last-element set ; inline
223 : $list ( element -- )
227 bullet get write-cell
228 [ print-element ] with-cell
233 : $table ( element -- )
237 [ [ print-element ] with-cell ] each
242 : a/an ( str -- str )
243 [ first ] [ length ] bi 1 =
244 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
246 GENERIC: ($instance) ( element -- )
249 dup name>> a/an write bl ($link) ;
251 M: string ($instance)
252 dup a/an write bl $snippet ;
257 : $instance ( element -- ) first ($instance) ;
261 { 1 [ first ($instance) ] }
262 { 2 [ first2 [ ($instance) " or " print-element ] [ ($instance) ] bi* ] }
266 [ [ ($instance) ", " print-element ] each ]
267 [ "or " print-element ($instance) ]
272 : $maybe ( element -- )
275 : $quotation ( element -- )
276 { "a " { $link quotation } " with stack effect " } print-element
279 : values-row ( seq -- seq )
280 unclip \ $snippet swap ?word-name 2array
281 swap dup first word? [ \ $instance prefix ] when 2array ;
283 : $values ( element -- )
284 "Inputs and outputs" $heading
285 [ values-row ] map $table ;
287 : $side-effects ( element -- )
288 "Side effects" $heading "Modifies " print-element
289 [ $snippet ] textual-list ;
291 : $errors ( element -- )
292 "Errors" $heading print-element ;
294 : $notes ( element -- )
295 "Notes" $heading print-element ;
297 : ($see) ( word quot -- )
300 code-style get swap with-nesting
304 : $see ( element -- ) first [ see ] ($see) ;
306 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
308 : $definition ( element -- )
309 "Definition" $heading $see ;
311 : $methods ( element -- )
317 : $value ( object -- )
318 "Variable value" $heading
319 "Current value in global namespace:" print-element
320 first dup [ pprint-short ] ($code) ;
322 : $curious ( element -- )
323 "For the curious..." $heading print-element ;
325 : $references ( element -- )
326 "References" $heading
327 unclip print-element [ \ $link swap ] { } map>assoc $list ;
329 : $shuffle ( element -- )
331 "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
333 : $low-level-note ( children -- )
335 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
337 : $values-x/y ( children -- )
338 drop { { "x" number } { "y" number } } $values ;
340 : $parsing-note ( children -- )
342 "This word should only be called from parsing words."
345 : $io-error ( children -- )
347 "Throws an error if the I/O operation fails." $errors ;
349 : $prettyprinting-note ( children -- )
351 "This word should only be called from inside the "
352 { $link with-pprint } " combinator."
355 GENERIC: elements* ( elt-type element -- )
357 M: simple-element elements*
358 [ elements* ] with each ;
360 M: object elements* 2drop ;
363 [ [ elements* ] with each ] 2keep
364 [ first eq? ] keep swap [ , ] [ drop ] if ;
366 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
368 : collect-elements ( element seq -- elements )
369 swap '[ _ elements [ rest ] map concat ] map concat prune ;
371 : <$link> ( topic -- element )
372 1array \ $link prefix ;
374 : <$snippet> ( str -- element )
375 1array \ $snippet prefix ;