site.cd 9.3 KB
Newer Older
1
2
(* This CDuce script produces CDuce web site. *)

3
4
5
6
7
8
9
10
11
(** Command line **)

let (input, php) =
  match argv with
  | [ "-php" s ] -> (s, `true)
  | [ s ] -> (s, `false)
  | _ -> raise "Please specify an input file on the command line";;


12
(** Output types **)
13

14
15
include "xhtml-strict.cd";;  (* XHTML 1 Strict DTD *)
include "xhtml-categ.cd";;   (* Categories (Inline, ...) from this DTD *)
16

17

18
19
(** Input types **)

20
type Page =  <page name=String>[ <title>String <banner>[InlineText*]? Item* ];;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
type External = <external {|href=String; title=String; name=String |}>[];;

type Item = 
   <box title=String; subtitle=?String; link=String>Content
 | <meta>Content
 | <left>Content
 | Page
 | External;;

type Author = <author>String;;
type Paper = 
  <paper file=?String>[ 
     <title>String Author+ <comment>[InlineText*] <abstract>Content ];;

type Slides = 
  <slides file=String>[ <title>String Author+ <comment>[InlineText*] ];;

type Link =
  <link url=String; title=String>[ InlineText* ];;

type Content =      
   [ ( <p {||}>[InlineText*]
     | <ul {||}>[<li {||}>Content +]
     | <section title=String>Content
45
     | <sample highlight=?"true"|"false">String
46
47
48
49
50
51
     | Xtable
     | Paper | Slides | Link
     | <boxes-toc>[]
     | <pages-toc>[]
     | <site-toc>[]
     | <local-links href=String>[]
52
     | <two-columns>[ <left>Content <right>Content ]
53
54
55
56
57
58
59
     | InlineText
     )* ];;

type InlineText =
     Char
   | <(`b|`i|`tt|`em) {||}>[InlineText*]
   | <code>String
60
   | <local href=String>String
61
62
63
64
65
66
67
   | Xa | Ximg | Xbr ;;


(** Generic purpose functions **)

(* Recursive inclusion of XML files and verbatim text files *)

68
69
let fun load_include (String -> [Any*])
 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
79
 | [ '{{' h ::(Char *?) '}}' ; rest ] -> 
          [ <var class="highlight">h; highlight rest ]
80
 | [ '%%' h ::(Char *?) '%%' ; rest ] ->
81
          [ <i>h; highlight rest ]
82
 | [ c; rest ] -> [ c; highlight rest ]
83
84
 | [] -> [];;

85
86
(* Split a comma-separated string *)

87
88
89
90
91
let fun split_comma (String -> [String*])
 | [ x::(Char*?) ',' ; rest ] -> (x, split_comma rest)
 | s -> [ s ];;


92
93
94
95
(** Ugly hack to introduce PHP code ...
    The idea is to produce first an XML document with a distinguished element.
    The function patch_css search for the textual representation of this
    element and replace it with the PHP code. **)
96

97
98
99
100
101
102
let css : String = 
  ['<link rel="stylesheet" href="cduce.css" type="text/css">'];;

let fun protect_quote (s : String) : String =
  transform s with '"' -> [ '\\"' ] | c -> [c];;

103
let php_css : String =
104
105
106
107
108
109
if php then
[' <?php $browser = getenv("HTTP_USER_AGENT");
if (preg_match("/Mozilla/i", "$browser") && !preg_match("/Mozilla\\/5.0/i", "$browser")) 
{ 
  echo "<blink>For better presentation use a more recent version of
your browser, like Netscape 6</blink>"; 
110
}
111
112
113
else { echo "' !(protect_quote css) '"; }
?> ']
else css;;
114
115
116
117
118
119
120
121


let fun patch_css (String -> String)
| [ a::_*? '<meta content="css"/>'; rem ] -> a @ php_css @ rem
| s -> s;;



122
(** Internal types **)
123

124
125
126
type Path = [ { url = String; title = String }* ];;
type Tree = { name = String; url = String; title = String; 
              children = [Tree*] } ;;
127

128
129
130
131
let fun url_of_name (String -> String)
   "index" -> "/"
 | s -> s @ ".html";;

132
133
134
135
136
let fun authors ([Author+] -> String)
   | [ <author>a ] -> a
   | [ <author>a1 <author>a2 ] -> a1 @ ", and " @ a2
   | [ <author>a; rem ] -> a @ ", " @ authors rem;;

137
let fun find_local_link (sitemap : [Tree*], l : String) : Tree =
138
139
match sitemap with
 | (h,t) ->
140
   if (h . name = l) then h
141
142
143
144
145
   else 
    (try find_local_link (t,l) with `Not_found -> 
         find_local_link (h . children,l))
 | [] -> raise `Not_found;;

146
147
148
149
150
let fun local_link (sitemap : Tree, l : String, txt : String) : Inline =
 try 
  let h = find_local_link ([sitemap],l)  in
  let txt = if txt = "" then h . title else txt in
  <a href=(h . url)>txt
151
152
153
154
155
 with `Not_found -> raise [ 'Local link not found: ' !l ];;
 
let fun compute_sitemap ((Page|External) -> Tree)
 <page name=name>[ <title>title (c::(Page|External) | _)* ] ->
   let children = map c with p -> compute_sitemap p in
156
   { name = name; url = url_of_name name; title = title; children =children }
157
158
159
160
161
162
163
164
|<external name=name; href=h; title=t>[] ->
   { name = name; url = h; title = t; children = [] };;

let fun display_sitemap (h : Tree) :  Xli =
  let ch = map h . children with x -> display_sitemap x in
  let ch = match ch with [] -> [] | l -> [ <ul>l ] in
  <li>[ <a href=(h . url)>(h . title); ch ];;

165
166
167
let fun link_to (Page -> Xa)
 <page name=n>[<title>t ; _ ] -> <a href=(url_of_name n)>t;;

168
169
(* Main transformation function *)

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
175
<page name=name>[ 
        <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
184
   | z -> z
 in
185

186
 let fun content (t : Content) : Flow =
187
  transform t with
188
189
   | <section title=title>c -> 
         [ <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
195
196
           <div class="abstract">[ 'Abstract:' !(content ab) ]
         ]
   | <slides file=f>[ <title>tit aut::Author* <comment>com ]  ->
197
        [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
198
199
   | <sample highlight="false">s ->
        [ <div class="code">[ <pre>s ] ]
200
   | <sample>s ->
201
        [ <div class="code">[ <pre>(highlight s) ] ]
202
203
204
205
206
207
   | <link url=url; title=title>com -> 
        [ <a href=url>title '. ' !(text com) ]
   | <ul>lis -> 
        [ <ul>(map lis with <li>x -> <li>(content x)) ]
   | Xtable & x -> 
        [ x ]
208
   | <p>x -> [ <p>(text x) ]
209
210
211
   | <pages-toc>[] ->
        let toc = 
         transform items with 
212
         | Page & p ->  [ <li>[ (link_to p) ] ]
213
214
215
216
217
218
219
220
221
222
223
224
         | <external href=l; title=t>[] -> [ <li>[ <a href=l>t ] ] in
        (match toc with [] -> [] | lis -> [ <ul>lis ])
   | <boxes-toc>[] ->
        let toc = 
         transform items with 
          <box title=t; link=l>_ -> [ <li>[ <a href=('#',l)>t ] ] in
        (match toc with [] -> [] | lis -> [ <ul>lis ])
   | <site-toc>[] ->
        [ <ul>[ (display_sitemap sitemap) ] ]
   | <local-links href=s>[] ->
         (match (split_comma s) with
           | [] -> []
225
           | l -> let l = map l with x -> <li>[ (local_link(sitemap,x,"")) ]
226
                  in [ <ul>l ])
227
228
229
230
231
   | <two-columns>[ <left>x <right>y ] ->
	[ <table width="100%">[ 
            <tr>[ 
              <td valign="top">(content x) 
              <td valign="top">(content y) ] ] ]
232
233
   | t -> text [ t ]
 in
234

235
236
237
238
239
240
241
242
243
244
245
246
 let main : Flow = transform items with
  | <box (r)>c ->
     [ <div class="box">[
         <h2>(r . title)
          !(match r with { subtitle = t } -> [<b>t] | _ -> [])
         <a name=r . link>[] 
         !(content c)  ] ]
  | <meta>c -> [ <div class="meta">(content c) ] 
 in
 let navig : Flow = transform items with 
  | <left>c -> [<div class="box">(content c)] 
 in
247
248
249
 let navig = match navig with
  | [] -> [<div class="box">(content [<boxes-toc>[]])]
  | n -> n in
250
251
 let dpath : Inlines = transform path with 
  | { url = f; title = t } -> [ <a href=f>t ' :: '] 
252
 in
253
254
 let npath = path @ [ { url = url_of_name name; title = title } ] in
 let subpages = transform items with p & Page -> [ p ] in
255
 let (next,last) = gen_page_seq (page, subpages, next, npath, sitemap) in
256
257
 let next = match next with [] -> [] | p -> [' Next : ' (link_to p)] in
 let prev = match prev with [] -> [] | p -> [' Prev : ' (link_to p)] in
258
 let html : Xhtml =
259
260
 <html>[
  <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
267
268
269
270
   <div class="title">[ 
        <h1>(text banner) 
        <p>[ <b>"You're here: " !dpath !title ]
        <p>[ !prev !next ]
   ]
271
   <div id="Sidelog">navig
272
   <div id="Content">main
273
  ]
274
275
276
 ]
 in
 let txt : String = 
277
   [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
278
279
     '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
     !(patch_css (print_xml html)) ] in
280
281
282
283
 let fn = "www/" @ name @ (if php then ".html.php" else ".html") in
 let [] = dump_to_file fn txt in
 last;;
	
284
285
286

let fun gen_page_seq 
 (prev : Page|[], pages : [Page*], next : Page|[], 
287
  path : Path, sitemap : Tree) : (Page|[], Page|[]) =
288
289
 match pages with
 | [ p1 p2 ; _ ] & [ _; rest ] -> 
290
291
292
     let last = gen_page (prev,p1,p2, path, sitemap) in
     let (_,last)  = gen_page_seq (last, rest, next, path, sitemap) in 
     (p1,last)
293
 | [ p ] ->
294
295
     let last = gen_page (prev,p,next, path, sitemap) in (p,last)
 | [] -> (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
303
 | _ -> raise ("Invalid input document " @ input);;