File:  [gforth] / gforth / wf.fs
Revision 1.14: download - view: text, annotated - select for diffs
Sun Nov 11 22:33:31 2001 UTC (22 years, 4 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added tags generation for vi
small changes on httpd for EuroForth paper
Added navigation button generation via Gimp to wf
Made nccomp from Lars Krueger work at least somehow

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

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