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
6 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 ;
31 M: word print-element { } swap execute ;
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 : write-link ( string object -- )
142 link-style get [ write-object ] with-style ;
144 : ($link) ( article -- )
145 [ [ article-name ] [ >link ] bi write-link ] ($span) ;
147 : $link ( element -- )
150 : ($long-link) ( object -- )
151 [ article-title ] [ >link ] bi write-link ;
153 : ($subsection) ( element quot -- )
155 subsection-style get [
161 : $subsection ( element -- )
162 [ first ($long-link) ] ($subsection) ;
164 : ($vocab-link) ( text vocab -- )
165 >vocab-link write-link ;
167 : $vocab-subsection ( element -- )
169 first2 dup vocab-help dup [
176 : $vocab-link ( element -- )
177 first dup vocab-name swap ($vocab-link) ;
179 : $vocabulary ( element -- )
181 "Vocabulary" $heading nl dup ($vocab-link)
184 : textual-list ( seq quot -- )
185 [ ", " print-element ] swap interleave ; inline
187 : $links ( topics -- )
188 [ [ ($link) ] textual-list ] ($span) ;
190 : $vocab-links ( vocabs -- )
191 [ vocab ] map $links ;
193 : $see-also ( topics -- )
194 "See also" $heading $links ;
196 : related-words ( seq -- )
197 dup [ "related" set-word-prop ] curry each ;
199 : $related ( element -- )
200 first dup "related" word-prop remove
201 [ $see-also ] unless-empty ;
203 : ($grid) ( style quot -- )
205 table-content-style get [
206 swap [ last-element off call ] tabular-output
208 ] ($block) table last-element set ; inline
210 : $list ( element -- )
214 bullet get write-cell
215 [ print-element ] with-cell
220 : $table ( element -- )
224 [ [ print-element ] with-cell ] each
229 : a/an ( str -- str )
230 [ first ] [ length ] bi 1 =
231 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
233 GENERIC: ($instance) ( element -- )
236 dup name>> a/an write bl ($link) ;
238 M: string ($instance)
239 dup a/an write bl $snippet ;
244 : $instance ( element -- ) first ($instance) ;
248 { 1 [ first ($instance) ] }
249 { 2 [ first2 [ ($instance) " or " print-element ] [ ($instance) ] bi* ] }
253 [ [ ($instance) ", " print-element ] each ]
254 [ "or " print-element ($instance) ]
259 : $maybe ( element -- )
262 : $quotation ( element -- )
263 { "a " { $link quotation } " with stack effect " } print-element
266 : values-row ( seq -- seq )
267 unclip \ $snippet swap ?word-name 2array
268 swap dup first word? [ \ $instance prefix ] when 2array ;
270 : $values ( element -- )
271 "Inputs and outputs" $heading
272 [ values-row ] map $table ;
274 : $side-effects ( element -- )
275 "Side effects" $heading "Modifies " print-element
276 [ $snippet ] textual-list ;
278 : $errors ( element -- )
279 "Errors" $heading print-element ;
281 : $notes ( element -- )
282 "Notes" $heading print-element ;
284 : ($see) ( word quot -- )
287 code-style get swap with-nesting
291 : $see ( element -- ) first [ see ] ($see) ;
293 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
295 : $definition ( element -- )
296 "Definition" $heading $see ;
298 : $methods ( element -- )
304 : $value ( object -- )
305 "Variable value" $heading
306 "Current value in global namespace:" print-element
307 first dup [ pprint-short ] ($code) ;
309 : $curious ( element -- )
310 "For the curious..." $heading print-element ;
312 : $references ( element -- )
313 "References" $heading
314 unclip print-element [ \ $link swap ] { } map>assoc $list ;
316 : $shuffle ( element -- )
318 "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
320 : $low-level-note ( children -- )
322 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
324 : $values-x/y ( children -- )
325 drop { { "x" number } { "y" number } } $values ;
327 : $parsing-note ( children -- )
329 "This word should only be called from parsing words."
332 : $io-error ( children -- )
334 "Throws an error if the I/O operation fails." $errors ;
336 : $prettyprinting-note ( children -- )
338 "This word should only be called from inside the "
339 { $link with-pprint } " combinator."
342 GENERIC: elements* ( elt-type element -- )
344 M: simple-element elements* [ elements* ] with each ;
346 M: object elements* 2drop ;
349 [ [ elements* ] with each ] 2keep
350 [ first eq? ] keep swap [ , ] [ drop ] if ;
352 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
354 : collect-elements ( element seq -- elements )
358 rest [ dup set ] each
361 ] H{ } make-assoc keys ;
363 : <$link> ( topic -- element )
364 \ $link swap 2array ;