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 quotations ;
9 ! Simple markup language.
11 ! <element> ::== <string> | <simple-element> | <fancy-element>
12 ! <simple-element> ::== { <element>* }
13 ! <fancy-element> ::== { <type> <element> }
15 ! Element types are words whose name begins with $.
17 PREDICATE: simple-element < array
18 [ t ] [ first word? not ] if-empty ;
25 : last-span? ( -- ? ) last-element get span eq? ;
26 : last-block? ( -- ? ) last-element get block eq? ;
29 last-block? [ nl ] when
33 GENERIC: print-element ( element -- )
35 M: simple-element print-element [ print-element ] each ;
36 M: string print-element [ write ] ($span) ;
37 M: array print-element unclip execute ;
38 M: word print-element { } swap execute ;
39 M: f print-element drop ;
41 : print-element* ( element style -- )
42 [ print-element ] with-style ;
44 : with-default-style ( quot -- )
45 default-span-style get [
47 default-block-style get swap with-nesting
50 : print-content ( element -- )
51 [ print-element ] with-default-style ;
53 : ($block) ( quot -- )
54 last-element get { f table } member? [ nl ] unless
57 block last-element set ; inline
61 : $snippet ( children -- )
62 [ snippet-style get print-element* ] ($span) ;
67 : $emphasis ( children -- )
68 [ emphasis-style get print-element* ] ($span) ;
70 : $strong ( children -- )
71 [ strong-style get print-element* ] ($span) ;
73 : $url ( children -- )
75 dup first href associate url-style get assoc-union
83 : ($heading) ( children quot -- )
84 last-element get [ nl ] when ($block) ; inline
86 : $heading ( element -- )
87 [ heading-style get print-element* ] ($heading) ;
89 : $subheading ( element -- )
90 [ strong-style get print-element* ] ($heading) ;
92 : ($code-style) ( presentation -- hash )
93 presented associate code-style get assoc-union ;
95 : ($code) ( presentation quot -- )
99 [ ($code-style) ] dip with-nesting
103 : $code ( element -- )
104 "\n" join dup <input> [ write ] ($code) ;
106 : $syntax ( element -- ) "Syntax" $heading $code ;
108 : $description ( element -- )
109 "Word description" $heading print-element ;
111 : $class-description ( element -- )
112 "Class description" $heading print-element ;
114 : $error-description ( element -- )
115 "Error description" $heading print-element ;
117 : $var-description ( element -- )
118 "Variable description" $heading print-element ;
120 : $contract ( element -- )
121 "Generic word contract" $heading print-element ;
123 : $examples ( element -- )
124 "Examples" $heading print-element ;
126 : $example ( element -- )
127 1 cut* swap "\n" join dup <input> [
128 input-style get format nl print-element
131 : $unchecked-example ( element -- )
132 #! help-lint ignores these.
135 : $markup-example ( element -- )
136 first dup unparse " print-element" append 1array $code
139 : $warning ( element -- )
143 "Warning" $heading print-element
148 : write-link ( string object -- )
149 link-style get [ write-object ] with-style ;
151 : ($link) ( article -- )
152 [ [ article-name ] [ >link ] bi write-link ] ($span) ;
154 : $link ( element -- )
157 : ($long-link) ( object -- )
158 [ article-title ] [ >link ] bi write-link ;
160 : ($subsection) ( element quot -- )
162 subsection-style get [
168 : $subsection ( element -- )
169 [ first ($long-link) ] ($subsection) ;
171 : ($vocab-link) ( text vocab -- )
172 >vocab-link write-link ;
174 : $vocab-subsection ( element -- )
176 first2 dup vocab-help dup [
183 : $vocab-link ( element -- )
184 first dup vocab-name swap ($vocab-link) ;
186 : $vocabulary ( element -- )
188 "Vocabulary" $heading nl dup ($vocab-link)
191 : textual-list ( seq quot -- )
192 [ ", " print-element ] swap interleave ; inline
194 : $links ( topics -- )
195 [ [ ($link) ] textual-list ] ($span) ;
197 : $vocab-links ( vocabs -- )
198 [ vocab ] map $links ;
200 : $see-also ( topics -- )
201 "See also" $heading $links ;
203 : related-words ( seq -- )
204 dup [ "related" set-word-prop ] curry each ;
206 : $related ( element -- )
207 first dup "related" word-prop remove
208 [ $see-also ] unless-empty ;
210 : ($grid) ( style quot -- )
212 table-content-style get [
213 swap [ last-element off call ] tabular-output
215 ] ($block) table last-element set ; inline
217 : $list ( element -- )
221 bullet get write-cell
222 [ print-element ] with-cell
227 : $table ( element -- )
231 [ [ print-element ] with-cell ] each
236 : a/an ( str -- str )
237 [ first ] [ length ] bi 1 =
238 "afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
240 GENERIC: ($instance) ( element -- )
243 dup name>> a/an write bl ($link) ;
245 M: string ($instance)
246 dup a/an write bl $snippet ;
251 : $instance ( element -- ) first ($instance) ;
253 : $maybe ( element -- )
254 $instance " or " print-element { f } $instance ;
256 : $quotation ( element -- )
257 { "a " { $link quotation } " with stack effect " } print-element
260 : values-row ( seq -- seq )
261 unclip \ $snippet swap ?word-name 2array
262 swap dup first word? [ \ $instance prefix ] when 2array ;
264 : $values ( element -- )
265 "Inputs and outputs" $heading
266 [ values-row ] map $table ;
268 : $side-effects ( element -- )
269 "Side effects" $heading "Modifies " print-element
270 [ $snippet ] textual-list ;
272 : $errors ( element -- )
273 "Errors" $heading print-element ;
275 : $notes ( element -- )
276 "Notes" $heading print-element ;
278 : ($see) ( word quot -- )
281 code-style get swap with-nesting
285 : $see ( element -- ) first [ see ] ($see) ;
287 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
289 : $definition ( element -- )
290 "Definition" $heading $see ;
292 : $methods ( element -- )
298 : $value ( object -- )
299 "Variable value" $heading
300 "Current value in global namespace:" print-element
301 first dup [ pprint-short ] ($code) ;
303 : $curious ( element -- )
304 "For the curious..." $heading print-element ;
306 : $references ( element -- )
307 "References" $heading
308 unclip print-element [ \ $link swap ] { } map>assoc $list ;
310 : $shuffle ( element -- )
312 "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
314 : $low-level-note ( children -- )
316 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
318 : $values-x/y ( children -- )
319 drop { { "x" number } { "y" number } } $values ;
321 : $parsing-note ( children -- )
323 "This word should only be called from parsing words."
326 : $io-error ( children -- )
328 "Throws an error if the I/O operation fails." $errors ;
330 : $prettyprinting-note ( children -- )
332 "This word should only be called from inside the "
333 { $link with-pprint } " combinator."
336 GENERIC: elements* ( elt-type element -- )
338 M: simple-element elements* [ elements* ] with each ;
340 M: object elements* 2drop ;
343 [ [ elements* ] with each ] 2keep
344 [ first eq? ] keep swap [ , ] [ drop ] if ;
346 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
348 : collect-elements ( element seq -- elements )
352 rest [ dup set ] each
355 ] H{ } make-assoc keys ;
357 : <$link> ( topic -- element )
358 1array \ $link prefix ;
360 : <$snippet> ( str -- element )
361 1array \ $snippet prefix ;