File:  [gforth] / gforth / wf.fs
Revision 1.18: download - view: text, annotated - select for diffs
Wed Jan 22 10:59:54 2003 UTC (21 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
factored out saving and restoring loadfilename (not relevant for new-input)

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

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