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