File:  [gforth] / gforth / wf.fs
Revision 1.11: download - view: text, annotated - select for diffs
Mon Aug 6 20:39:48 2001 UTC (18 years ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Another set of fixes for wf.fs

    1: \ wiki forth
    2: 
    3: require string.fs
    4: 
    5: : -scan ( addr u char -- addr' u' )
    6:   >r  BEGIN  dup  WHILE  1- 2dup + c@ r@ =  UNTIL  THEN
    7:   rdrop ;
    8: : -$split ( addr u char -- addr1 u1 addr2 u2 )
    9:   >r 2dup r@ -scan 2dup + c@ r> = negate over + >r
   10:   2swap r> /string ;
   11: 
   12: \ tag handling
   13: 
   14: : .' '' parse postpone SLiteral postpone type ; immediate
   15: : s' '' parse postpone SLiteral ; immediate
   16: 
   17: Variable tag-option
   18: s" " tag-option $!
   19: 
   20: : tag ( addr u -- ) '< emit type tag-option $@ type '> emit
   21:     s" " tag-option $! ;
   22: : /tag ( addr u -- ) '< emit '/ emit type '> emit ;
   23: : tagged ( addr1 u1 addr2 u2 -- )  2dup 2>r tag type 2r> /tag ;
   24: 
   25: : opt ( addr u opt u -- )  s"  " tag-option $+!
   26:     tag-option $+! s' ="' tag-option $+! tag-option $+!
   27:     s' "' tag-option $+! ;
   28: : href= ( addr u -- )  s" href" opt ;
   29: : name= ( addr u -- )  s" name" opt ;
   30: : src=  ( addr u -- )  s" src" opt ;
   31: : alt=  ( addr u -- )  s" alt" opt ;
   32: : width=  ( addr u -- )  s" width" opt ;
   33: : height=  ( addr u -- )  s" height" opt ;
   34: : align= ( addr u -- ) s" align" opt ;
   35: : mailto: ( addr u -- ) s'  href="mailto:' tag-option $+!
   36:     tag-option $+! s' "' tag-option $+! ;
   37: 
   38: \ environment handling
   39: 
   40: Variable end-sec
   41: Variable oldenv
   42: Variable envs 30 0 [DO] 0 , [LOOP]
   43: 
   44: : env$ ( -- addr ) envs dup @ 1+ cells + ;
   45: : env ( addr u -- ) env$ $! ;
   46: : env? ( -- ) envs @ oldenv @
   47:     2dup > IF  env$ $@ tag  THEN
   48:     2dup < IF  env$ cell+ $@ /tag  env$ cell+ $off  THEN
   49:     drop oldenv ! ;
   50: : +env  1 envs +! ;
   51: : -env end-sec @ envs @ 2 > or  IF  -1 envs +! env?  THEN ;
   52: : -envs envs @ 0 ?DO  -env cr  LOOP ;
   53: : >env ( addr u -- ) +env env env? ;
   54: 
   55: \ alignment
   56: 
   57: : >align ( c -- )
   58:     CASE
   59: 	'l OF  s" left"   align=  ENDOF
   60: 	'r OF  s" right"  align=  ENDOF
   61: 	'c OF  s" center" align=  ENDOF
   62: 	'< OF  s" left"   align=  ENDOF
   63: 	'> OF  s" right"  align=  ENDOF
   64: 	'= OF  s" center" align=  ENDOF
   65:     ENDCASE ;
   66: 
   67: : >border ( c -- )
   68:     case
   69: 	'- of  s" 0" s" border" opt endof
   70: 	'+ of  s" 1" s" border" opt endof
   71:     endcase ;
   72: 
   73: \ image handling
   74: 
   75: Create imgbuf $20 allot
   76: 
   77: Create pngsig $89 c, $50 c, $4E c, $47 c, $0D c, $0A c, $1A c, $0A c,
   78: Create jfif   $FF c, $D8 c, $FF c, $E0 c, $00 c, $10 c, $4A c, $46 c,
   79:               $49 c, $46 c,
   80: 
   81: : b@ ( addr -- x )   0 swap 4 bounds ?DO  8 lshift I c@ +  LOOP ;
   82: : bw@ ( addr -- x )  0 swap 2 bounds ?DO  8 lshift I c@ +  LOOP ;
   83: 
   84: : gif? ( -- flag )
   85:     s" GIF89a" imgbuf over compare 0=
   86:     s" GIF87a" imgbuf over compare 0= or ;
   87: : gif-size ( -- w h )
   88:     imgbuf 8 + c@ imgbuf 9 + c@ 8 lshift +
   89:     imgbuf 6 + c@ imgbuf 7 + c@ 8 lshift + ;
   90: 
   91: : png? ( -- flag )
   92:     pngsig 8 imgbuf over compare 0= ;
   93: : png-size ( -- w h )
   94:     imgbuf $14 + b@ imgbuf $10 + b@ ;
   95: 
   96: : jpg? ( -- flag )
   97:     jfif 10 imgbuf over compare 0= ;
   98: : jpg-size ( fd -- w h )  >r
   99:     2.  BEGIN
  100: 	2dup r@ reposition-file throw
  101: 	imgbuf $10 r@ read-file throw 0<>
  102: 	imgbuf bw@ $FFC0 $FFD0 within 0= and  WHILE
  103: 	imgbuf 2 + bw@ 2 + 0 d+  REPEAT
  104:     2drop imgbuf 5 + bw@ imgbuf 7 + bw@  rdrop ;
  105: 
  106: : img-size ( fd -- w h )  >r
  107:     gif? IF  gif-size  rdrop EXIT  THEN
  108:     jpg? IF  r> jpg-size  EXIT  THEN
  109:     png? IF  png-size  rdrop EXIT  THEN
  110:     0 0 ;
  111: 
  112: : .img-size ( addr u -- )
  113:     r/o open-file IF  drop  EXIT  THEN  >r
  114:     imgbuf $20 r@ read-file throw drop
  115:     r@ img-size
  116:     r> close-file throw
  117:     ?dup IF  0 <# #S #> width=   THEN
  118:     ?dup IF  0 <# #S #> height=  THEN ;
  119: 
  120: \ link creation
  121: 
  122: Variable link
  123: Variable link-sig
  124: Variable link-suffix
  125: Variable iconpath
  126: 
  127: Variable do-size
  128: Variable do-icon
  129: 
  130: Defer parse-line
  131: 
  132: : .img ( addr u -- ) dup >r '| -$split  dup r> = IF  2swap  THEN 
  133:     dup IF  2swap alt=  ELSE  2drop  THEN
  134:     tag-option $@len >r over c@ >align  tag-option $@len r> = 1+ /string
  135:     tag-option $@len >r over c@ >border tag-option $@len r> = 1+ /string
  136:     2dup .img-size src= s" img" tag ;
  137: : >img ( -- )   '{ parse type '} parse .img ;
  138: 
  139: : alt-suffix ( -- )
  140:     link-suffix $@len 2 - link-suffix $!len
  141:     s" [" link-suffix 0 $ins
  142:     s" ]" link-suffix $+!
  143:     link-suffix $@ alt= ;
  144: 
  145: : get-icon ( addr u -- )  iconpath @ IF  2drop  EXIT  THEN
  146:     link-suffix $! s" .*" link-suffix $+!
  147:     s" icons" open-dir throw >r
  148:     BEGIN
  149: 	pad $100 r@ read-dir throw  WHILE
  150: 	pad swap 2dup link-suffix $@ filename-match
  151: 	IF  s" icons/" iconpath $! iconpath $+!
  152: 	    iconpath $@ 2dup .img-size src= '- >border
  153: 	    alt-suffix  s" img" tag true
  154: 	ELSE  2drop  false  THEN
  155:     UNTIL  ELSE  drop  THEN
  156:     r> close-dir throw ;
  157: 
  158: : link-icon? ( -- )  do-icon @ 0= ?EXIT
  159:     iconpath @  IF  iconpath $off  THEN
  160:     link $@ + 1- c@ '/ = IF  s" index.html"  ELSE  link $@  THEN
  161:     BEGIN  '. $split 2swap 2drop dup  WHILE
  162: 	2dup get-icon  REPEAT  2drop ;
  163: 
  164: : link-size? ( -- )  do-size @ 0= ?EXIT
  165:     link $@ r/o open-file IF  drop  EXIT  THEN >r
  166:     r@ file-size throw $400 um/mod nip ."  (" 0 u.r ." k)"
  167:     r> close-file throw ;
  168: 
  169: : link-sig? ( -- )
  170:     link $@ link-sig $! s" .sig" link-sig $+!
  171:     link-sig $@ r/o open-file IF  drop  EXIT  THEN
  172:     close-file throw
  173:     ."  (" link-sig $@ href= s" a" tag
  174:     s" |-icons/sig.gif" .img ." sig" s" /a" tag ." )" ;
  175: 
  176: : link-options ( addr u -- addr' u' )
  177:     do-size off  do-icon on
  178:     over c@ '% = over 0> and IF  do-size on  1 /string  THEN
  179:     over c@ '\ = over 0> and IF  do-icon off 1 /string  THEN ;
  180: 
  181: : parse-string ( addr u -- )
  182:     evaluate-input cell new-tib #tib ! tib !
  183:     ['] parse-line catch pop-file throw ;
  184: 
  185: : .link ( addr u -- ) dup >r '| -$split  dup r> = IF  2swap  THEN 
  186:     link-options link $!
  187:     link $@len 0= IF  2dup link $! s" .html" link $+!  THEN
  188:     link $@ href= s" a" tag link-icon?
  189:     parse-string s" a" /tag link-size? link-sig? ;
  190: : >link ( -- )  '[ parse type '] parse .link ;
  191: 
  192: \ line handling
  193: 
  194: : char? ( -- c )  >in @ char swap >in ! ;
  195: : parse-tag ( addr u char -- )
  196:     >r r@ parse type
  197:     r> parse 2swap tagged ;
  198: 
  199: : .text ( -- ) 	>in @ >r char drop
  200:     source r@ /string >in @ r> - nip
  201:     bounds ?DO  I c@
  202: 	case
  203: 	    '& of  ." &amp;"  endof
  204: 	    '< of  ." &lt;"   endof
  205: 	    dup emit
  206: 	endcase
  207:     LOOP ;
  208: 
  209: Create do-words  $100 0 [DO] ' .text , [LOOP]
  210: 
  211: :noname '( emit 1 >in +! ; '( cells do-words + !
  212: 
  213: : bind-char ( xt -- )  char cells do-words + ! ;
  214: 
  215: : char>tag ( -- ) char >r
  216: :noname bl sword postpone SLiteral r@ postpone Literal
  217:     postpone parse-tag postpone ; r> cells do-words + ! ;
  218: 
  219: char>tag * b
  220: char>tag _ em
  221: char>tag # code
  222: 
  223: ' >link bind-char [
  224: ' >img  bind-char {
  225: 
  226: : do-word ( char -- )  cells do-words + perform ;
  227: 
  228: : word? ( -- addr u )  >in @ >r bl sword r> >in ! ;
  229: 
  230: wordlist Constant autoreplacements
  231: 
  232: :noname ( -- )
  233:     BEGIN char? do-word source nip >in @ = UNTIL ; is parse-line
  234: 
  235: : parse-line+ ( -- )
  236:     BEGIN
  237: 	word? autoreplacements search-wordlist
  238: 	IF    execute  bl sword 2drop
  239: 	    source >in @ 1- /string drop c@ bl = >in +!
  240: 	ELSE  char? do-word  THEN
  241: 	source nip >in @ = UNTIL ;
  242: 
  243: : parse-to ( char -- ) >r
  244:     BEGIN  char? dup r@ <> WHILE
  245: 	do-word source nip >in @ = UNTIL  ELSE  drop  THEN
  246:     r> parse type ;
  247: 
  248: \ autoreplace
  249: 
  250: : autoreplace ( <[string|url]> -- )
  251:     get-current autoreplacements set-current
  252:     Create set-current
  253:     here 0 , '[ parse 2drop '] parse rot $!
  254:     DOES> $@ .link ;
  255:     
  256: \ paragraph handling
  257: 
  258: : parse-par ( -- )
  259:     BEGIN  parse-line+ cr refill  WHILE
  260: 	source nip 0= UNTIL  THEN ;
  261: 
  262: : par ( addr u -- ) env? 2dup tag parse-par /tag cr cr ;
  263: : line ( addr u -- ) env? 2dup tag parse-line+ /tag cr cr ;
  264: 
  265: \ scan strings
  266: 
  267: : get-rest ( addr -- ) 0 parse -trailing rot $! ;
  268: Create $lf 1 c, #lf c,
  269: : get-par ( addr -- )  >r  s" " r@ $+!
  270:     BEGIN  0 parse 2dup s" ." compare  WHILE
  271: 	r@ $@len IF  $lf count r@ $+!  THEN  r@ $+!
  272: 	refill 0= UNTIL  ELSE  2drop  THEN
  273:     rdrop ;
  274: 
  275: \ toc handling
  276: 
  277: Variable toc-link
  278: 
  279: : >last ( addr link -- link' )
  280:     BEGIN  dup @  WHILE  @  REPEAT  ! 0 ;
  281: 
  282: : toc, ( n -- ) , 0 parse '| -$split 2swap here 0 , $! here 0 , $! ;
  283: : up-toc   align here toc-link >last , 0 toc, ;
  284: : top-toc  align here toc-link >last , 1 toc, ;
  285: : this-toc align here toc-link >last , 2 toc, ;
  286: : sub-toc  align here toc-link >last , 3 toc, ;
  287: 
  288: Variable toc-name
  289: 
  290: : .toc-entry ( toc flag -- )
  291:     swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href= s" a" tag
  292:     '# scan 1 /string toc-name $@ compare >r
  293:     $@ .img swap
  294:     IF
  295: 	case
  296: 	    2 of  s" ^]|-icons/arrow_up.jpg" .img  endof
  297: 	    3 of
  298: 		r@ 0= IF s" *]|-icons/circle.jpg"
  299: 		    ELSE s" v]|-icons/arrow_down.jpg"  THEN  .img  endof
  300: 	endcase
  301:     ELSE
  302: 	case
  303: 	    0 of  s" ^]|-icons/arrow_up.jpg" .img  endof
  304: 	    1 of  s" >]|-icons/arrow_right.jpg" .img  endof
  305: 	    2 of  s" *]|-icons/circle.jpg" .img  endof
  306: 	    3 of  s" v]|-icons/arrow_down.jpg" .img  endof
  307: 	endcase
  308:     THEN
  309:     s" a" /tag rdrop
  310:     ;
  311: : print-toc ( -- ) cr 0 parse
  312:     dup 0= IF  toc-name $! 0  ELSE
  313: 	toc-name $! toc-name $@ name= s" " s" a" tagged  2
  314:     THEN  >r
  315:     toc-link  BEGIN  @ dup  WHILE
  316: 	dup cell+ @ 3 = r@ 0= and IF  rdrop 1 >r s" br" tag cr  THEN
  317: 	dup cell+ @ r@ >= IF  dup r@ 2 = .toc-entry  THEN
  318: 	dup cell+ @ 2 = r@ 2 = and IF  s" br" tag cr  THEN
  319:     REPEAT  drop rdrop  cr ;
  320: 
  321: \ handle global tags
  322: 
  323: Variable indentlevel
  324: : indent ( n -- )  indentlevel @ over indentlevel !
  325:     2dup < IF swap DO  -env -env  LOOP  EXIT THEN
  326:     2dup > IF      DO  s" dl" >env s" dt" >env  LOOP EXIT THEN
  327:     2dup = IF drop IF  -env  s" dt" >env  THEN THEN ;
  328: : +indent ( -- )  indentlevel @ IF  -env s" dd" >env  THEN ;
  329: 
  330: wordlist constant longtags
  331: 
  332: longtags set-current
  333: 
  334: : --- 0 indent cr s" hr" tag cr +indent ;
  335: : *   1 indent s" h1" line +indent ;
  336: : **  1 indent s" h2" line +indent ;
  337: : *** 2 indent s" h3" line +indent ;
  338: : -- 0 indent cr print-toc ;
  339: : - s" ul" env s" li" par ;
  340: : + s" ol" env s" li" par ;
  341: : << +env ;
  342: : <* s" center" >env ;
  343: : <red  s" #ff0000" s" color" opt s" font" >env ;
  344: : red> -env ;
  345: : >> -env ;
  346: : *> -env ;
  347: : :: interpret ;
  348: : . end-sec on 0 indent ;
  349: : :code s" pre" >env
  350:     BEGIN  source >in @ /string type cr refill  WHILE
  351: 	source s" :endcode" compare 0= UNTIL  THEN
  352:   -env ;
  353: : \ postpone \ ;
  354: 
  355: definitions
  356:     
  357: \ Table
  358: 
  359: Variable table-format
  360: Variable table#
  361: 
  362: : |tag  table-format $@ table# @ /string drop c@ >align
  363:     >env  1 table# +! ;
  364: : |d  table# @ IF  -env  THEN  s" td" |tag ;
  365: : |h  table# @ IF  -env  THEN  s" th" |tag ;
  366: : |line  s" tr" >env  table# off ;
  367: : line|  -env -env cr ;
  368: 
  369: : next-char ( -- char )  source drop >in @ + c@ ;
  370: 
  371: longtags set-current
  372: 
  373: : <| s" table" >env bl sword table-format $! ;
  374: : |> -env ;
  375: : +| |line
  376:     BEGIN
  377: 	|h '| parse-to next-char '+ =  UNTIL line| ;
  378: : -| |line
  379:     BEGIN
  380: 	|d '| parse-to next-char '- =  UNTIL line| ;
  381: 
  382: definitions
  383: 
  384: \ parse a section
  385: 
  386: : section-line ( -- )  >in off
  387:     bl sword longtags search-wordlist
  388:     IF    execute
  389:     ELSE  source nip IF  >in off s" p" par  THEN  THEN ;
  390: : refill-loop ( -- )  end-sec off
  391:     BEGIN  refill  WHILE
  392: 	section-line end-sec @ UNTIL  THEN ;
  393: : parse-section ( -- )
  394:     refill-loop ;
  395: 
  396: \ HTML head
  397: 
  398: : .title ( addr u -- )
  399:     .' <!doctype html public "-//w3c//dtd html 4.0 transitional//en">' cr
  400:     s" html" >env s" head" >env
  401:     .'   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' cr
  402:     s" title" tagged cr
  403:     -env ;
  404: 
  405: \ HTML trailer
  406: 
  407: Variable mail
  408: Variable mail-name
  409: 
  410: : .trailer
  411:     s" address" >env s" center" >env
  412:     ." Last modified: " time&date rot 0 u.r swap 1-
  413:     s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
  414:     0 u.r ."  by "
  415:     s" Mail|icons/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
  416:     -envs ;
  417: 
  418: \ top word
  419: 
  420: : parse" ( -- addr u ) '" parse 2drop '" parse ;
  421: 
  422: : maintainer
  423:     bl sword mail $! parse" mail-name $! ;
  424: 
  425: Variable style$
  426: : style> style$ @ 0= IF  s" " style$ $!  THEN  style$ $@ tag-option $! ;
  427: : >style tag-option $@ style$ $! s" " tag-option $! ;
  428: 
  429: : style  style> opt >style ;
  430: : background ( -- )  parse" s" background" style ;
  431: : text ( -- )  parse" s" text" style ;
  432:     warnings @ warnings off
  433: : link ( -- )  parse" s" link" style ;
  434:     warnings !
  435: : vlink ( -- ) parse" s" vlink" style ;
  436: : marginheight ( -- ) parse" s" marginheight" style ;
  437: 
  438: : wf ( -- )
  439:     outfile-id >r
  440:     bl sword r/w create-file throw to outfile-id
  441:     parse" .title
  442:     +env style> s" body" env env?
  443:     ['] parse-section catch .trailer
  444:     outfile-id close-file throw
  445:     r> to outfile-id
  446:     dup 0< IF  throw  ELSE  drop  THEN ;
  447: 
  448: : eval-par ( addr u -- )
  449:   s" wf-temp.wf" r/w create-file throw >r
  450:   r@ write-file r> close-file throw
  451:   push-file s" wf-temp.wf" r/o open-file throw loadfile !
  452:   parse-par parse-section
  453:   loadfile @ close-file swap 2dup or
  454:   pop-file  drop throw throw
  455:   s" wf-temp.wf" delete-file throw ;
  456: 
  457: \ simple text data base
  458: 
  459: Variable last-entry
  460: Variable field#
  461: 
  462: : table: ( xt n -- )  Create , ,  1 field# !
  463:     DOES> 2@ >in @ >r longtags set-current
  464:     Create definitions swap , r> >in !
  465:     here last-entry !
  466:     dup 0 DO  0 ,  LOOP
  467:     1 DO  s" " last-entry @ I cells + $!  LOOP
  468:     last-entry @ get-rest
  469:     DOES> dup cell+ swap perform ;
  470: 
  471: : field:  Create field# @ , 1 field# +!
  472: DOES> @ cells last-entry @ + get-rest ;
  473: : par:  Create field# @ , 1 field# +!
  474: DOES> @ cells last-entry @ + get-par ;
  475: 
  476: : >field  ' >body @ cells postpone Literal postpone + ; immediate

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