[gforth] / gforth / wf.fs  

gforth: gforth/wf.fs


1 : pazsan 1.1 \ wiki forth
2 :    
3 :     require string.fs
4 :    
5 :     \ tag handling
6 :    
7 :     : .' '' parse postpone SLiteral postpone type ; immediate
8 : pazsan 1.3 : s' '' parse postpone SLiteral ; immediate
9 : pazsan 1.1
10 : pazsan 1.3 Variable tag-option
11 :     s" " tag-option $!
12 :    
13 :     : tag ( addr u -- ) '< emit type tag-option $@ type '> emit
14 :     s" " tag-option $! ;
15 : pazsan 1.1 : /tag ( addr u -- ) '< emit '/ emit type '> emit ;
16 : pazsan 1.2 : tagged ( addr1 u1 addr2 u2 -- ) 2dup 2>r tag type 2r> /tag ;
17 : pazsan 1.1
18 : pazsan 1.3 : opt ( addr u opt u -- ) s" " tag-option $+!
19 :     tag-option $+! s' ="' tag-option $+! tag-option $+!
20 :     s' "' tag-option $+! ;
21 :     : href= ( addr u -- ) s" href" opt ;
22 :     : src= ( addr u -- ) s" src" opt ;
23 : pazsan 1.4 : alt= ( addr u -- ) s" alt" opt ;
24 : pazsan 1.3 : align= ( addr u -- ) s" align" opt ;
25 :     : mailto: ( addr u -- ) s' href="mailto:' tag-option $+!
26 :     tag-option $+! s' "' tag-option $+! ;
27 :    
28 : pazsan 1.1 \ environment handling
29 :    
30 :     Variable oldenv
31 :     Variable envs 10 0 [DO] 0 , [LOOP]
32 :    
33 :     : env$ ( -- addr ) envs dup @ 1+ cells + ;
34 :     : env ( addr u -- ) env$ $! ;
35 :     : env? ( -- ) envs @ oldenv @
36 :     2dup > IF env$ $@ tag THEN
37 :     2dup < IF env$ cell+ $@ /tag env$ cell+ $off THEN
38 :     drop oldenv ! ;
39 :     : +env 1 envs +! ;
40 :     : -env -1 envs +! env? ;
41 :     : -envs envs @ 0 ?DO -env cr LOOP ;
42 : pazsan 1.2 : >env ( addr u -- ) +env env env? ;
43 : pazsan 1.1
44 :     \ link creation
45 :    
46 :     Variable link
47 :     Variable link-suffix
48 : pazsan 1.3 Variable iconpath
49 : pazsan 1.1
50 : pazsan 1.2 Variable do-size
51 :    
52 : pazsan 1.3 : link-icon? ( -- )
53 : pazsan 1.1 link $@ '. $split link-suffix $! 2drop s" .*" link-suffix $+!
54 :     s" icons" open-dir throw >r
55 :     BEGIN
56 :     pad $100 r@ read-dir throw WHILE
57 :     pad swap 2dup link-suffix $@ filename-match
58 : pazsan 1.3 IF s" icons/" iconpath $! iconpath $+!
59 :     iconpath $@ src= s" img" tag true
60 : pazsan 1.1 ELSE 2drop false THEN
61 : pazsan 1.3 UNTIL ELSE drop THEN \ ELSE '( emit link-suffix $@ 2 - type ') emit THEN
62 : pazsan 1.1 r> close-dir throw ;
63 :    
64 : pazsan 1.2 : link-size? ( -- ) do-size @ 0= ?EXIT
65 : pazsan 1.1 link $@ r/o open-file IF drop EXIT THEN >r
66 :     r@ file-size throw $400 um/mod nip ." (" 0 u.r ." k)"
67 :     r> close-file throw ;
68 :    
69 : pazsan 1.2 : link-options ( addr u -- addr' u' )
70 : pazsan 1.3 do-size off
71 :     over c@ '% = over 0> and IF do-size on 1 /string THEN ;
72 : pazsan 1.2
73 :     : .link ( -- ) '[ parse type '] parse '| $split
74 :     link-options link $!
75 : pazsan 1.1 link $@len 0= IF 2dup link $! s" .html" link $+! THEN
76 : pazsan 1.3 link-icon? link $@ href= s" a" tagged
77 : pazsan 1.1 link-size? ;
78 :    
79 : pazsan 1.4 : .img ( -- ) '{ parse type '} parse '| $split
80 :     dup IF 2swap alt= ELSE 2drop THEN src= s" img" tag ;
81 :    
82 : pazsan 1.1 \ line handling
83 :    
84 : pazsan 1.4 : char? ( -- c ) >in @ char swap >in ! ;
85 : pazsan 1.1 : parse-tag ( addr u char -- )
86 :     >r r@ parse type
87 : pazsan 1.2 r> parse 2swap tagged ;
88 : pazsan 1.1
89 :     : .bold ( -- ) s" b" '* parse-tag ;
90 :     : .em ( -- ) s" em" '_ parse-tag ;
91 :    
92 : pazsan 1.4 : do-word ( char -- )
93 :     CASE
94 :     '* OF .bold ENDOF
95 :     '_ OF .em ENDOF
96 :     '[ OF .link ENDOF
97 :     '{ OF .img ENDOF
98 :     >in @ >r char drop
99 :     source r@ /string >in @ r> - nip type
100 :     ENDCASE ;
101 :    
102 : pazsan 1.1 : parse-line ( -- )
103 : pazsan 1.4 BEGIN char? do-word source nip >in @ = UNTIL ;
104 :    
105 :     : parse-to ( char -- ) >r
106 :     BEGIN char? dup r@ <> WHILE
107 :     do-word source nip >in @ = UNTIL ELSE drop THEN
108 :     r> parse type ;
109 : pazsan 1.1
110 :     \ paragraph handling
111 :    
112 :     : parse-par ( -- )
113 :     BEGIN parse-line cr refill WHILE
114 :     source nip 0= UNTIL THEN ;
115 :    
116 :     : par ( addr u -- ) env? 2dup tag parse-par /tag cr cr ;
117 :     : line ( addr u -- ) env? 2dup tag parse-line /tag cr cr ;
118 :    
119 :     \ handle global tags
120 :    
121 :     wordlist constant longtags
122 :    
123 :     Variable end-sec
124 :    
125 :     longtags set-current
126 :    
127 : pazsan 1.3 : --- cr s" hr" tag cr ;
128 : pazsan 1.1 : * s" h1" line ;
129 :     : ** s" h2" line ;
130 :     : *** s" h3" line ;
131 :     : - s" ul" env s" li" par ;
132 :     : + s" ol" env s" li" par ;
133 :     : << +env ;
134 : pazsan 1.3 : <* s" center" >env ;
135 : pazsan 1.1 : >> -env ;
136 : pazsan 1.3 : *> -env ;
137 : pazsan 1.1 : . end-sec on ;
138 :     : \ postpone \ ;
139 :    
140 :     definitions
141 : pazsan 1.3
142 :     \ Table
143 :    
144 :     Variable table-format
145 :     Variable table#
146 :    
147 :     : |tag table-format $@ table# @ /string drop c@
148 :     CASE
149 :     'l OF s" left" align= ENDOF
150 :     'r OF s" right" align= ENDOF
151 :     'c OF s" center" align= ENDOF
152 :     ENDCASE >env 1 table# +! ;
153 :     : |d table# @ IF -env THEN s" td" |tag ;
154 :     : |h table# @ IF -env THEN s" th" |tag ;
155 :     : |line s" tr" >env table# off ;
156 :     : line| -env -env cr ;
157 :    
158 :     longtags set-current
159 :    
160 :     : <| s" table" >env bl sword table-format $! ;
161 :     : |> -env ;
162 :     : +| |line
163 :     BEGIN
164 : pazsan 1.4 |h '| parse-to char? '+ = UNTIL line| ;
165 : pazsan 1.3 : -| |line
166 :     BEGIN
167 : pazsan 1.4 |d '| parse-to char? '- = UNTIL line| ;
168 : pazsan 1.3
169 :     definitions
170 : pazsan 1.1
171 :     \ parse a section
172 :    
173 :     : refill-loop ( -- ) end-sec off
174 :     BEGIN refill WHILE >in off
175 :     bl sword find-name
176 :     ?dup IF name>int execute
177 :     ELSE source nip IF >in off s" p" par THEN THEN
178 :     end-sec @ UNTIL THEN ;
179 :     : parse-section ( -- )
180 :     get-order longtags 1 set-order refill-loop set-order ;
181 :    
182 :     \ HTML head
183 :    
184 :     : .title ( addr u -- )
185 :     .' <!doctype html public "-//w3c//dtd html 4.0 transitional//en">' cr
186 : pazsan 1.2 s" html" >env s" head" >env
187 : pazsan 1.1 .' <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' cr
188 : pazsan 1.2 s" title" tagged cr
189 : pazsan 1.1 -env ;
190 :    
191 :     \ HTML trailer
192 :    
193 :     Variable mail
194 :     Variable mail-name
195 :    
196 :     : .trailer
197 : pazsan 1.2 s" address" >env s" center" >env
198 :     ." Last modified: " time&date rot 0 u.r swap 1-
199 : pazsan 1.1 s" janfebmaraprmayjunjulaugsepoctnovdec" rot 3 * /string 3 min type
200 : pazsan 1.3 0 u.r ." by "
201 :     mail $@ mailto: mail-name $@ s" a" tagged
202 : pazsan 1.1 -envs ;
203 :    
204 :     \ top word
205 :    
206 :     : maintainer
207 :     bl sword mail $! '" parse 2drop '" parse mail-name $! ;
208 :    
209 :     : wf ( -- )
210 :     outfile-id >r
211 :     bl sword r/w create-file throw to outfile-id
212 :     '" parse 2drop '" parse .title
213 :     +env s" body" env
214 :     ['] parse-section catch .trailer
215 :     outfile-id close-file throw
216 :     r> to outfile-id
217 :     dup 0< IF throw ELSE drop THEN ;
218 :    
219 : pazsan 1.2 \ simple text data base
220 :    
221 :     : get-rest ( addr -- ) 0 parse -trailing rot $! ;
222 :    
223 :     Variable last-entry
224 :     Variable field#
225 :    
226 :     : table: ( xt n -- ) Create , , 1 field# !
227 :     DOES> 2@ >in @ >r longtags set-current
228 :     Create definitions swap , r> >in !
229 :     here last-entry !
230 :     dup 0 DO 0 , LOOP
231 :     1 DO s" " last-entry @ I cells + $! LOOP
232 :     last-entry @ get-rest
233 :     DOES> dup cell+ swap perform ;
234 :    
235 :     : field: Create field# @ , 1 field# +!
236 :     DOES> @ cells last-entry @ + get-rest ;
237 : pazsan 1.3
238 :     : >field ' >body @ cells postpone Literal postpone + ; immediate

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help