File:  [gforth] / gforth / wf.fs
Revision 1.15: download - view: text, annotated - select for diffs
Fri Jul 26 08:35:15 2002 UTC (17 years ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Old version of parse-string for Gforth 0.5.0 added

    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 compare 0=
  103:     s" GIF87a" imgbuf over compare 0= 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 compare 0= ;
  110: : png-size ( -- w h )
  111:     imgbuf $14 + b@ imgbuf $10 + b@ ;
  112: 
  113: : jpg? ( -- flag )
  114:     jfif 10 imgbuf over compare 0= ;
  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" compare 0= [IF] 
  199: : parse-string ( c-addr u -- ) \ core,block
  200:     loadfilename# @ >r
  201:     1 loadfilename# ! \ "*evaluated string*"
  202:     push-file #tib ! >tib !
  203:     >in off blk off loadfile off -1 loadline !
  204:     ['] parse-line catch
  205:     pop-file r> loadfilename# ! throw ;
  206: [ELSE]
  207: : parse-string ( addr u -- )
  208:     evaluate-input cell new-tib #tib ! tib !
  209:     ['] parse-line catch pop-file throw ;
  210: [THEN] [THEN]
  211: 
  212: : .link ( addr u -- ) dup >r '| -$split  dup r> = IF  2swap  THEN 
  213:     link-options link $!
  214:     link $@len 0= IF  2dup link $! s" .html" link $+!  THEN
  215:     link $@ href= s" a" tag link-icon?
  216:     parse-string s" a" /tag link-size? link-sig? ;
  217: : >link ( -- )  '[ parse type '] parse .link ;
  218: 
  219: \ line handling
  220: 
  221: : char? ( -- c )  >in @ char swap >in ! ;
  222: : parse-tag ( addr u char -- )
  223:     >r r@ parse type
  224:     r> parse 2swap tagged ;
  225: 
  226: : .text ( -- ) 	>in @ >r char drop
  227:     source r@ /string >in @ r> - nip
  228:     bounds ?DO  I c@
  229: 	case
  230: 	    '& of  ." &amp;"  endof
  231: 	    '< of  ." &lt;"   endof
  232: 	    dup emit
  233: 	endcase
  234:     LOOP ;
  235: 
  236: Create do-words  $100 0 [DO] ' .text , [LOOP]
  237: 
  238: :noname '( emit 1 >in +! ; '( cells do-words + !
  239: 
  240: : bind-char ( xt -- )  char cells do-words + ! ;
  241: 
  242: : char>tag ( -- ) char >r
  243: :noname bl sword postpone SLiteral r@ postpone Literal
  244:     postpone parse-tag postpone ; r> cells do-words + ! ;
  245: 
  246: : >tag '\ parse type '\ parse tag ;
  247: 
  248: char>tag * b
  249: char>tag _ em
  250: char>tag # code
  251: 
  252: ' >link bind-char [
  253: ' >img  bind-char {
  254: ' >tag  bind-char \
  255: 
  256: : do-word ( char -- )  cells do-words + perform ;
  257: 
  258: : word? ( -- addr u )  >in @ >r bl sword r> >in ! ;
  259: 
  260: wordlist Constant autoreplacements
  261: 
  262: :noname ( -- )
  263:     BEGIN char? do-word source nip >in @ = UNTIL ; is parse-line
  264: 
  265: : parse-line+ ( -- )
  266:     BEGIN
  267: 	word? autoreplacements search-wordlist
  268: 	IF    execute  bl sword 2drop
  269: 	    source >in @ 1- /string drop c@ bl = >in +!
  270: 	ELSE  char? do-word  THEN
  271: 	source nip >in @ = UNTIL ;
  272: 
  273: : parse-to ( char -- ) >r
  274:     BEGIN  char? dup r@ <> WHILE
  275: 	do-word source nip >in @ = UNTIL  ELSE  drop  THEN
  276:     r> parse type ;
  277: 
  278: \ autoreplace
  279: 
  280: : autoreplace ( <[string|url]> -- )
  281:     get-current autoreplacements set-current
  282:     Create set-current
  283:     here 0 , '[ parse 2drop '] parse rot $!
  284:     DOES> $@ .link ;
  285:     
  286: \ paragraph handling
  287: 
  288: : parse-par ( -- )
  289:     BEGIN  parse-line+ cr refill  WHILE
  290: 	source nip 0= UNTIL  THEN ;
  291: 
  292: : par ( addr u -- ) env? 2dup tag parse-par /tag cr cr ;
  293: : line ( addr u -- ) env? 2dup tag parse-line+ /tag cr cr ;
  294: 
  295: \ scan strings
  296: 
  297: : get-rest ( addr -- ) 0 parse -trailing rot $! ;
  298: Create $lf 1 c, #lf c,
  299: : get-par ( addr -- )  >r  s" " r@ $+!
  300:     BEGIN  0 parse 2dup s" ." compare  WHILE
  301: 	r@ $@len IF  $lf count r@ $+!  THEN  r@ $+!
  302: 	refill 0= UNTIL  ELSE  2drop  THEN
  303:     rdrop ;
  304: 
  305: \ toc handling
  306: 
  307: Variable toc-link
  308: 
  309: : >last ( addr link -- link' )
  310:     BEGIN  dup @  WHILE  @  REPEAT  ! 0 ;
  311: 
  312: Variable create-navs
  313: Variable nav$
  314: Variable nav-name
  315: Variable nav-file
  316: Create nav-buf 0 c,
  317: : nav+ ( char -- )  nav-buf c! nav-buf 1 nav-file $+! ;
  318: 
  319: : >nav ( addr u -- addr' u' )
  320:     nav-name $!  create-navs @ 0=
  321:     IF  s" navigate/nav.scm" r/w create-file throw create-navs !  THEN
  322:     s' (script-fu-nav-file "' nav$ $! nav-name $@ nav$ $+!
  323:     s' " "./navigate/' nav$ $+!  s" " nav-file $!
  324:     nav-name $@ bounds ?DO
  325: 	I c@  dup 'A 'Z 1+ within IF  bl + nav+
  326: 	ELSE  dup 'a 'z 1+ within IF  nav+
  327: 	ELSE  dup '0 '9 1+ within IF  nav+
  328: 	ELSE  dup  bl = swap '- = or IF  '- nav+
  329: 	THEN  THEN  THEN  THEN
  330: 	LOOP
  331:     nav-file $@ nav$ $+! s' .jpg")' nav$ $+!
  332:     nav$ $@ create-navs @ write-line throw
  333:     s" [" nav$ $! nav-name $@ nav$ $+!
  334:     s" |-navigate/" nav$ $+! nav-file $@ nav$ $+! s" .jpg" nav$ $+!
  335:     nav$ $@ ;
  336: 
  337: : toc, ( n -- ) , '| parse >nav here 0 , $! 0 parse here 0 , $! ;
  338: : up-toc   align here toc-link >last , 0 toc, ;
  339: : top-toc  align here toc-link >last , 1 toc, ;
  340: : this-toc align here toc-link >last , 2 toc, ;
  341: : sub-toc  align here toc-link >last , 3 toc, ;
  342: : new-toc  toc-link off ;
  343: 
  344: Variable toc-name
  345: 
  346: : .toc-entry ( toc flag -- )
  347:     swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href= s" a" tag
  348:     '# scan 1 /string toc-name $@ compare >r
  349:     $@ .img swap
  350:     IF
  351: 	case
  352: 	    2 of  s" ^]|-icons/arrow_up.jpg" .img  endof
  353: 	    3 of
  354: 		r@ 0= IF s" *]|-icons/circle.jpg"
  355: 		    ELSE s" v]|-icons/arrow_down.jpg"  THEN  .img  endof
  356: 	endcase
  357:     ELSE
  358: 	case
  359: 	    0 of  s" ^]|-icons/arrow_up.jpg" .img  endof
  360: 	    1 of  s" >]|-icons/arrow_right.jpg" .img  endof
  361: 	    2 of  s" *]|-icons/circle.jpg" .img  endof
  362: 	    3 of  s" v]|-icons/arrow_down.jpg" .img  endof
  363: 	endcase
  364:     THEN
  365:     s" a" /tag rdrop
  366:     ;
  367: : print-toc ( -- ) cr 0 parse
  368:     dup 0= IF  toc-name $! 0  ELSE
  369: 	toc-name $! toc-name $@ name= s" " s" a" tagged  2
  370:     THEN  >r
  371:     toc-link  BEGIN  @ dup  WHILE
  372: 	dup cell+ @ 3 = r@ 0= and IF  rdrop 1 >r s" br" tag cr  THEN
  373: 	dup cell+ @ r@ >= IF  dup r@ 2 = .toc-entry  THEN
  374: 	dup cell+ @ 2 = r@ 2 = and IF  s" br" tag cr  THEN
  375:     REPEAT  drop rdrop  cr ;
  376: 
  377: \ handle global tags
  378: 
  379: Variable indentlevel
  380: : indent ( n -- )  indentlevel @ over indentlevel !
  381:     2dup < IF swap DO  -env -env  LOOP  EXIT THEN
  382:     2dup > IF      DO  s" dl" >env s" dt" >env  LOOP EXIT THEN
  383:     2dup = IF drop IF  -env  s" dt" >env  THEN THEN ;
  384: : +indent ( -- )  indentlevel @ IF  -env s" dd" >env  THEN ;
  385: 
  386: wordlist constant longtags
  387: 
  388: longtags set-current
  389: 
  390: : --- 0 indent cr s" hr" tag cr +indent ;
  391: : *   1 indent s" h1" line +indent ;
  392: : **  1 indent s" h2" line +indent ;
  393: : *** 2 indent s" h3" line +indent ;
  394: : -- 0 indent cr print-toc ;
  395: : && 0 parse name= s" " s" a" tagged ;
  396: : - s" ul" env s" li" par ;
  397: : + s" ol" env s" li" par ;
  398: : << +env ;
  399: : <* s" center" >env ;
  400: : <red  s" #ff0000" s" color" opt s" font" >env ;
  401: : red> -env ;
  402: : >> -env ;
  403: : *> -env ;
  404: : :: interpret ;
  405: : . end-sec on 0 indent ;
  406: : :code s" pre" >env
  407:     BEGIN  source >in @ /string type cr refill  WHILE
  408: 	source s" :endcode" compare 0= UNTIL  THEN
  409:   -env ;
  410: : \ postpone \ ;
  411: 
  412: definitions
  413:     
  414: \ Table
  415: 
  416: : |tag  table-format $@ table# @ /string drop c@ >talign
  417:     >env  1 table# +! ;
  418: : |d  table# @ table-start @ > IF  -env  THEN  s" td" |tag ;
  419: : |h  table# @ table-start @ > IF  -env  THEN  s" th" |tag ;
  420: : |line  s" tr" >env  table-start @ table# ! ;
  421: : line|  -env -env cr ;
  422: 
  423: : next-char ( -- char )  source drop >in @ + c@ ;
  424: 
  425: longtags set-current
  426: 
  427: : <| bl sword table-format $! table-start off bl sword
  428:     dup IF  s" border" opt  ELSE  2drop  THEN s" table" >env ;
  429: : |> -env -env cr cr ;
  430: : +| |line
  431:     BEGIN
  432: 	|h '| parse-to next-char '+ =  UNTIL line| ;
  433: : -| |line
  434:     BEGIN
  435: 	|d '| parse-to next-char '- =  UNTIL line| ;
  436: 
  437: definitions
  438: 
  439: \ parse a section
  440: 
  441: : section-line ( -- )  >in off
  442:     bl sword longtags search-wordlist
  443:     IF    execute
  444:     ELSE  source nip IF  >in off s" p" par  THEN  THEN ;
  445: : refill-loop ( -- )  end-sec off
  446:     BEGIN  refill  WHILE
  447: 	section-line end-sec @ UNTIL  THEN ;
  448: : parse-section ( -- )
  449:     refill-loop ;
  450: 
  451: \ HTML head
  452: 
  453: : .title ( addr u -- )
  454:     .' <!doctype html public "-//w3c//dtd html 4.0 transitional//en">' cr
  455:     s" html" >env s" head" >env
  456:     .'   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' cr
  457:     s" title" tagged cr
  458:     -env ;
  459: 
  460: \ HTML trailer
  461: 
  462: Variable mail
  463: Variable mail-name
  464: Variable orig-date
  465: 
  466: : .trailer
  467:     s" address" >env s" center" >env
  468:     orig-date @ IF  ." Created " orig-date $@ type ." . "  THEN
  469:     ." Last modified: " time&date rot 0 u.r swap 1-
  470:     s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
  471:     0 u.r ."  by "
  472:     s" Mail|icons/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
  473:     -envs ;
  474: 
  475: \ top word
  476: 
  477: : parse" ( -- addr u ) '" parse 2drop '" parse ;
  478: 
  479: : maintainer ( -- )
  480:     bl sword mail $! parse" mail-name $! ;
  481: : created ( -- )
  482:     bl sword orig-date $! ;
  483: 
  484: Variable style$
  485: : style> style$ @ 0= IF  s" " style$ $!  THEN  style$ $@ tag-option $! ;
  486: : >style tag-option $@ style$ $! s" " tag-option $! ;
  487: 
  488: : style  style> opt >style ;
  489: : background ( -- )  parse" s" background" style ;
  490: : text ( -- )  parse" s" text" style ;
  491:     warnings @ warnings off
  492: : link ( -- )  parse" s" link" style ;
  493:     warnings !
  494: : vlink ( -- ) parse" s" vlink" style ;
  495: : marginheight ( -- ) parse" s" marginheight" style ;
  496: 
  497: : wf ( -- )
  498:     outfile-id >r
  499:     bl sword r/w create-file throw to outfile-id
  500:     parse" .title
  501:     +env style> s" body" env env?
  502:     ['] parse-section catch .trailer
  503:     outfile-id close-file throw
  504:     r> to outfile-id
  505:     dup 0< IF  throw  ELSE  drop  THEN ;
  506: 
  507: : eval-par ( addr u -- )
  508:   s" wf-temp.wf" r/w create-file throw >r
  509:   r@ write-file r> close-file throw
  510:   push-file s" wf-temp.wf" r/o open-file throw loadfile !
  511:   parse-par parse-section
  512:   loadfile @ close-file swap 2dup or
  513:   pop-file  drop throw throw
  514:   s" wf-temp.wf" delete-file throw ;
  515: 
  516: \ simple text data base
  517: 
  518: Variable last-entry
  519: Variable field#
  520: 
  521: : table: ( xt n -- )  Create , ,  1 field# !
  522:     DOES> 2@ >in @ >r longtags set-current
  523:     Create definitions swap , r> >in !
  524:     here last-entry !
  525:     dup 0 DO  0 ,  LOOP
  526:     1 DO  s" " last-entry @ I cells + $!  LOOP
  527:     last-entry @ get-rest
  528:     DOES> dup cell+ swap perform ;
  529: 
  530: : field:  Create field# @ , 1 field# +!
  531: DOES> @ cells last-entry @ + get-rest ;
  532: : par:  Create field# @ , 1 field# +!
  533: DOES> @ cells last-entry @ + get-par ;
  534: 
  535: : >field  ' >body @ cells postpone Literal postpone + ; immediate

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