]> gitweb.factorcode.org Git - factor.git/blob - basis/help/markup/markup.factor
3077a93ed4b6983b0213df7e5ab98ee46601bfbd
[factor.git] / basis / help / markup / markup.factor
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 parser prettyprint sequences strings
5 io.styles vectors words math sorting splitting classes slots
6 vocabs help.stylesheet help.topics vocabs.loader alias ;
7 IN: help.markup
8
9 ! Simple markup language.
10
11 ! <element> ::== <string> | <simple-element> | <fancy-element>
12 ! <simple-element> ::== { <element>* }
13 ! <fancy-element> ::== { <type> <element> }
14
15 ! Element types are words whose name begins with $.
16
17 PREDICATE: simple-element < array
18     [ t ] [ first word? not ] if-empty ;
19
20 SYMBOL: last-element
21 SYMBOL: span
22 SYMBOL: block
23 SYMBOL: table
24
25 : last-span? ( -- ? ) last-element get span eq? ;
26 : last-block? ( -- ? ) last-element get block eq? ;
27
28 : ($span) ( quot -- )
29     last-block? [ nl ] when
30     span last-element set
31     call ; inline
32
33 GENERIC: print-element ( element -- )
34
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 ;
40
41 : print-element* ( element style -- )
42     [ print-element ] with-style ;
43
44 : with-default-style ( quot -- )
45     default-span-style get [
46         last-element off
47         default-block-style get swap with-nesting
48     ] with-style ; inline
49
50 : print-content ( element -- )
51     [ print-element ] with-default-style ;
52
53 : ($block) ( quot -- )
54     last-element get { f table } member? [ nl ] unless
55     span last-element set
56     call
57     block last-element set ; inline
58
59 ! Some spans
60
61 : $snippet ( children -- )
62     [ snippet-style get print-element* ] ($span) ;
63
64 ! for help-lint
65 ALIAS: $slot $snippet
66
67 : $emphasis ( children -- )
68     [ emphasis-style get print-element* ] ($span) ;
69
70 : $strong ( children -- )
71     [ strong-style get print-element* ] ($span) ;
72
73 : $url ( children -- )
74     [ url-style get print-element* ] ($span) ;
75
76 : $nl ( children -- )
77     nl nl drop ;
78
79 ! Some blocks
80 : ($heading) ( children quot -- )
81     last-element get [ nl ] when ($block) ; inline
82
83 : $heading ( element -- )
84     [ heading-style get print-element* ] ($heading) ;
85
86 : $subheading ( element -- )
87     [ strong-style get print-element* ] ($heading) ;
88
89 : ($code-style) ( presentation -- hash )
90     presented associate code-style get assoc-union ;
91
92 : ($code) ( presentation quot -- )
93     [
94         snippet-style get [
95             last-element off
96             >r ($code-style) r> with-nesting
97         ] with-style
98     ] ($block) ; inline
99
100 : $code ( element -- )
101     "\n" join dup <input> [ write ] ($code) ;
102
103 : $syntax ( element -- ) "Syntax" $heading $code ;
104
105 : $description ( element -- )
106     "Word description" $heading print-element ;
107
108 : $class-description ( element -- )
109     "Class description" $heading print-element ;
110
111 : $error-description ( element -- )
112     "Error description" $heading print-element ;
113
114 : $var-description ( element -- )
115     "Variable description" $heading print-element ;
116
117 : $contract ( element -- )
118     "Generic word contract" $heading print-element ;
119
120 : $examples ( element -- )
121     "Examples" $heading print-element ;
122
123 : $example ( element -- )
124     1 cut* swap "\n" join dup <input> [
125         input-style get format nl print-element
126     ] ($code) ;
127
128 : $unchecked-example ( element -- )
129     #! help-lint ignores these.
130     $example ;
131
132 : $markup-example ( element -- )
133     first dup unparse " print-element" append 1array $code
134     print-element ;
135
136 : $warning ( element -- )
137     [
138         warning-style get [
139             last-element off
140             "Warning" $heading print-element
141         ] with-nesting
142     ] ($heading) ;
143
144 ! Some links
145 : write-link ( string object -- )
146     link-style get [ write-object ] with-style ;
147
148 : ($link) ( article -- )
149     [ [ article-name ] [ >link ] bi write-link ] ($span) ;
150
151 : $link ( element -- )
152     first ($link) ;
153
154 : ($long-link) ( object -- )
155     [ article-title ] [ >link ] bi write-link ;
156
157 : ($subsection) ( element quot -- )
158     [
159         subsection-style get [
160             bullet get write bl
161             call
162         ] with-style
163     ] ($block) ; inline
164
165 : $subsection ( element -- )
166     [ first ($long-link) ] ($subsection) ;
167
168 : ($vocab-link) ( text vocab -- )
169     >vocab-link write-link ;
170
171 : $vocab-subsection ( element -- )
172     [
173         first2 dup vocab-help dup [
174             2nip ($long-link)
175         ] [
176             drop ($vocab-link)
177         ] if
178     ] ($subsection) ;
179
180 : $vocab-link ( element -- )
181     first dup vocab-name swap ($vocab-link) ;
182
183 : $vocabulary ( element -- )
184     first vocabulary>> [
185         "Vocabulary" $heading nl dup ($vocab-link)
186     ] when* ;
187
188 : textual-list ( seq quot -- )
189     [ ", " print-element ] swap interleave ; inline
190
191 : $links ( topics -- )
192     [ [ ($link) ] textual-list ] ($span) ;
193
194 : $vocab-links ( vocabs -- )
195     [ vocab ] map $links ;
196
197 : $see-also ( topics -- )
198     "See also" $heading $links ;
199
200 : related-words ( seq -- )
201     dup [ "related" set-word-prop ] curry each ;
202
203 : $related ( element -- )
204     first dup "related" word-prop remove
205     [ $see-also ] unless-empty ;
206
207 : ($grid) ( style quot -- )
208     [
209         table-content-style get [
210             swap [ last-element off call ] tabular-output
211         ] with-style
212     ] ($block) table last-element set ; inline
213
214 : $list ( element -- )
215     list-style get [
216         [
217             [
218                 bullet get write-cell
219                 [ print-element ] with-cell
220             ] with-row
221         ] each
222     ] ($grid) ;
223
224 : $table ( element -- )
225     table-style get [
226         [
227             [
228                 [ [ print-element ] with-cell ] each
229             ] with-row
230         ] each
231     ] ($grid) ;
232
233 : a/an ( str -- str )
234     first "aeiou" member? "an" "a" ? ;
235
236 GENERIC: ($instance) ( element -- )
237
238 M: word ($instance)
239     dup name>> a/an write bl ($link) ;
240
241 M: string ($instance)
242     dup a/an write bl $snippet ;
243
244 : $instance ( children -- ) first ($instance) ;
245
246 : values-row ( seq -- seq )
247     unclip \ $snippet swap ?word-name 2array
248     swap dup first word? [ \ $instance prefix ] when 2array ;
249
250 : $values ( element -- )
251     "Inputs and outputs" $heading
252     [ values-row ] map $table ;
253
254 : $side-effects ( element -- )
255     "Side effects" $heading "Modifies " print-element
256     [ $snippet ] textual-list ;
257
258 : $errors ( element -- )
259     "Errors" $heading print-element ;
260
261 : $notes ( element -- )
262     "Notes" $heading print-element ;
263
264 : ($see) ( word quot -- )
265     [
266         snippet-style get [
267             code-style get swap with-nesting
268         ] with-style
269     ] ($block) ; inline
270
271 : $see ( element -- ) first [ see ] ($see) ;
272
273 : $synopsis ( element -- ) first [ synopsis write ] ($see) ;
274
275 : $definition ( element -- )
276     "Definition" $heading $see ;
277
278 : $value ( object -- )
279     "Variable value" $heading
280     "Current value in global namespace:" print-element
281     first dup [ pprint-short ] ($code) ;
282
283 : $curious ( element -- )
284     "For the curious..." $heading print-element ;
285
286 : $references ( element -- )
287     "References" $heading
288     unclip print-element [ \ $link swap ] { } map>assoc $list ;
289
290 : $shuffle ( element -- )
291     drop
292     "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
293
294 : $low-level-note ( children -- )
295     drop
296     "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
297
298 : $values-x/y ( children -- )
299     drop { { "x" number } { "y" number } } $values ;
300
301 : $parsing-note ( children -- )
302     drop
303     "This word should only be called from parsing words."
304     $notes ;
305
306 : $io-error ( children -- )
307     drop
308     "Throws an error if the I/O operation fails." $errors ;
309
310 : $prettyprinting-note ( children -- )
311     drop {
312         "This word should only be called from inside the "
313         { $link with-pprint } " combinator."
314     } $notes ;
315
316 GENERIC: elements* ( elt-type element -- )
317
318 M: simple-element elements* [ elements* ] with each ;
319
320 M: object elements* 2drop ;
321
322 M: array elements*
323     [ [ elements* ] with each ] 2keep
324     [ first eq? ] keep swap [ , ] [ drop ] if ;
325
326 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
327
328 : collect-elements ( element seq -- elements )
329     [
330         swap [
331             elements [
332                 rest [ dup set ] each
333             ] each
334         ] curry each
335     ] H{ } make-assoc keys ;