File:  [gforth] / gforth / wf.fs
Revision 1.61: download - view: text, annotated - select for diffs
Sat Feb 13 17:01:44 2010 UTC (9 years, 8 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Simplified indent

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

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