File:  [gforth] / gforth / wf.fs
Revision 1.52: download - view: text, annotated - select for diffs
Mon Dec 31 17:34:58 2007 UTC (11 years, 8 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

    1: \ wiki forth
    2: 
    3: \ Copyright (C) 2003,2004,2005,2006,2007 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 ! $FF umin ;
  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 = over '- = or IF  '- nav+
  461: 		    ELSE  nav+
  462: 		    THEN  THEN  THEN  THEN
  463:     LOOP ;
  464: : >nav ( addr u -- addr' u' )
  465:     nav-name $!  create-navs @ 0=
  466:     IF  s" navigate/nav.scm" r/w create-file throw create-navs !  THEN
  467:     s' (script-fu-nav-file "' nav$ $! nav-name $@ nav$ $+!
  468:     s' " "./navigate/' nav$ $+!  s" " nav-file $!
  469:     nav-name $@ filenamize
  470:     nav-file $@ nav$ $+! s' .jpg")' nav$ $+!
  471:     nav$ $@ create-navs @ write-line throw
  472:     s" [" nav$ $! nav-name $@ nav$ $+!
  473:     s" |-navigate/" nav$ $+! nav-file $@ nav$ $+! s" .jpg" nav$ $+!
  474:     nav$ $@ ;
  475: 
  476: : toc, ( n -- ) , '| parse >nav here 0 , $! 0 parse here 0 , $! ;
  477: : up-toc   align here toc-link >last , 0 toc, ;
  478: : top-toc  align here toc-link >last , 1 toc, ;
  479: : this-toc align here toc-link >last , 2 toc, ;
  480: : sub-toc  align here toc-link >last , 3 toc, ;
  481: : new-toc  toc-link off ;
  482: 
  483: Variable toc-name
  484: Variable toc-index
  485: 6 Value /toc-line
  486: true Value toc-image
  487: 
  488: : .toc-entry ( toc flag -- )
  489:     swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href=
  490:     '# scan 1 /string toc-name $@ compare >r
  491:     $@ toc-image IF  s" a" tag .img swap
  492: 	IF
  493: 	    case
  494: 		2 of  s" ^]|-@/arrow_up.jpg" .img  endof
  495: 		3 of
  496: 		    r@ 0= IF s" *]|-@/circle.jpg"
  497: 		    ELSE s" v]|-@/arrow_down.jpg"  THEN  .img  endof
  498: 	    endcase
  499: 	ELSE
  500: 	    case
  501: 		0 of  s" ^]|-@/arrow_up.jpg" .img  endof
  502: 		1 of  s" >]|-@/arrow_right.jpg" .img  endof
  503: 		2 of  s" *]|-@/circle.jpg" .img  endof
  504: 		3 of  s" v]|-@/arrow_down.jpg" .img  endof
  505: 	    endcase
  506: 	THEN
  507: 	s" a" /tag ." <!--" cr ." -->"
  508:     ELSE
  509: 	'[ skip  2dup '| scan nip - 2swap swap
  510: 	IF
  511: 	    CASE
  512: 		2 OF  s" up" class=  ENDOF
  513: 		3 OF  r@ 0= IF  s" circle" ELSE  s" down"  THEN class=  ENDOF
  514: 	    ENDCASE
  515: 	ELSE
  516: 	    CASE
  517: 		0  OF  s" up" class=  ENDOF
  518: 		1  OF  s" right" class=  ENDOF
  519: 		2  OF  s" circle" class=  ENDOF
  520: 		3  OF  s" down" class=  ENDOF
  521: 	    ENDCASE
  522: 	THEN
  523: 	s" a" tag parse-string s" a" /tag ." <!--" cr ." -->"
  524:     THEN
  525:     rdrop
  526:     1 toc-index +! toc-index @ /toc-line mod 0=
  527:     IF  -env cr s" p" >env  THEN ;
  528: 
  529: : print-toc ( -- ) toc-index off cr
  530:     toc-image IF  s" img-menu"  ELSE  s" menu"  THEN id=
  531:     s" div" >env cr s" p" >env
  532:     0 parse
  533:     dup 0= IF  toc-name $! 0  ELSE
  534: 	toc-name $! toc-name $@ id= s" " s" a" tagged  2
  535:     THEN  >r
  536:     toc-link  BEGIN  @ dup  WHILE
  537: 	dup cell+ @ 3 = r@ 0= and IF  rdrop 1 >r ( s" br" tag/ cr )  THEN
  538: 	dup cell+ @ r@ >= IF  dup r@ 2 = .toc-entry  THEN
  539: 	dup cell+ @ 2 = r@ 2 = and IF  s" br" tag/ toc-index off THEN
  540:     REPEAT  drop rdrop -env -env cr ;
  541: 
  542: \ handle global tags
  543: 
  544: : indent ( n -- )
  545:     indentlevel @ over
  546:     indentlevel !
  547:     2dup < IF swap DO  -env   LOOP  EXIT THEN
  548:     2dup > IF      DO   s" div" >env  LOOP EXIT THEN
  549:     2dup = IF drop IF  -env  s" div" >env  THEN THEN
  550: ;
  551: : +indent ( -- )
  552:     indentlevel @ IF  -env indent= s" div" >env  THEN
  553: ;
  554: 
  555: wordlist constant longtags
  556: 
  557: Variable divs
  558: 
  559: longtags set-current
  560: 
  561: : --- 0 indent cr s" hr" tag/ cr ;
  562: : *   1 indent s" h1" dclass= s" h1" par +indent s" " dclass= ;
  563: : **  1 indent s" h2" dclass= s" h2" par +indent s" " dclass= ;
  564: : *** 2 indent s" h3" dclass= s" h3" par +indent s" " dclass= ;
  565: : --  0 indent cr print-toc ;
  566: : &&  0 parse id= ;
  567: : -   s" ul" env s" li" par ;
  568: : +   s" ol" env s" li" par ;
  569: : ?   s" dl" env s" dt" par ;
  570: : :   s" dl" env s" dd" par ;
  571: : -<< s" ul" env env? s" li" >env ;
  572: : +<< s" ol" env env? s" li" >env ;
  573: \ : ?<< s" dl" env env? s" dt" >env ; \ not allowed
  574: : :<< s" dl" env env? s" dd" >env ;
  575: : p<< s" p" >env ;
  576: : <<  +env ;
  577: : <*  s" center" class= ;
  578: : <red  s" p" >env s" #ff0000" s" color" opt s" font" >env parse-par ;
  579: : red> -env -env ;
  580: : >>  -env ;
  581: : *> ;
  582: : ::  interpret ;
  583: : .   end-sec on 0 indent ;
  584: : :code s" pre" >env
  585:     BEGIN  source >in @ /string .type cr refill  WHILE
  586: 	source s" :endcode" str= UNTIL  THEN
  587:     -env ;
  588: : :code-file s" pre" >env
  589:     parse" slurp-file type -env ;
  590: : \   postpone \ ;
  591: 
  592: definitions
  593: 
  594: : LT  get-order longtags swap 1+ set-order
  595:     bl sword parser previous ; immediate
  596: 
  597: \ Table
  598: 
  599: : next-char ( -- char )  source drop >in @ + c@ ;
  600: : next-table ( -- )
  601:     BEGIN
  602: 	table-starts table# @ 2* + dup c@ dup
  603: 	IF    1- over c! 1+ c@ 1+  ELSE  swap 1+ c! 0  THEN
  604: 	dup WHILE  table# +!  REPEAT  drop
  605:     table-format $@ table# @ /string drop c@ taligned ! ;
  606: : next>align ( -- )
  607:     next-char dup bl <> over '\ <> and
  608:     IF  taligned ! 1 >in +!  ELSE  drop  THEN ;
  609: 
  610: : |tag ( addr u -- )
  611:     next-table
  612:     next-char '/ = IF  1 >in +!
  613: 	next-char digit?  IF
  614: 	    dup 1- table-starts table# @ 2* + c!
  615: 	    s" rowspan" opt# 1 >in +!  THEN
  616: 	next>align
  617:     THEN
  618:     next-char '\ = IF  1 >in +!
  619: 	next-char digit?  IF
  620: 	    dup 1- table-starts table# @ 2* + 1+ c!
  621: 	    dup 1- table# +!
  622: 	    s" colspan" opt# 1 >in +!  THEN
  623: 	next>align
  624:     THEN
  625:     taligned @ >talign >env
  626:     1 table# +! ;
  627: : |d  table# @ 0> IF  -env  THEN  s" td" |tag ;
  628: : |h  table# @ 0> IF  -env  THEN  s" th" |tag ;
  629: : |line  s" tr" >env table# off ;
  630: : line|  1 >in +! -env -env cr ;
  631: 
  632: longtags set-current
  633: 
  634: : <| ( -- )  table-starts &20 erase
  635:     s" table" class= s" div" >env
  636:     bl sword table-format $! bl sword
  637:     dup IF  s" border" opt  ELSE  2drop  THEN
  638:     s" table" >env ;
  639: : |> -env -env cr cr ;
  640: : +| ( -- )
  641:     |line  BEGIN  |h '| parse-to next-char '+ =  UNTIL line| ;
  642: : -| ( -- )
  643:     |line  BEGIN  |d '| parse-to next-char '- =  UNTIL line| ;
  644: : =| ( -- )
  645:     |line  |h '| parse-to
  646:            BEGIN  |d '| parse-to next-char '= =  UNTIL line| ;
  647: 
  648: definitions
  649: 
  650: \ parse a section
  651: 
  652: : section-par ( -- )  >in off
  653:     bl sword longtags search-wordlist
  654:     IF    execute
  655:     ELSE  source nip IF  >in off s" p" par  THEN  THEN ;
  656: : parse-section ( -- )  end-sec off
  657:     BEGIN  refill  WHILE
  658: 	section-par end-sec @  UNTIL  THEN  end-sec off ;
  659: 
  660: \ HTML head
  661: 
  662: Variable css-file
  663: Variable print-file
  664: Variable ie-css-file
  665: Variable content
  666: Variable _charset
  667: Variable _lang
  668: Variable _favicon
  669: 
  670: : lang@  ( -- addr u )
  671:     _lang @ IF  _lang $@  ELSE  s" en"  THEN ;
  672: : .css ( -- )
  673:     css-file @ IF  css-file $@len IF
  674: 	    s" StyleSheet" s" rel" opt
  675: 	    css-file $@ href= s" screen" s" media" opt
  676: 	    s" text/css" s" type" opt s" link" tag/ cr
  677: 	THEN  THEN
  678:     ie-css-file @ IF
  679: 	." <!--[if lt IE 7.0]>" cr
  680: 	.'    <style type="text/css">@import url(' ie-css-file $@ type ." );</style>" cr
  681: 	." <![endif]-->" cr
  682:     THEN ;
  683: : .print ( -- )
  684:     print-file @ IF  print-file $@len IF
  685:            s" StyleSheet" s" rel" opt
  686:            print-file $@ href= s" print" s" media" opt
  687:            s" text/css" s" type" opt s" link" tag/ cr
  688:        THEN  THEN ;
  689: : .title ( addr u -- )  1 envs ! oldenv off
  690:     _charset $@ s" utf-8" str= 0=
  691:     IF  .' <?xml version="1.0" encoding="' _charset $@ .upcase .' "?>' cr  THEN
  692:     .' <!DOCTYPE html' cr
  693:     .'   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' cr
  694:     .'   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' cr
  695:     s" http://www.w3.org/1999/xhtml" s" xmlns" opt
  696:     lang@ s" xml:lang" opt lang@ s" lang" opt
  697:     s" html" >env cr s" head" >env cr
  698:     s" Content-Type" s" http-equiv" opt
  699:     content $@ s" content" opt
  700:     s" meta" tag/ cr .css .print
  701:     _favicon @ IF
  702: 	s" shortcut icon" s" rel" opt
  703: 	_favicon $@ href=
  704: 	s" image/x-icon" s" type" opt
  705: 	s" link" tag/ cr  THEN
  706:     s" title" tagged cr
  707:     -env ;
  708: 
  709: \ HTML trailer
  710: 
  711: Variable public-key
  712: Variable mail
  713: Variable mail-name
  714: Variable orig-date
  715: 
  716: : .lastmod
  717:     ." Last modified: " time&date rot 0 u.r swap 1-
  718:     s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
  719:     0 u.r ;
  720: 
  721: : .trailer
  722:     s" center" class= s" address" >env
  723:     orig-date @ IF  ." Created " orig-date $@ type ." . "  THEN
  724:     .lastmod
  725:  ."  by "
  726:     s" Mail|@/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
  727:     public-key @ IF
  728: 	public-key $@ href=  s" a" tag
  729: 	s" PGP key|-@/gpg.asc.gif" .img s" a" /tag
  730:     THEN
  731:     -envs ;
  732: 
  733: \ top word
  734: 
  735: : maintainer ( -- )
  736:     '< sword -trailing mail-name $! '> sword mail $! ;
  737: : pgp-key ( -- )
  738:     bl sword -trailing public-key $! ;
  739: : charset ( -- )  s" text/xhtml; charset=" content $!
  740:     bl sword -trailing 2dup content $+! _charset $! ;
  741: 
  742: charset iso-8859-1
  743: 
  744: : created ( -- )
  745:     bl sword orig-date $! ;
  746: : icons
  747:     bl sword icon-prefix $! ;
  748: : lang
  749:     bl sword _lang $! ;
  750: : favicon
  751:     bl sword _favicon $! ;
  752: : expands '# sword expand-prefix $! bl sword expand-postfix $! ;
  753: 
  754: icons icons
  755: 
  756: Variable style$
  757: : style> style$ @ 0= IF  s" " style$ $!  THEN  style$ $@ tag-option $! ;
  758: : >style tag-option $@ style$ $! s" " tag-option $! ;
  759: 
  760: : style  style> opt >style ;
  761: : background ( -- )  parse" s" background" style ;
  762: : text ( -- )  parse" s" text" style ;
  763:     warnings @ warnings off
  764: : link ( -- )  parse" s" link" style ;
  765:     warnings !
  766: : vlink ( -- ) parse" s" vlink" style ;
  767: : marginheight ( -- ) parse" s" marginheight" style ;
  768: : css ( -- ) parse" css-file $! ;
  769: : print-css ( -- ) parse" print-file $! ;
  770: : ie-css ( -- ) parse" ie-css-file $! ;
  771: 
  772: : wf ( -- )
  773:     outfile-id >r
  774:     bl sword r/w create-file throw to outfile-id
  775:     parse" .title
  776:     +env style> s" body" env env?
  777:     ['] parse-section catch .trailer
  778:     outfile-id close-file throw
  779:     r> to outfile-id
  780:     dup 0< IF  throw  ELSE  drop  THEN ;
  781: 
  782: : eval-par ( addr u -- )
  783:   s" wf-temp.wf" r/w create-file throw >r
  784:   r@ write-file r> close-file throw
  785:   push-file s" wf-temp.wf" r/o open-file throw loadfile !
  786:   parse-par -env parse-section
  787:   loadfile @ close-file swap 2dup or
  788:   pop-file  drop throw throw
  789:   s" wf-temp.wf" delete-file throw ;
  790: 
  791: \ simple text data base
  792: 
  793: Variable last-entry
  794: Variable field#
  795: 
  796: : table: ( xt n -- )  Create 0 , ['] type , , ,  1 field# !
  797:     DOES> 2 cells + 2@ >in @ >r longtags set-current
  798:     Create definitions swap , r> >in !
  799:     here last-entry !
  800:     dup 0 DO  0 ,  LOOP
  801:     1 DO  s" " last-entry @ I cells + $!  LOOP
  802:     last-entry @ get-rest
  803:     DOES> dup cell+ swap perform ;
  804: 
  805: : field:  Create field# @ , ['] type , 1 field# +!
  806: DOES> @ cells last-entry @ + get-rest ;
  807: : par:  Create field# @ , ['] eval-par , 1 field# +!
  808: DOES> @ cells last-entry @ + get-par ;
  809: 
  810: : >field-rest >body @ cells postpone Literal postpone + ;
  811: : >field ' >field-rest ; immediate
  812: 
  813: : db-line ( -- )
  814:     BEGIN
  815: 	source >in @ /string nip  WHILE
  816: 	    '% parse  postpone SLiteral postpone type
  817: 	    '% parse dup IF
  818: 		'| $split 2swap
  819: 		sfind 0= abort" Field not found"
  820: 		dup postpone r@ >field-rest  postpone $@
  821: 		over IF  drop evaluate  ELSE
  822: 		    nip nip >body cell+ @ compile,
  823: 		THEN
  824: 	    ELSE  2drop  postpone cr  THEN
  825:     REPEAT ;
  826: 
  827: : db-par ( -- )  LT postpone p<< postpone >r
  828:     BEGIN  db-line refill  WHILE  next-char '. = UNTIL  1 >in +!  THEN
  829:     postpone rdrop ( LT postpone >> ) ; immediate

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