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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help