[gforth] / gforth / wf.fs  

gforth: gforth/wf.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help