File:  [gforth] / gforth / wf.fs
Revision 1.44: download - view: text, annotated - select for diffs
Sun Mar 19 23:24:57 2006 UTC (18 years ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Some minor improvements
reverted print-css

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

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