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