[gforth] / gforth / wf.fs  

gforth: gforth/wf.fs


1 : pazsan 1.1 \ wiki forth
2 :    
3 :     require string.fs
4 :    
5 :     \ tag handling
6 :    
7 :     : .' '' parse postpone SLiteral postpone type ; immediate
8 : pazsan 1.3 : s' '' parse postpone SLiteral ; immediate
9 : pazsan 1.1
10 : pazsan 1.3 Variable tag-option
11 :     s" " tag-option $!
12 :    
13 :     : tag ( addr u -- ) '< emit type tag-option $@ type '> emit
14 :     s" " tag-option $! ;
15 : pazsan 1.1 : /tag ( addr u -- ) '< emit '/ emit type '> emit ;
16 : pazsan 1.2 : tagged ( addr1 u1 addr2 u2 -- ) 2dup 2>r tag type 2r> /tag ;
17 : pazsan 1.1
18 : pazsan 1.3 : opt ( addr u opt u -- ) s" " tag-option $+!
19 :     tag-option $+! s' ="' tag-option $+! tag-option $+!
20 :     s' "' tag-option $+! ;
21 :     : href= ( addr u -- ) s" href" opt ;
22 : pazsan 1.10 : name= ( addr u -- ) s" name" opt ;
23 : pazsan 1.3 : src= ( addr u -- ) s" src" opt ;
24 : pazsan 1.4 : alt= ( addr u -- ) s" alt" opt ;
25 : pazsan 1.7 : width= ( addr u -- ) s" width" opt ;
26 :     : height= ( addr u -- ) s" height" opt ;
27 : pazsan 1.3 : align= ( addr u -- ) s" align" opt ;
28 :     : mailto: ( addr u -- ) s' href="mailto:' tag-option $+!
29 :     tag-option $+! s' "' tag-option $+! ;
30 :    
31 : pazsan 1.1 \ environment handling
32 :    
33 :     Variable oldenv
34 : pazsan 1.10 Variable envs 30 0 [DO] 0 , [LOOP]
35 : pazsan 1.1
36 :     : env$ ( -- addr ) envs dup @ 1+ cells + ;
37 :     : env ( addr u -- ) env$ $! ;
38 :     : env? ( -- ) envs @ oldenv @
39 :     2dup > IF env$ $@ tag THEN
40 :     2dup < IF env$ cell+ $@ /tag env$ cell+ $off THEN
41 :     drop oldenv ! ;
42 :     : +env 1 envs +! ;
43 :     : -env -1 envs +! env? ;
44 :     : -envs envs @ 0 ?DO -env cr LOOP ;
45 : pazsan 1.2 : >env ( addr u -- ) +env env env? ;
46 : pazsan 1.1
47 : pazsan 1.6 \ alignment
48 :    
49 :     : >align ( c -- )
50 :     CASE
51 :     'l OF s" left" align= ENDOF
52 :     'r OF s" right" align= ENDOF
53 :     'c OF s" center" align= ENDOF
54 :     '< OF s" left" align= ENDOF
55 :     '> OF s" right" align= ENDOF
56 :     '| OF s" center" align= ENDOF
57 :     ENDCASE ;
58 :    
59 : pazsan 1.10 : >border ( c -- )
60 :     case
61 :     '- of s" 0" s" border" opt endof
62 :     '+ of s" 1" s" border" opt endof
63 :     endcase ;
64 :    
65 : pazsan 1.7 \ image handling
66 :    
67 :     Create imgbuf $20 allot
68 :    
69 :     Create pngsig $89 c, $50 c, $4E c, $47 c, $0D c, $0A c, $1A c, $0A c,
70 :     Create jfif $FF c, $D8 c, $FF c, $E0 c, $00 c, $10 c, $4A c, $46 c,
71 :     $49 c, $46 c,
72 :    
73 :     : b@ ( addr -- x ) 0 swap 4 bounds ?DO 8 lshift I c@ + LOOP ;
74 :     : bw@ ( addr -- x ) 0 swap 2 bounds ?DO 8 lshift I c@ + LOOP ;
75 :    
76 :     : gif? ( -- flag )
77 :     s" GIF89a" imgbuf over compare 0=
78 :     s" GIF87a" imgbuf over compare 0= or ;
79 :     : gif-size ( -- w h )
80 : pazsan 1.10 imgbuf 8 + c@ imgbuf 9 + c@ 8 lshift +
81 :     imgbuf 6 + c@ imgbuf 7 + c@ 8 lshift + ;
82 : pazsan 1.7
83 :     : png? ( -- flag )
84 :     pngsig 8 imgbuf over compare 0= ;
85 :     : png-size ( -- w h )
86 : pazsan 1.10 imgbuf $14 + b@ imgbuf $10 + b@ ;
87 : pazsan 1.7
88 :     : jpg? ( -- flag )
89 :     jfif 10 imgbuf over compare 0= ;
90 :     : jpg-size ( fd -- w h ) >r
91 :     2. BEGIN
92 :     2dup r@ reposition-file throw
93 :     imgbuf $10 r@ read-file throw 0<>
94 : pazsan 1.8 imgbuf bw@ $FFC0 $FFD0 within 0= and WHILE
95 : pazsan 1.7 imgbuf 2 + bw@ 2 + 0 d+ REPEAT
96 :     2drop imgbuf 5 + bw@ imgbuf 7 + bw@ rdrop ;
97 :    
98 :     : img-size ( fd -- w h ) >r
99 :     gif? IF gif-size rdrop EXIT THEN
100 :     jpg? IF r> jpg-size EXIT THEN
101 :     png? IF png-size rdrop EXIT THEN
102 :     0 0 ;
103 :    
104 :     : .img-size ( addr u -- )
105 : pazsan 1.10 r/o open-file IF drop EXIT THEN >r
106 : pazsan 1.7 imgbuf $20 r@ read-file throw drop
107 :     r@ img-size
108 :     r> close-file throw
109 :     ?dup IF 0 <# #S #> width= THEN
110 :     ?dup IF 0 <# #S #> height= THEN ;
111 :    
112 : pazsan 1.1 \ link creation
113 :    
114 :     Variable link
115 : pazsan 1.10 Variable link-sig
116 : pazsan 1.1 Variable link-suffix
117 : pazsan 1.3 Variable iconpath
118 : pazsan 1.1
119 : pazsan 1.2 Variable do-size
120 : pazsan 1.9 Variable do-icon
121 : pazsan 1.2
122 : pazsan 1.8 Defer parse-line
123 :    
124 : pazsan 1.7 : alt-suffix ( -- )
125 :     link-suffix $@len 2 - link-suffix $!len
126 :     s" [" link-suffix 0 $ins
127 :     s" ]" link-suffix $+!
128 :     link-suffix $@ alt= ;
129 :    
130 : pazsan 1.6 : get-icon ( addr u -- ) iconpath @ IF 2drop EXIT THEN
131 :     link-suffix $! s" .*" link-suffix $+!
132 : pazsan 1.1 s" icons" open-dir throw >r
133 :     BEGIN
134 :     pad $100 r@ read-dir throw WHILE
135 :     pad swap 2dup link-suffix $@ filename-match
136 : pazsan 1.3 IF s" icons/" iconpath $! iconpath $+!
137 : pazsan 1.10 iconpath $@ 2dup .img-size src= '- >border
138 : pazsan 1.7 alt-suffix s" img" tag true
139 : pazsan 1.1 ELSE 2drop false THEN
140 : pazsan 1.6 UNTIL ELSE drop THEN
141 : pazsan 1.1 r> close-dir throw ;
142 :    
143 : pazsan 1.9 : link-icon? ( -- ) do-icon @ 0= ?EXIT
144 :     iconpath @ IF iconpath $off THEN
145 :     link $@ + 1- c@ '/ = IF s" index.html" ELSE link $@ THEN
146 : pazsan 1.8 BEGIN '. $split 2swap 2drop dup WHILE
147 :     2dup get-icon REPEAT 2drop ;
148 : pazsan 1.6
149 : pazsan 1.2 : link-size? ( -- ) do-size @ 0= ?EXIT
150 : pazsan 1.1 link $@ r/o open-file IF drop EXIT THEN >r
151 :     r@ file-size throw $400 um/mod nip ." (" 0 u.r ." k)"
152 :     r> close-file throw ;
153 :    
154 : pazsan 1.10 : link-sig? ( -- )
155 :     link $@ link-sig $! s" .sig" link-sig $+!
156 :     link-sig $@ r/o open-file IF drop EXIT THEN
157 :     close-file throw
158 :     ." (" link-sig $@ href= s" a" tag
159 :     s" icons/sig.gif" '- >border src= s" img" tag ." sig" s" /a" tag ." )" ;
160 :    
161 : pazsan 1.2 : link-options ( addr u -- addr' u' )
162 : pazsan 1.9 do-size off do-icon on
163 :     over c@ '% = over 0> and IF do-size on 1 /string THEN
164 :     over c@ '\ = over 0> and IF do-icon off 1 /string THEN ;
165 : pazsan 1.2
166 : pazsan 1.8 : parse-string ( addr u -- )
167 :     evaluate-input cell new-tib #tib ! tib !
168 :     ['] parse-line catch pop-file throw ;
169 :    
170 : pazsan 1.9 : .link ( addr u -- ) '| $split
171 : pazsan 1.2 link-options link $!
172 : pazsan 1.1 link $@len 0= IF 2dup link $! s" .html" link $+! THEN
173 : pazsan 1.10 link $@ href= s" a" tag link-icon?
174 :     parse-string s" a" /tag link-size? link-sig? ;
175 : pazsan 1.9 : >link ( -- ) '[ parse type '] parse .link ;
176 : pazsan 1.1
177 : pazsan 1.9 : .img ( addr u -- ) '| $split
178 : pazsan 1.6 dup IF 2swap alt= ELSE 2drop THEN
179 : pazsan 1.10 tag-option $@len >r over c@ >align tag-option $@len r> = 1+ /string
180 :     tag-option $@len >r over c@ >border tag-option $@len r> = 1+ /string
181 : pazsan 1.7 2dup .img-size src= s" img" tag ;
182 : pazsan 1.9 : >img ( -- ) '{ parse type '} parse .img ;
183 : pazsan 1.4
184 : pazsan 1.1 \ line handling
185 :    
186 : pazsan 1.4 : char? ( -- c ) >in @ char swap >in ! ;
187 : pazsan 1.1 : parse-tag ( addr u char -- )
188 :     >r r@ parse type
189 : pazsan 1.2 r> parse 2swap tagged ;
190 : pazsan 1.1
191 : pazsan 1.6 : .text ( -- ) >in @ >r char drop
192 : pazsan 1.9 source r@ /string >in @ r> - nip
193 :     bounds ?DO I c@
194 :     case
195 :     '& of ." &amp;" endof
196 :     '< of ." &lt;" endof
197 :     dup emit
198 :     endcase
199 :     LOOP ;
200 : pazsan 1.6
201 :     Create do-words $100 0 [DO] ' .text , [LOOP]
202 :    
203 : pazsan 1.9 :noname '( emit 1 >in +! ; '( cells do-words + !
204 :    
205 : pazsan 1.6 : bind-char ( xt -- ) char cells do-words + ! ;
206 :    
207 :     : char>tag ( -- ) char >r
208 :     :noname bl sword postpone SLiteral r@ postpone Literal
209 :     postpone parse-tag postpone ; r> cells do-words + ! ;
210 : pazsan 1.1
211 : pazsan 1.6 char>tag * b
212 :     char>tag _ em
213 :     char>tag # code
214 :    
215 : pazsan 1.9 ' >link bind-char [
216 :     ' >img bind-char {
217 : pazsan 1.6
218 :     : do-word ( char -- ) cells do-words + perform ;
219 : pazsan 1.4
220 : pazsan 1.9 : word? ( -- addr u ) >in @ >r bl sword r> >in ! ;
221 :    
222 :     wordlist Constant autoreplacements
223 :    
224 : pazsan 1.8 :noname ( -- )
225 : pazsan 1.9 BEGIN char? do-word source nip >in @ = UNTIL ; is parse-line
226 :    
227 :     : parse-line+ ( -- )
228 :     BEGIN
229 :     word? autoreplacements search-wordlist
230 :     IF execute bl sword 2drop
231 :     source >in @ 1- /string drop c@ bl = >in +!
232 :     ELSE char? do-word THEN
233 :     source nip >in @ = UNTIL ;
234 : pazsan 1.4
235 :     : parse-to ( char -- ) >r
236 :     BEGIN char? dup r@ <> WHILE
237 :     do-word source nip >in @ = UNTIL ELSE drop THEN
238 :     r> parse type ;
239 : pazsan 1.1
240 : pazsan 1.9 \ autoreplace
241 :    
242 :     : autoreplace ( <[string|url]> -- )
243 :     get-current autoreplacements set-current
244 :     Create set-current
245 :     here 0 , '[ parse 2drop '] parse rot $!
246 :     DOES> $@ .link ;
247 :    
248 : pazsan 1.1 \ paragraph handling
249 :    
250 :     : parse-par ( -- )
251 : pazsan 1.9 BEGIN parse-line+ cr refill WHILE
252 : pazsan 1.1 source nip 0= UNTIL THEN ;
253 :    
254 :     : par ( addr u -- ) env? 2dup tag parse-par /tag cr cr ;
255 : pazsan 1.9 : line ( addr u -- ) env? 2dup tag parse-line+ /tag cr cr ;
256 : pazsan 1.1
257 : pazsan 1.10 \ scan strings
258 :    
259 :     : get-rest ( addr -- ) 0 parse -trailing rot $! ;
260 :     Create $lf 1 c, #lf c,
261 :     : get-par ( addr -- ) >r s" " r@ $+!
262 :     BEGIN 0 parse 2dup s" ." compare WHILE
263 :     r@ $@len IF $lf count r@ $+! THEN r@ $+!
264 :     refill 0= UNTIL ELSE 2drop THEN
265 :     rdrop ;
266 :    
267 :     \ toc handling
268 :    
269 :     Variable toc-link
270 :    
271 :     : >last ( addr link -- link' )
272 :     BEGIN dup @ WHILE @ REPEAT ! 0 ;
273 :    
274 :     : toc, ( n -- ) , '| parse here 0 , $! here 0 , get-rest ;
275 :     : up-toc align here toc-link >last , 0 toc, ;
276 :     : top-toc align here toc-link >last , 1 toc, ;
277 :     : this-toc align here toc-link >last , 2 toc, ;
278 :     : sub-toc align here toc-link >last , 3 toc, ;
279 :    
280 :     Variable toc-name
281 :    
282 :     : .toc-entry ( toc flag -- )
283 :     swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href= s" a" tag
284 :     1 /string toc-name $@ compare >r
285 :     $@ .img swap
286 :     IF
287 :     case
288 :     2 of s" -icons/arrow_up.jpg" .img endof
289 :     3 of
290 :     r@ 0= IF s" -icons/circle.jpg"
291 :     ELSE s" -icons/arrow_down.jpg" THEN .img endof
292 :     endcase
293 :     ELSE
294 :     case
295 :     0 of s" -icons/arrow_up.jpg" .img endof
296 :     1 of s" -icons/arrow_right.jpg" .img endof
297 :     2 of s" -icons/circle.jpg" .img endof
298 :     3 of s" -icons/arrow_down.jpg" .img endof
299 :     endcase
300 :     THEN
301 :     s" a" /tag rdrop
302 :     ;
303 :     : print-toc ( -- ) cr 0 parse
304 :     dup 0= IF toc-name $! 0 ELSE
305 :     toc-name $! toc-name $@ name= s" " s" a" tagged 2
306 :     THEN >r
307 :     toc-link BEGIN @ dup WHILE
308 :     dup cell+ @ 3 = r@ 0= and IF rdrop 1 >r s" br" tag THEN
309 :     dup cell+ @ r@ >= IF dup r@ 2 = .toc-entry THEN
310 :     dup cell+ @ 2 = r@ 2 = and IF s" br" tag THEN
311 :     REPEAT drop rdrop cr ;
312 :    
313 : pazsan 1.1 \ handle global tags
314 :    
315 : pazsan 1.8 Variable indentlevel
316 : pazsan 1.10 : indent ( n -- ) indentlevel @ over indentlevel !
317 :     2dup < IF swap DO -env -env LOOP EXIT THEN
318 :     2dup > IF DO s" dl" >env s" dt" >env LOOP EXIT THEN
319 :     2dup = IF drop IF -env s" dt" >env THEN THEN ;
320 : pazsan 1.8 : +indent ( -- ) -env s" dd" >env ;
321 :    
322 : pazsan 1.1 wordlist constant longtags
323 :    
324 :     Variable end-sec
325 :    
326 :     longtags set-current
327 :    
328 : pazsan 1.10 : --- 0 indent cr s" hr" tag cr +indent ;
329 : pazsan 1.8 : * 1 indent s" h1" line +indent ;
330 :     : ** 1 indent s" h2" line +indent ;
331 :     : *** 2 indent s" h3" line +indent ;
332 : pazsan 1.10 : -- 0 indent cr print-toc ;
333 : pazsan 1.1 : - s" ul" env s" li" par ;
334 :     : + s" ol" env s" li" par ;
335 :     : << +env ;
336 : pazsan 1.3 : <* s" center" >env ;
337 : pazsan 1.10 : <red s" #ff0000" s" color" opt s" font" >env ;
338 :     : red> -env ;
339 : pazsan 1.1 : >> -env ;
340 : pazsan 1.3 : *> -env ;
341 : pazsan 1.9 : :: interpret ;
342 : pazsan 1.8 : . end-sec on indentlevel off ;
343 : pazsan 1.9 : :code s" pre" >env
344 :     BEGIN source >in @ /string type cr refill WHILE
345 :     source s" :endcode" compare 0= UNTIL THEN
346 :     -env ;
347 : pazsan 1.1 : \ postpone \ ;
348 :    
349 :     definitions
350 : pazsan 1.3
351 :     \ Table
352 :    
353 :     Variable table-format
354 :     Variable table#
355 :    
356 : pazsan 1.6 : |tag table-format $@ table# @ /string drop c@ >align
357 :     >env 1 table# +! ;
358 : pazsan 1.3 : |d table# @ IF -env THEN s" td" |tag ;
359 :     : |h table# @ IF -env THEN s" th" |tag ;
360 :     : |line s" tr" >env table# off ;
361 :     : line| -env -env cr ;
362 :    
363 : pazsan 1.5 : next-char ( -- char ) source drop >in @ + c@ ;
364 :    
365 : pazsan 1.3 longtags set-current
366 :    
367 :     : <| s" table" >env bl sword table-format $! ;
368 :     : |> -env ;
369 :     : +| |line
370 :     BEGIN
371 : pazsan 1.5 |h '| parse-to next-char '+ = UNTIL line| ;
372 : pazsan 1.3 : -| |line
373 :     BEGIN
374 : pazsan 1.5 |d '| parse-to next-char '- = UNTIL line| ;
375 : pazsan 1.3
376 :     definitions
377 : pazsan 1.1
378 :     \ parse a section
379 :    
380 : pazsan 1.8 : section-line ( -- ) >in off
381 : pazsan 1.9 bl sword longtags search-wordlist
382 :     IF execute
383 : pazsan 1.8 ELSE source nip IF >in off s" p" par THEN THEN ;
384 : pazsan 1.1 : refill-loop ( -- ) end-sec off
385 : pazsan 1.8 BEGIN refill WHILE
386 :     section-line end-sec @ UNTIL THEN ;
387 : pazsan 1.1 : parse-section ( -- )
388 : pazsan 1.9 refill-loop ;
389 : pazsan 1.1
390 :     \ HTML head
391 :    
392 :     : .title ( addr u -- )
393 :     .' <!doctype html public "-//w3c//dtd html 4.0 transitional//en">' cr
394 : pazsan 1.2 s" html" >env s" head" >env
395 : pazsan 1.1 .' <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' cr
396 : pazsan 1.2 s" title" tagged cr
397 : pazsan 1.1 -env ;
398 :    
399 :     \ HTML trailer
400 :    
401 :     Variable mail
402 :     Variable mail-name
403 :    
404 :     : .trailer
405 : pazsan 1.2 s" address" >env s" center" >env
406 :     ." Last modified: " time&date rot 0 u.r swap 1-
407 : pazsan 1.1 s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
408 : pazsan 1.3 0 u.r ." by "
409 : pazsan 1.10 s" Mail|icons/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
410 : pazsan 1.1 -envs ;
411 :    
412 :     \ top word
413 :    
414 : pazsan 1.6 : parse" ( -- addr u ) '" parse 2drop '" parse ;
415 :    
416 : pazsan 1.1 : maintainer
417 : pazsan 1.6 bl sword mail $! parse" mail-name $! ;
418 :    
419 :     Variable style$
420 :     : style> style$ @ 0= IF s" " style$ $! THEN style$ $@ tag-option $! ;
421 :     : >style tag-option $@ style$ $! s" " tag-option $! ;
422 :    
423 :     : style style> opt >style ;
424 :     : background ( -- ) parse" s" background" style ;
425 :     : text ( -- ) parse" s" text" style ;
426 :     warnings @ warnings off
427 :     : link ( -- ) parse" s" link" style ;
428 :     warnings !
429 :     : vlink ( -- ) parse" s" vlink" style ;
430 :     : marginheight ( -- ) parse" s" marginheight" style ;
431 : pazsan 1.1
432 :     : wf ( -- )
433 :     outfile-id >r
434 :     bl sword r/w create-file throw to outfile-id
435 : pazsan 1.6 parse" .title
436 :     +env style> s" body" env env?
437 : pazsan 1.1 ['] parse-section catch .trailer
438 :     outfile-id close-file throw
439 :     r> to outfile-id
440 :     dup 0< IF throw ELSE drop THEN ;
441 :    
442 : pazsan 1.8 : eval-par ( addr u -- )
443 :     s" wf-temp.wf" r/w create-file throw >r
444 :     r@ write-file r> close-file throw
445 :     push-file s" wf-temp.wf" r/o open-file throw loadfile !
446 :     parse-par parse-section
447 :     loadfile @ close-file swap 2dup or
448 :     pop-file drop throw throw
449 :     s" wf-temp.wf" delete-file throw ;
450 :    
451 : pazsan 1.2 \ simple text data base
452 :    
453 :     Variable last-entry
454 :     Variable field#
455 :    
456 :     : table: ( xt n -- ) Create , , 1 field# !
457 :     DOES> 2@ >in @ >r longtags set-current
458 :     Create definitions swap , r> >in !
459 :     here last-entry !
460 :     dup 0 DO 0 , LOOP
461 :     1 DO s" " last-entry @ I cells + $! LOOP
462 :     last-entry @ get-rest
463 :     DOES> dup cell+ swap perform ;
464 :    
465 :     : field: Create field# @ , 1 field# +!
466 :     DOES> @ cells last-entry @ + get-rest ;
467 : pazsan 1.8 : par: Create field# @ , 1 field# +!
468 :     DOES> @ cells last-entry @ + get-par ;
469 : pazsan 1.3
470 :     : >field ' >body @ cells postpone Literal postpone + ; immediate

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help