]> 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: 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 ;
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 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 : $vocab-links ( vocabs -- )
192     [ vocab ] map $links ;
193
194 : $see-also ( topics -- )
195     "See also" $heading $links ;
196
197 : related-words ( seq -- )
198     dup [ "related" set-word-prop ] curry each ;
199
200 : $related ( element -- )
201     first dup "related" word-prop remove dup empty?
202     [ drop ] [ $see-also ] if ;
203
204 : ($grid) ( style quot -- )
205     [
206         table-content-style get [
207             swap [ last-element off call ] tabular-output
208         ] with-style
209     ] ($block) table last-element set ; inline
210
211 : $list ( element -- )
212     list-style get [
213         [
214             [
215                 bullet get write-cell
216                 [ print-element ] with-cell
217             ] with-row
218         ] each
219     ] ($grid) ;
220
221 : $table ( element -- )
222     table-style get [
223         [
224             [
225                 [ [ print-element ] with-cell ] each
226             ] with-row
227         ] each
228     ] ($grid) ;
229
230 : a/an ( str -- str )
231     first "aeiou" member? "an" "a" ? ;
232
233 GENERIC: ($instance) ( element -- )
234
235 M: word ($instance)
236     dup name>> a/an write bl ($link) ;
237
238 M: string ($instance)
239     dup a/an write bl $snippet ;
240
241 : $instance ( children -- ) first ($instance) ;
242
243 : values-row ( seq -- seq )
244     unclip \ $snippet swap ?word-name 2array
245     swap dup first word? [ \ $instance prefix ] when 2array ;
246
247 : $values ( element -- )
248     "Inputs and outputs" $heading
249     [ values-row ] map $table ;
250
251 : $side-effects ( element -- )
252     "Side effects" $heading "Modifies " print-element
253     [ $snippet ] textual-list ;
254
255 : $errors ( element -- )
256     "Errors" $heading print-element ;
257
258 : $notes ( element -- )
259     "Notes" $heading print-element ;
260
261 : ($see) ( word -- )
262     [
263         snippet-style get [
264             code-style get [ see ] with-nesting
265         ] with-style
266     ] ($block) ;
267
268 : $see ( element -- ) first ($see) ;
269
270 : $definition ( element -- )
271     "Definition" $heading $see ;
272
273 : $value ( object -- )
274     "Variable value" $heading
275     "Current value in global namespace:" print-element
276     first dup [ pprint-short ] ($code) ;
277
278 : $curious ( element -- )
279     "For the curious..." $heading print-element ;
280
281 : $references ( element -- )
282     "References" $heading
283     unclip print-element [ \ $link swap ] { } map>assoc $list ;
284
285 : $shuffle ( element -- )
286     drop
287     "Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
288
289 : $low-level-note ( children -- )
290     drop
291     "Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
292
293 : $values-x/y ( children -- )
294     drop { { "x" number } { "y" number } } $values ;
295
296 : $parsing-note ( children -- )
297     drop
298     "This word should only be called from parsing words."
299     $notes ;
300
301 : $io-error ( children -- )
302     drop
303     "Throws an error if the I/O operation fails." $errors ;
304
305 : $prettyprinting-note ( children -- )
306     drop {
307         "This word should only be called from inside the "
308         { $link with-pprint } " combinator."
309     } $notes ;
310
311 GENERIC: elements* ( elt-type element -- )
312
313 M: simple-element elements* [ elements* ] with each ;
314
315 M: object elements* 2drop ;
316
317 M: array elements*
318     [ [ elements* ] with each ] 2keep
319     [ first eq? ] keep swap [ , ] [ drop ] if ;
320
321 : elements ( elt-type element -- seq ) [ elements* ] { } make ;
322
323 : collect-elements ( element seq -- elements )
324     [
325         swap [
326             elements [
327                 rest [ dup set ] each
328             ] each
329         ] curry each
330     ] H{ } make-assoc keys ;