1: \ wiki forth
2:
3: \ Copyright (C) 2003,2004 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: \ basic stuff
24:
25: : -scan ( addr u char -- addr' u' )
26: >r BEGIN dup WHILE 1- 2dup + c@ r@ = UNTIL THEN
27: rdrop ;
28: : -$split ( addr u char -- addr1 u1 addr2 u2 )
29: >r 2dup r@ -scan 2dup + c@ r> = negate over + >r
30: 2swap r> /string ;
31: : parse" ( -- addr u ) '" parse 2drop '" parse ;
32: : .' '' parse postpone SLiteral postpone type ; immediate
33: : s' '' parse postpone SLiteral ; immediate
34:
35: \ character recoding
36:
37: [IFDEF] maxascii $100 to maxascii 8-bit-io [THEN]
38: \ UTF-8 IO fails with .type:
39:
40: : .type ( addr u -- )
41: bounds ?DO I c@
42: case
43: '& of ." &" endof
44: '< of ." <" endof
45: '¤ of ." €" endof
46: dup emit
47: endcase
48: LOOP ;
49:
50: \ tag handling
51:
52: Variable indentlevel
53: Variable tag-option
54: Variable tag-class
55: s" " tag-option $!
56: s" " tag-class $!
57:
58: : tag ( addr u -- ) '< emit type
59: tag-class $@len IF .\" class=\"" tag-class $@ type '" emit THEN
60: tag-option $@ type
61: '> emit
62: s" " tag-option $! s" " tag-class $! ;
63: : tag/ ( addr u -- ) s" /" tag-option $+! tag ;
64: : /tag ( addr u -- ) '< emit '/ emit type '> emit ;
65: : tagged ( addr1 u1 addr2 u2 -- ) 2dup 2>r tag .type 2r> /tag ;
66:
67: : opt ( addr u opt u -- ) s" " tag-option $+!
68: tag-option $+! s' ="' tag-option $+! tag-option $+!
69: s' "' tag-option $+! ;
70: : n>string ( n -- addr u ) 0 <# #S #> ;
71: : xy>string ( x y -- ) swap 0 <# #S 'x hold 2drop 0 #S 's hold #> ;
72: : opt# ( n opt u -- ) rot n>string 2swap opt ;
73: : href= ( addr u -- ) s" href" opt ;
74: : id= ( addr u -- ) s" id" opt ;
75: : src= ( addr u -- ) s" src" opt ;
76: : alt= ( addr u -- ) s" alt" opt ;
77: : width= ( n -- ) s" width" opt# ;
78: : height= ( n -- ) s" height" opt# ;
79: : align= ( addr u -- ) s" align" opt ;
80: : class= ( addr u -- )
81: tag-class $@len IF s" " tag-class $+! THEN
82: tag-class $+! ;
83: : indent= ( -- )
84: indentlevel @ 0 <# #S 'p hold #> class= ;
85: : mailto: ( addr u -- ) s' href="mailto:' tag-option $+!
86: tag-option $+! s' "' tag-option $+! ;
87:
88: \ environment handling
89:
90: Variable end-sec
91: Variable oldenv
92: Variable envs 30 0 [DO] 0 , [LOOP]
93:
94: : env$ ( -- addr ) envs dup @ 1+ cells + ;
95: : env ( addr u -- ) env$ $! ;
96: : env? ( -- ) envs @ oldenv @ over oldenv !
97: 2dup > IF env$ $@ tag THEN
98: 2dup < IF env$ cell+ $@ /tag env$ cell+ $off THEN
99: 2drop ;
100: : +env 1 envs +! ;
101: : -env end-sec @ envs @ 1 > or IF -1 envs +! env? THEN ;
102: : -envs envs @ 0 ?DO -env cr LOOP ;
103: : -tenvs envs @ 1 ?DO -env cr LOOP ;
104: : >env ( addr u -- ) +env env env? ;
105:
106: \ alignment
107:
108: Variable table-format
109: Variable table#
110: Create table-starts &10 0 [DO] 0 c, 0 c, [LOOP]
111: Variable taligned
112:
113: : >align ( c -- )
114: CASE
115: 'l OF s" left" class= ENDOF
116: 'r OF s" right" class= ENDOF
117: 'c OF s" center" class= ENDOF
118: '< OF s" left" class= ENDOF
119: '> OF s" right" class= ENDOF
120: '= OF s" center" class= ENDOF
121: '~ OF s" middle" class= ENDOF
122: ENDCASE ;
123:
124: : >talign ( c -- )
125: CASE
126: 'l OF s" left" align= ENDOF
127: 'r OF s" right" align= ENDOF
128: 'c OF s" center" align= ENDOF
129: '< OF s" left" align= ENDOF
130: '> OF s" right" align= ENDOF
131: '= OF s" center" align= ENDOF
132: ENDCASE taligned on ;
133:
134: : >border ( c -- )
135: case
136: '- of s" border0" class= endof
137: '+ of s" border1" class= endof
138: endcase ;
139:
140: \ image handling
141:
142: wordlist Constant img-sizes
143:
144: Create imgbuf $20 allot
145:
146: Create pngsig $89 c, $50 c, $4E c, $47 c, $0D c, $0A c, $1A c, $0A c,
147: Create jfif $FF c, $D8 c, $FF c, $E0 c, $00 c, $10 c, $4A c, $46 c,
148: $49 c, $46 c,
149:
150: : b@ ( addr -- x ) 0 swap 4 bounds ?DO 8 lshift I c@ + LOOP ;
151: : bw@ ( addr -- x ) 0 swap 2 bounds ?DO 8 lshift I c@ + LOOP ;
152:
153: : gif? ( -- flag )
154: s" GIF89a" imgbuf over str=
155: s" GIF87a" imgbuf over str= or ;
156: : gif-size ( -- w h )
157: imgbuf 8 + c@ imgbuf 9 + c@ 8 lshift +
158: imgbuf 6 + c@ imgbuf 7 + c@ 8 lshift + ;
159:
160: : png? ( -- flag )
161: pngsig 8 imgbuf over str= ;
162: : png-size ( -- w h )
163: imgbuf $14 + b@ imgbuf $10 + b@ ;
164:
165: : jpg? ( -- flag )
166: jfif 10 imgbuf over str= ;
167: : jpg-size ( fd -- w h ) >r
168: 2. BEGIN
169: 2dup r@ reposition-file throw
170: imgbuf $10 r@ read-file throw 0<>
171: imgbuf bw@ $FFC0 $FFD0 within 0= and WHILE
172: imgbuf 2 + bw@ 2 + 0 d+ REPEAT
173: 2drop imgbuf 5 + bw@ imgbuf 7 + bw@ rdrop ;
174:
175: : img-size ( fd -- w h ) >r
176: gif? IF gif-size rdrop EXIT THEN
177: jpg? IF r> jpg-size EXIT THEN
178: png? IF png-size rdrop EXIT THEN
179: 0 0 rdrop ;
180:
181: 3 set-precision
182:
183: : f.size ( r -- )
184: f$ dup >r 0<=
185: IF '0 emit
186: ELSE scratch r@ min type r@ precision - zeros THEN
187: r@ negate zeros
188: scratch r> 0 max /string 0 max -zeros
189: dup IF '. emit THEN type ;
190:
191: : size-does> ( -- ) DOES> ( -- )
192: ." img." dup body> >name .name
193: 2@ ." { width: "
194: s>d d>f 13.8e f/ f.size ." em; height: "
195: s>d d>f 13.8e f/ f.size ." em; }" cr ;
196:
197: : size-css ( file< > -- )
198: outfile-id >r
199: bl sword r/w create-file throw to outfile-id
200: img-sizes wordlist-id
201: BEGIN @ dup WHILE
202: dup name>int execute
203: REPEAT drop
204: outfile-id close-file throw
205: r> to outfile-id
206: dup 0< IF throw ELSE drop THEN ;
207:
208: : size-class ( x y addr u -- x y )
209: 2dup class=
210: 2dup img-sizes search-wordlist IF drop 2drop
211: ELSE
212: get-current >r img-sizes set-current
213: nextname Create 2dup , , size-does>
214: r> set-current
215: THEN ;
216:
217: : .img-size ( addr u -- )
218: r/o open-file IF drop EXIT THEN >r
219: imgbuf $20 r@ read-file throw drop
220: r@ img-size
221: r> close-file throw
222: 2dup or IF 2dup xy>string size-class THEN
223: ?dup IF width= THEN
224: ?dup IF height= THEN
225: ;
226:
227: \ link creation
228:
229: Variable link
230: Variable link-sig
231: Variable link-suffix
232: Variable iconpath
233: Variable icon-prefix
234: Variable icon-tmp
235:
236: Variable do-size
237: Variable do-icon
238: Variable do-expand
239:
240: Defer parse-line
241:
242: : .img ( addr u -- )
243: dup >r '@ -$split dup r> = IF 2swap 2drop
244: ELSE 2swap icon-tmp $! icon-prefix $@ icon-tmp $+! icon-tmp $+!
245: icon-tmp $@ THEN
246: dup >r '| -$split dup r> = IF 2swap THEN
247: dup IF 2swap alt= ELSE 2drop THEN
248: tag-class $@len >r over c@ >align tag-class $@len r> = 1+ /string
249: tag-class $@len >r over c@ >border tag-class $@len r> = 1+ /string
250: 2dup .img-size src= s" img" tag/ ;
251: : >img ( -- ) '{ parse type '} parse .img ;
252:
253: : alt-suffix ( -- )
254: link-suffix $@len 2 - link-suffix $!len
255: s" [" link-suffix 0 $ins
256: s" ]" link-suffix $+!
257: link-suffix $@ alt= ;
258:
259: : get-icon ( addr u -- ) iconpath @ IF 2drop EXIT THEN
260: link-suffix $! s" .*" link-suffix $+!
261: icon-prefix $@ open-dir throw >r
262: BEGIN
263: pad $100 r@ read-dir throw WHILE
264: pad swap 2dup link-suffix $@ filename-match
265: IF icon-prefix $@ iconpath $! s" /" iconpath $+! iconpath $+!
266: iconpath $@ 2dup .img-size src= '- >border
267: alt-suffix s" img" tag/ true
268: ELSE 2drop false THEN
269: UNTIL ELSE drop THEN
270: r> close-dir throw ;
271:
272: : link-icon? ( -- ) do-icon @ 0= ?EXIT
273: iconpath @ IF iconpath $off THEN
274: link $@ + 1- c@ '/ = IF s" index.html" ELSE link $@ THEN
275: '# $split 2drop
276: BEGIN '. $split 2swap 2drop dup WHILE
277: 2dup get-icon REPEAT 2drop ;
278:
279: : link-size? ( -- ) do-size @ 0= ?EXIT
280: link $@ r/o open-file IF drop EXIT THEN >r
281: r@ file-size throw $400 um/mod nip
282: dup $800 < IF ." (" 0 u.r ." k)"
283: ELSE $400 / ." (" 0 u.r ." M)" THEN
284: r> close-file throw ;
285:
286: : link-sig? ( -- )
287: link $@ link-sig $! s" .sig" link-sig $+!
288: link-sig $@ r/o open-file IF drop EXIT THEN
289: close-file throw
290: ." (" link-sig $@ href= s" a" tag
291: s" |-@/sig.gif" .img ." sig" s" /a" tag ." )" ;
292:
293: : link-warn? ( -- ) \ local links only
294: link $@ ': scan nip ?EXIT
295: link $@ '# $split 2drop dup IF
296: r/o open-file nip IF
297: s" Dead Link '" stderr write-file throw
298: link $@ stderr write-file throw
299: s\" ' !!!\n" stderr write-file throw
300: THEN
301: ELSE 2drop THEN ;
302:
303: : link-options ( addr u -- addr' u' )
304: do-size off do-icon on do-expand off
305: over c@ '% = over 0> and IF do-size on 1 /string THEN
306: over c@ '\ = over 0> and IF do-icon off 1 /string THEN
307: over c@ '* = over 0> and IF do-expand on 1 /string THEN ;
308:
309: s" Gforth" environment? [IF] s" 0.5.0" str= [IF]
310: : parse-string ( c-addr u -- ) \ core,block
311: s" *evaluated string*" loadfilename>r
312: push-file #tib ! >tib !
313: >in off blk off loadfile off -1 loadline !
314: ['] parse-line catch
315: pop-file r>loadfilename throw ;
316: [ELSE]
317: : parse-string ( addr u -- )
318: evaluate-input cell new-tib #tib ! tib !
319: ['] parse-line catch pop-file throw ;
320: [THEN] [THEN]
321:
322: Variable expand-link
323: Variable expand-prefix
324: Variable expand-postfix
325:
326: : ?expand ( addr u -- ) expand-link $!
327: do-expand @ IF
328: expand-prefix $@ expand-link 0 $ins
329: expand-postfix $@ expand-link $+! THEN
330: expand-link $@ ;
331:
332: : .link ( addr u -- ) dup >r '| -$split dup r> = IF 2swap THEN
333: link-options link $!
334: link $@len 0= IF 2dup link $! ( s" .html" link $+! ) THEN
335: link $@ ?expand
336: href= s" a" tag link-icon?
337: parse-string s" a" /tag link-size? link-sig? link-warn? ;
338: : >link ( -- ) '[ parse type '] parse .link ;
339:
340: \ line handling
341:
342: : char? ( -- c ) >in @ char swap >in ! ;
343:
344: : parse-tag ( addr u char -- )
345: >r r@ parse .type
346: r> parse 2swap tagged ;
347:
348: : .text ( -- ) >in @ >r char drop
349: source r@ /string >in @ r> - nip .type ;
350:
351: Create do-words $100 0 [DO] ' .text , [LOOP]
352:
353: :noname '( emit 1 >in +! ; '( cells do-words + !
354:
355: : bind-char ( xt -- ) char cells do-words + ! ;
356:
357: : char>tag ( -- ) char >r
358: :noname bl sword postpone SLiteral r@ postpone Literal
359: postpone parse-tag postpone ; r> cells do-words + ! ;
360:
361: : >tag '\ parse type '\ parse tag ;
362:
363: char>tag * b
364: char>tag _ em
365: char>tag # code
366: :noname '~ parse .type '~ parse .type ; '~ cells do-words + !
367:
368: ' >link bind-char [
369: ' >img bind-char {
370: ' >tag bind-char \
371:
372: : do-word ( char -- ) cells do-words + perform ;
373:
374: : word? ( -- addr u ) >in @ >r bl sword r> >in ! ;
375:
376: wordlist Constant autoreplacements
377:
378: :noname ( -- )
379: BEGIN char? do-word source nip >in @ = UNTIL ; is parse-line
380:
381: : parse-line+ ( -- )
382: BEGIN
383: word? autoreplacements search-wordlist
384: IF execute bl sword 2drop
385: source >in @ 1- /string drop c@ bl = >in +!
386: ELSE char? do-word THEN
387: source nip >in @ = UNTIL ;
388:
389: : parse-to ( char -- ) >r
390: BEGIN
391: word? autoreplacements search-wordlist
392: IF execute bl sword 2drop
393: source >in @ 1- /string drop c@ bl = >in +! bl true
394: ELSE char? dup r@ <> THEN WHILE
395: do-word source nip >in @ = UNTIL ELSE drop THEN
396: r> parse type ;
397:
398: \ autoreplace
399:
400: : autoreplace ( <[string|url]> -- )
401: get-current autoreplacements set-current
402: Create set-current
403: here 0 , '[ parse 2drop '] parse rot $!
404: DOES> $@ .link ;
405:
406: \ paragraph handling
407:
408: : parse-par ( -- )
409: BEGIN
410: parse-line+ cr refill WHILE
411: source nip 0= UNTIL THEN ;
412:
413: : par ( addr u -- ) env?
414: 2dup tag parse-par /tag cr cr ;
415:
416: \ scan strings
417:
418: : get-rest ( addr -- ) 0 parse -trailing rot $! ;
419: Create $lf 1 c, #lf c,
420: : get-par ( addr -- ) >r s" " r@ $+!
421: BEGIN 0 parse 2dup s" ." str= 0= WHILE
422: r@ $@len IF $lf count r@ $+! THEN r@ $+!
423: refill 0= UNTIL ELSE 2drop THEN
424: rdrop ;
425:
426: \ toc handling
427:
428: Variable toc-link
429:
430: : >last ( addr link -- link' )
431: BEGIN dup @ WHILE @ REPEAT ! 0 ;
432:
433: Variable create-navs
434: Variable nav$
435: Variable nav-name
436: Variable nav-file
437: Create nav-buf 0 c,
438: : nav+ ( char -- ) nav-buf c! nav-buf 1 nav-file $+! ;
439:
440: : filenamize ( addr u -- )
441: bounds ?DO
442: I c@ dup 'A 'Z 1+ within IF bl + nav+
443: ELSE dup 'a 'z 1+ within IF nav+
444: ELSE dup '0 '9 1+ within IF nav+
445: ELSE dup bl = swap '- = or IF '- nav+
446: THEN THEN THEN THEN
447: LOOP ;
448: : >nav ( addr u -- addr' u' )
449: nav-name $! create-navs @ 0=
450: IF s" navigate/nav.scm" r/w create-file throw create-navs ! THEN
451: s' (script-fu-nav-file "' nav$ $! nav-name $@ nav$ $+!
452: s' " "./navigate/' nav$ $+! s" " nav-file $!
453: nav-name $@ filenamize
454: nav-file $@ nav$ $+! s' .jpg")' nav$ $+!
455: nav$ $@ create-navs @ write-line throw
456: s" [" nav$ $! nav-name $@ nav$ $+!
457: s" |-navigate/" nav$ $+! nav-file $@ nav$ $+! s" .jpg" nav$ $+!
458: nav$ $@ ;
459:
460: : toc, ( n -- ) , '| parse >nav here 0 , $! 0 parse here 0 , $! ;
461: : up-toc align here toc-link >last , 0 toc, ;
462: : top-toc align here toc-link >last , 1 toc, ;
463: : this-toc align here toc-link >last , 2 toc, ;
464: : sub-toc align here toc-link >last , 3 toc, ;
465: : new-toc toc-link off ;
466:
467: Variable toc-name
468: Variable toc-index
469: 6 Value /toc-line
470:
471: : .toc-entry ( toc flag -- )
472: swap cell+ dup @ swap cell+ dup cell+ $@ 2dup href= s" a" tag
473: '# scan 1 /string toc-name $@ compare >r
474: $@ .img swap
475: IF
476: case
477: 2 of s" ^]|-@/arrow_up.jpg" .img endof
478: 3 of
479: r@ 0= IF s" *]|-@/circle.jpg"
480: ELSE s" v]|-@/arrow_down.jpg" THEN .img endof
481: endcase
482: ELSE
483: case
484: 0 of s" ^]|-@/arrow_up.jpg" .img endof
485: 1 of s" >]|-@/arrow_right.jpg" .img endof
486: 2 of s" *]|-@/circle.jpg" .img endof
487: 3 of s" v]|-@/arrow_down.jpg" .img endof
488: endcase
489: THEN
490: s" a" /tag rdrop ." <!--" cr ." -->"
491: 1 toc-index +! toc-index @ /toc-line mod 0=
492: IF s" br" tag/ THEN ;
493:
494: : print-toc ( -- ) toc-index off cr s" menu" class= s" div" >env cr
495: 0 parse
496: dup 0= IF toc-name $! 0 ELSE
497: toc-name $! toc-name $@ id= s" " s" a" tagged 2
498: THEN >r
499: toc-link BEGIN @ dup WHILE
500: dup cell+ @ 3 = r@ 0= and IF rdrop 1 >r ( s" br" tag/ cr ) THEN
501: dup cell+ @ r@ >= IF dup r@ 2 = .toc-entry THEN
502: dup cell+ @ 2 = r@ 2 = and IF s" br" tag/ toc-index off THEN
503: REPEAT drop rdrop -env cr ;
504:
505: \ handle global tags
506:
507: : indent ( n -- )
508: indentlevel @ over
509: indentlevel !
510: 2dup < IF swap DO -env LOOP EXIT THEN
511: 2dup > IF DO s" div" >env LOOP EXIT THEN
512: 2dup = IF drop IF -env s" div" >env THEN THEN
513: ;
514: : +indent ( -- )
515: indentlevel @ IF -env indent= s" div" >env THEN
516: ;
517:
518: wordlist constant longtags
519:
520: Variable divs
521:
522: longtags set-current
523:
524: : --- 0 indent cr s" hr" tag/ cr ;
525: : * 1 indent s" h1" par +indent ;
526: : ** 1 indent s" h2" par +indent ;
527: : *** 2 indent s" h3" par +indent ;
528: : -- 0 indent cr print-toc ;
529: : && 0 parse id= ;
530: : - s" ul" env s" li" par ;
531: : + s" ol" env s" li" par ;
532: : ? s" dl" env s" dt" par ;
533: : : s" dl" env s" dd" par ;
534: : -<< s" ul" env env? s" li" >env ;
535: : +<< s" ol" env env? s" li" >env ;
536: \ : ?<< s" dl" env env? s" dt" >env ; \ not allowed
537: : :<< s" dl" env env? s" dd" >env ;
538: : p<< s" p" >env ;
539: : << +env ;
540: : <* s" center" class= ;
541: : <red s" p" >env s" #ff0000" s" color" opt s" font" >env parse-par ;
542: : red> -env -env ;
543: : >> -env ;
544: : *> ;
545: : :: interpret ;
546: : . end-sec on 0 indent ;
547: : :code s" pre" >env
548: BEGIN source >in @ /string .type cr refill WHILE
549: source s" :endcode" str= UNTIL THEN
550: -env ;
551: : :code-file s" pre" >env
552: parse" slurp-file type -env ;
553: : \ postpone \ ;
554:
555: definitions
556:
557: : LT get-order longtags swap 1+ set-order
558: bl sword parser previous ; immediate
559:
560: \ Table
561:
562: : next-char ( -- char ) source drop >in @ + c@ ;
563: : next-table ( -- )
564: BEGIN
565: table-starts table# @ 2* + dup c@ dup
566: IF 1- over c! 1+ c@ 1+ ELSE swap 1+ c! 0 THEN
567: dup WHILE table# +! REPEAT drop
568: table-format $@ table# @ /string drop c@ taligned ! ;
569: : next>align ( -- )
570: next-char dup bl <> over '\ <> and
571: IF taligned ! 1 >in +! ELSE drop THEN ;
572:
573: : |tag ( addr u -- )
574: next-table
575: next-char '/ = IF 1 >in +!
576: next-char digit? IF
577: dup 1- table-starts table# @ 2* + c!
578: s" rowspan" opt# 1 >in +! THEN
579: next>align
580: THEN
581: next-char '\ = IF 1 >in +!
582: next-char digit? IF
583: dup 1- table-starts table# @ 2* + 1+ c!
584: dup 1- table# +!
585: s" colspan" opt# 1 >in +! THEN
586: next>align
587: THEN
588: taligned @ >talign >env
589: 1 table# +! ;
590: : |d table# @ 0> IF -env THEN s" td" |tag ;
591: : |h table# @ 0> IF -env THEN s" th" |tag ;
592: : |line s" tr" >env table# off ;
593: : line| 1 >in +! -env -env cr ;
594:
595: longtags set-current
596:
597: : <| ( -- ) table-starts &20 erase
598: s" table" class= s" div" >env
599: bl sword table-format $! bl sword
600: dup IF s" border" opt ELSE 2drop THEN
601: s" table" >env ;
602: : |> -env -env cr cr ;
603: : +| ( -- )
604: |line BEGIN |h '| parse-to next-char '+ = UNTIL line| ;
605: : -| ( -- )
606: |line BEGIN |d '| parse-to next-char '- = UNTIL line| ;
607: : =| ( -- )
608: |line |h '| parse-to
609: BEGIN |d '| parse-to next-char '= = UNTIL line| ;
610:
611: definitions
612:
613: \ parse a section
614:
615: : section-par ( -- ) >in off
616: bl sword longtags search-wordlist
617: IF execute
618: ELSE source nip IF >in off s" p" par THEN THEN ;
619: : parse-section ( -- ) end-sec off
620: BEGIN refill WHILE
621: section-par end-sec @ UNTIL THEN end-sec off ;
622:
623: \ HTML head
624:
625: Variable css-file
626: Variable content
627: Variable lang
628:
629: : lang@ ( -- addr u )
630: lang @ IF lang $@ ELSE s" en" THEN ;
631: : .css ( -- )
632: css-file @ IF css-file $@len IF
633: s" StyleSheet" s" rel" opt
634: css-file $@ href=
635: s" text/css" s" type" opt s" link" tag/ cr
636: THEN THEN ;
637: : .title ( addr u -- ) 1 envs ! oldenv off
638: .' <!DOCTYPE html' cr
639: .' PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' cr
640: .' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' cr
641: s" http://www.w3.org/1999/xhtml" s" xmlns" opt
642: lang@ s" xml:lang" opt lang@ s" lang" opt
643: s" html" >env cr s" head" >env cr
644: s" Content-Type" s" http-equiv" opt
645: content $@ s" content" opt
646: s" meta" tag/ cr .css
647: s" title" tagged cr
648: -env ;
649:
650: \ HTML trailer
651:
652: Variable public-key
653: Variable mail
654: Variable mail-name
655: Variable orig-date
656:
657: : .lastmod
658: ." Last modified: " time&date rot 0 u.r swap 1-
659: s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
660: 0 u.r ;
661:
662: : .trailer
663: s" center" class= s" address" >env
664: orig-date @ IF ." Created " orig-date $@ type ." . " THEN
665: .lastmod
666: ." by "
667: s" Mail|@/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
668: public-key @ IF
669: public-key $@ href= s" a" tag
670: s" PGP key|@/gpg.asc.gif" .img s" a" /tag
671: THEN
672: -envs ;
673:
674: \ top word
675:
676: : maintainer ( -- )
677: '< sword -trailing mail-name $! '> sword mail $! ;
678: : pgp-key ( -- )
679: bl sword -trailing public-key $! ;
680: : charset ( -- ) s" text/xhtml; charset=" content $!
681: bl sword -trailing content $+! ;
682:
683: charset iso-8859-1
684:
685: : created ( -- )
686: bl sword orig-date $! ;
687: : icons
688: bl sword icon-prefix $! ;
689: : lang
690: bl sword lang $! ;
691: : expands '# sword expand-prefix $! bl sword expand-postfix $! ;
692:
693: icons icons
694:
695: Variable style$
696: : style> style$ @ 0= IF s" " style$ $! THEN style$ $@ tag-option $! ;
697: : >style tag-option $@ style$ $! s" " tag-option $! ;
698:
699: : style style> opt >style ;
700: : background ( -- ) parse" s" background" style ;
701: : text ( -- ) parse" s" text" style ;
702: warnings @ warnings off
703: : link ( -- ) parse" s" link" style ;
704: warnings !
705: : vlink ( -- ) parse" s" vlink" style ;
706: : marginheight ( -- ) parse" s" marginheight" style ;
707: : css ( -- ) parse" css-file $! ;
708:
709: : wf ( -- )
710: outfile-id >r
711: bl sword r/w create-file throw to outfile-id
712: parse" .title
713: +env style> s" body" env env?
714: ['] parse-section catch .trailer
715: outfile-id close-file throw
716: r> to outfile-id
717: dup 0< IF throw ELSE drop THEN ;
718:
719: : eval-par ( addr u -- )
720: s" wf-temp.wf" r/w create-file throw >r
721: r@ write-file r> close-file throw
722: push-file s" wf-temp.wf" r/o open-file throw loadfile !
723: parse-par -env parse-section
724: loadfile @ close-file swap 2dup or
725: pop-file drop throw throw
726: s" wf-temp.wf" delete-file throw ;
727:
728: \ simple text data base
729:
730: Variable last-entry
731: Variable field#
732:
733: : table: ( xt n -- ) Create 0 , ['] type , , , 1 field# !
734: DOES> 2 cells + 2@ >in @ >r longtags set-current
735: Create definitions swap , r> >in !
736: here last-entry !
737: dup 0 DO 0 , LOOP
738: 1 DO s" " last-entry @ I cells + $! LOOP
739: last-entry @ get-rest
740: DOES> dup cell+ swap perform ;
741:
742: : field: Create field# @ , ['] type , 1 field# +!
743: DOES> @ cells last-entry @ + get-rest ;
744: : par: Create field# @ , ['] eval-par , 1 field# +!
745: DOES> @ cells last-entry @ + get-par ;
746:
747: : >field-rest >body @ cells postpone Literal postpone + ;
748: : >field ' >field-rest ; immediate
749:
750: : db-line ( -- )
751: BEGIN
752: source >in @ /string nip WHILE
753: '% parse postpone SLiteral postpone type
754: '% parse dup IF
755: '| $split 2swap
756: sfind 0= abort" Field not found"
757: dup postpone r@ >field-rest postpone $@
758: over IF drop evaluate ELSE
759: nip nip >body cell+ @ compile,
760: THEN
761: ELSE 2drop postpone cr THEN
762: REPEAT ;
763:
764: : db-par ( -- ) LT postpone p<< postpone >r
765: BEGIN db-line refill WHILE next-char '. = UNTIL 1 >in +! THEN
766: postpone rdrop ( LT postpone >> ) ; immediate
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>