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

3
4
5
6
(* The types *)

include "siteTypes.cd";;

7
8
(** Command line **)

9
let (input,outdir) =
10
  match argv [] with
11
  | [ s ("-o" o | /(o := "www")) ] -> (s,o)
12
  | _ -> raise "Please use --arg to specify an input file on the command line"
13

14
15
16
17
(** Generic purpose functions **)

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

18
let load_include (Latin1 -> [Any*])
19
 name ->
20
(*   let _ = print [ 'Loading ' !name '... \n' ] in  *)
21
   xtransform [ (load_xml name) ] with 
22
23
   | <include file=(s & Latin1)>[] -> load_include s
   | <include-verbatim file=(s & Latin1)>[] -> load_file s 
24
25
26
27
   | <include-forest file=(s & Latin1)>[] -> 
         match load_xml ("string:<fake>"@(load_file s)@"</fake>") with
            <fake> x -> x | _ -> raise "Uhh?"

28

29
30
(* Loading *)

31
32
33
let [<site>[ <title>site 
             (<header>header | /(header:=[])) 
             (<footer>footer | /(footer:=[])) 
34
             extra_head::H.script*
35
             main_page ] ] = 
36
 try (load_include input :? [ Site ])
37
 with err & Latin1 -> 
38
   print ['Invalid input document:\n' !err '\n']; 
39
40
   exit 2 

41
(* Highlighting text between {{...}} *)
42

43
let highlight (String -> [ (Char | H.strong | H.i)* ] )
44
 | [ '{{ON}}'; rest ] -> xhighlight rest
45
46
 | [ '{{%%' h ::(Char *?) '%%}}' ; rest ] -> 
          [ <strong class="highlight">[<i>h]; highlight rest ]
47
 | [ '{{' h ::(Char *?) '}}' ; rest ] -> 
48
          [ <strong class="highlight">h; highlight rest ]
49
50
51
52
 | [ '$$%%' h ::(Char *?) '%%$$' ; rest ] -> 
          [ <strong class="ocaml">[<i>h]; highlight rest ]
 | [ '$$' h ::(Char *?) '$$' ; rest ] -> 
          [ <strong class="ocaml">h; highlight rest ]
53
 | [ '%%' h ::(Char *?) '%%' ; rest ] ->
54
          [ <i>h; highlight rest ] 
55
 | [ c; rest ] -> [ c; highlight rest ]
56
 | [] -> []
57

58
59
60
61
62
63
64
let xhighlight (String -> [ (Char | H.strong | H.i)* ] )
 | [ x::('}}' | ':}' | '{{' | '{:') h::Char*? 
     y::('}}' | ':}' | '{:' | '{{'); rest ] -> 
          [ !x <strong class="highlight">h !y; xhighlight rest ]
 | [ c; rest ] -> [ c; xhighlight rest ]
 | [] -> []

65
66
(* Split a comma-separated string *)

67
let split_comma (String -> [String*])
68
 | [ x::(Char*?) ',' ; rest ] -> (x, split_comma rest)
69
 | s -> [ s ]
70

71
72
73
74
75
76
77
78
type wschar = ' ' | '\n' | '\t' | '\r'

let split_thumbnails (String -> [(String,String)*])
 | [ wschar* x::(Char\wschar\':')+ ':' y::_*? '.'; rest ] -> 
        ((x,y), split_thumbnails rest)
 | [ wschar* x::(Char\wschar)+; rest ] -> 
        ((x,""), split_thumbnails rest)
 | [ wschar* ] -> []
79

80
(** Internal types **)
81

82
83
84
type Path = [ { url=String title=String }* ]
type Tree = { name=String url=String title=String
              children=[Tree*] boxes=[H.ul?] } 
85

86
let url_of_page (Page -> String)
87
88
 | <page url=u ..>_ -> u
 | <page name=n ..>_ -> n @ ".html"
89

90
91
92
93
let render(a : String)(p : {presenter=?"yes"|"no" ..}) : H.Flow =
 match p with
 | {presenter="yes" ..} -> [<strong class="ocaml">a] 
 | _ -> a
94

95
let authors ([Author+] -> H.Flow)
96
97
98
 | [ <author (p)>a ] -> render a p
 | [ <author (p1)>a1 <author (p2)>a2 ] -> (render a1 p1) @ ", and " @ (render a2 p2)
 | [ <author (p)>a; rem ] -> (render a p)@ ", " @ authors rem
99

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

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

125
let ul([H.li*] -> [H.ul?]) [] -> [] | l -> [ <ul>l ]
126

127
128
129
let ol(([H.li*],{style=?String}) -> [H.ol?]) 
 | ([],_) -> [] 
 | (l,s) -> [ <ol (s)>l ] 
130

131
let display_sitemap (h : Tree) :  H.li =
132
  let ch = map h . children with x -> display_sitemap x in
133
  <li>[ <a href=(h . url)>[ '[' !(h . title) ']' ] !(h . boxes); (ul ch) ]
134

135

136
137
let boxes_of (Page -> [H.ul?])
<page ..>[ (items::Item | _)*] & p ->
138
 let toc = transform items with 
139
 | <box title=t link=l ..>_ -> [ <li>[ <a href=[ !(url_of_page p) '#' !l ]>t ] ] 
140
141
142
 in
 ul toc

143
144
145
let link_to (<page (r)>[<title>t _* ] & p : Page) : H.a =
 let t = match r with
 | {new="" ..} -> t @ [ <img src="img/new.gif" alt="(new)" style="border:0">[]]
146
 | _ -> t in
147
 <a href=(url_of_page p)>t
148

149
let small_box (x : H.Flow) : H.Block =
150
 <table cellpadding="2" 
151
152
    style="font-size:11px ; font-family:arial,sans-serif;
           border: solid 2px black; background: #ffffff" width="100%">
153
154
   [ <tr> [<td>x] ]

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

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

169
let box (x : H.Flow) : H.Block =
170
171
172
173
 <table cellpadding="5" 
    style="border: solid 2px black; background: #ffffff" width="100%">
   [ <tr> [<td>x] ]

174
175
176
177
178
179
let style = "
a:link:hover, a:visited:hover {
  text-decoration: none;
  background: #FFFFD0;
  color: #FF0000;
}
180
181
182
183
184
185
186
187
a.old, a.old:hover, a.old:visited:hover { text-decoration: line-through; }
p { text-align: justify; margin: 1ex 1em 0 1em; }
pre { margin: 1ex 1em 0 1em; }
strong.ocaml{ color: #333b8e; }
strong.highlight { color: #FF0000; }
img.icon { border: 0; }
div.code { background: #E0E0E0; margin: 0.5ex 0.5em 0 0.5em; padding: 0.2ex; }
div.xmlcode { background:#ebefa2; margin: 0.5ex 0.5em 0 0.5em; padding: 0.2ex;}
188

189
190
191
192
193
div.abstract { 
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  background: #F0F0F0;
}
194
195

div.note { 
196
  text-align: justify;
197
198
199
200
201
  margin: 1ex 3em 1ex 3em;
  padding: 1ex 1em 1ex 1em;
  background: #D0E2D2;
}

202
203
204
205
206
div.session
 { 
  font: bold 80% helvetica;
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
207
  border: solid .5px gray;
208
209
}

210
div.abstract p { font-family: sans-serif; font-size:12px; }
211
212
"

213
214
type PageO = Page | []

215

216
let button(title : String)(onclick : String) : H.Inline =
217
218
  <input type="submit" style="font-size:8px;" value=title onclick=onclick>[] 
let button_id(id : String)(title : String)(onclick : String)(style : String) 
219
: H.Inline =
220
221
222
  <input type="submit" id=id 
   style=("font-size:8px;"@style) value=title 
   onclick=onclick>[] 
223

224
let demo(no : Int)(name : String)(prefix : String)(txt : String) : H.Flow = 
225
226
 let n = [ 'a' !name '_' ] in
 let prefix = if prefix = "" then "" else [ 'a' !prefix '_' ] in
227
 [ !(if (no = 1) then [<script src="demo.js" type="text/javascript">" "]
228
229
230
231
     else [])
  <table style="width:100%">[
   <tr>[ 
    <td style="width:50%">[
232
     (button_id (n@"btn") "Edit" ("editable('"@n@"','');") "")
233
     (button "Evaluate" ("submit('"@n@"');"))
234
     (button "Default" ("defreq('"@n@"');"))
235
     (button_id (n@"btnclear") "Clear" ("clearreq('"@n@"');") "visibility:hidden;")
236
237
    ]
    <td style="width:50%">[
238
     <input id=(n@"def") type="hidden" value=txt>[]
239
     <input id=(n@"prefix") type="hidden" value=prefix>[]
240
     (button "Clear" ("clearres('"@n@"');"))
241
242
    ] ]
   <tr>[
243
    <td valign="top">[ 
244
245
246
     <div id=(n@"container")>[
      <pre id=(n@"req")>txt
      <textarea id=(n@"edit") cols="50" rows="25" style="display:none;border:1px solid #CCCCCC; background-color:#F0F0F0;">txt 
247
248
     ]
    ]
249
250
    <td valign="top">[ <div id=(n@"res")>[] ] ] ] 
  ]
251

252
253
(* Main transformation function *)

254
(* returns the last page of the descendance *)
255
256

let thumbnail(w : String, h : String)
257
 (url : String)(title : String) : H.Inlines =
258
259
260
 [ <a href=url>[ 
   <img src=url width=w height=h alt="Click to enlarge" title=title>[] ] ]

261
let thumbwh({ width=?IntStr height=?IntStr ..} -> 
262
   (String -> String ->H.Inlines))
263
264
265
266
267
268
269
  | { width = w; height = h } ->
      let w = int_of w in let h = int_of h in
      (match h with
       | 0 -> raise "Thumbnail height = 0"
       | h -> let w = string_of ((w * 200) div h) in thumbnail (w,"200"))
  | _ -> thumbnail ("266","200")

270
271
let gen_page (site : String,
              prev : PageO, page : Page, next : PageO, 
272
273
              path : Path, sitemap : Tree) : PageO = 
match page with 
274
<page name=name leftbar="false"&(leftbar:=`false) else (leftbar:=`true) ..>[ 
275
276
 (<title>title <banner>banner | <title>(title & banner)) 
 items::_* ] ->
277
 let items = header @ items @ footer in
278

279
 let footnote_counter = ref Int 0 in 
280
 let footnotes = ref H.Flow [] in
281
 let demo_no = ref Int 0 in
282
 let last_demo = ref String "" in
283

284
 let text (t : [InlineText*]) : H.Inlines =
285
286
  transform t with
   | <code>x -> [ <b>[ <tt>(highlight x) ] ]
287
   | <local href=l>txt -> local_link (sitemap,l,txt)
288
   | <(tag & (`b|`i|`tt|`em)) (attr)>x -> [ <(tag) (attr)>(text x) ]
289
   | <footnote nocount="true">_ -> 
290
291
292
293
      let n = string_of !footnote_counter in
      [ <a name=[ 'bnote' !n ]>[]
        <a href=[ '#note' !n ]>[ '[' !n ']' ] ]

294
295
296
297
298
299
300
301
302
303
304
   | <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 ']' ] ]
305
   | <thumbnail ({href=url ..} & r)>[] ->
306
      thumbwh r url ""
307
   | <thumbnails ({href=url ..} & r)>l ->
308
309
310
311
312
313
314
      let l = split_thumbnails l in
      let f = thumbwh r in
      let c = ref Int 0 in
      (transform l with (x,y) -> 
          let t = f (url @ x) y in
          if (!c = 4) then (c := 1; [ <br>[] ] @ t)
          else (c := !c + 1; t))
315
   | z -> [ z ] 
316
 in
317

318
 let content (t : Content) : H.Flow =
319
  transform t with
320
   | <section title=title>c -> 
321
         [ <p>[ <b style="color: #008000">title ] !(content c) ]
322
   | <paper (r)>[ <title>tit aut::Author* <comment>com <abstract>ab  ] ->
323
         [ (match r with
324
           | { file = f; old = "" } -> <a class="old" href=f>tit
325
326
           | { file = f } -> <a href=f>tit
           | _ -> <b>tit) '. '
327
           !(authors aut) '. '
328
	   !(text com)
329
330
331
           <div class="abstract">[ 'Abstract:' !(content ab) ]
         ]
   | <slides file=f>[ <title>tit aut::Author* <comment>com ]  ->
332
        [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
333
334
   | <sample highlight="false">s ->
        [ <div class="code">[ <pre>s ] ]
335
   | <sample ..>s ->
336
        [ <div class="code">[ <pre>(highlight s) ] ]
337
338
   | <xmlsample highlight="false">s ->
        [ <div class="xmlcode">[ <pre>s ] ]
339
   | <xmlsample ..>s ->
340
        [ <div class="xmlcode">[ <pre>(highlight s) ] ]
341
342
   | <sessionsample highlight="false">s ->
        [ <div class="session">[ <pre>s ] ]
343
   | <sessionsample ..>s ->
344
        [ <div class="session">[ <pre>(highlight s) ] ]
345
   | <link url=url title=title>com -> 
346
        [ <ul>[ <li>[ <a href=url>title '. ' !(text com) ] ] ]
347
   | <ul>lis -> 
348
        ul (map lis with <li>x -> <li>(content x))
349
350
   | <ol (attr) >lis -> 
        ol ((map lis with <li>x -> <li>(content x) ),(attr))
351
   | H.table & x -> 
352
       [ <table width="100%">[<tr>[<td align="center">[x]]] ]
353
   | <p (attr)>x -> [ <p (attr)>(text x) ]
354
   | <pages-toc (a)>[] ->
355
356
      let toc = transform items with 
      | Page & p -> 
357
        let sects = match a with {sections=_ ..} -> boxes_of p | _ -> [] in
358
        [ <li>[ (link_to p) ; sects ] ]
359
      | <external href title=t ..>[] -> [ <li>[ <a href>t ] ] in
360
      ul toc
361
   | <boxes-toc (a)>[] ->
362
363
      let sections = match a with { sections=_ ..} -> `true | _ -> `false in
      let short = match a with { short=_ ..} -> `true | _ -> `false in
364
      let toc = transform items with 
365
      | <box ({title=t link=l ..} & ({short=s ..} | {title=s ..}))>b ->
366
367
368
        let t = if short then s else t in
        let sects = 
         if sections then
369
          (transform b with <section title=t>_ -> [<br>[] '-' !t])
370
         else [] in
371
372
        [ <li>[ <a href=('#',l)>t !sects ]] in
      ul toc
373
374
375
   | <site-toc>[] ->
        [ <ul>[ (display_sitemap sitemap) ] ]
   | <local-links href=s>[] ->
376
377
        ul (transform (split_comma s) with x -> 
             match local_link(sitemap,x,"") with [] -> [] | x -> [<li>x])
378
379
380
381
382
   | <two-columns>[ <left>x <right>y ] ->
	[ <table width="100%">[ 
            <tr>[ 
              <td valign="top">(content x) 
              <td valign="top">(content y) ] ] ]
383
   | <note title=t>c ->  [ <div class="note">[ <b>[!t ':  '] !(content c) ]]
384
385
386
387
388
   | <note>c ->  [ <div class="note">[ <b>"Note:  " !(content c) ]]
   | <footnotes>[] -> 
       (match !footnotes with 
        | [] -> [] 
        | n -> footnotes := []; [ <br>[] (meta n) ] )
389
   | <xhtml>i -> i
390
391
   | <demo (r)>s -> 
       demo_no := !demo_no + 1; 
392
       let name = match r with { label .. } -> label | _ -> string_of !demo_no in
393
       let prefix = 
394
395
           match r with { prefix = "last" .. } -> !last_demo 
                      | { prefix .. } -> prefix
396
397
398
                      | _ -> "" in
       last_demo := name;
       demo !demo_no name prefix s
399
400
   | t -> text [ t ]
 in
401

402
(* Preparing left panel *)
403

404
 let left =
405
406
407
408
  if leftbar then
  let navig = transform items with <left>c -> [ c ] in
  let left = match navig with [] -> [ [<boxes-toc>[]] ] | n -> n in
  [
409
  
410
   <td valign="top" align="left">[
411
     <span style="position:fixed;background:#ffffff;border: solid 2px black; cursor:e-resize;" onclick="javascript:var s=document.getElementById('leftbar').style; var d=s.display=='none'?'block':'none'; s.display=d; document.cookie='leftbar='+d;">"*"
412
     <table cellpadding="0" cellspacing="15" id="leftbar"
413
414
            width="200"
            style="font-size:80%; border: 1px dashed black;
415
                   background: #ffcd72">	(* altbg 9aa8ba *)
416
417
     (map left with x -> <tr>[ <td>[ (small_box (content x)) ] ]) ] ]
 else [] in
418

419
 let dpath : H.Inlines = transform path with 
420
  | { url = f title = t } -> [ <a href=f>t ': '] 
421
 in
422
 let npath = path @ [ { url = (url_of_page page); title = title } ] in
423
 let subpages = transform items with p & Page -> [ p ] in
424
 let (next,last) = gen_page_seq (site,page, subpages, next, npath, sitemap) in
425
 let next = match next with [] -> [] 
426
   | <page ..>[ <title>t; _ ] & p -> 
427
      [ <a href=(url_of_page p)>[ 
428
          <img width="16" height="16" class="icon" alt="Next page:"
429
               src="img/right.gif">[]
430
431
432
          ' ' !t
        ] ] in
 let prev = match prev with [] -> [] 
433
   | <page ..>[ <title>t; _ ] & p -> 
434
      [ <a href=(url_of_page p)>[ 
435
          <img width="16" height="16" class="icon"
436
               alt="Previous page:" src="img/left.gif">[]
437
438
          ' ' !t
        ] ] in
439
 let navig = 
440
   if prev = [] then [] else
441
   [ (small_box [
442
     <p>[ !dpath !title ]
443
444
445
446
     <p>[ !prev ' ' !next ] ]) ] in

(* Preparing main panel *)
 let main = transform items with
447
   | <box title=t link=l ..>c -> [ (box_title (content c, l, t)) ]
448
   | <box>c -> [ (box (content c)) ]
449
450
451
452
   | <footnotes>[] -> 
       (match !footnotes with 
        | [] -> [] 
        | n -> footnotes := []; [ (meta n) ] )
453
   | <meta>c -> [ (meta (content c)) ]
454
 in
455
456
457
458
 let notes = match !footnotes with
   | [] -> []
   | n -> [ (meta n) ] in
 let main = match (navig @ main @ notes @ navig) with
459
460
461
   | [] -> raise "Empty page !"
   | x -> x in

462
 let right : H.td =
463
464
465
466
467
468
469
470
471
472
  <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"
473
          style="border: 1px solid black; background: #fccead">[ (* altbg c8ccd1 *)
474
       <table width="100%" cellpadding="0" cellspacing="17">
475
476
477
478
         (map main with x -> <tr>[ <td>[x] ])
      ] ]
  ] ] in

479
 let html : H.html =
480
481
 <html>[
  <head>[ 
482
   <title>[ !site ': ' !title ]
483
   <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">[]
484
   <style type="text/css">style
485
   !extra_head
486
  ]
487
488
  <body style="margin: 0; padding : 0; background: #fcb333"
        onload="javascript:if (document.cookie.indexOf('leftbar=none')>=0) document.getElementById('leftbar').style.display='none';">[  (* altbg  4e6e99 *)
489
   <table cellspacing="10" cellpadding="0" width="100%" border="0">[
490
    <tr>[ !left right ]
491
   ]
492
  ]
493
494
 ]
 in
495
 let txt : Latin1 = 
496
   [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
497
     '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
498
     !(print_xml html) ] in
499
 let fn = outdir @ "/" @ name @ ".html" in
500
 dump_to_file fn txt;
501
 last
502
	
503

504
let gen_page_seq 
505
506
 (site : String,
  prev : PageO, pages : [Page*], next : PageO, 
507
  path : Path, sitemap : Tree) : (PageO, PageO) =
508
509
 match pages with
 | [ p1 p2 ; _ ] & [ _; rest ] -> 
510
511
     let last = gen_page (site,prev,p1,p2, path, sitemap) in
     let (_,last)  = gen_page_seq (site,last, rest, next, path, sitemap) in 
512
     (p1,last)
513
 | [ p ] ->
514
     let last = gen_page (site,prev,p,next, path, sitemap) in (p,last)
515
 | [] -> (next,prev)
516

517

518
;;
519

520
gen_page (site,[],main_page,[], [], compute_sitemap main_page)