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 IF drop EXIT THEN >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 / i
384: char>tag _ em
385: char>tag # code
386: :noname '~ parse .type '~ parse .type ; '~ cells do-words + !
387:
388: ' >link bind-char [
389: ' >img bind-char {
390: ' >tag bind-char \
391:
392: : do-word ( char -- ) cells do-words + perform ;
393:
394: : word? ( -- addr u ) >in @ >r bl sword r> >in ! ;
395:
396: wordlist Constant autoreplacements
397:
398: :noname ( -- )
399: BEGIN char? do-word source nip >in @ = UNTIL ; is parse-line
400:
401: : parse-line+ ( -- )
402: BEGIN
403: word? autoreplacements search-wordlist
404: IF execute bl sword 2drop
405: source >in @ 1- /string drop c@ bl = >in +!
406: ELSE char? do-word THEN
407: source nip >in @ = UNTIL ;
408:
409: : parse-to ( char -- ) >r
410: BEGIN
411: word? autoreplacements search-wordlist
412: IF execute bl sword 2drop
413: source >in @ 1- /string drop c@ bl = >in +! bl true
414: ELSE char? dup r@ <> THEN WHILE
415: do-word source nip >in @ = UNTIL ELSE drop THEN
416: r> parse type ;
417:
418: \ autoreplace
419:
420: : autoreplace ( <[string|url]> -- )
421: get-current autoreplacements set-current
422: Create set-current
423: here 0 , '[ parse 2drop '] parse rot $!
424: DOES> $@ .link ;
425:
426: \ paragraph handling
427:
428: : parse-par ( -- )
429: BEGIN
430: parse-line+ cr refill WHILE
431: source nip 0= UNTIL THEN ;
432:
433: : par ( addr u -- ) env?
434: 2dup tag parse-par /tag cr cr ;
435:
436: \ scan strings
437:
438: : get-rest ( addr -- ) 0 parse -trailing rot $! ;
439: Create $lf 1 c, #lf c,
440: : get-par ( addr -- ) >r s" " r@ $+!
441: BEGIN 0 parse 2dup s" ." str= 0= WHILE
442: r@ $@len IF $lf count r@ $+! THEN r@ $+!
443: refill 0= UNTIL ELSE 2drop THEN
444: rdrop ;
445:
446: \ toc handling
447:
448: Variable toc-link
449:
450: : >last ( addr link -- link' )
451: BEGIN dup @ WHILE @ REPEAT ! 0 ;
452:
453: Variable create-navs
454: Variable nav$
455: Variable nav-name
456: Variable nav-file
457: Create nav-buf 0 c,
458: : nav+ ( char -- ) nav-buf c! nav-buf 1 nav-file $+! ;
459:
460: : filenamize ( addr u -- )
461: bounds ?DO
462: I c@ dup 'A 'Z 1+ within IF bl + nav+
463: ELSE dup 'a 'z 1+ within IF nav+
464: ELSE dup '0 '9 1+ within IF nav+
465: ELSE dup bl = over '- = or IF '- nav+
466: ELSE nav+
467: THEN THEN THEN THEN
468: LOOP ;
469: : >nav ( addr u -- addr' u' )
470: nav-name $! create-navs @ 0=
471: IF s" navigate/nav.scm" r/w create-file throw create-navs ! THEN
472: s' (script-fu-nav-file "' nav$ $! nav-name $@ nav$ $+!
473: s' " "./navigate/' nav$ $+! s" " nav-file $!
474: nav-name $@ filenamize
475: nav-file $@ nav$ $+! s' .jpg")' nav$ $+!
476: nav$ $@ create-navs @ write-line throw
477: s" [" nav$ $! nav-name $@ nav$ $+!
478: s" |-navigate/" nav$ $+! nav-file $@ nav$ $+! s" .jpg" nav$ $+!
479: nav$ $@ ;
480:
481: : toc, ( n -- ) , '| parse >nav here 0 , $! 0 parse here 0 , $! ;
482: : up-toc align here toc-link >last , 0 toc, ;
483: : top-toc align here toc-link >last , 1 toc, ;
484: : this-toc align here toc-link >last , 2 toc, ;
485: : sub-toc align here toc-link >last , 3 toc, ;
486: : new-toc toc-link off ;
487:
488: Variable toc-name
489: Variable toc-index
490: 6 Value /toc-line
491: true Value toc-image
492:
493: : .toc-entry ( toc flag -- )
494: swap cell+ dup @ swap cell+ dup cell+ $@ 2dup ?expand href=
495: '# scan 1 /string toc-name $@ compare >r
496: $@ toc-image IF s" a" tag .img swap
497: IF
498: case
499: 2 of s" ^]|-@/arrow_up.jpg" .img endof
500: 3 of
501: r@ 0= IF s" *]|-@/circle.jpg"
502: ELSE s" v]|-@/arrow_down.jpg" THEN .img endof
503: endcase
504: ELSE
505: case
506: 0 of s" ^]|-@/arrow_up.jpg" .img endof
507: 1 of s" >]|-@/arrow_right.jpg" .img endof
508: 2 of s" *]|-@/circle.jpg" .img endof
509: 3 of s" v]|-@/arrow_down.jpg" .img endof
510: endcase
511: THEN
512: s" a" /tag ." <!--" cr ." -->"
513: ELSE
514: '[ skip 2dup '| scan nip - 2swap swap
515: IF
516: CASE
517: 2 OF s" up" class= ENDOF
518: 3 OF r@ 0= IF s" circle" ELSE s" down" THEN class= ENDOF
519: ENDCASE
520: ELSE
521: CASE
522: 0 OF s" up" class= ENDOF
523: 1 OF s" right" class= ENDOF
524: 2 OF s" circle" class= ENDOF
525: 3 OF s" down" class= ENDOF
526: ENDCASE
527: THEN
528: s" a" tag parse-string s" a" /tag ." <!--" cr ." -->"
529: THEN
530: rdrop
531: 1 toc-index +! toc-index @ /toc-line mod 0=
532: IF -env cr s" p" >env THEN ;
533:
534: : print-toc ( -- ) toc-index off cr
535: toc-image IF s" img-menu" ELSE s" menu" THEN class=
536: s" div" >env cr s" p" >env
537: 0 parse
538: dup 0= IF toc-name $! 0 ELSE
539: toc-name $! toc-name $@ id= s" " s" a" tagged 2
540: THEN >r
541: toc-link BEGIN @ dup WHILE
542: dup cell+ @ 3 = r@ 0= and IF rdrop 1 >r ( s" br" tag/ cr ) THEN
543: dup cell+ @ r@ >= IF dup r@ 2 = .toc-entry THEN
544: dup cell+ @ 2 = r@ 2 = and IF s" br" tag/ toc-index off THEN
545: REPEAT drop rdrop -env -env cr ;
546:
547: \ handle global tags
548:
549: : indent ( n -- )
550: indentlevel @ over
551: indentlevel !
552: 2dup < IF swap DO -env LOOP EXIT THEN
553: 2dup > IF DO s" div" >env LOOP EXIT THEN
554: 2dup = IF drop IF -env s" div" >env THEN THEN
555: ;
556: : +indent ( -- )
557: indentlevel @ IF -env indent= s" div" >env THEN
558: ;
559:
560: wordlist constant longtags
561:
562: Variable divs
563:
564: longtags set-current
565:
566: : --- 0 indent cr s" hr" tag/ cr ;
567: : * 1 indent +indent s" h1" dclass= s" h1" par s" " dclass= ;
568: : ** 1 indent +indent s" h2" dclass= s" h2" par s" " dclass= ;
569: : *** 2 indent +indent s" h3" dclass= s" h3" par s" " dclass= ;
570: : -- 0 indent cr print-toc ;
571: : && 0 parse id= ;
572: : - s" ul" env s" li" par ;
573: : + s" ol" env s" li" par ;
574: : ? s" dl" env s" dt" par ;
575: : : s" dl" env s" dd" par ;
576: : -<< s" ul" env env? s" li" >env ;
577: : +<< s" ol" env env? s" li" >env ;
578: \ : ?<< s" dl" env env? s" dt" >env ; \ not allowed
579: : :<< s" dl" env env? s" dd" >env ;
580: : p<< s" p" >env ;
581: : << +env ;
582: : <* s" center" class= ;
583: : <red s" red" class= s" p" >env parse-par ;
584: : red> -env ;
585: : >> -env ;
586: : *> ;
587: : :: interpret ;
588: : . end-sec on 0 indent ;
589: : :code s" pre" >env
590: BEGIN source >in @ /string .type cr refill WHILE
591: source s" :endcode" str= UNTIL THEN
592: -env ;
593: : :code-file s" pre" >env
594: parse" slurp-file type -env ;
595: : \ postpone \ ;
596:
597: definitions
598:
599: : LT get-order longtags swap 1+ set-order
600: bl sword parser previous ; immediate
601:
602: \ Table
603:
604: : next-char ( -- char ) source drop >in @ + c@ ;
605: : next-table ( -- )
606: BEGIN
607: table-starts table# @ 2* + dup c@ dup
608: IF 1- over c! 1+ c@ 1+ ELSE swap 1+ c! 0 THEN
609: dup WHILE table# +! REPEAT drop
610: table-format $@ table# @ /string drop c@ taligned ! ;
611: : next>align ( -- )
612: next-char dup bl <> over '\ <> and
613: IF taligned ! 1 >in +! ELSE drop THEN ;
614:
615: : |tag ( addr u -- )
616: next-table
617: next-char '/ = IF 1 >in +!
618: next-char digit? IF
619: dup 1- table-starts table# @ 2* + c!
620: s" rowspan" opt# 1 >in +! THEN
621: next>align
622: THEN
623: next-char '\ = IF 1 >in +!
624: next-char digit? IF
625: dup 1- table-starts table# @ 2* + 1+ c!
626: dup 1- table# +!
627: s" colspan" opt# 1 >in +! THEN
628: next>align
629: THEN
630: taligned @ >talign >env
631: 1 table# +! ;
632: : |d table# @ 0> IF -env THEN s" td" |tag ;
633: : |h table# @ 0> IF -env THEN s" th" |tag ;
634: : |line s" tr" >env table# off ;
635: : line| 1 >in +! -env -env cr ;
636:
637: longtags set-current
638:
639: : <| ( -- ) table-starts &20 erase
640: s" table" class= s" div" >env
641: bl sword table-format $! bl sword
642: dup IF s" border" opt ELSE 2drop THEN
643: s" table" >env ;
644: : |> -env -env cr cr ;
645: : +| ( -- )
646: |line BEGIN |h '| parse-to next-char '+ = UNTIL line| ;
647: : -| ( -- )
648: |line BEGIN |d '| parse-to next-char '- = UNTIL line| ;
649: : =| ( -- )
650: |line |h '| parse-to
651: BEGIN |d '| parse-to next-char '= = UNTIL line| ;
652:
653: definitions
654:
655: \ parse a section
656:
657: : section-par ( -- ) >in off
658: bl sword longtags search-wordlist
659: IF execute
660: ELSE source nip IF >in off s" p" par THEN THEN ;
661: : parse-section ( -- ) end-sec off
662: BEGIN refill WHILE
663: section-par end-sec @ UNTIL THEN end-sec off ;
664:
665: \ HTML head
666:
667: Variable css-file
668: Variable print-file
669: Variable ie-css-file
670: Variable content
671: Variable _charset
672: Variable _lang
673: Variable _favicon
674:
675: : lang@ ( -- addr u )
676: _lang @ IF _lang $@ ELSE s" en" THEN ;
677: : .css ( -- )
678: css-file @ IF css-file $@len IF
679: s" StyleSheet" s" rel" opt
680: css-file $@ href= s" screen" s" media" opt
681: s" text/css" s" type" opt s" link" tag/ cr
682: THEN THEN
683: ie-css-file @ IF
684: ." <!--[if lt IE 7.0]>" cr
685: .' <style type="text/css">@import url(' ie-css-file $@ type ." );</style>" cr
686: ." <![endif]-->" cr
687: THEN ;
688: : .print ( -- )
689: print-file @ IF print-file $@len IF
690: s" StyleSheet" s" rel" opt
691: print-file $@ href= s" print" s" media" opt
692: s" text/css" s" type" opt s" link" tag/ cr
693: THEN THEN ;
694: : .title ( addr u -- ) 1 envs ! oldenv off
695: _charset $@ s" utf-8" str= 0=
696: IF .' <?xml version="1.0" encoding="' _charset $@ .upcase .' "?>' cr THEN
697: .' <!DOCTYPE html' cr
698: .' PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' cr
699: .' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' cr
700: s" http://www.w3.org/1999/xhtml" s" xmlns" opt
701: lang@ s" xml:lang" opt lang@ s" lang" opt
702: s" html" >env cr s" head" >env cr
703: s" Content-Type" s" http-equiv" opt
704: content $@ s" content" opt
705: s" meta" tag/ cr .css .print
706: _favicon @ IF
707: s" shortcut icon" s" rel" opt
708: _favicon $@ href=
709: s" image/x-icon" s" type" opt
710: s" link" tag/ cr THEN
711: s" title" tagged cr
712: -env ;
713:
714: \ HTML trailer
715:
716: Variable public-key
717: Variable mail
718: Variable mail-name
719: Variable orig-date
720:
721: : .lastmod
722: ." Last modified: " time&date rot 0 u.r swap 1-
723: s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
724: 0 u.r ;
725:
726: : .trailer
727: s" center" class= s" address" >env
728: orig-date @ IF ." Created " orig-date $@ type ." . " THEN
729: .lastmod
730: ." by "
731: s" Mail|@/mail.gif" .img mail $@ mailto: mail-name $@ s" a" tagged
732: public-key @ IF
733: public-key $@ href= s" a" tag
734: s" PGP key|-@/gpg.asc.gif" .img s" a" /tag
735: THEN
736: -envs ;
737:
738: \ top word
739:
740: : maintainer ( -- )
741: '< sword -trailing mail-name $! '> sword mail $! ;
742: : pgp-key ( -- )
743: bl sword -trailing public-key $! ;
744: : charset ( -- ) s" text/xhtml; charset=" content $!
745: bl sword -trailing 2dup content $+! _charset $! ;
746:
747: charset iso-8859-1
748:
749: : created ( -- )
750: bl sword orig-date $! ;
751: : icons
752: bl sword icon-prefix $! ;
753: : lang
754: bl sword _lang $! ;
755: : favicon
756: bl sword _favicon $! ;
757: : expands '# sword expand-prefix $! bl sword expand-postfix $! ;
758:
759: icons icons
760:
761: Variable style$
762: : style> style$ @ 0= IF s" " style$ $! THEN style$ $@ tag-option $! ;
763: : >style tag-option $@ style$ $! s" " tag-option $! ;
764:
765: : style style> opt >style ;
766: : background ( -- ) parse" s" background" style ;
767: : text ( -- ) parse" s" text" style ;
768: warnings @ warnings off
769: : link ( -- ) parse" s" link" style ;
770: warnings !
771: : vlink ( -- ) parse" s" vlink" style ;
772: : marginheight ( -- ) parse" s" marginheight" style ;
773: : css ( -- ) parse" css-file $! ;
774: : print-css ( -- ) parse" print-file $! ;
775: : ie-css ( -- ) parse" ie-css-file $! ;
776:
777: : wf ( -- )
778: outfile-id >r
779: bl sword r/w create-file throw to outfile-id
780: parse" .title
781: +env style> s" body" env env?
782: ['] parse-section catch .trailer
783: outfile-id close-file throw
784: r> to outfile-id
785: dup 0< IF throw ELSE drop THEN ;
786:
787: : eval-par ( addr u -- )
788: s" wf-temp.wf" r/w create-file throw >r
789: r@ write-file r> close-file throw
790: push-file s" wf-temp.wf" r/o open-file throw loadfile !
791: parse-par -env parse-section
792: loadfile @ close-file swap 2dup or
793: pop-file drop throw throw
794: s" wf-temp.wf" delete-file throw ;
795:
796: \ simple text data base
797:
798: Variable last-entry
799: Variable field#
800:
801: : table: ( xt n -- ) Create 0 , ['] type , , , 1 field# !
802: DOES> 2 cells + 2@ >in @ >r longtags set-current
803: Create definitions swap , r> >in !
804: here last-entry !
805: dup 0 DO 0 , LOOP
806: 1 DO s" " last-entry @ I cells + $! LOOP
807: last-entry @ get-rest
808: DOES> dup cell+ swap perform ;
809:
810: : field: Create field# @ , ['] type , 1 field# +!
811: DOES> @ cells last-entry @ + get-rest ;
812: : par: Create field# @ , ['] eval-par , 1 field# +!
813: DOES> @ cells last-entry @ + get-par ;
814:
815: : >field-rest >body @ cells postpone Literal postpone + ;
816: : >field ' >field-rest ; immediate
817:
818: : db-line ( -- )
819: BEGIN
820: source >in @ /string nip WHILE
821: '% parse postpone SLiteral postpone type
822: '% parse dup IF
823: '| $split 2swap
824: sfind 0= abort" Field not found"
825: dup postpone r@ >field-rest postpone $@
826: over IF drop evaluate ELSE
827: nip nip >body cell+ @ compile,
828: THEN
829: ELSE 2drop postpone cr THEN
830: REPEAT ;
831:
832: : db-par ( -- ) LT postpone p<< postpone >r
833: BEGIN db-line refill WHILE next-char '. = UNTIL 1 >in +! THEN
834: postpone rdrop ( LT postpone >> ) ; immediate
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>