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