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