1: \ wiki forth
2:
3: \ Copyright (C) 2003 Free Software Foundation, Inc.
4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20:
21: require string.fs
22:
23: : -scan ( addr u char -- addr' u' )
24: >r BEGIN dup WHILE 1- 2dup + c@ r@ = UNTIL THEN
25: rdrop ;
26: : -$split ( addr u char -- addr1 u1 addr2 u2 )
27: >r 2dup r@ -scan 2dup + c@ r> = negate over + >r
28: 2swap r> /string ;
29: : parse" ( -- addr u ) '" parse 2drop '" parse ;
30:
31: \ tag handling
32:
33: : .' '' parse postpone SLiteral postpone type ; immediate
34: : s' '' parse postpone SLiteral ; immediate
35:
36: Variable indentlevel
37: Variable tag-option
38: s" " tag-option $!
39:
40: : .type ( addr u -- )
41: bounds ?DO I c@
42: case
43: '& of ." &" endof
44: '< of ." <" endof
45: dup emit
46: endcase
47: LOOP ;
48:
49: : tag ( addr u -- ) '< emit type tag-option $@ type '> emit
50: s" " tag-option $! ;
51: : tag/ ( addr u -- ) s" /" tag-option $+! tag ;
52: : /tag ( addr u -- ) '< emit '/ emit type '> emit ;
53: : tagged ( addr1 u1 addr2 u2 -- ) 2dup 2>r tag .type 2r> /tag ;
54:
55: : opt ( addr u opt u -- ) s" " tag-option $+!
56: tag-option $+! s' ="' tag-option $+! tag-option $+!
57: s' "' tag-option $+! ;
58: : href= ( addr u -- ) s" href" opt ;
59: : id= ( addr u -- ) s" id" opt ;
60: : src= ( addr u -- ) s" src" opt ;
61: : alt= ( addr u -- ) s" alt" opt ;
62: : width= ( addr u -- ) s" width" opt ;
63: : height= ( addr u -- ) s" height" opt ;
64: : align= ( addr u -- ) s" align" opt ;
65: : class= ( addr u -- ) s" class" opt ;
66: : indent= ( -- )
67: indentlevel @ 0 <# #S 'p hold #> class= ;
68: : mailto: ( addr u -- ) s' href="mailto:' tag-option $+!
69: tag-option $+! s' "' tag-option $+! ;
70:
71: \ environment handling
72:
73: Variable end-sec
74: Variable oldenv
75: Variable envs 30 0 [DO] 0 , [LOOP]
76:
77: : env$ ( -- addr ) envs dup @ 1+ cells + ;
78: : env ( addr u -- ) env$ $! ;
79: : env? ( -- ) envs @ oldenv @
80: 2dup > IF env$ $@ tag THEN
81: 2dup < IF env$ cell+ $@ /tag env$ cell+ $off THEN
82: drop oldenv ! ;
83: : +env 1 envs +! ;
84: : -env end-sec @ envs @ 2 > or IF -1 envs +! env? THEN ;
85: : -envs envs @ 0 ?DO -env cr LOOP ;
86: : >env ( addr u -- ) +env env env? ;
87:
88: \ alignment
89:
90: Variable table-format
91: Variable table#
92: Variable table-start
93:
94: : >align ( c -- )
95: CASE
96: 'l OF s" left" class= ENDOF
97: 'r OF s" right" class= ENDOF
98: 'c OF s" center" align= ENDOF
99: '< OF s" left" class= ENDOF
100: '> OF s" right" class= ENDOF
101: '= OF s" center" align= ENDOF
102: '~ OF s" absmiddle" align= ENDOF
103: ENDCASE ;
104:
105: : >talign ( c -- )
106: CASE
107: 'l OF s" left" align= ENDOF
108: 'r OF s" right" align= ENDOF
109: 'c OF s" center" align= ENDOF
110: '< OF s" left" align= ENDOF
111: '> OF s" right" align= ENDOF
112: '= OF s" center" align= ENDOF
113: digit? IF 0 <# #S #> s" rowspan" opt
114: table# @ 1+ table-start ! THEN 0
115: ENDCASE ;
116:
117: : >border ( c -- )
118: case
119: '- of s" border0" class= endof
120: '+ of s" border1" class= endof
121: endcase ;
122:
123: \ image handling
124:
125: Create imgbuf $20 allot
126:
127: Create pngsig $89 c, $50 c, $4E c, $47 c, $0D c, $0A c, $1A c, $0A c,
128: Create jfif $FF c, $D8 c, $FF c, $E0 c, $00 c, $10 c, $4A c, $46 c,
129: $49 c, $46 c,
130:
131: : b@ ( addr -- x ) 0 swap 4 bounds ?DO 8 lshift I c@ + LOOP ;
132: : bw@ ( addr -- x ) 0 swap 2 bounds ?DO 8 lshift I c@ + LOOP ;
133:
134: : gif? ( -- flag )
135: s" GIF89a" imgbuf over str=
136: s" GIF87a" imgbuf over str= or ;
137: : gif-size ( -- w h )
138: imgbuf 8 + c@ imgbuf 9 + c@ 8 lshift +
139: imgbuf 6 + c@ imgbuf 7 + c@ 8 lshift + ;
140:
141: : png? ( -- flag )
142: pngsig 8 imgbuf over str= ;
143: : png-size ( -- w h )
144: imgbuf $14 + b@ imgbuf $10 + b@ ;
145:
146: : jpg? ( -- flag )
147: jfif 10 imgbuf over str= ;
148: : jpg-size ( fd -- w h ) >r
149: 2. BEGIN
150: 2dup r@ reposition-file throw
151: imgbuf $10 r@ read-file throw 0<>
152: imgbuf bw@ $FFC0 $FFD0 within 0= and WHILE
153: imgbuf 2 + bw@ 2 + 0 d+ REPEAT
154: 2drop imgbuf 5 + bw@ imgbuf 7 + bw@ rdrop ;
155:
156: : img-size ( fd -- w h ) >r
157: gif? IF gif-size rdrop EXIT THEN
158: jpg? IF r> jpg-size EXIT THEN
159: png? IF png-size rdrop EXIT THEN
160: 0 0 ;
161:
162: : .img-size ( addr u -- )
163: r/o open-file IF drop EXIT THEN >r
164: imgbuf $20 r@ read-file throw drop
165: r@ img-size
166: r> close-file throw
167: ?dup IF 0 <# #S #> width= THEN
168: ?dup IF 0 <# #S #> height= THEN ;
169:
170: \ link creation
171:
172: Variable link
173: Variable link-sig
174: Variable link-suffix
175: Variable iconpath
176:
177: Variable do-size
178: Variable do-icon
179:
180: Defer parse-line
181:
182: : .img ( addr u -- ) dup >r '| -$split dup r> = IF 2swap THEN
183: dup IF 2swap alt= ELSE 2drop THEN
184: tag-option $@len >r over c@ >align tag-option $@len r> = 1+ /string
185: tag-option $@len >r over c@ >border tag-option $@len r> = 1+ /string
186: 2dup .img-size src= s" img" tag/ ;
187: : >img ( -- ) '{ parse type '} parse .img ;
188:
189: : alt-suffix ( -- )
190: link-suffix $@len 2 - link-suffix $!len
191: s" [" link-suffix 0 $ins
192: s" ]" link-suffix $+!
193: link-suffix $@ alt= ;
194:
195: : get-icon ( addr u -- ) iconpath @ IF 2drop EXIT THEN
196: link-suffix $! s" .*" link-suffix $+!
197: s" icons" open-dir throw >r
198: BEGIN
199: pad $100 r@ read-dir throw WHILE
200: pad swap 2dup link-suffix $@ filename-match
201: IF s" icons/" iconpath $! iconpath $+!
202: iconpath $@ 2dup .img-size src= '- >border
203: alt-suffix s" img" tag/ true
204: ELSE 2drop false THEN
205: UNTIL ELSE drop THEN
206: r> close-dir throw ;
207:
208: : link-icon? ( -- ) do-icon @ 0= ?EXIT
209: iconpath @ IF iconpath $off THEN
210: link $@ + 1- c@ '/ = IF s" index.html" ELSE link $@ THEN
211: '# $split 2drop
212: BEGIN '. $split 2swap 2drop dup WHILE
213: 2dup get-icon REPEAT 2drop ;
214:
215: : link-size? ( -- ) do-size @ 0= ?EXIT
216: link $@ r/o open-file IF drop EXIT THEN >r
217: r@ file-size throw $400 um/mod nip ." (" 0 u.r ." k)"
218: r> close-file throw ;
219:
220: : link-sig? ( -- )
221: link $@ link-sig $! s" .sig" link-sig $+!
222: link-sig $@ r/o open-file IF drop EXIT THEN
223: close-file throw
224: ." (" link-sig $@ href= s" a" tag
225: s" |-icons/sig.gif" .img ." sig" s" /a" tag ." )" ;
226:
227: : link-options ( addr u -- addr' u' )
228: do-size off do-icon on
229: over c@ '% = over 0> and IF do-size on 1 /string THEN
230: over c@ '\ = over 0> and IF do-icon off 1 /string THEN ;
231:
232: s" Gforth" environment? [IF] s" 0.5.0" str= [IF]
233: : parse-string ( c-addr u -- ) \ core,block
234: s" *evaluated string*" loadfilename>r
235: push-file #tib ! >tib !
236: >in off blk off loadfile off -1 loadline !
237: ['] parse-line catch
238: pop-file r>loadfilename throw ;
239: [ELSE]
240: : parse-string ( addr u -- )
241: evaluate-input cell new-tib #tib ! tib !
242: ['] parse-line catch pop-file throw ;
243: [THEN] [THEN]
244:
245: : .link ( addr u -- ) dup >r '| -$split dup r> = IF 2swap THEN
246: link-options link $!
247: link $@len 0= IF 2dup link $! ( s" .html" link $+! ) THEN
248: link $@ href= s" a" tag link-icon?
249: parse-string s" a" /tag link-size? link-sig? ;
250: : >link ( -- ) '[ parse type '] parse .link ;
251:
252: \ line handling
253:
254: : char? ( -- c ) >in @ char swap >in ! ;
255:
256: : parse-tag ( addr u char -- )
257: >r r@ parse .type
258: r> parse 2swap tagged ;
259:
260: : .text ( -- ) >in @ >r char drop
261: source r@ /string >in @ r> - nip .type ;
262:
263: Create do-words $100 0 [DO] ' .text , [LOOP]
264:
265: :noname '( emit 1 >in +! ; '( cells do-words + !
266:
267: : bind-char ( xt -- ) char cells do-words + ! ;
268:
269: : char>tag ( -- ) char >r
270: :noname bl sword postpone SLiteral r@ postpone Literal
271: postpone parse-tag postpone ; r> cells do-words + ! ;
272:
273: : >tag '\ parse type '\ parse tag ;
274:
275: char>tag * b
276: char>tag _ em
277: char>tag # code
278:
279: ' >link bind-char [
280: ' >img bind-char {
281: ' >tag bind-char \
282:
283: : do-word ( char -- ) cells do-words + perform ;
284:
285: : word? ( -- addr u ) >in @ >r bl sword r> >in ! ;
286:
287: wordlist Constant autoreplacements
288:
289: :noname ( -- )
290: BEGIN char? do-word source nip >in @ = UNTIL ; is parse-line
291:
292: : parse-line+ ( -- )
293: BEGIN
294: word? autoreplacements search-wordlist
295: IF execute bl sword 2drop
296: source >in @ 1- /string drop c@ bl = >in +!
297: ELSE char? do-word THEN
298: source nip >in @ = UNTIL ;
299:
300: : parse-to ( char -- ) >r
301: BEGIN char? dup r@ <> WHILE
302: do-word source nip >in @ = UNTIL ELSE drop THEN
303: r> parse type ;
304:
305: \ autoreplace
306:
307: : autoreplace ( <[string|url]> -- )
308: get-current autoreplacements set-current
309: Create set-current
310: here 0 , '[ parse 2drop '] parse rot $!
311: DOES> $@ .link ;
312:
313: \ paragraph handling
314:
315: : parse-par ( -- )
316: BEGIN parse-line+ cr refill WHILE
317: source nip 0= UNTIL THEN ;
318:
319: : par ( addr u -- ) env? indent=
320: 2dup tag parse-par /tag cr cr ;
321: : line ( addr u -- ) env? 2dup tag parse-line+ /tag cr cr ;
322:
323: \ scan strings
324:
325: : get-rest ( addr -- ) 0 parse -trailing rot $! ;
326: Create $lf 1 c, #lf c,
327: : get-par ( addr -- ) >r s" " r@ $+!
328: BEGIN 0 parse 2dup s" ." str= 0= WHILE
329: r@ $@len IF $lf count r@ $+! THEN r@ $+!
330: refill 0= UNTIL ELSE 2drop THEN
331: rdrop ;
332:
333: \ toc handling
334:
335: Variable toc-link
336:
337: : >last ( addr link -- link' )
338: BEGIN dup @ WHILE @ REPEAT ! 0 ;
339:
340: Variable create-navs
341: Variable nav$
342: Variable nav-name
343: Variable nav-file
344: Create nav-buf 0 c,
345: : nav+ ( char -- ) nav-buf c! nav-buf 1 nav-file $+! ;
346:
347: : >nav ( addr u -- addr' u' )
348: nav-name $! create-navs @ 0=
349: IF s" navigate/nav.scm" r/w create-file throw create-navs ! THEN
350: s' (script-fu-nav-file "' nav$ $! nav-name $@ nav$ $+!
351: s' " "./navigate/' nav$ $+! s" " nav-file $!
352: nav-name $@ bounds ?DO
353: I c@ dup 'A 'Z 1+ within IF bl + nav+
354: ELSE dup 'a 'z 1+ within IF nav+
355: ELSE dup '0 '9 1+ within IF nav+
356: ELSE dup bl = swap '- = or IF '- nav+
357: THEN THEN THEN THEN
358: LOOP
359: nav-file $@ nav$ $+! s' .jpg")' nav$ $+!
360: nav$ $@ create-navs @ write-line throw
361: s" [" nav$ $! nav-name $@ nav$ $+!
362: s" |-navigate/" nav$ $+! nav-file $@ nav$ $+! s" .jpg" nav$ $+!
363: nav$ $@ ;
364:
365: : toc, ( n -- ) , '| parse >nav here 0 , $! 0 parse here 0 , $! ;
366: : up-toc align here toc-link >last , 0 toc, ;
367: : top-toc align here toc-link >last , 1 toc, ;
368: : this-toc align here toc-link >last , 2 toc, ;
369: : sub-toc align here toc-link >last , 3 toc, ;
370: : new-toc toc-link off ;
371:
372: Variable toc-name
373: Variable toc-index
374: 6 Value /toc-line
375:
376: : .toc-entry ( toc flag -- )
377: swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href= s" a" tag
378: '# scan 1 /string toc-name $@ compare >r
379: $@ .img swap
380: IF
381: case
382: 2 of s" ^]|-icons/arrow_up.jpg" .img endof
383: 3 of
384: r@ 0= IF s" *]|-icons/circle.jpg"
385: ELSE s" v]|-icons/arrow_down.jpg" THEN .img endof
386: endcase
387: ELSE
388: case
389: 0 of s" ^]|-icons/arrow_up.jpg" .img endof
390: 1 of s" >]|-icons/arrow_right.jpg" .img endof
391: 2 of s" *]|-icons/circle.jpg" .img endof
392: 3 of s" v]|-icons/arrow_down.jpg" .img endof
393: endcase
394: THEN
395: s" a" /tag rdrop ." <!--" cr ." -->"
396: 1 toc-index +! toc-index @ /toc-line mod 0=
397: IF s" br" tag/ THEN ;
398:
399: : print-toc ( -- ) toc-index off cr s" menu" id= s" div" >env cr
400: 0 parse
401: dup 0= IF toc-name $! 0 ELSE
402: toc-name $! toc-name $@ id= s" " s" a" tagged 2
403: THEN >r
404: toc-link BEGIN @ dup WHILE
405: dup cell+ @ 3 = r@ 0= and IF rdrop 1 >r ( s" br" tag/ cr ) THEN
406: dup cell+ @ r@ >= IF dup r@ 2 = .toc-entry THEN
407: dup cell+ @ 2 = r@ 2 = and IF s" br" tag/ toc-index off THEN
408: REPEAT drop rdrop -env cr ;
409:
410: \ handle global tags
411:
412: : indent ( n -- )
413: indentlevel @ over
414: indentlevel !
415: 2dup < IF swap DO -env -env LOOP EXIT THEN
416: over 1 = IF = IF -env -env THEN EXIT THEN
417: 2dup > IF DO s" dl" >env s" dt" >env LOOP EXIT THEN
418: 2dup = IF drop IF -env s" dt" >env THEN THEN
419: ;
420: : +indent ( -- )
421: indentlevel @ IF -env -env s" dl" >env s" dd" >env THEN
422: ;
423:
424: wordlist constant longtags
425:
426: Variable divs
427:
428: longtags set-current
429:
430: : --- 0 indent cr s" hr" tag/ cr +indent ;
431: : * 1 indent s" h1" line +indent ;
432: : ** 1 indent s" h2" line +indent ;
433: : *** 2 indent s" h3" line +indent ;
434: : -- 0 indent cr print-toc ;
435: : && ( -- ) divs @ IF -env THEN +env
436: 0 parse id= s" div" env env? divs on ;
437: : - s" ul" env s" li" par ;
438: : + s" ol" env s" li" par ;
439: : << +env ;
440: : <* s" center" class= ;
441: : <red s" #ff0000" s" color" opt s" font" >env ;
442: : red> -env ;
443: : >> -env ;
444: : *> ;
445: : :: interpret ;
446: : . end-sec on 0 indent ;
447: : :code indent= s" pre" >env
448: BEGIN source >in @ /string type cr refill WHILE
449: source s" :endcode" str= UNTIL THEN
450: -env ;
451: : :code-file indent= s" pre" >env
452: parse" r/o open-file throw >r
453: r@ file-size throw drop dup allocate throw
454: 2dup swap r@ read-file throw 2dup type drop
455: -env free throw drop
456: r> close-file throw ;
457: : \ postpone \ ;
458:
459: definitions
460:
461: \ Table
462:
463: : |tag table-format $@ table# @ /string drop c@ >talign
464: >env 1 table# +! ;
465: : |d table# @ table-start @ > IF -env THEN s" td" |tag ;
466: : |h table# @ table-start @ > IF -env THEN s" th" |tag ;
467: : |line s" tr" >env table-start @ table# ! ;
468: : line| -env -env cr ;
469:
470: : next-char ( -- char ) source drop >in @ + c@ ;
471:
472: longtags set-current
473:
474: : <| bl sword table-format $! table-start off bl sword
475: dup IF s" border" opt ELSE 2drop THEN s" table" >env ;
476: : |> -env -env cr cr ;
477: : +| |line
478: BEGIN
479: |h '| parse-to next-char '+ = UNTIL line| ;
480: : -| |line
481: BEGIN
482: |d '| parse-to next-char '- = UNTIL line| ;
483:
484: definitions
485:
486: \ parse a section
487:
488: : section-line ( -- ) >in off
489: bl sword longtags search-wordlist
490: IF execute
491: ELSE source nip IF >in off s" p" par THEN THEN ;
492: : refill-loop ( -- ) end-sec off
493: BEGIN refill WHILE
494: section-line end-sec @ UNTIL THEN ;
495: : parse-section ( -- )
496: refill-loop ;
497:
498: \ HTML head
499:
500: Variable css-file
501:
502: : .title ( addr u -- )
503: .' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' cr
504: s" html" >env s" head" >env cr
505: s" Content-Type" s" http-equiv" opt
506: s" text/xhtml; charset=iso-8859-1" s" content" opt
507: s" meta" tag/
508: css-file @ IF css-file $@len IF
509: s" StyleSheet" s" rel" opt
510: css-file $@ href=
511: s" text/css" s" type" opt s" link" tag/
512: THEN THEN
513: s" title" tagged cr
514: -env ;
515:
516: \ HTML trailer
517:
518: Variable mail
519: Variable mail-name
520: Variable orig-date
521:
522: : .lastmod
523: ." Last modified: " time&date rot 0 u.r swap 1-
524: s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
525: 0 u.r ;
526:
527: : .trailer
528: s" center" class= s" address" >env
529: orig-date @ IF ." Created " orig-date $@ type ." . " THEN
530: .lastmod
531: ." by "
532: s" Mail|icons/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
533: -envs ;
534:
535: \ top word
536:
537: : maintainer ( -- )
538: bl sword mail $! parse" mail-name $! ;
539: : created ( -- )
540: bl sword orig-date $! ;
541:
542: Variable style$
543: : style> style$ @ 0= IF s" " style$ $! THEN style$ $@ tag-option $! ;
544: : >style tag-option $@ style$ $! s" " tag-option $! ;
545:
546: : style style> opt >style ;
547: : background ( -- ) parse" s" background" style ;
548: : text ( -- ) parse" s" text" style ;
549: warnings @ warnings off
550: : link ( -- ) parse" s" link" style ;
551: warnings !
552: : vlink ( -- ) parse" s" vlink" style ;
553: : marginheight ( -- ) parse" s" marginheight" style ;
554: : css ( -- ) parse" css-file $! ;
555:
556: : wf ( -- )
557: outfile-id >r
558: bl sword r/w create-file throw to outfile-id
559: parse" .title
560: +env style> s" body" env env?
561: ['] parse-section catch .trailer
562: outfile-id close-file throw
563: r> to outfile-id
564: dup 0< IF throw ELSE drop THEN ;
565:
566: : eval-par ( addr u -- )
567: s" wf-temp.wf" r/w create-file throw >r
568: r@ write-file r> close-file throw
569: push-file s" wf-temp.wf" r/o open-file throw loadfile !
570: parse-par parse-section
571: loadfile @ close-file swap 2dup or
572: pop-file drop throw throw
573: s" wf-temp.wf" delete-file throw ;
574:
575: \ simple text data base
576:
577: Variable last-entry
578: Variable field#
579:
580: : table: ( xt n -- ) Create , , 1 field# !
581: DOES> 2@ >in @ >r longtags set-current
582: Create definitions swap , r> >in !
583: here last-entry !
584: dup 0 DO 0 , LOOP
585: 1 DO s" " last-entry @ I cells + $! LOOP
586: last-entry @ get-rest
587: DOES> dup cell+ swap perform ;
588:
589: : field: Create field# @ , 1 field# +!
590: DOES> @ cells last-entry @ + get-rest ;
591: : par: Create field# @ , 1 field# +!
592: DOES> @ cells last-entry @ + get-par ;
593:
594: : >field ' >body @ cells postpone Literal postpone + ; immediate
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>