[gforth] / gforth / wf.fs  

gforth: gforth/wf.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help