--- gforth/wf.fs 2004/07/18 13:31:06 1.26 +++ gforth/wf.fs 2006/02/25 14:01:19 1.43 @@ -1,6 +1,6 @@ \ wiki forth -\ Copyright (C) 2003 Free Software Foundation, Inc. +\ Copyright (C) 2003,2004,2005 Free Software Foundation, Inc. \ This file is part of Gforth. @@ -34,11 +34,15 @@ require string.fs \ character recoding +[IFDEF] maxascii $100 to maxascii 8-bit-io [THEN] +\ UTF-8 IO fails with .type: + : .type ( addr u -- ) bounds ?DO I c@ case '& of ." &" endof '< of ." <" endof +\ '¤ of ." €" endof dup emit endcase LOOP ; @@ -47,10 +51,17 @@ require string.fs Variable indentlevel Variable tag-option +Variable tag-class +Variable default-class s" " tag-option $! +s" " tag-class $! +s" " default-class $! -: tag ( addr u -- ) '< emit type tag-option $@ type '> emit - s" " tag-option $! ; +: tag ( addr u -- ) '< emit type + tag-class $@len IF .\" class=\"" tag-class $@ type '" emit THEN + tag-option $@ type + '> emit + s" " tag-option $! default-class $@ tag-class $! ; : tag/ ( addr u -- ) s" /" tag-option $+! tag ; : /tag ( addr u -- ) '< emit '/ emit type '> emit ; : tagged ( addr1 u1 addr2 u2 -- ) 2dup 2>r tag .type 2r> /tag ; @@ -59,6 +70,7 @@ s" " tag-option $! tag-option $+! s' ="' tag-option $+! tag-option $+! s' "' tag-option $+! ; : n>string ( n -- addr u ) 0 <# #S #> ; +: xy>string ( x y -- ) swap 0 <# #S 'x hold 2drop 0 #S 's hold #> ; : opt# ( n opt u -- ) rot n>string 2swap opt ; : href= ( addr u -- ) s" href" opt ; : id= ( addr u -- ) s" id" opt ; @@ -67,7 +79,11 @@ s" " tag-option $! : width= ( n -- ) s" width" opt# ; : height= ( n -- ) s" height" opt# ; : align= ( addr u -- ) s" align" opt ; -: class= ( addr u -- ) s" class" opt ; +: class= ( addr u -- ) + tag-class $@len IF s" " tag-class $+! THEN + tag-class $+! ; +: dclass= ( addr u -- ) 2dup class= + default-class $! ; : indent= ( -- ) indentlevel @ 0 <# #S 'p hold #> class= ; : mailto: ( addr u -- ) s' href="mailto:' tag-option $+! @@ -81,13 +97,14 @@ Variable envs 30 0 [DO] 0 , [LOOP] : env$ ( -- addr ) envs dup @ 1+ cells + ; : env ( addr u -- ) env$ $! ; -: env? ( -- ) envs @ oldenv @ +: env? ( -- ) envs @ oldenv @ over oldenv ! 2dup > IF env$ $@ tag THEN 2dup < IF env$ cell+ $@ /tag env$ cell+ $off THEN - drop oldenv ! ; + 2drop ; : +env 1 envs +! ; -: -env end-sec @ envs @ 2 > or IF -1 envs +! env? THEN ; +: -env end-sec @ envs @ 1 > or IF -1 envs +! env? THEN ; : -envs envs @ 0 ?DO -env cr LOOP ; +: -tenvs envs @ 1 ?DO -env cr LOOP ; : >env ( addr u -- ) +env env env? ; \ alignment @@ -126,6 +143,8 @@ Variable taligned \ image handling +wordlist Constant img-sizes + Create imgbuf $20 allot Create pngsig $89 c, $50 c, $4E c, $47 c, $0D c, $0A c, $1A c, $0A c, @@ -161,15 +180,59 @@ Create jfif $FF c, $D8 c, $FF c, $E0 c gif? IF gif-size rdrop EXIT THEN jpg? IF r> jpg-size EXIT THEN png? IF png-size rdrop EXIT THEN - 0 0 ; + 0 0 rdrop ; + +3 set-precision + +: f.size ( r -- ) + f$ dup >r 0<= + IF '0 emit + ELSE scratch r@ min type r@ precision - zeros THEN + r@ negate zeros + scratch r> 0 max /string 0 max -zeros + dup IF '. emit THEN type ; + +12.9e FConstant pixels +FVariable factor 1e factor f! + +: size-does> ( -- ) DOES> ( -- ) + ." img." dup body> >name .name + 2@ ." { width: " + s>d d>f pixels f/ f.size ." em; height: " + s>d d>f pixels f/ f.size ." em; }" cr ; + +: size-css ( file< > -- ) + outfile-id >r + bl sword r/w create-file throw to outfile-id + img-sizes wordlist-id + BEGIN @ dup WHILE + dup name>int execute + REPEAT drop + outfile-id close-file throw + r> to outfile-id + dup 0< IF throw ELSE drop THEN ; + +: size-class ( x y addr u -- x y ) + 2dup class= + 2dup img-sizes search-wordlist IF drop 2drop + ELSE + get-current >r img-sizes set-current + nextname Create 2dup + s>d d>f factor f@ f* f>d d>s , + s>d d>f factor f@ f* f>d d>s , + size-does> + r> set-current + THEN ; : .img-size ( addr u -- ) r/o open-file IF drop EXIT THEN >r imgbuf $20 r@ read-file throw drop r@ img-size r> close-file throw + 2dup or IF 2dup xy>string size-class THEN ?dup IF width= THEN - ?dup IF height= THEN ; + ?dup IF height= THEN +; \ link creation @@ -177,16 +240,23 @@ Variable link Variable link-sig Variable link-suffix Variable iconpath +Variable icon-prefix +Variable icon-tmp Variable do-size Variable do-icon +Variable do-expand Defer parse-line -: .img ( addr u -- ) dup >r '| -$split dup r> = IF 2swap THEN +: .img ( addr u -- ) + dup >r '@ -$split dup r> = IF 2swap 2drop + ELSE 2swap icon-tmp $! icon-prefix $@ icon-tmp $+! icon-tmp $+! + icon-tmp $@ THEN + dup >r '| -$split dup r> = IF 2swap THEN dup IF 2swap alt= ELSE 2drop THEN - tag-option $@len >r over c@ >align tag-option $@len r> = 1+ /string - tag-option $@len >r over c@ >border tag-option $@len r> = 1+ /string + tag-class $@len >r over c@ >align tag-class $@len r> = 1+ /string + tag-class $@len >r over c@ >border tag-class $@len r> = 1+ /string 2dup .img-size src= s" img" tag/ ; : >img ( -- ) '{ parse type '} parse .img ; @@ -198,11 +268,11 @@ Defer parse-line : get-icon ( addr u -- ) iconpath @ IF 2drop EXIT THEN link-suffix $! s" .*" link-suffix $+! - s" icons" open-dir throw >r + icon-prefix $@ open-dir throw >r BEGIN pad $100 r@ read-dir throw WHILE pad swap 2dup link-suffix $@ filename-match - IF s" icons/" iconpath $! iconpath $+! + IF icon-prefix $@ iconpath $! s" /" iconpath $+! iconpath $+! iconpath $@ 2dup .img-size src= '- >border alt-suffix s" img" tag/ true ELSE 2drop false THEN @@ -228,20 +298,23 @@ Defer parse-line link-sig $@ r/o open-file IF drop EXIT THEN close-file throw ." (" link-sig $@ href= s" a" tag - s" |-icons/sig.gif" .img ." sig" s" /a" tag ." )" ; + s" |-@/sig.gif" .img ." sig" s" /a" tag ." )" ; : link-warn? ( -- ) \ local links only link $@ ': scan nip ?EXIT - link $@ '# $split 2drop r/o open-file nip IF - s" Dead Link '" stderr write-file throw - link $@ stderr write-file throw - s\" ' !!!\n" stderr write-file throw - THEN ; + link $@ '# $split 2drop dup IF + r/o open-file nip IF + s" Dead Link '" stderr write-file throw + link $@ stderr write-file throw + s\" ' !!!\n" stderr write-file throw + THEN + ELSE 2drop THEN ; : link-options ( addr u -- addr' u' ) - do-size off do-icon on - over c@ '% = over 0> and IF do-size on 1 /string THEN - over c@ '\ = over 0> and IF do-icon off 1 /string THEN ; + do-size off do-icon on do-expand off + over c@ '% = over 0> and IF do-size on 1 /string THEN + over c@ '\ = over 0> and IF do-icon off 1 /string THEN + over c@ '* = over 0> and IF do-expand on 1 /string THEN ; s" Gforth" environment? [IF] s" 0.5.0" str= [IF] : parse-string ( c-addr u -- ) \ core,block @@ -256,10 +329,21 @@ s" Gforth" environment? [IF] s" 0.5.0" s ['] parse-line catch pop-file throw ; [THEN] [THEN] +Variable expand-link +Variable expand-prefix +Variable expand-postfix + +: ?expand ( addr u -- ) expand-link $! + do-expand @ IF + expand-prefix $@ expand-link 0 $ins + expand-postfix $@ expand-link $+! THEN + expand-link $@ ; + : .link ( addr u -- ) dup >r '| -$split dup r> = IF 2swap THEN link-options link $! link $@len 0= IF 2dup link $! ( s" .html" link $+! ) THEN - link $@ href= s" a" tag link-icon? + link $@ ?expand + href= s" a" tag link-icon? parse-string s" a" /tag link-size? link-sig? link-warn? ; : >link ( -- ) '[ parse type '] parse .link ; @@ -332,7 +416,8 @@ wordlist Constant autoreplacements \ paragraph handling : parse-par ( -- ) - BEGIN parse-line+ cr refill WHILE + BEGIN + parse-line+ cr refill WHILE source nip 0= UNTIL THEN ; : par ( addr u -- ) env? @@ -392,31 +477,52 @@ Create nav-buf 0 c, Variable toc-name Variable toc-index 6 Value /toc-line +true Value toc-image : .toc-entry ( toc flag -- ) - swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href= s" a" tag + swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href= '# scan 1 /string toc-name $@ compare >r - $@ .img swap - IF - case - 2 of s" ^]|-icons/arrow_up.jpg" .img endof - 3 of - r@ 0= IF s" *]|-icons/circle.jpg" - ELSE s" v]|-icons/arrow_down.jpg" THEN .img endof - endcase + $@ toc-image IF s" a" tag .img swap + IF + case + 2 of s" ^]|-@/arrow_up.jpg" .img endof + 3 of + r@ 0= IF s" *]|-@/circle.jpg" + ELSE s" v]|-@/arrow_down.jpg" THEN .img endof + endcase + ELSE + case + 0 of s" ^]|-@/arrow_up.jpg" .img endof + 1 of s" >]|-@/arrow_right.jpg" .img endof + 2 of s" *]|-@/circle.jpg" .img endof + 3 of s" v]|-@/arrow_down.jpg" .img endof + endcase + THEN + s" a" /tag ." " ELSE - case - 0 of s" ^]|-icons/arrow_up.jpg" .img endof - 1 of s" >]|-icons/arrow_right.jpg" .img endof - 2 of s" *]|-icons/circle.jpg" .img endof - 3 of s" v]|-icons/arrow_down.jpg" .img endof - endcase + '[ skip 2dup '| scan nip - 2swap swap + IF + CASE + 2 OF s" up" class= ENDOF + 3 OF r@ 0= IF s" circle" ELSE s" down" THEN class= ENDOF + ENDCASE + ELSE + CASE + 0 OF s" up" class= ENDOF + 1 OF s" right" class= ENDOF + 2 OF s" circle" class= ENDOF + 3 OF s" down" class= ENDOF + ENDCASE + THEN + s" a" tag parse-string s" a" /tag THEN - s" a" /tag rdrop ." " + rdrop 1 toc-index +! toc-index @ /toc-line mod 0= - IF s" br" tag/ THEN ; + IF -env cr s" p" >env THEN ; -: print-toc ( -- ) toc-index off cr s" menu" id= s" div" >env cr +: print-toc ( -- ) toc-index off cr + toc-image IF s" img-menu" ELSE s" menu" THEN id= + s" div" >env cr s" p" >env 0 parse dup 0= IF toc-name $! 0 ELSE toc-name $! toc-name $@ id= s" " s" a" tagged 2 @@ -425,7 +531,7 @@ Variable toc-index dup cell+ @ 3 = r@ 0= and IF rdrop 1 >r ( s" br" tag/ cr ) THEN dup cell+ @ r@ >= IF dup r@ 2 = .toc-entry THEN dup cell+ @ 2 = r@ 2 = and IF s" br" tag/ toc-index off THEN - REPEAT drop rdrop -env cr ; + REPEAT drop rdrop -env -env cr ; \ handle global tags @@ -447,9 +553,9 @@ Variable divs longtags set-current : --- 0 indent cr s" hr" tag/ cr ; -: * 1 indent s" h1" par +indent ; -: ** 1 indent s" h2" par +indent ; -: *** 2 indent s" h3" par +indent ; +: * 1 indent s" h1" dclass= s" h1" par +indent s" " dclass= ; +: ** 1 indent s" h2" dclass= s" h2" par +indent s" " dclass= ; +: *** 2 indent s" h3" dclass= s" h3" par +indent s" " dclass= ; : -- 0 indent cr print-toc ; : && 0 parse id= ; : - s" ul" env s" li" par ; @@ -458,19 +564,19 @@ longtags set-current : : s" dl" env s" dd" par ; : -<< s" ul" env env? s" li" >env ; : +<< s" ol" env env? s" li" >env ; -: ?<< s" dl" env env? s" dt" >env ; +\ : ?<< s" dl" env env? s" dt" >env ; \ not allowed : :<< s" dl" env env? s" dd" >env ; : p<< s" p" >env ; : << +env ; : <* s" center" class= ; -: env ; -: red> -env ; +: env s" #ff0000" s" color" opt s" font" >env parse-par ; +: red> -env -env ; : >> -env ; : *> ; : :: interpret ; : . end-sec on 0 indent ; : :code s" pre" >env - BEGIN source >in @ /string type cr refill WHILE + BEGIN source >in @ /string .type cr refill WHILE source s" :endcode" str= UNTIL THEN -env ; : :code-file s" pre" >env @@ -543,28 +649,44 @@ definitions ELSE source nip IF >in off s" p" par THEN THEN ; : parse-section ( -- ) end-sec off BEGIN refill WHILE - section-par end-sec @ UNTIL THEN ; + section-par end-sec @ UNTIL THEN end-sec off ; \ HTML head Variable css-file - -: .title ( addr u -- ) - .' ' cr - s" html" >env s" head" >env cr - s" Content-Type" s" http-equiv" opt - s" text/xhtml; charset=iso-8859-1" s" content" opt - s" meta" tag/ +Variable content +Variable _lang +Variable _favicon + +: lang@ ( -- addr u ) + _lang @ IF _lang $@ ELSE s" en" THEN ; +: .css ( -- ) css-file @ IF css-file $@len IF - s" StyleSheet" s" rel" opt - css-file $@ href= - s" text/css" s" type" opt s" link" tag/ - THEN THEN + s" StyleSheet" s" rel" opt + css-file $@ href= + s" text/css" s" type" opt s" link" tag/ cr + THEN THEN ; +: .title ( addr u -- ) 1 envs ! oldenv off + .' ' cr + s" http://www.w3.org/1999/xhtml" s" xmlns" opt + lang@ s" xml:lang" opt lang@ s" lang" opt + s" html" >env cr s" head" >env cr + s" Content-Type" s" http-equiv" opt + content $@ s" content" opt + s" meta" tag/ cr .css + _favicon @ IF + s" shortcut icon" s" rel" opt + _favicon $@ href= + s" image/x-icon" s" type" opt + s" link" tag/ cr THEN s" title" tagged cr -env ; \ HTML trailer +Variable public-key Variable mail Variable mail-name Variable orig-date @@ -579,15 +701,35 @@ Variable orig-date orig-date @ IF ." Created " orig-date $@ type ." . " THEN .lastmod ." by " - s" Mail|icons/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged + s" Mail|@/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged + public-key @ IF + public-key $@ href= s" a" tag + s" PGP key|-@/gpg.asc.gif" .img s" a" /tag + THEN -envs ; \ top word : maintainer ( -- ) '< sword -trailing mail-name $! '> sword mail $! ; +: pgp-key ( -- ) + bl sword -trailing public-key $! ; +: charset ( -- ) s" text/xhtml; charset=" content $! + bl sword -trailing content $+! ; + +charset iso-8859-1 + : created ( -- ) bl sword orig-date $! ; +: icons + bl sword icon-prefix $! ; +: lang + bl sword _lang $! ; +: favicon + bl sword _favicon $! ; +: expands '# sword expand-prefix $! bl sword expand-postfix $! ; + +icons icons Variable style$ : style> style$ @ 0= IF s" " style$ $! THEN style$ $@ tag-option $! ; @@ -617,7 +759,7 @@ Variable style$ s" wf-temp.wf" r/w create-file throw >r r@ write-file r> close-file throw push-file s" wf-temp.wf" r/o open-file throw loadfile ! - parse-par parse-section + parse-par -env parse-section loadfile @ close-file swap 2dup or pop-file drop throw throw s" wf-temp.wf" delete-file throw ; @@ -660,4 +802,4 @@ DOES> @ cells last-entry @ + get-par ; : db-par ( -- ) LT postpone p<< postpone >r BEGIN db-line refill WHILE next-char '. = UNTIL 1 >in +! THEN - postpone rdrop LT postpone >> ; immediate + postpone rdrop ( LT postpone >> ) ; immediate