Annotation of gforth/wf.fs, revision 1.36

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>