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