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

3
4
(** Command line **)

5
let input =
6
  match argv with
7
  | [ s ] -> s
8
  | _ -> raise "Please use --arg to specify an input file on the command line"
9

10
(** Output types **)
11

12
using H = "xhtml"
13

14
15
(** Input types **)

16
type Page =  <page name=String new=?Any>[ <title>String <banner>[InlineText*]? Item* ]
17
type External = <external {|href=String; title=String; name=String |}>[]
18
19

type Item = 
20
   <box title=String link=String>Content
21
22
23
 | <meta>Content
 | <left>Content
 | Page
24
 | External
25

26
type Author = <author>String
27
type Paper = 
28
  <paper file=?String old=?String>[ 
29
     <title>String Author+ <comment>[InlineText*] <abstract>Content ]
30
31

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

type Link =
35
  <link url=String title=String>[ InlineText* ]
36
37

type Content =      
38
   [ ( <p {|style=?String|}>[InlineText*]
39
40
     | <ul {||}>[<li {||}>Content +]
     | <section title=String>Content
41
     | <sample highlight=?"true"|"false">String
42
     | H:Xtable
43
     | Paper | Slides | Link
44
45
     | <boxes-toc sections=?"">[]
     | <pages-toc sections=?"">[]
46
47
     | <site-toc>[]
     | <local-links href=String>[]
48
     | <two-columns>[ <left>Content <right>Content ]
49
     | InlineText
50
     )* ]
51
52
53

type InlineText =
     Char
54
   | <(`b|`i|`tt|`em) {| style=?String |}>[InlineText*]
55
   | <code>String
56
   | <local href=String>String
57
   | <footnote>[InlineText*]
58
   | H:Xa | H:Ximg | H:Xbr 
59

60
61
62
63
(** Generic purpose functions **)

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

64
let load_include (String -> [Any*])
65
 name ->
66
(*   let _ = print [ 'Loading ' !name '... \n' ] in *)
67
   xtransform [ (load_xml name) ] with 
68
   | <include file=(s & String)>[] -> load_include s
69
   | <include-verbatim file=(s & String)>[] -> load_file s 
70

71
(* Highlighting text between {{...}} *)
72

73
let highlight (String -> [ (Char | H:Xvar | H:Xi)* ] )
74
75
 | [ '{{' h ::(Char *?) '}}' ; rest ] -> 
          [ <var class="highlight">h; highlight rest ]
76
 | [ '%%' h ::(Char *?) '%%' ; rest ] ->
77
          [ <i>h; highlight rest ] 
78
 | [ c; rest ] -> [ c; highlight rest ]
79
 | [] -> []
80

81
82
(* Split a comma-separated string *)

83
let split_comma (String -> [String*])
84
 | [ x::(Char*?) ',' ; rest ] -> (x, split_comma rest)
85
 | s -> [ s ]
86
87


88
(** Internal types **)
89

90
type Path = [ { url = String; title = String }* ]
91
type Tree = { name = String; url = String; title = String; 
92
              children = [Tree*] } 
93

94
let url_of_name (String -> String)
95
   "index" -> "/"
96
 | s -> s @ ".html"
97

98
let authors ([Author+] -> String)
99
100
   | [ <author>a ] -> a
   | [ <author>a1 <author>a2 ] -> a1 @ ", and " @ a2
101
   | [ <author>a; rem ] -> a @ ", " @ authors rem
102

103
let find_local_link (sitemap : [Tree*], l : String) : Tree =
104
105
match sitemap with
 | (h,t) ->
106
   if (h . name = l) then h
107
108
109
   else 
    (try find_local_link (t,l) with `Not_found -> 
         find_local_link (h . children,l))
110
 | [] -> raise `Not_found
111

112
let local_link (sitemap : Tree, l : String, txt : String) : H:Inline =
113
114
115
116
 try 
  let h = find_local_link ([sitemap],l)  in
  let txt = if txt = "" then h . title else txt in
  <a href=(h . url)>txt
117
 with `Not_found -> raise [ 'Local link not found: ' !l ]
118
 
119
let compute_sitemap ((Page|External) -> Tree)
120
121
 <page name=name>[ <title>title (c::(Page|External) | _)* ] ->
   let children = map c with p -> compute_sitemap p in
122
   { name = name; url = (url_of_name name); title = title; children =children }
123
|<external name=name href=h title=t>[] ->
124
   { name = name; url = h; title = t; children = [] }
125

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

131

132
let boxes_of  (Page -> [H:Xul?])
133
     <page name=n>[ (items::Item | _)*] ->
134
135
        let toc = 
         transform items with 
136
          <box title=t link=l>_ -> [ <li>[ <a href=((url_of_name n)@('#',l))>t ] ] in
137
138
        (match toc with [] -> [] | lis -> [ <ul>lis ])
 
139
let link_to (Page -> H:Xa)
140
141
142
 <page name=n new=_>[<title>t ; _ ] -> 
               <a href=(url_of_name n)>[!t 
                      <img src="img/new.gif" alt="(new)" style="border:0">[]]
143
| <page name=n>[<title>t ; _ ] -> <a href=(url_of_name n)>t
144

145
let box (x : H:Flow) : H:Block =
146
 <table cellpadding="2" 
147
148
    style="font-size:11px ; font-family:arial,sans-serif;
           border: solid 2px black; background: #ffffff" width="100%">
149
150
   [ <tr> [<td>x] ]

151
let meta (x : H:Flow) : H:Block =
152
153
154
155
156
 <table cellpadding="2" 
    style="border: solid 1px #b0b0b0; background: #e0e0e0; font-size: 80%" 
    width="100%">
   [ <tr> [<td>x] ]

157
let box_title (x : H:Flow, t : String) : H:Block =
158
159
160
161
162
163
164
165
166
167
168
 <table cellpadding="5" 
    style="border: solid 2px black; background: #ffffff" width="100%">
   [ <tr>[ <td style="background: #fff0f0; color: #0000ff; font: bold
100% helvetica">t ] <tr> [<td>x] ]

let style = "
a:link:hover, a:visited:hover {
  text-decoration: none;
  background: #FFFFD0;
  color: #FF0000;
}
169
170
171
a.old, a.old:hover, a.old:visited:hover {
  text-decoration: line-through;
}
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
p {
  text-align: justify;
  margin: 1ex 1em 0 1em;
}
pre {
  margin: 1ex 1em 0 1em;
}
var.highlight {
  color: #FF0000;
}
img.icon {
  border: 0;
}
div.code {
  background: #E0E0E0;
  margin: 0.5ex 0.5em 0 0.5em;
  padding: 0.2ex;
}
div.abstract { 
  font: bold 80% helvetica;
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  background: #F0F0F0;
}
div.abstract p { 
  font: 100% sans-serif;
}
"

201
202
(* Main transformation function *)

203
(* returns the last page of the descendance *)
204
let gen_page (prev : Page|[], page : Page, next : Page|[], 
205
                  path : Path, sitemap : Tree) : (Page|[]) = 
206
match page with
207
208
<page name=name>[ 
        <title>title <banner>banner | <title>(title & banner); items ] ->
209

210
 let footnote_counter = ref Int 0 in 
211
 let footnotes = ref H:Flow [] in
212

213
 let text (t : [InlineText*]) : H:Inlines =
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  transform t with
   | <code>x -> [ <b>[ <tt>(highlight x) ] ]
   | <local href=l>txt -> [ (local_link (sitemap,l,txt)) ]
   | <(tag & (`b|`i|`tt|`em)) (attr)>x -> [ <(tag) (attr)>(text x) ]
(*   | <a href=url>_ & z -> let [] = print [ 'Link: ' !url '\n'] in [z] *)
   | <footnote>c -> 
      footnote_counter := !footnote_counter + 1;
      let n = string_of !footnote_counter in
      let fn = !footnotes in
      footnotes := [];
      let c = <p>[ <a name=[ 'note' !n ]>[] 
                   <a href=[ '#bnote' !n ]>[ '[' !n ']' ]
		   ' ' ; text c ] in
      footnotes := fn @ [ c ] @ !footnotes;
      [ <a name=[ 'bnote' !n ]>[]
        <a href=[ '#note' !n ]>[ '[' !n ']' ] ]
230
   | z -> [ z ] 
231
 in
232

233
 let content (t : Content) : H:Flow =
234
  transform t with
235
   | <section title=title>c -> 
236
         [ <p>[ <b style="color: #008000">title ] !(content c) ]
237
   | <paper (r)>[ <title>tit aut::Author* <comment>com <abstract>ab  ]  ->
238
239
240
241
         [ (match r with
           | { file = f; old = "true" } -> <a class="old" href=f>tit
           | { file = f } -> <a href=f>tit
           | _ -> <b>tit) '. '
242
           !(authors aut) '. '
243
	   !(text com)
244
245
246
           <div class="abstract">[ 'Abstract:' !(content ab) ]
         ]
   | <slides file=f>[ <title>tit aut::Author* <comment>com ]  ->
247
        [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
248
249
   | <sample highlight="false">s ->
        [ <div class="code">[ <pre>s ] ]
250
   | <sample>s ->
251
        [ <div class="code">[ <pre>(highlight s) ] ]
252
   | <link url=url title=title>com -> 
253
254
255
        [ <a href=url>title '. ' !(text com) ]
   | <ul>lis -> 
        [ <ul>(map lis with <li>x -> <li>(content x)) ]
256
   | H:Xtable & x -> 
257
        [ x ]
258
   | <p (attr)>x -> [ <p (attr)>(text x) ]
259
260
   | <pages-toc (a)>[] ->
       let toc = 
261
         transform items with 
262
263
         | Page & p ->  [ <li>[ (link_to p) 
                           !(match a with {|sections=_|} -> (boxes_of p) | _ -> [])] ]
264
         | <external href=l title=t>[] -> [ <li>[ <a href=l>t ] ] in
265
        (match toc with [] -> [] | lis -> [ <ul>lis ])
266
   | <boxes-toc (a)>[] ->
267
268
        let toc = 
         transform items with 
269
270
271
272
273
274
          <box title=t link=l>b -> [ <li>[ <a href=('#',l)>t 
             !(match a with 
                | {|sections=_|} -> 
                     (transform b with <section title=t>_ -> [<br>[] '-' !t])
                | _ ->[])]]
        in (match toc with [] -> [] | lis -> [ <ul>lis ])
275
276
277
278
279
   | <site-toc>[] ->
        [ <ul>[ (display_sitemap sitemap) ] ]
   | <local-links href=s>[] ->
         (match (split_comma s) with
           | [] -> []
280
           | l -> let l = map l with x -> <li>[ (local_link(sitemap,x,"")) ]
281
                  in [ <ul>l ])
282
283
284
285
286
   | <two-columns>[ <left>x <right>y ] ->
	[ <table width="100%">[ 
            <tr>[ 
              <td valign="top">(content x) 
              <td valign="top">(content y) ] ] ]
287
288
   | t -> text [ t ]
 in
289

290
(* Preparing left panel *)
291

292
293
294
295
 let navig = transform items with <left>c -> [ c ] in
 let left = match navig with [] -> [ [<boxes-toc>[]] ] | n -> n in
 let left = 
   <td valign="top" align="left">[
296
     <table cellpadding="0" cellspacing="15" 
297
298
299
            width="200"
            style="font-size:80%; border: 1px dashed black;
                   background: #ffcd72">
300
301
     (map left with x -> <tr>[ <td>[ (box (content x)) ] ]) ] in

302
 let dpath : H:Inlines = transform path with 
303
  | { url = f; title = t } -> [ <a href=f>t ': '] 
304
 in
305
 let npath = path @ [ { url = (url_of_name name); title = title } ] in
306
 let subpages = transform items with p & Page -> [ p ] in
307
 let (next,last) = gen_page_seq (page, subpages, next, npath, sitemap) in
308
309
310
 let next = match next with [] -> [] 
   | <page name=n>[ <title>t; _ ] -> 
      [ <a href=(url_of_name n)>[ 
311
          <img width="16" height="16" class="icon" alt="Next page:"
312
               src="img/right.gif">[]
313
314
315
316
317
          ' ' !t
        ] ] in
 let prev = match prev with [] -> [] 
   | <page name=n>[ <title>t; _ ] -> 
      [ <a href=(url_of_name n)>[ 
318
          <img width="16" height="16" class="icon"
319
               alt="Previous page:" src="img/left.gif">[]
320
321
          ' ' !t
        ] ] in
322
 let navig = 
323
   if prev = [] then [] else
324
   [ (box [
325
     <p>[ !dpath !title ]
326
327
328
329
330
331
332
333
334
335
336
     <p>[ !prev ' ' !next ] ]) ] in

(* Preparing main panel *)
 let main = transform items with
  | <box (r)>c ->
      let b = [
         <a name=(r . link)>[] 
         !(content c) ] in
      [ (box_title (b,r . title)) ]
 | <meta>c -> [ (meta (content c)) ]
 in
337
338
339
340
 let notes = match !footnotes with
   | [] -> []
   | n -> [ (meta n) ] in
 let main = match (navig @ main @ notes @ navig) with
341
342
343
   | [] -> raise "Empty page !"
   | x -> x in

344
 let right : H:Xtd =
345
346
347
348
349
350
351
352
353
354
355
  <td valign="top" align="left" style="width:100%">[
   <table width="100%">[
    <tr>[ <td valign="top" align="left"
              style="border: 2px solid black; background: #ffffff;
text-align:center; color: #aa0000; font: bold 200% helvetica" >
          (text banner)
        ]
 
    <tr>[
      <td valign="top" align="left"
          style="border: 1px solid black; background: #fccead">[
356
       <table width="100%" cellpadding="0" cellspacing="17">
357
358
359
360
         (map main with x -> <tr>[ <td>[x] ])
      ] ]
  ] ] in

361
 let html : H:Xhtml =
362
363
 <html>[
  <head>[ 
364
   <title>[ 'CDuce: ' !title ]
365
   <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">[]
366
   <style type="text/css">style
367
  ]
368
369
370
371
  <body style="margin: 0; padding : 0; background: #fcb333">[ 
   <table cellspacing="10" cellpadding="0" width="100%" border="0">[
    <tr>[ left right ]
   ]
372
  ]
373
374
 ]
 in
375
 let txt : Latin1 = 
376
   [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
377
     '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
378
379
     !(print_xml html) ] in
 let fn = "www/" @ name @ ".html" in
380
 dump_to_file fn txt;
381
 last
382
	
383

384
let gen_page_seq 
385
 (prev : Page|[], pages : [Page*], next : Page|[], 
386
  path : Path, sitemap : Tree) : (Page|[], Page|[]) =
387
388
 match pages with
 | [ p1 p2 ; _ ] & [ _; rest ] -> 
389
390
391
     let last = gen_page (prev,p1,p2, path, sitemap) in
     let (_,last)  = gen_page_seq (last, rest, next, path, sitemap) in 
     (p1,last)
392
 | [ p ] ->
393
     let last = gen_page (prev,p,next, path, sitemap) in (p,last)
394
 | [] -> (next,prev)
395

396

397
;;
398

399
match load_include input with
400
 | [ Page & p ] -> let _ = gen_page ([],p,[], [], compute_sitemap p) in []
401
 | _ -> raise ("Invalid input document " @ input)
402

403