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