File:  [gforth] / gforth / wf.fs
Revision 1.50: download - view: text, annotated - select for diffs
Sun Dec 31 13:39:14 2006 UTC (17 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

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

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