[gforth] / gforth / wf.fs  

gforth: gforth/wf.fs


1 : pazsan 1.1 \ wiki forth
2 :    
3 : anton 1.41 \ Copyright (C) 2003,2004,2005 Free Software Foundation, Inc.
4 : anton 1.19
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 :     \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 :    
21 : pazsan 1.1 require string.fs
22 :    
23 : pazsan 1.25 \ basic stuff
24 :    
25 : pazsan 1.11 : -scan ( addr u char -- addr' u' )
26 :     >r BEGIN dup WHILE 1- 2dup + c@ r@ = UNTIL THEN
27 :     rdrop ;
28 :     : -$split ( addr u char -- addr1 u1 addr2 u2 )
29 :     >r 2dup r@ -scan 2dup + c@ r> = negate over + >r
30 :     2swap r> /string ;
31 : pazsan 1.23 : parse" ( -- addr u ) '" parse 2drop '" parse ;
32 : pazsan 1.1 : .' '' parse postpone SLiteral postpone type ; immediate
33 : pazsan 1.3 : s' '' parse postpone SLiteral ; immediate
34 : pazsan 1.44 : .upcase ( addr u -- ) bounds ?DO I c@ toupper emit LOOP ;
35 : pazsan 1.1
36 : pazsan 1.25 \ character recoding
37 : pazsan 1.3
38 : pazsan 1.31 [IFDEF] maxascii $100 to maxascii 8-bit-io [THEN]
39 : pazsan 1.29 \ UTF-8 IO fails with .type:
40 :    
41 : pazsan 1.24 : .type ( addr u -- )
42 :     bounds ?DO I c@
43 :     case
44 :     '& of ." &" endof
45 :     '< of ." &lt;" endof
46 : pazsan 1.39 \ '¤ of ." &euro;" endof
47 : pazsan 1.24 dup emit
48 :     endcase
49 :     LOOP ;
50 :    
51 : pazsan 1.25 \ tag handling
52 :    
53 :     Variable indentlevel
54 :     Variable tag-option
55 : pazsan 1.29 Variable tag-class
56 : pazsan 1.35 Variable default-class
57 : pazsan 1.25 s" " tag-option $!
58 : pazsan 1.29 s" " tag-class $!
59 : pazsan 1.35 s" " default-class $!
60 : pazsan 1.25
61 : pazsan 1.29 : tag ( addr u -- ) '< emit type
62 :     tag-class $@len IF .\" class=\"" tag-class $@ type '" emit THEN
63 :     tag-option $@ type
64 :     '> emit
65 : pazsan 1.35 s" " tag-option $! default-class $@ tag-class $! ;
66 : pazsan 1.21 : tag/ ( addr u -- ) s" /" tag-option $+! tag ;
67 : pazsan 1.1 : /tag ( addr u -- ) '< emit '/ emit type '> emit ;
68 : pazsan 1.24 : tagged ( addr1 u1 addr2 u2 -- ) 2dup 2>r tag .type 2r> /tag ;
69 : pazsan 1.1
70 : pazsan 1.3 : opt ( addr u opt u -- ) s" " tag-option $+!
71 :     tag-option $+! s' ="' tag-option $+! tag-option $+!
72 :     s' "' tag-option $+! ;
73 : pazsan 1.25 : n>string ( n -- addr u ) 0 <# #S #> ;
74 : pazsan 1.29 : xy>string ( x y -- ) swap 0 <# #S 'x hold 2drop 0 #S 's hold #> ;
75 : pazsan 1.25 : opt# ( n opt u -- ) rot n>string 2swap opt ;
76 : pazsan 1.3 : href= ( addr u -- ) s" href" opt ;
77 : pazsan 1.21 : id= ( addr u -- ) s" id" opt ;
78 : pazsan 1.3 : src= ( addr u -- ) s" src" opt ;
79 : pazsan 1.4 : alt= ( addr u -- ) s" alt" opt ;
80 : pazsan 1.25 : width= ( n -- ) s" width" opt# ;
81 :     : height= ( n -- ) s" height" opt# ;
82 : pazsan 1.3 : align= ( addr u -- ) s" align" opt ;
83 : pazsan 1.29 : class= ( addr u -- )
84 :     tag-class $@len IF s" " tag-class $+! THEN
85 :     tag-class $+! ;
86 : pazsan 1.35 : dclass= ( addr u -- ) 2dup class=
87 :     default-class $! ;
88 : pazsan 1.21 : indent= ( -- )
89 :     indentlevel @ 0 <# #S 'p hold #> class= ;
90 : pazsan 1.3 : mailto: ( addr u -- ) s' href="mailto:' tag-option $+!
91 :     tag-option $+! s' "' tag-option $+! ;
92 :    
93 : pazsan 1.1 \ environment handling
94 :    
95 : pazsan 1.11 Variable end-sec
96 : pazsan 1.1 Variable oldenv
97 : pazsan 1.10 Variable envs 30 0 [DO] 0 , [LOOP]
98 : pazsan 1.1
99 :     : env$ ( -- addr ) envs dup @ 1+ cells + ;
100 :     : env ( addr u -- ) env$ $! ;
101 : pazsan 1.34 : env? ( -- ) envs @ oldenv @ over oldenv !
102 : pazsan 1.1 2dup > IF env$ $@ tag THEN
103 :     2dup < IF env$ cell+ $@ /tag env$ cell+ $off THEN
104 : pazsan 1.34 2drop ;
105 : pazsan 1.1 : +env 1 envs +! ;
106 : pazsan 1.34 : -env end-sec @ envs @ 1 > or IF -1 envs +! env? THEN ;
107 : pazsan 1.1 : -envs envs @ 0 ?DO -env cr LOOP ;
108 : pazsan 1.34 : -tenvs envs @ 1 ?DO -env cr LOOP ;
109 : pazsan 1.2 : >env ( addr u -- ) +env env env? ;
110 : pazsan 1.1
111 : pazsan 1.6 \ alignment
112 :    
113 : pazsan 1.12 Variable table-format
114 :     Variable table#
115 : pazsan 1.25 Create table-starts &10 0 [DO] 0 c, 0 c, [LOOP]
116 :     Variable taligned
117 : pazsan 1.12
118 : pazsan 1.6 : >align ( c -- )
119 :     CASE
120 : pazsan 1.21 'l OF s" left" class= ENDOF
121 :     'r OF s" right" class= ENDOF
122 : pazsan 1.25 'c OF s" center" class= ENDOF
123 : pazsan 1.21 '< OF s" left" class= ENDOF
124 :     '> OF s" right" class= ENDOF
125 : pazsan 1.25 '= OF s" center" class= ENDOF
126 :     '~ OF s" middle" class= ENDOF
127 : pazsan 1.13 ENDCASE ;
128 :    
129 :     : >talign ( c -- )
130 :     CASE
131 : pazsan 1.6 'l OF s" left" align= ENDOF
132 :     'r OF s" right" align= ENDOF
133 :     'c OF s" center" align= ENDOF
134 :     '< OF s" left" align= ENDOF
135 :     '> OF s" right" align= ENDOF
136 : pazsan 1.11 '= OF s" center" align= ENDOF
137 : pazsan 1.25 ENDCASE taligned on ;
138 : pazsan 1.6
139 : pazsan 1.10 : >border ( c -- )
140 :     case
141 : pazsan 1.21 '- of s" border0" class= endof
142 :     '+ of s" border1" class= endof
143 : pazsan 1.10 endcase ;
144 :    
145 : pazsan 1.7 \ image handling
146 :    
147 : pazsan 1.29 wordlist Constant img-sizes
148 :    
149 : pazsan 1.7 Create imgbuf $20 allot
150 :    
151 :     Create pngsig $89 c, $50 c, $4E c, $47 c, $0D c, $0A c, $1A c, $0A c,
152 :     Create jfif $FF c, $D8 c, $FF c, $E0 c, $00 c, $10 c, $4A c, $46 c,
153 :     $49 c, $46 c,
154 :    
155 :     : b@ ( addr -- x ) 0 swap 4 bounds ?DO 8 lshift I c@ + LOOP ;
156 :     : bw@ ( addr -- x ) 0 swap 2 bounds ?DO 8 lshift I c@ + LOOP ;
157 :    
158 :     : gif? ( -- flag )
159 : anton 1.16 s" GIF89a" imgbuf over str=
160 :     s" GIF87a" imgbuf over str= or ;
161 : pazsan 1.7 : gif-size ( -- w h )
162 : pazsan 1.10 imgbuf 8 + c@ imgbuf 9 + c@ 8 lshift +
163 :     imgbuf 6 + c@ imgbuf 7 + c@ 8 lshift + ;
164 : pazsan 1.7
165 :     : png? ( -- flag )
166 : anton 1.16 pngsig 8 imgbuf over str= ;
167 : pazsan 1.7 : png-size ( -- w h )
168 : pazsan 1.10 imgbuf $14 + b@ imgbuf $10 + b@ ;
169 : pazsan 1.7
170 :     : jpg? ( -- flag )
171 : anton 1.16 jfif 10 imgbuf over str= ;
172 : pazsan 1.7 : jpg-size ( fd -- w h ) >r
173 :     2. BEGIN
174 :     2dup r@ reposition-file throw
175 :     imgbuf $10 r@ read-file throw 0<>
176 : pazsan 1.8 imgbuf bw@ $FFC0 $FFD0 within 0= and WHILE
177 : pazsan 1.7 imgbuf 2 + bw@ 2 + 0 d+ REPEAT
178 :     2drop imgbuf 5 + bw@ imgbuf 7 + bw@ rdrop ;
179 :    
180 :     : img-size ( fd -- w h ) >r
181 :     gif? IF gif-size rdrop EXIT THEN
182 :     jpg? IF r> jpg-size EXIT THEN
183 :     png? IF png-size rdrop EXIT THEN
184 : pazsan 1.28 0 0 rdrop ;
185 : pazsan 1.7
186 : pazsan 1.29 3 set-precision
187 :    
188 :     : f.size ( r -- )
189 :     f$ dup >r 0<=
190 :     IF '0 emit
191 :     ELSE scratch r@ min type r@ precision - zeros THEN
192 : pazsan 1.34 r@ negate zeros
193 :     scratch r> 0 max /string 0 max -zeros
194 :     dup IF '. emit THEN type ;
195 : pazsan 1.29
196 : pazsan 1.37 12.9e FConstant pixels
197 : pazsan 1.39 FVariable factor 1e factor f!
198 : pazsan 1.37
199 : pazsan 1.29 : size-does> ( -- ) DOES> ( -- )
200 :     ." img." dup body> >name .name
201 :     2@ ." { width: "
202 : pazsan 1.37 s>d d>f pixels f/ f.size ." em; height: "
203 :     s>d d>f pixels f/ f.size ." em; }" cr ;
204 : pazsan 1.29
205 :     : size-css ( file< > -- )
206 :     outfile-id >r
207 :     bl sword r/w create-file throw to outfile-id
208 :     img-sizes wordlist-id
209 :     BEGIN @ dup WHILE
210 :     dup name>int execute
211 :     REPEAT drop
212 :     outfile-id close-file throw
213 :     r> to outfile-id
214 :     dup 0< IF throw ELSE drop THEN ;
215 :    
216 :     : size-class ( x y addr u -- x y )
217 :     2dup class=
218 :     2dup img-sizes search-wordlist IF drop 2drop
219 :     ELSE
220 :     get-current >r img-sizes set-current
221 : pazsan 1.39 nextname Create 2dup
222 :     s>d d>f factor f@ f* f>d d>s ,
223 :     s>d d>f factor f@ f* f>d d>s ,
224 :     size-does>
225 : pazsan 1.29 r> set-current
226 :     THEN ;
227 :    
228 : pazsan 1.7 : .img-size ( addr u -- )
229 : pazsan 1.10 r/o open-file IF drop EXIT THEN >r
230 : pazsan 1.7 imgbuf $20 r@ read-file throw drop
231 :     r@ img-size
232 :     r> close-file throw
233 : pazsan 1.29 2dup or IF 2dup xy>string size-class THEN
234 : pazsan 1.25 ?dup IF width= THEN
235 : pazsan 1.29 ?dup IF height= THEN
236 :     ;
237 : pazsan 1.7
238 : pazsan 1.1 \ link creation
239 :    
240 :     Variable link
241 : pazsan 1.10 Variable link-sig
242 : pazsan 1.1 Variable link-suffix
243 : pazsan 1.3 Variable iconpath
244 : pazsan 1.27 Variable icon-prefix
245 :     Variable icon-tmp
246 : pazsan 1.1
247 : pazsan 1.2 Variable do-size
248 : pazsan 1.9 Variable do-icon
249 : pazsan 1.32 Variable do-expand
250 : pazsan 1.2
251 : pazsan 1.8 Defer parse-line
252 :    
253 : pazsan 1.27 : .img ( addr u -- )
254 :     dup >r '@ -$split dup r> = IF 2swap 2drop
255 :     ELSE 2swap icon-tmp $! icon-prefix $@ icon-tmp $+! icon-tmp $+!
256 :     icon-tmp $@ THEN
257 :     dup >r '| -$split dup r> = IF 2swap THEN
258 : pazsan 1.11 dup IF 2swap alt= ELSE 2drop THEN
259 : pazsan 1.29 tag-class $@len >r over c@ >align tag-class $@len r> = 1+ /string
260 :     tag-class $@len >r over c@ >border tag-class $@len r> = 1+ /string
261 : pazsan 1.21 2dup .img-size src= s" img" tag/ ;
262 : pazsan 1.11 : >img ( -- ) '{ parse type '} parse .img ;
263 :    
264 : pazsan 1.7 : alt-suffix ( -- )
265 :     link-suffix $@len 2 - link-suffix $!len
266 :     s" [" link-suffix 0 $ins
267 :     s" ]" link-suffix $+!
268 :     link-suffix $@ alt= ;
269 :    
270 : pazsan 1.6 : get-icon ( addr u -- ) iconpath @ IF 2drop EXIT THEN
271 :     link-suffix $! s" .*" link-suffix $+!
272 : pazsan 1.27 icon-prefix $@ open-dir throw >r
273 : pazsan 1.1 BEGIN
274 :     pad $100 r@ read-dir throw WHILE
275 :     pad swap 2dup link-suffix $@ filename-match
276 : pazsan 1.27 IF icon-prefix $@ iconpath $! s" /" iconpath $+! iconpath $+!
277 : pazsan 1.10 iconpath $@ 2dup .img-size src= '- >border
278 : pazsan 1.21 alt-suffix s" img" tag/ true
279 : pazsan 1.1 ELSE 2drop false THEN
280 : pazsan 1.6 UNTIL ELSE drop THEN
281 : pazsan 1.1 r> close-dir throw ;
282 :    
283 : pazsan 1.9 : link-icon? ( -- ) do-icon @ 0= ?EXIT
284 :     iconpath @ IF iconpath $off THEN
285 :     link $@ + 1- c@ '/ = IF s" index.html" ELSE link $@ THEN
286 : pazsan 1.20 '# $split 2drop
287 : pazsan 1.8 BEGIN '. $split 2swap 2drop dup WHILE
288 :     2dup get-icon REPEAT 2drop ;
289 : pazsan 1.6
290 : pazsan 1.2 : link-size? ( -- ) do-size @ 0= ?EXIT
291 : pazsan 1.1 link $@ r/o open-file IF drop EXIT THEN >r
292 : pazsan 1.25 r@ file-size throw $400 um/mod nip
293 :     dup $800 < IF ." (" 0 u.r ." k)"
294 :     ELSE $400 / ." (" 0 u.r ." M)" THEN
295 : pazsan 1.1 r> close-file throw ;
296 :    
297 : pazsan 1.10 : link-sig? ( -- )
298 :     link $@ link-sig $! s" .sig" link-sig $+!
299 :     link-sig $@ r/o open-file IF drop EXIT THEN
300 :     close-file throw
301 :     ." (" link-sig $@ href= s" a" tag
302 : pazsan 1.27 s" |-@/sig.gif" .img ." sig" s" /a" tag ." )" ;
303 : pazsan 1.10
304 : pazsan 1.25 : link-warn? ( -- ) \ local links only
305 :     link $@ ': scan nip ?EXIT
306 : pazsan 1.33 link $@ '# $split 2drop dup IF
307 :     r/o open-file nip IF
308 :     s" Dead Link '" stderr write-file throw
309 :     link $@ stderr write-file throw
310 :     s\" ' !!!\n" stderr write-file throw
311 :     THEN
312 :     ELSE 2drop THEN ;
313 : pazsan 1.25
314 : pazsan 1.2 : link-options ( addr u -- addr' u' )
315 : pazsan 1.32 do-size off do-icon on do-expand off
316 :     over c@ '% = over 0> and IF do-size on 1 /string THEN
317 :     over c@ '\ = over 0> and IF do-icon off 1 /string THEN
318 :     over c@ '* = over 0> and IF do-expand on 1 /string THEN ;
319 : pazsan 1.2
320 : anton 1.16 s" Gforth" environment? [IF] s" 0.5.0" str= [IF]
321 : pazsan 1.15 : parse-string ( c-addr u -- ) \ core,block
322 : anton 1.18 s" *evaluated string*" loadfilename>r
323 : pazsan 1.15 push-file #tib ! >tib !
324 :     >in off blk off loadfile off -1 loadline !
325 :     ['] parse-line catch
326 : anton 1.18 pop-file r>loadfilename throw ;
327 : pazsan 1.15 [ELSE]
328 : pazsan 1.8 : parse-string ( addr u -- )
329 :     evaluate-input cell new-tib #tib ! tib !
330 :     ['] parse-line catch pop-file throw ;
331 : pazsan 1.15 [THEN] [THEN]
332 : pazsan 1.8
333 : pazsan 1.32 Variable expand-link
334 :     Variable expand-prefix
335 :     Variable expand-postfix
336 :    
337 :     : ?expand ( addr u -- ) expand-link $!
338 :     do-expand @ IF
339 :     expand-prefix $@ expand-link 0 $ins
340 :     expand-postfix $@ expand-link $+! THEN
341 :     expand-link $@ ;
342 :    
343 : pazsan 1.11 : .link ( addr u -- ) dup >r '| -$split dup r> = IF 2swap THEN
344 : pazsan 1.2 link-options link $!
345 : pazsan 1.20 link $@len 0= IF 2dup link $! ( s" .html" link $+! ) THEN
346 : pazsan 1.32 link $@ ?expand
347 :     href= s" a" tag link-icon?
348 : pazsan 1.25 parse-string s" a" /tag link-size? link-sig? link-warn? ;
349 : pazsan 1.9 : >link ( -- ) '[ parse type '] parse .link ;
350 : pazsan 1.1
351 :     \ line handling
352 :    
353 : pazsan 1.4 : char? ( -- c ) >in @ char swap >in ! ;
354 : pazsan 1.24
355 : pazsan 1.1 : parse-tag ( addr u char -- )
356 : pazsan 1.24 >r r@ parse .type
357 : pazsan 1.2 r> parse 2swap tagged ;
358 : pazsan 1.1
359 : pazsan 1.6 : .text ( -- ) >in @ >r char drop
360 : pazsan 1.24 source r@ /string >in @ r> - nip .type ;
361 : pazsan 1.6
362 :     Create do-words $100 0 [DO] ' .text , [LOOP]
363 :    
364 : pazsan 1.9 :noname '( emit 1 >in +! ; '( cells do-words + !
365 :    
366 : pazsan 1.6 : bind-char ( xt -- ) char cells do-words + ! ;
367 :    
368 :     : char>tag ( -- ) char >r
369 :     :noname bl sword postpone SLiteral r@ postpone Literal
370 :     postpone parse-tag postpone ; r> cells do-words + ! ;
371 : pazsan 1.1
372 : pazsan 1.12 : >tag '\ parse type '\ parse tag ;
373 :    
374 : pazsan 1.6 char>tag * b
375 :     char>tag _ em
376 :     char>tag # code
377 : pazsan 1.25 :noname '~ parse .type '~ parse .type ; '~ cells do-words + !
378 : pazsan 1.6
379 : pazsan 1.9 ' >link bind-char [
380 :     ' >img bind-char {
381 : pazsan 1.12 ' >tag bind-char \
382 : pazsan 1.6
383 :     : do-word ( char -- ) cells do-words + perform ;
384 : pazsan 1.4
385 : pazsan 1.9 : word? ( -- addr u ) >in @ >r bl sword r> >in ! ;
386 :    
387 :     wordlist Constant autoreplacements
388 :    
389 : pazsan 1.8 :noname ( -- )
390 : pazsan 1.9 BEGIN char? do-word source nip >in @ = UNTIL ; is parse-line
391 :    
392 :     : parse-line+ ( -- )
393 :     BEGIN
394 :     word? autoreplacements search-wordlist
395 :     IF execute bl sword 2drop
396 :     source >in @ 1- /string drop c@ bl = >in +!
397 :     ELSE char? do-word THEN
398 :     source nip >in @ = UNTIL ;
399 : pazsan 1.4
400 :     : parse-to ( char -- ) >r
401 : pazsan 1.25 BEGIN
402 :     word? autoreplacements search-wordlist
403 :     IF execute bl sword 2drop
404 :     source >in @ 1- /string drop c@ bl = >in +! bl true
405 :     ELSE char? dup r@ <> THEN WHILE
406 : pazsan 1.4 do-word source nip >in @ = UNTIL ELSE drop THEN
407 :     r> parse type ;
408 : pazsan 1.1
409 : pazsan 1.9 \ autoreplace
410 :    
411 :     : autoreplace ( <[string|url]> -- )
412 :     get-current autoreplacements set-current
413 :     Create set-current
414 :     here 0 , '[ parse 2drop '] parse rot $!
415 :     DOES> $@ .link ;
416 :    
417 : pazsan 1.1 \ paragraph handling
418 :    
419 :     : parse-par ( -- )
420 : pazsan 1.31 BEGIN
421 :     parse-line+ cr refill WHILE
422 : pazsan 1.1 source nip 0= UNTIL THEN ;
423 :    
424 : pazsan 1.25 : par ( addr u -- ) env?
425 : pazsan 1.21 2dup tag parse-par /tag cr cr ;
426 : pazsan 1.1
427 : pazsan 1.10 \ scan strings
428 :    
429 :     : get-rest ( addr -- ) 0 parse -trailing rot $! ;
430 :     Create $lf 1 c, #lf c,
431 :     : get-par ( addr -- ) >r s" " r@ $+!
432 : anton 1.16 BEGIN 0 parse 2dup s" ." str= 0= WHILE
433 : pazsan 1.10 r@ $@len IF $lf count r@ $+! THEN r@ $+!
434 :     refill 0= UNTIL ELSE 2drop THEN
435 :     rdrop ;
436 :    
437 :     \ toc handling
438 :    
439 :     Variable toc-link
440 :    
441 :     : >last ( addr link -- link' )
442 :     BEGIN dup @ WHILE @ REPEAT ! 0 ;
443 :    
444 : pazsan 1.14 Variable create-navs
445 :     Variable nav$
446 :     Variable nav-name
447 :     Variable nav-file
448 :     Create nav-buf 0 c,
449 :     : nav+ ( char -- ) nav-buf c! nav-buf 1 nav-file $+! ;
450 :    
451 : pazsan 1.25 : filenamize ( addr u -- )
452 :     bounds ?DO
453 :     I c@ dup 'A 'Z 1+ within IF bl + nav+
454 :     ELSE dup 'a 'z 1+ within IF nav+
455 :     ELSE dup '0 '9 1+ within IF nav+
456 :     ELSE dup bl = swap '- = or IF '- nav+
457 :     THEN THEN THEN THEN
458 :     LOOP ;
459 : pazsan 1.14 : >nav ( addr u -- addr' u' )
460 :     nav-name $! create-navs @ 0=
461 :     IF s" navigate/nav.scm" r/w create-file throw create-navs ! THEN
462 :     s' (script-fu-nav-file "' nav$ $! nav-name $@ nav$ $+!
463 :     s' " "./navigate/' nav$ $+! s" " nav-file $!
464 : pazsan 1.25 nav-name $@ filenamize
465 : pazsan 1.14 nav-file $@ nav$ $+! s' .jpg")' nav$ $+!
466 :     nav$ $@ create-navs @ write-line throw
467 :     s" [" nav$ $! nav-name $@ nav$ $+!
468 :     s" |-navigate/" nav$ $+! nav-file $@ nav$ $+! s" .jpg" nav$ $+!
469 :     nav$ $@ ;
470 :    
471 :     : toc, ( n -- ) , '| parse >nav here 0 , $! 0 parse here 0 , $! ;
472 : pazsan 1.10 : up-toc align here toc-link >last , 0 toc, ;
473 :     : top-toc align here toc-link >last , 1 toc, ;
474 :     : this-toc align here toc-link >last , 2 toc, ;
475 :     : sub-toc align here toc-link >last , 3 toc, ;
476 : pazsan 1.12 : new-toc toc-link off ;
477 : pazsan 1.10
478 :     Variable toc-name
479 : pazsan 1.22 Variable toc-index
480 :     6 Value /toc-line
481 : pazsan 1.36 true Value toc-image
482 : pazsan 1.10
483 :     : .toc-entry ( toc flag -- )
484 : pazsan 1.36 swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href=
485 : pazsan 1.11 '# scan 1 /string toc-name $@ compare >r
486 : pazsan 1.36 $@ toc-image IF s" a" tag .img swap
487 :     IF
488 :     case
489 :     2 of s" ^]|-@/arrow_up.jpg" .img endof
490 :     3 of
491 :     r@ 0= IF s" *]|-@/circle.jpg"
492 : pazsan 1.27 ELSE s" v]|-@/arrow_down.jpg" THEN .img endof
493 : pazsan 1.36 endcase
494 :     ELSE
495 :     case
496 :     0 of s" ^]|-@/arrow_up.jpg" .img endof
497 :     1 of s" >]|-@/arrow_right.jpg" .img endof
498 :     2 of s" *]|-@/circle.jpg" .img endof
499 :     3 of s" v]|-@/arrow_down.jpg" .img endof
500 :     endcase
501 :     THEN
502 :     s" a" /tag ." <!--" cr ." -->"
503 : pazsan 1.10 ELSE
504 : pazsan 1.36 '[ skip 2dup '| scan nip - 2swap swap
505 :     IF
506 :     CASE
507 :     2 OF s" up" class= ENDOF
508 :     3 OF r@ 0= IF s" circle" ELSE s" down" THEN class= ENDOF
509 :     ENDCASE
510 :     ELSE
511 :     CASE
512 :     0 OF s" up" class= ENDOF
513 :     1 OF s" right" class= ENDOF
514 :     2 OF s" circle" class= ENDOF
515 :     3 OF s" down" class= ENDOF
516 :     ENDCASE
517 :     THEN
518 : pazsan 1.38 s" a" tag parse-string s" a" /tag
519 : pazsan 1.10 THEN
520 : pazsan 1.36 rdrop
521 : pazsan 1.22 1 toc-index +! toc-index @ /toc-line mod 0=
522 : pazsan 1.36 IF -env cr s" p" >env THEN ;
523 : pazsan 1.22
524 : pazsan 1.36 : print-toc ( -- ) toc-index off cr
525 :     toc-image IF s" img-menu" ELSE s" menu" THEN id=
526 :     s" div" >env cr s" p" >env
527 : pazsan 1.22 0 parse
528 : pazsan 1.10 dup 0= IF toc-name $! 0 ELSE
529 : pazsan 1.21 toc-name $! toc-name $@ id= s" " s" a" tagged 2
530 : pazsan 1.10 THEN >r
531 :     toc-link BEGIN @ dup WHILE
532 : pazsan 1.21 dup cell+ @ 3 = r@ 0= and IF rdrop 1 >r ( s" br" tag/ cr ) THEN
533 : pazsan 1.10 dup cell+ @ r@ >= IF dup r@ 2 = .toc-entry THEN
534 : pazsan 1.22 dup cell+ @ 2 = r@ 2 = and IF s" br" tag/ toc-index off THEN
535 : pazsan 1.36 REPEAT drop rdrop -env -env cr ;
536 : pazsan 1.10
537 : pazsan 1.1 \ handle global tags
538 :    
539 : pazsan 1.21 : indent ( n -- )
540 : pazsan 1.22 indentlevel @ over
541 : pazsan 1.21 indentlevel !
542 : pazsan 1.25 2dup < IF swap DO -env LOOP EXIT THEN
543 :     2dup > IF DO s" div" >env LOOP EXIT THEN
544 :     2dup = IF drop IF -env s" div" >env THEN THEN
545 : pazsan 1.21 ;
546 :     : +indent ( -- )
547 : pazsan 1.25 indentlevel @ IF -env indent= s" div" >env THEN
548 : pazsan 1.21 ;
549 : pazsan 1.8
550 : pazsan 1.1 wordlist constant longtags
551 :    
552 : pazsan 1.21 Variable divs
553 :    
554 : pazsan 1.1 longtags set-current
555 :    
556 : pazsan 1.25 : --- 0 indent cr s" hr" tag/ cr ;
557 : pazsan 1.35 : * 1 indent s" h1" dclass= s" h1" par +indent s" " dclass= ;
558 :     : ** 1 indent s" h2" dclass= s" h2" par +indent s" " dclass= ;
559 :     : *** 2 indent s" h3" dclass= s" h3" par +indent s" " dclass= ;
560 : pazsan 1.25 : -- 0 indent cr print-toc ;
561 :     : && 0 parse id= ;
562 :     : - s" ul" env s" li" par ;
563 :     : + s" ol" env s" li" par ;
564 :     : ? s" dl" env s" dt" par ;
565 :     : : s" dl" env s" dd" par ;
566 :     : -<< s" ul" env env? s" li" >env ;
567 :     : +<< s" ol" env env? s" li" >env ;
568 : pazsan 1.34 \ : ?<< s" dl" env env? s" dt" >env ; \ not allowed
569 : pazsan 1.25 : :<< s" dl" env env? s" dd" >env ;
570 :     : p<< s" p" >env ;
571 :     : << +env ;
572 :     : <* s" center" class= ;
573 : pazsan 1.34 : <red s" p" >env s" #ff0000" s" color" opt s" font" >env parse-par ;
574 :     : red> -env -env ;
575 : pazsan 1.25 : >> -env ;
576 : pazsan 1.23 : *> ;
577 : pazsan 1.25 : :: interpret ;
578 :     : . end-sec on 0 indent ;
579 :     : :code s" pre" >env
580 : pazsan 1.34 BEGIN source >in @ /string .type cr refill WHILE
581 : anton 1.16 source s" :endcode" str= UNTIL THEN
582 : pazsan 1.23 -env ;
583 : pazsan 1.25 : :code-file s" pre" >env
584 :     parse" slurp-file type -env ;
585 :     : \ postpone \ ;
586 : pazsan 1.1
587 :     definitions
588 : pazsan 1.25
589 :     : LT get-order longtags swap 1+ set-order
590 :     bl sword parser previous ; immediate
591 :    
592 : pazsan 1.3 \ Table
593 :    
594 : pazsan 1.5 : next-char ( -- char ) source drop >in @ + c@ ;
595 : pazsan 1.25 : next-table ( -- )
596 :     BEGIN
597 :     table-starts table# @ 2* + dup c@ dup
598 :     IF 1- over c! 1+ c@ 1+ ELSE swap 1+ c! 0 THEN
599 :     dup WHILE table# +! REPEAT drop
600 :     table-format $@ table# @ /string drop c@ taligned ! ;
601 :     : next>align ( -- )
602 :     next-char dup bl <> over '\ <> and
603 :     IF taligned ! 1 >in +! ELSE drop THEN ;
604 :    
605 :     : |tag ( addr u -- )
606 :     next-table
607 :     next-char '/ = IF 1 >in +!
608 :     next-char digit? IF
609 :     dup 1- table-starts table# @ 2* + c!
610 :     s" rowspan" opt# 1 >in +! THEN
611 :     next>align
612 :     THEN
613 :     next-char '\ = IF 1 >in +!
614 :     next-char digit? IF
615 :     dup 1- table-starts table# @ 2* + 1+ c!
616 :     dup 1- table# +!
617 :     s" colspan" opt# 1 >in +! THEN
618 :     next>align
619 :     THEN
620 :     taligned @ >talign >env
621 :     1 table# +! ;
622 :     : |d table# @ 0> IF -env THEN s" td" |tag ;
623 :     : |h table# @ 0> IF -env THEN s" th" |tag ;
624 :     : |line s" tr" >env table# off ;
625 :     : line| 1 >in +! -env -env cr ;
626 : pazsan 1.5
627 : pazsan 1.3 longtags set-current
628 :    
629 : pazsan 1.25 : <| ( -- ) table-starts &20 erase
630 :     s" table" class= s" div" >env
631 :     bl sword table-format $! bl sword
632 :     dup IF s" border" opt ELSE 2drop THEN
633 :     s" table" >env ;
634 : pazsan 1.12 : |> -env -env cr cr ;
635 : pazsan 1.25 : +| ( -- )
636 :     |line BEGIN |h '| parse-to next-char '+ = UNTIL line| ;
637 :     : -| ( -- )
638 :     |line BEGIN |d '| parse-to next-char '- = UNTIL line| ;
639 :     : =| ( -- )
640 :     |line |h '| parse-to
641 :     BEGIN |d '| parse-to next-char '= = UNTIL line| ;
642 : pazsan 1.3
643 :     definitions
644 : pazsan 1.1
645 :     \ parse a section
646 :    
647 : pazsan 1.25 : section-par ( -- ) >in off
648 : pazsan 1.9 bl sword longtags search-wordlist
649 :     IF execute
650 : pazsan 1.8 ELSE source nip IF >in off s" p" par THEN THEN ;
651 : pazsan 1.25 : parse-section ( -- ) end-sec off
652 : pazsan 1.8 BEGIN refill WHILE
653 : pazsan 1.34 section-par end-sec @ UNTIL THEN end-sec off ;
654 : pazsan 1.1
655 :     \ HTML head
656 :    
657 : pazsan 1.21 Variable css-file
658 : pazsan 1.44 Variable print-file
659 :     Variable ie-css-file
660 : pazsan 1.34 Variable content
661 : pazsan 1.44 Variable _charset
662 : pazsan 1.40 Variable _lang
663 :     Variable _favicon
664 : pazsan 1.21
665 : pazsan 1.34 : lang@ ( -- addr u )
666 : pazsan 1.40 _lang @ IF _lang $@ ELSE s" en" THEN ;
667 : pazsan 1.34 : .css ( -- )
668 :     css-file @ IF css-file $@len IF
669 :     s" StyleSheet" s" rel" opt
670 : pazsan 1.44 css-file $@ href= s" screen" s" media" opt
671 : pazsan 1.34 s" text/css" s" type" opt s" link" tag/ cr
672 : pazsan 1.44 THEN THEN
673 :     ie-css-file @ IF
674 :     ." <!--[if lt IE 7.0]>" cr
675 :     .' <style type="text/css">@import url(' ie-css-file $@ type ." );</style>" cr
676 :     ." <![endif]-->" cr
677 :     THEN ;
678 :     : .print ( -- )
679 :     print-file @ IF print-file $@len IF
680 :     s" StyleSheet" s" rel" opt
681 :     print-file $@ href= s" print" s" media" opt
682 :     s" text/css" s" type" opt s" link" tag/ cr
683 :     THEN THEN ;
684 : pazsan 1.34 : .title ( addr u -- ) 1 envs ! oldenv off
685 : pazsan 1.45 _charset $@ s" utf-8" str= 0=
686 :     IF .' <?xml version="1.0" encoding="' _charset $@ .upcase .' "?>' cr THEN
687 : pazsan 1.34 .' <!DOCTYPE html' cr
688 :     .' PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' cr
689 :     .' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' cr
690 :     s" http://www.w3.org/1999/xhtml" s" xmlns" opt
691 :     lang@ s" xml:lang" opt lang@ s" lang" opt
692 :     s" html" >env cr s" head" >env cr
693 : pazsan 1.21 s" Content-Type" s" http-equiv" opt
694 : pazsan 1.34 content $@ s" content" opt
695 : pazsan 1.44 s" meta" tag/ cr .css .print
696 : pazsan 1.40 _favicon @ IF
697 :     s" shortcut icon" s" rel" opt
698 :     _favicon $@ href=
699 :     s" image/x-icon" s" type" opt
700 :     s" link" tag/ cr THEN
701 : pazsan 1.2 s" title" tagged cr
702 : pazsan 1.1 -env ;
703 :    
704 :     \ HTML trailer
705 :    
706 : pazsan 1.29 Variable public-key
707 : pazsan 1.1 Variable mail
708 :     Variable mail-name
709 : pazsan 1.12 Variable orig-date
710 : pazsan 1.1
711 : pazsan 1.20 : .lastmod
712 :     ." Last modified: " time&date rot 0 u.r swap 1-
713 :     s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
714 :     0 u.r ;
715 :    
716 : pazsan 1.1 : .trailer
717 : pazsan 1.21 s" center" class= s" address" >env
718 : pazsan 1.12 orig-date @ IF ." Created " orig-date $@ type ." . " THEN
719 : pazsan 1.20 .lastmod
720 :     ." by "
721 : pazsan 1.27 s" Mail|@/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
722 : pazsan 1.29 public-key @ IF
723 :     public-key $@ href= s" a" tag
724 : pazsan 1.36 s" PGP key|-@/gpg.asc.gif" .img s" a" /tag
725 : pazsan 1.29 THEN
726 : pazsan 1.1 -envs ;
727 :    
728 :     \ top word
729 : pazsan 1.6
730 : pazsan 1.12 : maintainer ( -- )
731 : pazsan 1.25 '< sword -trailing mail-name $! '> sword mail $! ;
732 : pazsan 1.29 : pgp-key ( -- )
733 :     bl sword -trailing public-key $! ;
734 : pazsan 1.34 : charset ( -- ) s" text/xhtml; charset=" content $!
735 : pazsan 1.44 bl sword -trailing 2dup content $+! _charset $! ;
736 : pazsan 1.34
737 :     charset iso-8859-1
738 :    
739 : pazsan 1.12 : created ( -- )
740 :     bl sword orig-date $! ;
741 : pazsan 1.27 : icons
742 :     bl sword icon-prefix $! ;
743 : pazsan 1.34 : lang
744 : pazsan 1.40 bl sword _lang $! ;
745 :     : favicon
746 :     bl sword _favicon $! ;
747 : pazsan 1.32 : expands '# sword expand-prefix $! bl sword expand-postfix $! ;
748 :    
749 : pazsan 1.27 icons icons
750 : pazsan 1.6
751 :     Variable style$
752 :     : style> style$ @ 0= IF s" " style$ $! THEN style$ $@ tag-option $! ;
753 :     : >style tag-option $@ style$ $! s" " tag-option $! ;
754 :    
755 :     : style style> opt >style ;
756 :     : background ( -- ) parse" s" background" style ;
757 :     : text ( -- ) parse" s" text" style ;
758 :     warnings @ warnings off
759 :     : link ( -- ) parse" s" link" style ;
760 :     warnings !
761 :     : vlink ( -- ) parse" s" vlink" style ;
762 :     : marginheight ( -- ) parse" s" marginheight" style ;
763 : pazsan 1.21 : css ( -- ) parse" css-file $! ;
764 : pazsan 1.44 : print-css ( -- ) parse" print-file $! ;
765 :     : ie-css ( -- ) parse" ie-css-file $! ;
766 : pazsan 1.1
767 :     : wf ( -- )
768 :     outfile-id >r
769 :     bl sword r/w create-file throw to outfile-id
770 : pazsan 1.6 parse" .title
771 :     +env style> s" body" env env?
772 : pazsan 1.1 ['] parse-section catch .trailer
773 :     outfile-id close-file throw
774 :     r> to outfile-id
775 :     dup 0< IF throw ELSE drop THEN ;
776 :    
777 : pazsan 1.8 : eval-par ( addr u -- )
778 :     s" wf-temp.wf" r/w create-file throw >r
779 :     r@ write-file r> close-file throw
780 :     push-file s" wf-temp.wf" r/o open-file throw loadfile !
781 : pazsan 1.34 parse-par -env parse-section
782 : pazsan 1.8 loadfile @ close-file swap 2dup or
783 :     pop-file drop throw throw
784 :     s" wf-temp.wf" delete-file throw ;
785 :    
786 : pazsan 1.2 \ simple text data base
787 :    
788 :     Variable last-entry
789 :     Variable field#
790 :    
791 : pazsan 1.25 : table: ( xt n -- ) Create 0 , ['] type , , , 1 field# !
792 :     DOES> 2 cells + 2@ >in @ >r longtags set-current
793 : pazsan 1.2 Create definitions swap , r> >in !
794 :     here last-entry !
795 :     dup 0 DO 0 , LOOP
796 :     1 DO s" " last-entry @ I cells + $! LOOP
797 :     last-entry @ get-rest
798 :     DOES> dup cell+ swap perform ;
799 :    
800 : pazsan 1.25 : field: Create field# @ , ['] type , 1 field# +!
801 : pazsan 1.2 DOES> @ cells last-entry @ + get-rest ;
802 : pazsan 1.25 : par: Create field# @ , ['] eval-par , 1 field# +!
803 : pazsan 1.8 DOES> @ cells last-entry @ + get-par ;
804 : pazsan 1.3
805 : pazsan 1.25 : >field-rest >body @ cells postpone Literal postpone + ;
806 :     : >field ' >field-rest ; immediate
807 :    
808 :     : db-line ( -- )
809 :     BEGIN
810 :     source >in @ /string nip WHILE
811 :     '% parse postpone SLiteral postpone type
812 :     '% parse dup IF
813 :     '| $split 2swap
814 :     sfind 0= abort" Field not found"
815 :     dup postpone r@ >field-rest postpone $@
816 :     over IF drop evaluate ELSE
817 :     nip nip >body cell+ @ compile,
818 :     THEN
819 :     ELSE 2drop postpone cr THEN
820 :     REPEAT ;
821 :    
822 :     : db-par ( -- ) LT postpone p<< postpone >r
823 :     BEGIN db-line refill WHILE next-char '. = UNTIL 1 >in +! THEN
824 : pazsan 1.34 postpone rdrop ( LT postpone >> ) ; immediate

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help