1 ! Copyright (C) 2005, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays definitions generic io kernel assocs hashtables
4 namespaces parser prettyprint sequences strings io.styles
5 vectors words math sorting splitting classes
6 slots vocabs help.stylesheet help.topics vocabs.loader ;
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: array simple-element
18 dup empty? [ drop t ] [ first word? not ] if ;
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 -- )
47 H{ } 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 [ snippet-style get print-element* ] ($span) ;
63 : $emphasis [ emphasis-style get print-element* ] ($span) ;
65 : $strong [ strong-style get print-element* ] ($span) ;
67 : $url [ url-style get print-element* ] ($span) ;
73 last-element get [ nl ] when ($block) ; inline
75 : $heading ( element -- )
76 [ heading-style get print-element* ] ($heading) ;
78 : $subheading ( element -- )
79 [ strong-style get print-element* ] ($heading) ;
81 : ($code-style) ( presentation -- hash )
82 presented associate code-style get union ;
84 : ($code) ( presentation quot -- )
88 >r ($code-style) r> with-nesting
92 : $code ( element -- )
93 "\n" join dup <input> [ write ] ($code) ;
95 : $syntax ( element -- ) "Syntax" $heading $code ;
97 : $description ( element -- )
98 "Word description" $heading print-element ;
100 : $class-description ( element -- )
101 "Class description" $heading print-element ;
103 : $error-description ( element -- )
104 "Error description" $heading print-element ;
106 : $var-description ( element -- )
107 "Variable description" $heading print-element ;
109 : $contract ( element -- )
110 "Generic word contract" $heading print-element ;
112 : $examples ( element -- )
113 "Examples" $heading print-element ;
115 : $example ( element -- )
116 1 swap cut* swap "\n" join dup <input> [
117 input-style get format nl print-element
120 : $unchecked-example ( element -- )
121 #! help-lint ignores these.
124 : $markup-example ( element -- )
125 first dup unparse " print-element" append 1array $code
128 : $warning ( element -- )
132 "Warning" $heading print-element
137 : write-link ( string object -- )
138 link-style get [ write-object ] with-style ;
140 : ($link) ( article -- )
141 dup article-name swap >link write-link
142 span last-element set ;
144 : $link ( element -- )
147 : ($subsection) ( object -- )
148 [ article-title ] keep >link write-object ;
150 : $subsection ( element -- )
152 subsection-style get [
158 : ($vocab-link) ( vocab -- ) dup f >vocab-link write-link ;
160 : $vocab-link ( element -- ) first ($vocab-link) ;
162 : $vocabulary ( element -- )
163 first word-vocabulary [
164 "Vocabulary" $heading nl ($vocab-link)
167 : textual-list ( seq quot -- )
168 [ ", " print-element ] swap interleave ; inline
170 : $links ( topics -- )
171 [ [ ($link) ] textual-list ] ($span) ;
173 : $see-also ( topics -- )
174 "See also" $heading $links ;
176 : related-words ( seq -- )
177 dup [ "related" set-word-prop ] curry each ;
179 : $related ( element -- )
180 first dup "related" word-prop remove dup empty?
181 [ drop ] [ $see-also ] if ;
183 : ($grid) ( style quot -- )
185 table-content-style get [
186 swap [ last-element off call ] tabular-output
188 ] ($block) table last-element set ; inline
190 : $list ( element -- )
194 bullet get write-cell
195 [ print-element ] with-cell
200 : $table ( element -- )
204 [ [ print-element ] with-cell ] each
209 : a/an ( str -- str )
210 first "aeiou" member? "an" "a" ? ;
212 GENERIC: ($instance) ( element -- )
215 dup word-name a/an write bl ($link) ;
217 M: string ($instance)
218 dup a/an write bl $snippet ;
220 : $instance first ($instance) ;
222 : values-row ( seq -- seq )
223 unclip \ $snippet swap ?word-name 2array
224 swap dup first word? [ \ $instance add* ] when 2array ;
226 : $values ( element -- )
227 "Inputs and outputs" $heading
228 [ values-row ] map $table ;
230 : $side-effects ( element -- )
231 "Side effects" $heading "Modifies " print-element
232 [ $snippet ] textual-list ;
234 : $errors ( element -- )
235 "Errors" $heading print-element ;
237 : $notes ( element -- )
238 "Notes" $heading print-element ;
243 code-style get [ see ] with-nesting
247 : $see ( element -- ) first ($see) ;
249 : $definition ( element -- )
250 "Definition" $heading $see ;
252 : $value ( object -- )
253 "Variable value" $heading
254 "Current value in global namespace:" print-element
255 first dup [ pprint-short ] ($code) ;
257 : $curious ( element -- )
258 "For the curious..." $heading print-element ;
260 : $references ( element -- )
261 "References" $heading
262 unclip print-element [ \ $link swap ] { } map>assoc $list ;
264 : $shuffle ( element -- )
266 "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
270 "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
273 drop { { "x" number } { "y" number } } $values ;
277 "Throws an error if the I/O operation fails." $errors ;
279 : $prettyprinting-note
281 "This word should only be called from inside the "
282 { $link with-pprint } " combinator."
285 : ($spec-reader-values) ( slot-spec class -- element )
286 dup ?word-name swap 2array
288 rot slot-spec-type 2array 2array
289 [ { $instance } swap add ] assoc-map ;
291 : $spec-reader-values ( slot-spec class -- )
292 ($spec-reader-values) $values ;
294 : $spec-reader-description ( slot-spec class -- )
296 "Outputs the value stored in the " ,
297 { $snippet } rot slot-spec-name add ,
299 { $instance } swap add ,
301 ] { } make $description ;
303 : $spec-reader ( reader slot-specs class -- )
306 2dup $spec-reader-values
307 2dup $spec-reader-description
310 GENERIC: slot-specs ( help-type -- specs )
312 M: word slot-specs "slots" word-prop ;
314 : $slot-reader ( reader -- )
315 first dup "reading" word-prop [ slot-specs ] keep
318 : $spec-writer-values ( slot-spec class -- )
319 ($spec-reader-values) reverse $values ;
321 : $spec-writer-description ( slot-spec class -- )
323 "Stores a new value to the " ,
324 { $snippet } rot slot-spec-name add ,
326 { $instance } swap add ,
328 ] { } make $description ;
330 : $spec-writer ( writer slot-specs class -- )
333 2dup $spec-writer-values
334 2dup $spec-writer-description
335 dup ?word-name 1array $side-effects
338 : $slot-writer ( reader -- )
339 first dup "writing" word-prop [ slot-specs ] keep
342 GENERIC: elements* ( elt-type element -- )
344 M: simple-element elements* [ elements* ] curry* each ;
346 M: object elements* 2drop ;
349 [ [ elements* ] curry* each ] 2keep
350 [ first eq? ] keep swap [ , ] [ drop ] if ;
352 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
354 : collect-elements ( element seq -- elements )
358 1 tail [ dup set ] each
361 ] H{ } make-assoc keys ;