File:  [gforth] / gforth / wf.fs
Revision 1.26: download - view: text, annotated - select for diffs
Sun Jul 18 13:31:06 2004 UTC (15 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
fixed link icon generation for #field URLS.

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

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