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 ." &" endof
221: '< of ." <" 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: : toc, ( n -- ) , 0 parse '| -$split 2swap here 0 , $! here 0 , $! ;
303: : up-toc align here toc-link >last , 0 toc, ;
304: : top-toc align here toc-link >last , 1 toc, ;
305: : this-toc align here toc-link >last , 2 toc, ;
306: : sub-toc align here toc-link >last , 3 toc, ;
307: : new-toc toc-link off ;
308:
309: Variable toc-name
310:
311: : .toc-entry ( toc flag -- )
312: swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href= s" a" tag
313: '# scan 1 /string toc-name $@ compare >r
314: $@ .img swap
315: IF
316: case
317: 2 of s" ^]|-icons/arrow_up.jpg" .img endof
318: 3 of
319: r@ 0= IF s" *]|-icons/circle.jpg"
320: ELSE s" v]|-icons/arrow_down.jpg" THEN .img endof
321: endcase
322: ELSE
323: case
324: 0 of s" ^]|-icons/arrow_up.jpg" .img endof
325: 1 of s" >]|-icons/arrow_right.jpg" .img endof
326: 2 of s" *]|-icons/circle.jpg" .img endof
327: 3 of s" v]|-icons/arrow_down.jpg" .img endof
328: endcase
329: THEN
330: s" a" /tag rdrop
331: ;
332: : print-toc ( -- ) cr 0 parse
333: dup 0= IF toc-name $! 0 ELSE
334: toc-name $! toc-name $@ name= s" " s" a" tagged 2
335: THEN >r
336: toc-link BEGIN @ dup WHILE
337: dup cell+ @ 3 = r@ 0= and IF rdrop 1 >r s" br" tag cr THEN
338: dup cell+ @ r@ >= IF dup r@ 2 = .toc-entry THEN
339: dup cell+ @ 2 = r@ 2 = and IF s" br" tag cr THEN
340: REPEAT drop rdrop cr ;
341:
342: \ handle global tags
343:
344: Variable indentlevel
345: : indent ( n -- ) indentlevel @ over indentlevel !
346: 2dup < IF swap DO -env -env LOOP EXIT THEN
347: 2dup > IF DO s" dl" >env s" dt" >env LOOP EXIT THEN
348: 2dup = IF drop IF -env s" dt" >env THEN THEN ;
349: : +indent ( -- ) indentlevel @ IF -env s" dd" >env THEN ;
350:
351: wordlist constant longtags
352:
353: longtags set-current
354:
355: : --- 0 indent cr s" hr" tag cr +indent ;
356: : * 1 indent s" h1" line +indent ;
357: : ** 1 indent s" h2" line +indent ;
358: : *** 2 indent s" h3" line +indent ;
359: : -- 0 indent cr print-toc ;
360: : && 0 parse name= s" " s" a" tagged ;
361: : - s" ul" env s" li" par ;
362: : + s" ol" env s" li" par ;
363: : << +env ;
364: : <* s" center" >env ;
365: : <red s" #ff0000" s" color" opt s" font" >env ;
366: : red> -env ;
367: : >> -env ;
368: : *> -env ;
369: : :: interpret ;
370: : . end-sec on 0 indent ;
371: : :code s" pre" >env
372: BEGIN source >in @ /string type cr refill WHILE
373: source s" :endcode" compare 0= UNTIL THEN
374: -env ;
375: : \ postpone \ ;
376:
377: definitions
378:
379: \ Table
380:
381: : |tag table-format $@ table# @ /string drop c@ >talign
382: >env 1 table# +! ;
383: : |d table# @ table-start @ > IF -env THEN s" td" |tag ;
384: : |h table# @ table-start @ > IF -env THEN s" th" |tag ;
385: : |line s" tr" >env table-start @ table# ! ;
386: : line| -env -env cr ;
387:
388: : next-char ( -- char ) source drop >in @ + c@ ;
389:
390: longtags set-current
391:
392: : <| bl sword table-format $! table-start off bl sword
393: dup IF s" border" opt ELSE 2drop THEN s" table" >env ;
394: : |> -env -env cr cr ;
395: : +| |line
396: BEGIN
397: |h '| parse-to next-char '+ = UNTIL line| ;
398: : -| |line
399: BEGIN
400: |d '| parse-to next-char '- = UNTIL line| ;
401:
402: definitions
403:
404: \ parse a section
405:
406: : section-line ( -- ) >in off
407: bl sword longtags search-wordlist
408: IF execute
409: ELSE source nip IF >in off s" p" par THEN THEN ;
410: : refill-loop ( -- ) end-sec off
411: BEGIN refill WHILE
412: section-line end-sec @ UNTIL THEN ;
413: : parse-section ( -- )
414: refill-loop ;
415:
416: \ HTML head
417:
418: : .title ( addr u -- )
419: .' <!doctype html public "-//w3c//dtd html 4.0 transitional//en">' cr
420: s" html" >env s" head" >env
421: .' <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' cr
422: s" title" tagged cr
423: -env ;
424:
425: \ HTML trailer
426:
427: Variable mail
428: Variable mail-name
429: Variable orig-date
430:
431: : .trailer
432: s" address" >env s" center" >env
433: orig-date @ IF ." Created " orig-date $@ type ." . " THEN
434: ." Last modified: " time&date rot 0 u.r swap 1-
435: s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
436: 0 u.r ." by "
437: s" Mail|icons/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
438: -envs ;
439:
440: \ top word
441:
442: : parse" ( -- addr u ) '" parse 2drop '" parse ;
443:
444: : maintainer ( -- )
445: bl sword mail $! parse" mail-name $! ;
446: : created ( -- )
447: bl sword orig-date $! ;
448:
449: Variable style$
450: : style> style$ @ 0= IF s" " style$ $! THEN style$ $@ tag-option $! ;
451: : >style tag-option $@ style$ $! s" " tag-option $! ;
452:
453: : style style> opt >style ;
454: : background ( -- ) parse" s" background" style ;
455: : text ( -- ) parse" s" text" style ;
456: warnings @ warnings off
457: : link ( -- ) parse" s" link" style ;
458: warnings !
459: : vlink ( -- ) parse" s" vlink" style ;
460: : marginheight ( -- ) parse" s" marginheight" style ;
461:
462: : wf ( -- )
463: outfile-id >r
464: bl sword r/w create-file throw to outfile-id
465: parse" .title
466: +env style> s" body" env env?
467: ['] parse-section catch .trailer
468: outfile-id close-file throw
469: r> to outfile-id
470: dup 0< IF throw ELSE drop THEN ;
471:
472: : eval-par ( addr u -- )
473: s" wf-temp.wf" r/w create-file throw >r
474: r@ write-file r> close-file throw
475: push-file s" wf-temp.wf" r/o open-file throw loadfile !
476: parse-par parse-section
477: loadfile @ close-file swap 2dup or
478: pop-file drop throw throw
479: s" wf-temp.wf" delete-file throw ;
480:
481: \ simple text data base
482:
483: Variable last-entry
484: Variable field#
485:
486: : table: ( xt n -- ) Create , , 1 field# !
487: DOES> 2@ >in @ >r longtags set-current
488: Create definitions swap , r> >in !
489: here last-entry !
490: dup 0 DO 0 , LOOP
491: 1 DO s" " last-entry @ I cells + $! LOOP
492: last-entry @ get-rest
493: DOES> dup cell+ swap perform ;
494:
495: : field: Create field# @ , 1 field# +!
496: DOES> @ cells last-entry @ + get-rest ;
497: : par: Create field# @ , 1 field# +!
498: DOES> @ cells last-entry @ + get-par ;
499:
500: : >field ' >body @ cells postpone Literal postpone + ; immediate
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>