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