/[svn]/web/site.cd
ViewVC logotype

Contents of /web/site.cd

Parent Directory Parent Directory | Revision Log Revision Log


Revision 386 - (show annotations)
Tue Jul 10 17:30:43 2007 UTC (5 years, 10 months ago) by abate
File size: 9519 byte(s)
[r2003-05-21 21:43:26 by cvscast] Bugs in print_xml and site.cd

Original author: cvscast
Date: 2003-05-21 21:43:26+00:00
1 (* This CDuce script produces CDuce web site. *)
2
3 (** Command line **)
4
5 let (input, php) =
6 match argv with
7 | [ "-php" s ] -> (s, `true)
8 | [ s ] -> (s, `false)
9 | _ -> raise "Please specify an input file on the command line";;
10
11
12 (** Output types **)
13
14 include "xhtml-strict.cd";; (* XHTML 1 Strict DTD *)
15 include "xhtml-categ.cd";; (* Categories (Inline, ...) from this DTD *)
16
17
18 (** Input types **)
19
20 type Page = <page name=String>[ <title>String <banner>[InlineText*]? Item* ];;
21 type External = <external {|href=String; title=String; name=String |}>[];;
22
23 type Item =
24 <box title=String; subtitle=?String; link=String>Content
25 | <meta>Content
26 | <left>Content
27 | Page
28 | External;;
29
30 type Author = <author>String;;
31 type Paper =
32 <paper file=?String>[
33 <title>String Author+ <comment>[InlineText*] <abstract>Content ];;
34
35 type Slides =
36 <slides file=String>[ <title>String Author+ <comment>[InlineText*] ];;
37
38 type Link =
39 <link url=String; title=String>[ InlineText* ];;
40
41 type Content =
42 [ ( <p {||}>[InlineText*]
43 | <ul {||}>[<li {||}>Content +]
44 | <section title=String>Content
45 | <sample highlight=?"true"|"false">String
46 | Xtable
47 | Paper | Slides | Link
48 | <boxes-toc>[]
49 | <pages-toc>[]
50 | <site-toc>[]
51 | <local-links href=String>[]
52 | <two-columns>[ <left>Content <right>Content ]
53 | InlineText
54 )* ];;
55
56 type InlineText =
57 Char
58 | <(`b|`i|`tt|`em) {||}>[InlineText*]
59 | <code>String
60 | <local href=String>String
61 | Xa | Ximg | Xbr ;;
62
63
64 (** Generic purpose functions **)
65
66 (* Recursive inclusion of XML files and verbatim text files *)
67
68 let fun load_include (String -> [Any*])
69 name ->
70 (* let _ = print [ 'Loading ' !name '... \n' ] in *)
71 xtransform [ (load_xml name) ] with
72 | <include file=(s & String)>[] -> load_include s
73 | <include-verbatim file=(s & String)>[] -> let [] = print s in load_file s;;
74
75 (* Highlighting text between {{...}} *)
76
77 let fun highlight (String -> [ (Char | Xvar | Xi)* ] )
78 | [ '{{' h ::(Char *?) '}}' ; rest ] ->
79 [ <var class="highlight">h; highlight rest ]
80 | [ '%%' h ::(Char *?) '%%' ; rest ] ->
81 [ <i>h; highlight rest ]
82 | [ c; rest ] -> [ c; highlight rest ]
83 | [] -> [];;
84
85 (* Split a comma-separated string *)
86
87 let fun split_comma (String -> [String*])
88 | [ x::(Char*?) ',' ; rest ] -> (x, split_comma rest)
89 | s -> [ s ];;
90
91
92 (** Ugly hack to introduce PHP code ...
93 The idea is to produce first an XML document with a distinguished element.
94 The function patch_css search for the textual representation of this
95 element and replace it with the PHP code. **)
96
97 let css : String =
98 ['<link rel="stylesheet" href="cduce.css" type="text/css">'];;
99
100 let fun protect_quote (s : String) : String =
101 transform s with '"' -> [ '\\"' ] | c -> [c];;
102
103 let php_css : String =
104 if php then
105 [' <?php $browser = getenv("HTTP_USER_AGENT");
106 if (preg_match("/Mozilla/i", "$browser") && !preg_match("/Mozilla\\/5.0/i", "$browser"))
107 {
108 echo "<blink>For better presentation use a more recent version of
109 your browser, like Netscape 6</blink>";
110 }
111 else { echo "' !(protect_quote css) '"; }
112 ?> ']
113 else css;;
114
115
116 let fun patch_css (String -> String)
117 | [ a::_*? '<meta content="css"/>'; rem ] -> a @ php_css @ rem
118 | s -> s;;
119
120
121
122 (** Internal types **)
123
124 type Path = [ { url = String; title = String }* ];;
125 type Tree = { name = String; url = String; title = String;
126 children = [Tree*] } ;;
127
128 let fun url_of_name (String -> String)
129 "index" -> "/"
130 | s -> s @ ".html";;
131
132 let fun authors ([Author+] -> String)
133 | [ <author>a ] -> a
134 | [ <author>a1 <author>a2 ] -> a1 @ ", and " @ a2
135 | [ <author>a; rem ] -> a @ ", " @ authors rem;;
136
137 let fun find_local_link (sitemap : [Tree*], l : String) : Tree =
138 match sitemap with
139 | (h,t) ->
140 if (h . name = l) then h
141 else
142 (try find_local_link (t,l) with `Not_found ->
143 find_local_link (h . children,l))
144 | [] -> raise `Not_found;;
145
146 let fun local_link (sitemap : Tree, l : String, txt : String) : Inline =
147 try
148 let h = find_local_link ([sitemap],l) in
149 let txt = if txt = "" then h . title else txt in
150 <a href=(h . url)>txt
151 with `Not_found -> raise [ 'Local link not found: ' !l ];;
152
153 let fun compute_sitemap ((Page|External) -> Tree)
154 <page name=name>[ <title>title (c::(Page|External) | _)* ] ->
155 let children = map c with p -> compute_sitemap p in
156 { name = name; url = url_of_name name; title = title; children =children }
157 |<external name=name; href=h; title=t>[] ->
158 { name = name; url = h; title = t; children = [] };;
159
160 let fun display_sitemap (h : Tree) : Xli =
161 let ch = map h . children with x -> display_sitemap x in
162 let ch = match ch with [] -> [] | l -> [ <ul>l ] in
163 <li>[ <a href=(h . url)>(h . title); ch ];;
164
165 let fun link_to (Page -> Xa)
166 <page name=n>[<title>t ; _ ] -> <a href=(url_of_name n)>t;;
167
168 (* Main transformation function *)
169
170 (* returns the last page of the descendance *)
171 let fun gen_page (prev : Page|[], page : Page, next : Page|[],
172 path : Path, sitemap : Tree) : (Page|[]) =
173 match page with
174 <page name=name>[
175 <title>title <banner>banner | <title>(title & banner); items ] ->
176
177 let fun text (t : [InlineText*]) : Inlines =
178 map t with
179 | <code>x -> <b>[ <tt>(highlight x) ]
180 | <local href=l>txt -> local_link (sitemap,l,txt)
181 | <(tag & (`b|`i|`tt|`em))>x -> <(tag)>(text x)
182 (* | <a href=url>_ & z -> let [] = print [ 'Link: ' !url '\n'] in z *)
183 | z -> z
184 in
185
186 let fun content (t : Content) : Flow =
187 transform t with
188 | <section title=title>c ->
189 [ <h4>title !(content c) ]
190 | <paper (r)>[ <title>tit aut::Author* <comment>com <abstract>ab ] ->
191 [ (match r with { file = f } -> <a href=f>tit | _ -> <b>tit) '. '
192 !(authors aut) '. '
193 !(text com)
194 <div class="abstract">[ 'Abstract:' !(content ab) ]
195 ]
196 | <slides file=f>[ <title>tit aut::Author* <comment>com ] ->
197 [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
198 | <sample highlight="false">s ->
199 [ <div class="code">[ <pre>s ] ]
200 | <sample>s ->
201 [ <div class="code">[ <pre>(highlight s) ] ]
202 | <link url=url; title=title>com ->
203 [ <a href=url>title '. ' !(text com) ]
204 | <ul>lis ->
205 [ <ul>(map lis with <li>x -> <li>(content x)) ]
206 | Xtable & x ->
207 [ x ]
208 | <p>x -> [ <p>(text x) ]
209 | <pages-toc>[] ->
210 let toc =
211 transform items with
212 | Page & p -> [ <li>[ (link_to p) ] ]
213 | <external href=l; title=t>[] -> [ <li>[ <a href=l>t ] ] in
214 (match toc with [] -> [] | lis -> [ <ul>lis ])
215 | <boxes-toc>[] ->
216 let toc =
217 transform items with
218 <box title=t; link=l>_ -> [ <li>[ <a href=('#',l)>t ] ] in
219 (match toc with [] -> [] | lis -> [ <ul>lis ])
220 | <site-toc>[] ->
221 [ <ul>[ (display_sitemap sitemap) ] ]
222 | <local-links href=s>[] ->
223 (match (split_comma s) with
224 | [] -> []
225 | l -> let l = map l with x -> <li>[ (local_link(sitemap,x,"")) ]
226 in [ <ul>l ])
227 | <two-columns>[ <left>x <right>y ] ->
228 [ <table width="100%">[
229 <tr>[
230 <td valign="top">(content x)
231 <td valign="top">(content y) ] ] ]
232 | t -> text [ t ]
233 in
234
235 let main : Flow = transform items with
236 | <box (r)>c ->
237 [ <div class="box">[
238 <h2>(r . title)
239 !(match r with { subtitle = t } -> [<b>t] | _ -> [])
240 <a name=r . link>[]
241 !(content c) ] ]
242 | <meta>c -> [ <div class="meta">(content c) ]
243 in
244 let navig : Flow = transform items with
245 | <left>c -> [<div class="box">(content c)]
246 in
247 let navig = match navig with
248 | [] -> [<div class="box">(content [<boxes-toc>[]])]
249 | n -> n in
250 let dpath : Inlines = transform path with
251 | { url = f; title = t } -> [ <a href=f>t ' :: ']
252 in
253 let npath = path @ [ { url = url_of_name name; title = title } ] in
254 let subpages = transform items with p & Page -> [ p ] in
255 let (next,last) = gen_page_seq (page, subpages, next, npath, sitemap) in
256 let next = match next with [] -> [] | p -> [' Next : ' (link_to p)] in
257 let prev = match prev with [] -> [] | p -> [' Prev : ' (link_to p)] in
258 let html : Xhtml =
259 <html>[
260 <head>[
261 <title>[ 'CDuce: ' !title ]
262 <meta content="text/html; charset=iso-8859-1"; http-equiv="Content-Type">[]
263 <meta content="css">[] (* Placeholder for PHP code *)
264 ]
265 <body>[
266 <div class="title">[
267 <h1>(text banner)
268 <p>[ <b>"You're here: " !dpath !title ]
269 <p>[ !prev !next ]
270 ]
271 <div id="Sidelog">navig
272 <div id="Content">main
273 ]
274 ]
275 in
276 let txt : String =
277 [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
278 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
279 !(patch_css (print_xml html)) ] in
280 let fn = "www/" @ name @ (if php then ".html.php" else ".html") in
281 let [] = dump_to_file fn txt in
282 last;;
283
284
285 let fun gen_page_seq
286 (prev : Page|[], pages : [Page*], next : Page|[],
287 path : Path, sitemap : Tree) : (Page|[], Page|[]) =
288 match pages with
289 | [ p1 p2 ; _ ] & [ _; rest ] ->
290 let last = gen_page (prev,p1,p2, path, sitemap) in
291 let (_,last) = gen_page_seq (last, rest, next, path, sitemap) in
292 (p1,last)
293 | [ p ] ->
294 let last = gen_page (prev,p,next, path, sitemap) in (p,last)
295 | [] -> (next,prev);;
296
297
298 (* Entry point *)
299
300 match load_include input with
301 | [ Page & p ] -> let _ = gen_page ([],p,[], [], compute_sitemap p) in []
302 | _ -> raise ("Invalid input document " @ input);;
303

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5