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
34
let [<site>[ <title>site 
             (<header>header | /(header:=[])) 
             (<footer>footer | /(footer:=[])) 
             main_page ] ] = 
35
 try (load_include input :? [ Site ])
36
 with err & Latin1 -> 
37
   print ['Invalid input document:\n' !err '\n']; 
38
39
   exit 2 

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

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

57
58
59
60
61
62
63
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 ]
 | [] -> []

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

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

70
71
72
73
74
75
76
77
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* ] -> []
78

79
(** Internal types **)
80

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

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

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

94
let authors ([Author+] -> H.Flow)
95
96
97
 | [ <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
98

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

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

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

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

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

134

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

142
143
144
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">[]]
145
 | _ -> t in
146
 <a href=(url_of_page p)>t
147

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

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

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

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

173
174
175
176
177
178
let style = "
a:link:hover, a:visited:hover {
  text-decoration: none;
  background: #FFFFD0;
  color: #FF0000;
}
179
180
181
182
183
184
185
186
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;}
187

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

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

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

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

212
213
type PageO = Page | []

214

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

223
let demo(no : Int)(name : String)(prefix : String)(txt : String) : H.Flow = 
224
225
 let n = [ 'a' !name '_' ] in
 let prefix = if prefix = "" then "" else [ 'a' !prefix '_' ] in
226
 [ !(if (no = 1) then [<script src="demo.js" type="text/javascript">" "]
227
228
229
230
     else [])
  <table style="width:100%">[
   <tr>[ 
    <td style="width:50%">[
231
     (button_id (n@"btn") "Edit" ("editable('"@n@"','');") "")
232
     (button "Evaluate" ("submit('"@n@"');"))
233
     (button "Default" ("defreq('"@n@"');"))
234
     (button_id (n@"btnclear") "Clear" ("clearreq('"@n@"');") "visibility:hidden;")
235
236
    ]
    <td style="width:50%">[
237
     <input id=(n@"def") type="hidden" value=txt>[]
238
     <input id=(n@"prefix") type="hidden" value=prefix>[]
239
     (button "Clear" ("clearres('"@n@"');"))
240
241
    ] ]
   <tr>[
242
    <td valign="top">[ 
243
244
245
     <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 
246
247
     ]
    ]
248
249
    <td valign="top">[ <div id=(n@"res")>[] ] ] ] 
  ]
250

251
252
(* Main transformation function *)

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

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

260
let thumbwh({ width=?IntStr height=?IntStr ..} -> 
261
   (String -> String ->H.Inlines))
262
263
264
265
266
267
268
  | { 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")

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

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

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

293
294
295
296
297
298
299
300
301
302
303
   | <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 ']' ] ]
304
   | <thumbnail ({href=url ..} & r)>[] ->
305
      thumbwh r url ""
306
   | <thumbnails ({href=url ..} & r)>l ->
307
308
309
310
311
312
313
      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))
314
   | z -> [ z ] 
315
 in
316

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

401
(* Preparing left panel *)
402

403
 let left =
404
405
406
407
  if leftbar then
  let navig = transform items with <left>c -> [ c ] in
  let left = match navig with [] -> [ [<boxes-toc>[]] ] | n -> n in
  [
408
  
409
   <td valign="top" align="left">[
410
     <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;">"*"
411
     <table cellpadding="0" cellspacing="15" id="leftbar"
412
413
            width="200"
            style="font-size:80%; border: 1px dashed black;
414
                   background: #ffcd72">	(* altbg 9aa8ba *)
415
416
     (map left with x -> <tr>[ <td>[ (small_box (content x)) ] ]) ] ]
 else [] in
417

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

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

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

478
 let html : H.html =
479
480
 <html>[
  <head>[ 
481
   <title>[ !site ': ' !title ]
482
   <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">[]
483
   <style type="text/css">style
484
  ]
485
486
  <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 *)
487
   <table cellspacing="10" cellpadding="0" width="100%" border="0">[
488
    <tr>[ !left right ]
489
   ]
490
  ]
491
492
 ]
 in
493
 let txt : Latin1 = 
494
   [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
495
     '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
496
     !(print_xml html) ] in
497
 let fn = outdir @ "/" @ name @ ".html" in
498
 dump_to_file fn txt;
499
 last
500
	
501

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

515

516
;;
517

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