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 ;
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 -- )
74 [ url-style get print-element* ] ($span) ;
80 : ($heading) ( children quot -- )
81 last-element get [ nl ] when ($block) ; inline
83 : $heading ( element -- )
84 [ heading-style get print-element* ] ($heading) ;
86 : $subheading ( element -- )
87 [ strong-style get print-element* ] ($heading) ;
89 : ($code-style) ( presentation -- hash )
90 presented associate code-style get assoc-union ;
92 : ($code) ( presentation quot -- )
96 >r ($code-style) r> with-nesting
100 : $code ( element -- )
101 "\n" join dup <input> [ write ] ($code) ;
103 : $syntax ( element -- ) "Syntax" $heading $code ;
105 : $description ( element -- )
106 "Word description" $heading print-element ;
108 : $class-description ( element -- )
109 "Class description" $heading print-element ;
111 : $error-description ( element -- )
112 "Error description" $heading print-element ;
114 : $var-description ( element -- )
115 "Variable description" $heading print-element ;
117 : $contract ( element -- )
118 "Generic word contract" $heading print-element ;
120 : $examples ( element -- )
121 "Examples" $heading print-element ;
123 : $example ( element -- )
124 1 cut* swap "\n" join dup <input> [
125 input-style get format nl print-element
128 : $unchecked-example ( element -- )
129 #! help-lint ignores these.
132 : $markup-example ( element -- )
133 first dup unparse " print-element" append 1array $code
136 : $warning ( element -- )
140 "Warning" $heading print-element
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 : ($long-link) ( object -- )
155 [ article-title ] [ >link ] bi write-link ;
157 : ($subsection) ( element quot -- )
159 subsection-style get [
165 : $subsection ( element -- )
166 [ first ($long-link) ] ($subsection) ;
168 : ($vocab-link) ( text vocab -- )
169 >vocab-link write-link ;
171 : $vocab-subsection ( element -- )
173 first2 dup vocab-help dup [
180 : $vocab-link ( element -- )
181 first dup vocab-name swap ($vocab-link) ;
183 : $vocabulary ( element -- )
185 "Vocabulary" $heading nl dup ($vocab-link)
188 : textual-list ( seq quot -- )
189 [ ", " print-element ] swap interleave ; inline
191 : $links ( topics -- )
192 [ [ ($link) ] textual-list ] ($span) ;
194 : $vocab-links ( vocabs -- )
195 [ vocab ] map $links ;
197 : $see-also ( topics -- )
198 "See also" $heading $links ;
200 : related-words ( seq -- )
201 dup [ "related" set-word-prop ] curry each ;
203 : $related ( element -- )
204 first dup "related" word-prop remove
205 [ $see-also ] unless-empty ;
207 : ($grid) ( style quot -- )
209 table-content-style get [
210 swap [ last-element off call ] tabular-output
212 ] ($block) table last-element set ; inline
214 : $list ( element -- )
218 bullet get write-cell
219 [ print-element ] with-cell
224 : $table ( element -- )
228 [ [ print-element ] with-cell ] each
233 : a/an ( str -- str )
234 first "aeiou" member? "an" "a" ? ;
236 GENERIC: ($instance) ( element -- )
239 dup name>> a/an write bl ($link) ;
241 M: string ($instance)
242 dup a/an write bl $snippet ;
244 : $instance ( children -- ) first ($instance) ;
246 : values-row ( seq -- seq )
247 unclip \ $snippet swap ?word-name 2array
248 swap dup first word? [ \ $instance prefix ] when 2array ;
250 : $values ( element -- )
251 "Inputs and outputs" $heading
252 [ values-row ] map $table ;
254 : $side-effects ( element -- )
255 "Side effects" $heading "Modifies " print-element
256 [ $snippet ] textual-list ;
258 : $errors ( element -- )
259 "Errors" $heading print-element ;
261 : $notes ( element -- )
262 "Notes" $heading print-element ;
264 : ($see) ( word quot -- )
267 code-style get swap with-nesting
271 : $see ( element -- ) first [ see ] ($see) ;
273 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
275 : $definition ( element -- )
276 "Definition" $heading $see ;
278 : $value ( object -- )
279 "Variable value" $heading
280 "Current value in global namespace:" print-element
281 first dup [ pprint-short ] ($code) ;
283 : $curious ( element -- )
284 "For the curious..." $heading print-element ;
286 : $references ( element -- )
287 "References" $heading
288 unclip print-element [ \ $link swap ] { } map>assoc $list ;
290 : $shuffle ( element -- )
292 "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
294 : $low-level-note ( children -- )
296 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
298 : $values-x/y ( children -- )
299 drop { { "x" number } { "y" number } } $values ;
301 : $parsing-note ( children -- )
303 "This word should only be called from parsing words."
306 : $io-error ( children -- )
308 "Throws an error if the I/O operation fails." $errors ;
310 : $prettyprinting-note ( children -- )
312 "This word should only be called from inside the "
313 { $link with-pprint } " combinator."
316 GENERIC: elements* ( elt-type element -- )
318 M: simple-element elements* [ elements* ] with each ;
320 M: object elements* 2drop ;
323 [ [ elements* ] with each ] 2keep
324 [ first eq? ] keep swap [ , ] [ drop ] if ;
326 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
328 : collect-elements ( element seq -- elements )
332 rest [ dup set ] each
335 ] H{ } make-assoc keys ;