1 ! Copyright (C) 2005, 2008 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 alias
10 ! Simple markup language.
12 ! <element> ::== <string> | <simple-element> | <fancy-element>
13 ! <simple-element> ::== { <element>* }
14 ! <fancy-element> ::== { <type> <element> }
16 ! Element types are words whose name begins with $.
18 PREDICATE: simple-element < array
19 [ t ] [ first word? not ] if-empty ;
26 : last-span? ( -- ? ) last-element get span eq? ;
27 : last-block? ( -- ? ) last-element get block eq? ;
30 last-block? [ nl ] when
34 GENERIC: print-element ( element -- )
36 M: simple-element print-element [ print-element ] each ;
37 M: string print-element [ write ] ($span) ;
38 M: array print-element unclip execute ;
39 M: word print-element { } swap execute ;
40 M: f print-element drop ;
42 : print-element* ( element style -- )
43 [ print-element ] with-style ;
45 : with-default-style ( quot -- )
46 default-span-style get [
48 default-block-style get swap with-nesting
51 : print-content ( element -- )
52 [ print-element ] with-default-style ;
54 : ($block) ( quot -- )
55 last-element get { f table } member? [ nl ] unless
58 block last-element set ; inline
62 : $snippet ( children -- )
63 [ snippet-style get print-element* ] ($span) ;
68 : $emphasis ( children -- )
69 [ emphasis-style get print-element* ] ($span) ;
71 : $strong ( children -- )
72 [ strong-style get print-element* ] ($span) ;
74 : $url ( children -- )
76 dup first href associate url-style get assoc-union
84 : ($heading) ( children quot -- )
85 last-element get [ nl ] when ($block) ; inline
87 : $heading ( element -- )
88 [ heading-style get print-element* ] ($heading) ;
90 : $subheading ( element -- )
91 [ strong-style get print-element* ] ($heading) ;
93 : ($code-style) ( presentation -- hash )
94 presented associate code-style get assoc-union ;
96 : ($code) ( presentation quot -- )
100 [ ($code-style) ] dip with-nesting
104 : $code ( element -- )
105 "\n" join dup <input> [ write ] ($code) ;
107 : $syntax ( element -- ) "Syntax" $heading $code ;
109 : $description ( element -- )
110 "Word description" $heading print-element ;
112 : $class-description ( element -- )
113 "Class description" $heading print-element ;
115 : $error-description ( element -- )
116 "Error description" $heading print-element ;
118 : $var-description ( element -- )
119 "Variable description" $heading print-element ;
121 : $contract ( element -- )
122 "Generic word contract" $heading print-element ;
124 : $examples ( element -- )
125 "Examples" $heading print-element ;
127 : $example ( element -- )
128 1 cut* swap "\n" join dup <input> [
129 input-style get format nl print-element
132 : $unchecked-example ( element -- )
133 #! help-lint ignores these.
136 : $markup-example ( element -- )
137 first dup unparse " print-element" append 1array $code
140 : $warning ( element -- )
144 "Warning" $heading print-element
149 : write-link ( string object -- )
150 link-style get [ write-object ] with-style ;
152 : ($link) ( article -- )
153 [ [ article-name ] [ >link ] bi write-link ] ($span) ;
155 : $link ( element -- )
158 : ($long-link) ( object -- )
159 [ article-title ] [ >link ] bi write-link ;
161 : ($subsection) ( element quot -- )
163 subsection-style get [
169 : $subsection ( element -- )
170 [ first ($long-link) ] ($subsection) ;
172 : ($vocab-link) ( text vocab -- )
173 >vocab-link write-link ;
175 : $vocab-subsection ( element -- )
177 first2 dup vocab-help dup [
184 : $vocab-link ( element -- )
185 first dup vocab-name swap ($vocab-link) ;
187 : $vocabulary ( element -- )
189 "Vocabulary" $heading nl dup ($vocab-link)
192 : textual-list ( seq quot -- )
193 [ ", " print-element ] swap interleave ; inline
195 : $links ( topics -- )
196 [ [ ($link) ] textual-list ] ($span) ;
198 : $vocab-links ( vocabs -- )
199 [ vocab ] map $links ;
201 : $see-also ( topics -- )
202 "See also" $heading $links ;
204 : related-words ( seq -- )
205 dup [ "related" set-word-prop ] curry each ;
207 : $related ( element -- )
208 first dup "related" word-prop remove
209 [ $see-also ] unless-empty ;
211 : ($grid) ( style quot -- )
213 table-content-style get [
214 swap [ last-element off call ] tabular-output
216 ] ($block) table last-element set ; inline
218 : $list ( element -- )
222 bullet get write-cell
223 [ print-element ] with-cell
228 : $table ( element -- )
232 [ [ print-element ] with-cell ] each
237 : a/an ( str -- str )
238 [ first ] [ length ] bi 1 =
239 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
241 GENERIC: ($instance) ( element -- )
244 dup name>> a/an write bl ($link) ;
246 M: string ($instance)
247 dup a/an write bl $snippet ;
252 : $instance ( element -- ) first ($instance) ;
254 : $maybe ( element -- )
255 $instance " or " print-element { f } $instance ;
257 : $quotation ( element -- )
258 { "a " { $link quotation } " with stack effect " } print-element
261 : values-row ( seq -- seq )
262 unclip \ $snippet swap ?word-name 2array
263 swap dup first word? [ \ $instance prefix ] when 2array ;
265 : $values ( element -- )
266 "Inputs and outputs" $heading
267 [ values-row ] map $table ;
269 : $side-effects ( element -- )
270 "Side effects" $heading "Modifies " print-element
271 [ $snippet ] textual-list ;
273 : $errors ( element -- )
274 "Errors" $heading print-element ;
276 : $notes ( element -- )
277 "Notes" $heading print-element ;
279 : ($see) ( word quot -- )
282 code-style get swap with-nesting
286 : $see ( element -- ) first [ see ] ($see) ;
288 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
290 : $definition ( element -- )
291 "Definition" $heading $see ;
293 : $methods ( element -- )
299 : $value ( object -- )
300 "Variable value" $heading
301 "Current value in global namespace:" print-element
302 first dup [ pprint-short ] ($code) ;
304 : $curious ( element -- )
305 "For the curious..." $heading print-element ;
307 : $references ( element -- )
308 "References" $heading
309 unclip print-element [ \ $link swap ] { } map>assoc $list ;
311 : $shuffle ( element -- )
313 "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
315 : $low-level-note ( children -- )
317 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
319 : $values-x/y ( children -- )
320 drop { { "x" number } { "y" number } } $values ;
322 : $parsing-note ( children -- )
324 "This word should only be called from parsing words."
327 : $io-error ( children -- )
329 "Throws an error if the I/O operation fails." $errors ;
331 : $prettyprinting-note ( children -- )
333 "This word should only be called from inside the "
334 { $link with-pprint } " combinator."
337 GENERIC: elements* ( elt-type element -- )
339 M: simple-element elements* [ elements* ] with each ;
341 M: object elements* 2drop ;
344 [ [ elements* ] with each ] 2keep
345 [ first eq? ] keep swap [ , ] [ drop ] if ;
347 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
349 : collect-elements ( element seq -- elements )
353 rest [ dup set ] each
356 ] H{ } make-assoc keys ;
358 : <$link> ( topic -- element )
359 \ $link swap 2array ;