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