File:  [gforth] / gforth / wf.fs
Revision 1.12: download - view: text, annotated - select for diffs
Sat Aug 25 21:12:24 2001 UTC (17 years, 9 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed 386 assembler jump table
Some improvements to Wiki Forth

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

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