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

Contents of /website/trunk/web/site.cd

Parent Directory Parent Directory | Revision Log Revision Log


Revision 370 - (hide annotations)
Tue Jul 10 17:29:11 2007 UTC (5 years, 10 months ago) by abate
Original Path: web/site.cd
File size: 9197 byte(s)
[r2003-05-18 20:07:55 by cvscast] Preparation for a public release

Original author: cvscast
Date: 2003-05-18 20:07:56+00:00
1 abate 284 (* This CDuce script produces CDuce web site. *)
2    
3 abate 369 (** 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 abate 343 (** Output types **)
13 abate 284
14 abate 258 include "xhtml-strict.cd";; (* XHTML 1 Strict DTD *)
15     include "xhtml-categ.cd";; (* Categories (Inline, ...) from this DTD *)
16 abate 250
17 abate 336
18 abate 343 (** 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 abate 344 | <sample highlight=?"true"|"false">String
46 abate 343 | Xtable
47     | Paper | Slides | Link
48     | <boxes-toc>[]
49     | <pages-toc>[]
50     | <site-toc>[]
51     | <local-links href=String>[]
52 abate 356 | <two-columns>[ <left>Content <right>Content ]
53 abate 343 | InlineText
54     )* ];;
55    
56     type InlineText =
57     Char
58     | <(`b|`i|`tt|`em) {||}>[InlineText*]
59     | <code>String
60 abate 347 | <local href=String>String
61 abate 343 | Xa | Ximg | Xbr ;;
62    
63    
64     (** Generic purpose functions **)
65    
66     (* Recursive inclusion of XML files and verbatim text files *)
67    
68 abate 336 let fun load_include (String -> [Any*])
69     name ->
70 abate 368 (* let _ = print [ 'Loading ' !name '... \n' ] in *)
71 abate 336 xtransform [ (load_xml name) ] with
72 abate 341 | <include file=(s & String)>[] -> load_include s
73     | <include-verbatim file=(s & String)>[] -> load_file s;;
74 abate 336
75 abate 343 (* Highlighting text between {{...}} *)
76 abate 336
77 abate 370 let fun highlight (String -> [ (Char | Xvar | Xi)* ] )
78 abate 340 | [ '{{' h ::(Char *?) '}}' ; rest ] ->
79     [ <var class="highlight">h; highlight rest ]
80 abate 370 | [ '{/{' h ::(Char *?) '}}' ; rest ] ->
81     [ <i>h; highlight rest ]
82 abate 340 | [ c; rest ] -> [ c; highlight rest ]
83 abate 336 | [] -> [];;
84    
85 abate 343 (* Split a comma-separated string *)
86    
87 abate 341 let fun split_comma (String -> [String*])
88     | [ x::(Char*?) ',' ; rest ] -> (x, split_comma rest)
89     | s -> [ s ];;
90 abate 336
91 abate 253
92 abate 343 (** 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 abate 341
97 abate 369 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 abate 341 let php_css : String =
104 abate 369 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 abate 341 }
111 abate 369 else { echo "' !(protect_quote css) '"; }
112     ?> ']
113     else css;;
114 abate 341
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 abate 343 (** Internal types **)
123 abate 341
124     type Path = [ { url = String; title = String }* ];;
125     type Tree = { name = String; url = String; title = String;
126     children = [Tree*] } ;;
127 abate 250
128 abate 346 let fun url_of_name (String -> String)
129     "index" -> "/"
130     | s -> s @ ".html";;
131    
132 abate 250 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 abate 347 let fun find_local_link (sitemap : [Tree*], l : String) : Tree =
138 abate 341 match sitemap with
139     | (h,t) ->
140 abate 347 if (h . name = l) then h
141 abate 341 else
142     (try find_local_link (t,l) with `Not_found ->
143     find_local_link (h . children,l))
144     | [] -> raise `Not_found;;
145    
146 abate 347 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 abate 341 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 abate 346 { name = name; url = url_of_name name; title = title; children =children }
157 abate 341 |<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 abate 351 let fun link_to (Page -> Xa)
166     <page name=n>[<title>t ; _ ] -> <a href=(url_of_name n)>t;;
167    
168 abate 343 (* Main transformation function *)
169    
170 abate 351 let fun gen_page (prev : Page|[], page : Page, next : Page|[],
171     path : Path, sitemap : Tree) : [] =
172 abate 341 match page with
173     <page name=name>[ <title>title <banner>banner ; items ] ->
174    
175     let fun text (t : [InlineText*]) : Inlines =
176 abate 284 map t with
177 abate 341 | <code>x -> <b>[ <tt>(highlight x) ]
178 abate 347 | <local href=l>txt -> local_link (sitemap,l,txt)
179 abate 336 | <(tag & (`b|`i|`tt|`em))>x -> <(tag)>(text x)
180 abate 368 (* | <a href=url>_ & z -> let [] = print [ 'Link: ' !url '\n'] in z *)
181 abate 341 | z -> z
182     in
183 abate 254
184 abate 341 let fun content (t : Content) : Flow =
185 abate 250 transform t with
186 abate 284 | <section title=title>c ->
187     [ <h4>title !(content c) ]
188 abate 250 | <paper (r)>[ <title>tit aut::Author* <comment>com <abstract>ab ] ->
189 abate 284 [ (match r with { file = f } -> <a href=f>tit | _ -> <b>tit) '. '
190 abate 250 !(authors aut) '. '
191 abate 254 !(text com)
192 abate 250 <div class="abstract">[ 'Abstract:' !(content ab) ]
193     ]
194     | <slides file=f>[ <title>tit aut::Author* <comment>com ] ->
195 abate 254 [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
196 abate 344 | <sample highlight="false">s ->
197     [ <div class="code">[ <pre>s ] ]
198 abate 336 | <sample>s ->
199 abate 340 [ <div class="code">[ <pre>(highlight s) ] ]
200 abate 284 | <link url=url; title=title>com ->
201     [ <a href=url>title '. ' !(text com) ]
202     | <ul>lis ->
203     [ <ul>(map lis with <li>x -> <li>(content x)) ]
204     | Xtable & x ->
205     [ x ]
206 abate 254 | <p>x -> [ <p>(text x) ]
207 abate 341 | <pages-toc>[] ->
208     let toc =
209     transform items with
210 abate 351 | Page & p -> [ <li>[ (link_to p) ] ]
211 abate 341 | <external href=l; title=t>[] -> [ <li>[ <a href=l>t ] ] in
212     (match toc with [] -> [] | lis -> [ <ul>lis ])
213     | <boxes-toc>[] ->
214     let toc =
215     transform items with
216     <box title=t; link=l>_ -> [ <li>[ <a href=('#',l)>t ] ] in
217     (match toc with [] -> [] | lis -> [ <ul>lis ])
218     | <site-toc>[] ->
219     [ <ul>[ (display_sitemap sitemap) ] ]
220     | <local-links href=s>[] ->
221     (match (split_comma s) with
222     | [] -> []
223 abate 347 | l -> let l = map l with x -> <li>[ (local_link(sitemap,x,"")) ]
224 abate 341 in [ <ul>l ])
225 abate 356 | <two-columns>[ <left>x <right>y ] ->
226     [ <table width="100%">[
227     <tr>[
228     <td valign="top">(content x)
229     <td valign="top">(content y) ] ] ]
230 abate 341 | t -> text [ t ]
231     in
232 abate 250
233 abate 341 let main : Flow = transform items with
234     | <box (r)>c ->
235     [ <div class="box">[
236     <h2>(r . title)
237     !(match r with { subtitle = t } -> [<b>t] | _ -> [])
238     <a name=r . link>[]
239     !(content c) ] ]
240     | <meta>c -> [ <div class="meta">(content c) ]
241 abate 250 in
242 abate 341 let navig : Flow = transform items with
243     | <left>c -> [<div class="box">(content c)]
244     in
245     let dpath : Inlines = transform path with
246     | { url = f; title = t } -> [ <a href=f>t ' :: ']
247     in
248 abate 351 let npath = path @ [ { url = url_of_name name; title = title } ] in
249     let subpages = transform items with p & Page -> [ p ] in
250     let next = gen_page_seq (page, subpages, next, npath, sitemap) in
251     let next = match next with [] -> [] | p -> [' Next : ' (link_to p)] in
252     let prev = match prev with [] -> [] | p -> [' Prev : ' (link_to p)] in
253 abate 341 let html : Xhtml =
254 abate 250 <html>[
255     <head>[
256 abate 350 <title>[ 'CDuce: ' !title ]
257 abate 250 <meta content="text/html; charset=iso-8859-1"; http-equiv="Content-Type">[]
258 abate 284 <meta content="css">[] (* Placeholder for PHP code *)
259 abate 250 ]
260     <body>[
261 abate 351 <div class="title">[
262     <h1>(text banner)
263     <p>[ <b>"You're here: " !dpath !title ]
264     <p>[ !prev !next ]
265     ]
266 abate 250 <div id="Sidelog">navig
267 abate 341 <div id="Content">main
268 abate 250 ]
269 abate 341 ]
270     in
271     let txt : String =
272 abate 351 [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
273 abate 341 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
274     !(patch_css (print_xml html)) ] in
275 abate 370 dump_to_file (name @ (if php then ".html.php" else ".html")) txt;;
276 abate 250
277 abate 351 let fun gen_page_seq
278     (prev : Page|[], pages : [Page*], next : Page|[],
279     path : Path, sitemap : Tree) : Page|[] =
280     match pages with
281     | [ p1 p2 ; _ ] & [ _; rest ] ->
282     let [] = gen_page (prev,p1,p2, path, sitemap) in
283     let _ = gen_page_seq (p1, rest, next, path, sitemap) in p1
284     | [ p ] ->
285     let [] = gen_page (prev,p,next, path, sitemap) in p
286     | [] -> next;;
287    
288    
289 abate 343 (* Entry point *)
290 abate 284
291 abate 369 match load_include input with
292     | [ Page & p ] -> gen_page ([],p,[], [], compute_sitemap p)
293     | _ -> raise ("Invalid input document " @ input);;
294    

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