[gforth] / gforth / wf.fs  

gforth: gforth/wf.fs


1 : pazsan 1.1 \ wiki forth
2 :    
3 : anton 1.62 \ Copyright (C) 2003,2004,2005,2006,2007,2008,2010 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.60 : replace.- ( addr u -- )
274 :     bounds ?DO I c@ '. = IF '- I c! THEN LOOP ;
275 :    
276 : pazsan 1.6 : get-icon ( addr u -- ) iconpath @ IF 2drop EXIT THEN
277 : pazsan 1.60 link-suffix $! link-suffix $@ replace.-
278 :     s" .*" link-suffix $+!
279 : pazsan 1.58 icon-prefix $@ open-dir IF drop EXIT THEN >r
280 : pazsan 1.1 BEGIN
281 :     pad $100 r@ read-dir throw WHILE
282 :     pad swap 2dup link-suffix $@ filename-match
283 : pazsan 1.27 IF icon-prefix $@ iconpath $! s" /" iconpath $+! iconpath $+!
284 : pazsan 1.10 iconpath $@ 2dup .img-size src= '- >border
285 : pazsan 1.21 alt-suffix s" img" tag/ true
286 : pazsan 1.1 ELSE 2drop false THEN
287 : pazsan 1.6 UNTIL ELSE drop THEN
288 : pazsan 1.1 r> close-dir throw ;
289 :    
290 : pazsan 1.9 : link-icon? ( -- ) do-icon @ 0= ?EXIT
291 :     iconpath @ IF iconpath $off THEN
292 :     link $@ + 1- c@ '/ = IF s" index.html" ELSE link $@ THEN
293 : pazsan 1.20 '# $split 2drop
294 : pazsan 1.8 BEGIN '. $split 2swap 2drop dup WHILE
295 :     2dup get-icon REPEAT 2drop ;
296 : pazsan 1.6
297 : pazsan 1.2 : link-size? ( -- ) do-size @ 0= ?EXIT
298 : pazsan 1.1 link $@ r/o open-file IF drop EXIT THEN >r
299 : pazsan 1.25 r@ file-size throw $400 um/mod nip
300 :     dup $800 < IF ." (" 0 u.r ." k)"
301 :     ELSE $400 / ." (" 0 u.r ." M)" THEN
302 : pazsan 1.1 r> close-file throw ;
303 :    
304 : pazsan 1.10 : link-sig? ( -- )
305 :     link $@ link-sig $! s" .sig" link-sig $+!
306 :     link-sig $@ r/o open-file IF drop EXIT THEN
307 :     close-file throw
308 :     ." (" link-sig $@ href= s" a" tag
309 : pazsan 1.27 s" |-@/sig.gif" .img ." sig" s" /a" tag ." )" ;
310 : pazsan 1.10
311 : pazsan 1.25 : link-warn? ( -- ) \ local links only
312 :     link $@ ': scan nip ?EXIT
313 : pazsan 1.33 link $@ '# $split 2drop dup IF
314 :     r/o open-file nip IF
315 :     s" Dead Link '" stderr write-file throw
316 :     link $@ stderr write-file throw
317 :     s\" ' !!!\n" stderr write-file throw
318 :     THEN
319 :     ELSE 2drop THEN ;
320 : pazsan 1.25
321 : pazsan 1.2 : link-options ( addr u -- addr' u' )
322 : pazsan 1.32 do-size off do-icon on do-expand off
323 :     over c@ '% = over 0> and IF do-size on 1 /string THEN
324 :     over c@ '\ = over 0> and IF do-icon off 1 /string THEN
325 :     over c@ '* = over 0> and IF do-expand on 1 /string THEN ;
326 : pazsan 1.2
327 : anton 1.16 s" Gforth" environment? [IF] s" 0.5.0" str= [IF]
328 : pazsan 1.15 : parse-string ( c-addr u -- ) \ core,block
329 : anton 1.18 s" *evaluated string*" loadfilename>r
330 : pazsan 1.15 push-file #tib ! >tib !
331 :     >in off blk off loadfile off -1 loadline !
332 :     ['] parse-line catch
333 : anton 1.18 pop-file r>loadfilename throw ;
334 : pazsan 1.15 [ELSE]
335 : pazsan 1.8 : parse-string ( addr u -- )
336 :     evaluate-input cell new-tib #tib ! tib !
337 : pazsan 1.49 ['] parse-line catch pop-file throw ;
338 : pazsan 1.15 [THEN] [THEN]
339 : pazsan 1.8
340 : pazsan 1.32 Variable expand-link
341 :     Variable expand-prefix
342 :     Variable expand-postfix
343 :    
344 : pazsan 1.54 : ?expand ( addr u -- addr u' ) expand-link $!
345 : pazsan 1.32 do-expand @ IF
346 :     expand-prefix $@ expand-link 0 $ins
347 :     expand-postfix $@ expand-link $+! THEN
348 : pazsan 1.54 0 >r
349 :     BEGIN expand-link $@ r@ /string WHILE
350 :     r> 1+ >r
351 :     c@ '& = IF s" amp;" expand-link r@ $ins THEN
352 :     REPEAT drop rdrop
353 : pazsan 1.32 expand-link $@ ;
354 :    
355 : pazsan 1.11 : .link ( addr u -- ) dup >r '| -$split dup r> = IF 2swap THEN
356 : pazsan 1.2 link-options link $!
357 : pazsan 1.20 link $@len 0= IF 2dup link $! ( s" .html" link $+! ) THEN
358 : pazsan 1.32 link $@ ?expand
359 :     href= s" a" tag link-icon?
360 : pazsan 1.25 parse-string s" a" /tag link-size? link-sig? link-warn? ;
361 : pazsan 1.9 : >link ( -- ) '[ parse type '] parse .link ;
362 : pazsan 1.1
363 :     \ line handling
364 :    
365 : pazsan 1.51 : char? ( -- c ) >in @ char swap >in ! $FF umin ;
366 : pazsan 1.24
367 : pazsan 1.1 : parse-tag ( addr u char -- )
368 : pazsan 1.24 >r r@ parse .type
369 : pazsan 1.2 r> parse 2swap tagged ;
370 : pazsan 1.1
371 : pazsan 1.6 : .text ( -- ) >in @ >r char drop
372 : pazsan 1.24 source r@ /string >in @ r> - nip .type ;
373 : pazsan 1.6
374 :     Create do-words $100 0 [DO] ' .text , [LOOP]
375 :    
376 : pazsan 1.9 :noname '( emit 1 >in +! ; '( cells do-words + !
377 :    
378 : pazsan 1.6 : bind-char ( xt -- ) char cells do-words + ! ;
379 :    
380 :     : char>tag ( -- ) char >r
381 :     :noname bl sword postpone SLiteral r@ postpone Literal
382 :     postpone parse-tag postpone ; r> cells do-words + ! ;
383 : pazsan 1.1
384 : pazsan 1.12 : >tag '\ parse type '\ parse tag ;
385 :    
386 : pazsan 1.6 char>tag * b
387 : pazsan 1.57 char>tag / i
388 : pazsan 1.6 char>tag _ em
389 :     char>tag # code
390 : pazsan 1.25 :noname '~ parse .type '~ parse .type ; '~ cells do-words + !
391 : pazsan 1.6
392 : pazsan 1.9 ' >link bind-char [
393 :     ' >img bind-char {
394 : pazsan 1.12 ' >tag bind-char \
395 : pazsan 1.6
396 :     : do-word ( char -- ) cells do-words + perform ;
397 : pazsan 1.4
398 : pazsan 1.9 : word? ( -- addr u ) >in @ >r bl sword r> >in ! ;
399 :    
400 :     wordlist Constant autoreplacements
401 :    
402 : pazsan 1.8 :noname ( -- )
403 : pazsan 1.9 BEGIN char? do-word source nip >in @ = UNTIL ; is parse-line
404 :    
405 :     : parse-line+ ( -- )
406 :     BEGIN
407 :     word? autoreplacements search-wordlist
408 :     IF execute bl sword 2drop
409 :     source >in @ 1- /string drop c@ bl = >in +!
410 :     ELSE char? do-word THEN
411 :     source nip >in @ = UNTIL ;
412 : pazsan 1.4
413 :     : parse-to ( char -- ) >r
414 : pazsan 1.25 BEGIN
415 :     word? autoreplacements search-wordlist
416 :     IF execute bl sword 2drop
417 :     source >in @ 1- /string drop c@ bl = >in +! bl true
418 :     ELSE char? dup r@ <> THEN WHILE
419 : pazsan 1.4 do-word source nip >in @ = UNTIL ELSE drop THEN
420 :     r> parse type ;
421 : pazsan 1.1
422 : pazsan 1.9 \ autoreplace
423 :    
424 :     : autoreplace ( <[string|url]> -- )
425 :     get-current autoreplacements set-current
426 :     Create set-current
427 :     here 0 , '[ parse 2drop '] parse rot $!
428 :     DOES> $@ .link ;
429 :    
430 : pazsan 1.1 \ paragraph handling
431 :    
432 :     : parse-par ( -- )
433 : pazsan 1.31 BEGIN
434 :     parse-line+ cr refill WHILE
435 : pazsan 1.1 source nip 0= UNTIL THEN ;
436 :    
437 : pazsan 1.25 : par ( addr u -- ) env?
438 : pazsan 1.21 2dup tag parse-par /tag cr cr ;
439 : pazsan 1.1
440 : pazsan 1.10 \ scan strings
441 :    
442 :     : get-rest ( addr -- ) 0 parse -trailing rot $! ;
443 :     Create $lf 1 c, #lf c,
444 :     : get-par ( addr -- ) >r s" " r@ $+!
445 : anton 1.16 BEGIN 0 parse 2dup s" ." str= 0= WHILE
446 : pazsan 1.10 r@ $@len IF $lf count r@ $+! THEN r@ $+!
447 :     refill 0= UNTIL ELSE 2drop THEN
448 :     rdrop ;
449 :    
450 :     \ toc handling
451 :    
452 :     Variable toc-link
453 :    
454 :     : >last ( addr link -- link' )
455 :     BEGIN dup @ WHILE @ REPEAT ! 0 ;
456 :    
457 : pazsan 1.14 Variable create-navs
458 :     Variable nav$
459 :     Variable nav-name
460 :     Variable nav-file
461 :     Create nav-buf 0 c,
462 :     : nav+ ( char -- ) nav-buf c! nav-buf 1 nav-file $+! ;
463 :    
464 : pazsan 1.25 : filenamize ( addr u -- )
465 :     bounds ?DO
466 :     I c@ dup 'A 'Z 1+ within IF bl + nav+
467 :     ELSE dup 'a 'z 1+ within IF nav+
468 : pazsan 1.51 ELSE dup '0 '9 1+ within IF nav+
469 :     ELSE dup bl = over '- = or IF '- nav+
470 : pazsan 1.59 ELSE drop
471 : pazsan 1.51 THEN THEN THEN THEN
472 : pazsan 1.25 LOOP ;
473 : pazsan 1.14 : >nav ( addr u -- addr' u' )
474 :     nav-name $! create-navs @ 0=
475 :     IF s" navigate/nav.scm" r/w create-file throw create-navs ! THEN
476 :     s' (script-fu-nav-file "' nav$ $! nav-name $@ nav$ $+!
477 :     s' " "./navigate/' nav$ $+! s" " nav-file $!
478 : pazsan 1.25 nav-name $@ filenamize
479 : pazsan 1.14 nav-file $@ nav$ $+! s' .jpg")' nav$ $+!
480 :     nav$ $@ create-navs @ write-line throw
481 :     s" [" nav$ $! nav-name $@ nav$ $+!
482 :     s" |-navigate/" nav$ $+! nav-file $@ nav$ $+! s" .jpg" nav$ $+!
483 :     nav$ $@ ;
484 :    
485 :     : toc, ( n -- ) , '| parse >nav here 0 , $! 0 parse here 0 , $! ;
486 : pazsan 1.10 : up-toc align here toc-link >last , 0 toc, ;
487 :     : top-toc align here toc-link >last , 1 toc, ;
488 :     : this-toc align here toc-link >last , 2 toc, ;
489 :     : sub-toc align here toc-link >last , 3 toc, ;
490 : pazsan 1.12 : new-toc toc-link off ;
491 : pazsan 1.10
492 :     Variable toc-name
493 : pazsan 1.22 Variable toc-index
494 :     6 Value /toc-line
495 : pazsan 1.36 true Value toc-image
496 : pazsan 1.10
497 :     : .toc-entry ( toc flag -- )
498 : pazsan 1.54 swap cell+ dup @ swap cell+ dup cell+ $@ 2dup ?expand href=
499 : pazsan 1.11 '# scan 1 /string toc-name $@ compare >r
500 : pazsan 1.36 $@ toc-image IF s" a" tag .img swap
501 :     IF
502 :     case
503 :     2 of s" ^]|-@/arrow_up.jpg" .img endof
504 :     3 of
505 :     r@ 0= IF s" *]|-@/circle.jpg"
506 : pazsan 1.27 ELSE s" v]|-@/arrow_down.jpg" THEN .img endof
507 : pazsan 1.36 endcase
508 :     ELSE
509 :     case
510 :     0 of s" ^]|-@/arrow_up.jpg" .img endof
511 :     1 of s" >]|-@/arrow_right.jpg" .img endof
512 :     2 of s" *]|-@/circle.jpg" .img endof
513 :     3 of s" v]|-@/arrow_down.jpg" .img endof
514 :     endcase
515 :     THEN
516 :     s" a" /tag ." <!--" cr ." -->"
517 : pazsan 1.10 ELSE
518 : pazsan 1.36 '[ skip 2dup '| scan nip - 2swap swap
519 :     IF
520 :     CASE
521 :     2 OF s" up" class= ENDOF
522 :     3 OF r@ 0= IF s" circle" ELSE s" down" THEN class= ENDOF
523 :     ENDCASE
524 :     ELSE
525 :     CASE
526 :     0 OF s" up" class= ENDOF
527 :     1 OF s" right" class= ENDOF
528 :     2 OF s" circle" class= ENDOF
529 :     3 OF s" down" class= ENDOF
530 :     ENDCASE
531 :     THEN
532 : pazsan 1.51 s" a" tag parse-string s" a" /tag ." <!--" cr ." -->"
533 : pazsan 1.10 THEN
534 : pazsan 1.36 rdrop
535 : pazsan 1.22 1 toc-index +! toc-index @ /toc-line mod 0=
536 : pazsan 1.36 IF -env cr s" p" >env THEN ;
537 : pazsan 1.22
538 : pazsan 1.36 : print-toc ( -- ) toc-index off cr
539 : pazsan 1.56 toc-image IF s" img-menu" ELSE s" menu" THEN class=
540 : pazsan 1.36 s" div" >env cr s" p" >env
541 : pazsan 1.22 0 parse
542 : pazsan 1.10 dup 0= IF toc-name $! 0 ELSE
543 : pazsan 1.21 toc-name $! toc-name $@ id= s" " s" a" tagged 2
544 : pazsan 1.10 THEN >r
545 :     toc-link BEGIN @ dup WHILE
546 : pazsan 1.21 dup cell+ @ 3 = r@ 0= and IF rdrop 1 >r ( s" br" tag/ cr ) THEN
547 : pazsan 1.10 dup cell+ @ r@ >= IF dup r@ 2 = .toc-entry THEN
548 : pazsan 1.22 dup cell+ @ 2 = r@ 2 = and IF s" br" tag/ toc-index off THEN
549 : pazsan 1.36 REPEAT drop rdrop -env -env cr ;
550 : pazsan 1.10
551 : pazsan 1.1 \ handle global tags
552 :    
553 : pazsan 1.21 : indent ( n -- )
554 : pazsan 1.22 indentlevel @ over
555 : pazsan 1.21 indentlevel !
556 : pazsan 1.25 2dup < IF swap DO -env LOOP EXIT THEN
557 : pazsan 1.61 2dup > IF DO indent= s" div" >env LOOP EXIT THEN
558 :     2dup = IF drop IF -env indent= s" div" >env THEN THEN
559 : pazsan 1.21 ;
560 : pazsan 1.8
561 : pazsan 1.1 wordlist constant longtags
562 :    
563 : pazsan 1.21 Variable divs
564 :    
565 : pazsan 1.1 longtags set-current
566 :    
567 : pazsan 1.25 : --- 0 indent cr s" hr" tag/ cr ;
568 : pazsan 1.61 : * 1 indent s" h1" dclass= s" h1" par s" " dclass= ;
569 :     : ** 1 indent s" h2" dclass= s" h2" par s" " dclass= ;
570 :     : *** 2 indent s" h3" dclass= s" h3" par s" " dclass= ;
571 : pazsan 1.25 : -- 0 indent cr print-toc ;
572 :     : && 0 parse id= ;
573 :     : - s" ul" env s" li" par ;
574 :     : + s" ol" env s" li" par ;
575 :     : ? s" dl" env s" dt" par ;
576 :     : : s" dl" env s" dd" par ;
577 :     : -<< s" ul" env env? s" li" >env ;
578 :     : +<< s" ol" env env? s" li" >env ;
579 : pazsan 1.34 \ : ?<< s" dl" env env? s" dt" >env ; \ not allowed
580 : pazsan 1.25 : :<< s" dl" env env? s" dd" >env ;
581 :     : p<< s" p" >env ;
582 :     : << +env ;
583 :     : <* s" center" class= ;
584 : pazsan 1.56 : <red s" red" class= s" p" >env parse-par ;
585 :     : red> -env ;
586 : pazsan 1.25 : >> -env ;
587 : pazsan 1.23 : *> ;
588 : pazsan 1.25 : :: interpret ;
589 :     : . end-sec on 0 indent ;
590 :     : :code s" pre" >env
591 : pazsan 1.34 BEGIN source >in @ /string .type cr refill WHILE
592 : anton 1.16 source s" :endcode" str= UNTIL THEN
593 : pazsan 1.23 -env ;
594 : pazsan 1.25 : :code-file s" pre" >env
595 :     parse" slurp-file type -env ;
596 :     : \ postpone \ ;
597 : pazsan 1.1
598 :     definitions
599 : pazsan 1.25
600 :     : LT get-order longtags swap 1+ set-order
601 :     bl sword parser previous ; immediate
602 :    
603 : pazsan 1.3 \ Table
604 :    
605 : pazsan 1.5 : next-char ( -- char ) source drop >in @ + c@ ;
606 : pazsan 1.25 : next-table ( -- )
607 :     BEGIN
608 :     table-starts table# @ 2* + dup c@ dup
609 :     IF 1- over c! 1+ c@ 1+ ELSE swap 1+ c! 0 THEN
610 :     dup WHILE table# +! REPEAT drop
611 :     table-format $@ table# @ /string drop c@ taligned ! ;
612 :     : next>align ( -- )
613 :     next-char dup bl <> over '\ <> and
614 :     IF taligned ! 1 >in +! ELSE drop THEN ;
615 :    
616 :     : |tag ( addr u -- )
617 :     next-table
618 :     next-char '/ = IF 1 >in +!
619 :     next-char digit? IF
620 :     dup 1- table-starts table# @ 2* + c!
621 :     s" rowspan" opt# 1 >in +! THEN
622 :     next>align
623 :     THEN
624 :     next-char '\ = IF 1 >in +!
625 :     next-char digit? IF
626 :     dup 1- table-starts table# @ 2* + 1+ c!
627 :     dup 1- table# +!
628 :     s" colspan" opt# 1 >in +! THEN
629 :     next>align
630 :     THEN
631 :     taligned @ >talign >env
632 :     1 table# +! ;
633 :     : |d table# @ 0> IF -env THEN s" td" |tag ;
634 :     : |h table# @ 0> IF -env THEN s" th" |tag ;
635 :     : |line s" tr" >env table# off ;
636 :     : line| 1 >in +! -env -env cr ;
637 : pazsan 1.5
638 : pazsan 1.3 longtags set-current
639 :    
640 : pazsan 1.25 : <| ( -- ) table-starts &20 erase
641 :     s" table" class= s" div" >env
642 :     bl sword table-format $! bl sword
643 :     dup IF s" border" opt ELSE 2drop THEN
644 :     s" table" >env ;
645 : pazsan 1.12 : |> -env -env cr cr ;
646 : pazsan 1.25 : +| ( -- )
647 :     |line BEGIN |h '| parse-to next-char '+ = UNTIL line| ;
648 :     : -| ( -- )
649 :     |line BEGIN |d '| parse-to next-char '- = UNTIL line| ;
650 :     : =| ( -- )
651 :     |line |h '| parse-to
652 :     BEGIN |d '| parse-to next-char '= = UNTIL line| ;
653 : pazsan 1.3
654 :     definitions
655 : pazsan 1.1
656 :     \ parse a section
657 :    
658 : pazsan 1.25 : section-par ( -- ) >in off
659 : pazsan 1.9 bl sword longtags search-wordlist
660 :     IF execute
661 : pazsan 1.8 ELSE source nip IF >in off s" p" par THEN THEN ;
662 : pazsan 1.25 : parse-section ( -- ) end-sec off
663 : pazsan 1.8 BEGIN refill WHILE
664 : pazsan 1.34 section-par end-sec @ UNTIL THEN end-sec off ;
665 : pazsan 1.1
666 :     \ HTML head
667 :    
668 : pazsan 1.21 Variable css-file
669 : pazsan 1.44 Variable print-file
670 :     Variable ie-css-file
671 : pazsan 1.34 Variable content
672 : pazsan 1.44 Variable _charset
673 : pazsan 1.40 Variable _lang
674 :     Variable _favicon
675 : pazsan 1.21
676 : pazsan 1.34 : lang@ ( -- addr u )
677 : pazsan 1.40 _lang @ IF _lang $@ ELSE s" en" THEN ;
678 : pazsan 1.34 : .css ( -- )
679 :     css-file @ IF css-file $@len IF
680 :     s" StyleSheet" s" rel" opt
681 : pazsan 1.44 css-file $@ href= s" screen" s" media" opt
682 : pazsan 1.34 s" text/css" s" type" opt s" link" tag/ cr
683 : pazsan 1.44 THEN THEN
684 :     ie-css-file @ IF
685 :     ." <!--[if lt IE 7.0]>" cr
686 :     .' <style type="text/css">@import url(' ie-css-file $@ type ." );</style>" cr
687 :     ." <![endif]-->" cr
688 :     THEN ;
689 :     : .print ( -- )
690 :     print-file @ IF print-file $@len IF
691 :     s" StyleSheet" s" rel" opt
692 :     print-file $@ href= s" print" s" media" opt
693 :     s" text/css" s" type" opt s" link" tag/ cr
694 :     THEN THEN ;
695 : pazsan 1.34 : .title ( addr u -- ) 1 envs ! oldenv off
696 : pazsan 1.45 _charset $@ s" utf-8" str= 0=
697 :     IF .' <?xml version="1.0" encoding="' _charset $@ .upcase .' "?>' cr THEN
698 : pazsan 1.34 .' <!DOCTYPE html' cr
699 : pazsan 1.54 .' PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' cr
700 :     .' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' cr
701 : pazsan 1.34 s" http://www.w3.org/1999/xhtml" s" xmlns" opt
702 :     lang@ s" xml:lang" opt lang@ s" lang" opt
703 :     s" html" >env cr s" head" >env cr
704 : pazsan 1.21 s" Content-Type" s" http-equiv" opt
705 : pazsan 1.34 content $@ s" content" opt
706 : pazsan 1.44 s" meta" tag/ cr .css .print
707 : pazsan 1.40 _favicon @ IF
708 :     s" shortcut icon" s" rel" opt
709 :     _favicon $@ href=
710 :     s" image/x-icon" s" type" opt
711 :     s" link" tag/ cr THEN
712 : pazsan 1.2 s" title" tagged cr
713 : pazsan 1.1 -env ;
714 :    
715 :     \ HTML trailer
716 :    
717 : pazsan 1.29 Variable public-key
718 : pazsan 1.1 Variable mail
719 :     Variable mail-name
720 : pazsan 1.12 Variable orig-date
721 : pazsan 1.1
722 : pazsan 1.20 : .lastmod
723 :     ." Last modified: " time&date rot 0 u.r swap 1-
724 :     s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
725 :     0 u.r ;
726 :    
727 : pazsan 1.1 : .trailer
728 : pazsan 1.21 s" center" class= s" address" >env
729 : pazsan 1.12 orig-date @ IF ." Created " orig-date $@ type ." . " THEN
730 : pazsan 1.20 .lastmod
731 :     ." by "
732 : pazsan 1.27 s" Mail|@/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
733 : pazsan 1.29 public-key @ IF
734 :     public-key $@ href= s" a" tag
735 : pazsan 1.60 s" PGP key|-@/gpg-asc.gif" .img s" a" /tag
736 : pazsan 1.29 THEN
737 : pazsan 1.1 -envs ;
738 :    
739 :     \ top word
740 : pazsan 1.6
741 : pazsan 1.12 : maintainer ( -- )
742 : pazsan 1.25 '< sword -trailing mail-name $! '> sword mail $! ;
743 : pazsan 1.29 : pgp-key ( -- )
744 :     bl sword -trailing public-key $! ;
745 : pazsan 1.59 : charset ( -- ) s" application/xhtml+xml; charset=" content $!
746 : pazsan 1.44 bl sword -trailing 2dup content $+! _charset $! ;
747 : pazsan 1.34
748 :     charset iso-8859-1
749 :    
750 : pazsan 1.12 : created ( -- )
751 :     bl sword orig-date $! ;
752 : pazsan 1.27 : icons
753 :     bl sword icon-prefix $! ;
754 : pazsan 1.34 : lang
755 : pazsan 1.40 bl sword _lang $! ;
756 :     : favicon
757 :     bl sword _favicon $! ;
758 : pazsan 1.32 : expands '# sword expand-prefix $! bl sword expand-postfix $! ;
759 :    
760 : pazsan 1.27 icons icons
761 : pazsan 1.6
762 :     Variable style$
763 :     : style> style$ @ 0= IF s" " style$ $! THEN style$ $@ tag-option $! ;
764 :     : >style tag-option $@ style$ $! s" " tag-option $! ;
765 :    
766 :     : style style> opt >style ;
767 :     : background ( -- ) parse" s" background" style ;
768 :     : text ( -- ) parse" s" text" style ;
769 :     warnings @ warnings off
770 :     : link ( -- ) parse" s" link" style ;
771 :     warnings !
772 :     : vlink ( -- ) parse" s" vlink" style ;
773 :     : marginheight ( -- ) parse" s" marginheight" style ;
774 : pazsan 1.21 : css ( -- ) parse" css-file $! ;
775 : pazsan 1.44 : print-css ( -- ) parse" print-file $! ;
776 :     : ie-css ( -- ) parse" ie-css-file $! ;
777 : pazsan 1.1
778 :     : wf ( -- )
779 :     outfile-id >r
780 :     bl sword r/w create-file throw to outfile-id
781 : pazsan 1.6 parse" .title
782 :     +env style> s" body" env env?
783 : pazsan 1.1 ['] parse-section catch .trailer
784 :     outfile-id close-file throw
785 :     r> to outfile-id
786 :     dup 0< IF throw ELSE drop THEN ;
787 :    
788 : pazsan 1.8 : eval-par ( addr u -- )
789 :     s" wf-temp.wf" r/w create-file throw >r
790 :     r@ write-file r> close-file throw
791 :     push-file s" wf-temp.wf" r/o open-file throw loadfile !
792 : pazsan 1.34 parse-par -env parse-section
793 : pazsan 1.8 loadfile @ close-file swap 2dup or
794 :     pop-file drop throw throw
795 :     s" wf-temp.wf" delete-file throw ;
796 :    
797 : pazsan 1.2 \ simple text data base
798 :    
799 :     Variable last-entry
800 :     Variable field#
801 :    
802 : pazsan 1.25 : table: ( xt n -- ) Create 0 , ['] type , , , 1 field# !
803 :     DOES> 2 cells + 2@ >in @ >r longtags set-current
804 : pazsan 1.2 Create definitions swap , r> >in !
805 :     here last-entry !
806 :     dup 0 DO 0 , LOOP
807 :     1 DO s" " last-entry @ I cells + $! LOOP
808 :     last-entry @ get-rest
809 :     DOES> dup cell+ swap perform ;
810 :    
811 : pazsan 1.25 : field: Create field# @ , ['] type , 1 field# +!
812 : pazsan 1.2 DOES> @ cells last-entry @ + get-rest ;
813 : pazsan 1.25 : par: Create field# @ , ['] eval-par , 1 field# +!
814 : pazsan 1.8 DOES> @ cells last-entry @ + get-par ;
815 : pazsan 1.3
816 : pazsan 1.25 : >field-rest >body @ cells postpone Literal postpone + ;
817 :     : >field ' >field-rest ; immediate
818 :    
819 :     : db-line ( -- )
820 :     BEGIN
821 :     source >in @ /string nip WHILE
822 :     '% parse postpone SLiteral postpone type
823 :     '% parse dup IF
824 :     '| $split 2swap
825 :     sfind 0= abort" Field not found"
826 :     dup postpone r@ >field-rest postpone $@
827 :     over IF drop evaluate ELSE
828 :     nip nip >body cell+ @ compile,
829 :     THEN
830 :     ELSE 2drop postpone cr THEN
831 :     REPEAT ;
832 :    
833 :     : db-par ( -- ) LT postpone p<< postpone >r
834 :     BEGIN db-line refill WHILE next-char '. = UNTIL 1 >in +! THEN
835 : pazsan 1.34 postpone rdrop ( LT postpone >> ) ; immediate

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help