File:  [gforth] / gforth / wf.fs
Revision 1.38: download - view: text, annotated - select for diffs
Tue Jun 28 15:22:45 2005 UTC (14 years, 3 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Adapted interpretative loops

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

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