File:  [gforth] / gforth / wf.fs
Revision 1.31: download - view: text, annotated - select for diffs
Tue Jan 4 22:09:04 2005 UTC (19 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
added set-dir/get-dir primitives (directory access words)
added "maxascii" value as single switch between 8 bit and UTF-8 encoding
Improvements in wf.fs

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

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