File:  [gforth] / gforth / wf.fs
Revision 1.10: download - view: text, annotated - select for diffs
Sun Aug 5 22:23:45 2001 UTC (18 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added toc handling to wf
Wrote wrapper code for ncex

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

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