site.cd 13.9 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
17
type Site = <site>[ <title>String Page ]
type Page =  <page name=String url=?String new=?"">[ <title>String <banner>[InlineText*]? Item* ]
18
type External = <external {|href=String; title=String; name=String |}>[]
19
20

type Item = 
21
   <box ({| title=String; link=String; short=?String |} | {| |})>Content
22
23
 | <meta>Content
 | <left>Content
24
 | <footnotes>[]
25
 | Page
26
 | External
27

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

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

type Link =
37
  <link url=String title=String>[ InlineText* ]
38
39

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

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

66
67
68
69
(** Generic purpose functions **)

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

70
let load_include (String -> [Any*])
71
 name ->
72
(*   let _ = print [ 'Loading ' !name '... \n' ] in *)
73
   xtransform [ (load_xml name) ] with 
74
   | <include file=(s & String)>[] -> load_include s
75
   | <include-verbatim file=(s & String)>[] -> load_file s 
76

77
(* Highlighting text between {{...}} *)
78

79
80
81
let highlight (String -> [ (Char | H:Xstrong | H:Xi)* ] )
 | [ '{{%%' h ::(Char *?) '%%}}' ; rest ] -> 
          [ <strong class="highlight">[<i>h]; highlight rest ]
82
 | [ '{{' h ::(Char *?) '}}' ; rest ] -> 
83
          [ <strong class="highlight">h; highlight rest ]
84
85
86
87
 | [ '$$%%' h ::(Char *?) '%%$$' ; rest ] -> 
          [ <strong class="ocaml">[<i>h]; highlight rest ]
 | [ '$$' h ::(Char *?) '$$' ; rest ] -> 
          [ <strong class="ocaml">h; highlight rest ]
88
 | [ '%%' h ::(Char *?) '%%' ; rest ] ->
89
          [ <i>h; highlight rest ] 
90
 | [ c; rest ] -> [ c; highlight rest ]
91
 | [] -> []
92

93
94
(* Split a comma-separated string *)

95
let split_comma (String -> [String*])
96
 | [ x::(Char*?) ',' ; rest ] -> (x, split_comma rest)
97
 | s -> [ s ]
98
99


100
(** Internal types **)
101

102
type Path = [ { url = String; title = String }* ]
103
type Tree = { name = String; url = String; title = String; 
104
              children = [Tree*]; boxes = [H:Xul?] } 
105

106
107
108
let url_of_page (Page -> String)
 | <page url=u>_ -> u
 | <page name=n>_ -> n @ ".html"
109

110
let authors ([Author+] -> String)
111
112
113
 | [ <author>a ] -> a
 | [ <author>a1 <author>a2 ] -> a1 @ ", and " @ a2
 | [ <author>a; rem ] -> a @ ", " @ authors rem
114

115
let find_local_link (sitemap : [Tree*], l : String) : Tree =
116
117
match sitemap with
 | (h,t) ->
118
   if (h . name = l) then h
119
120
121
   else 
    (try find_local_link (t,l) with `Not_found -> 
         find_local_link (h . children,l))
122
 | [] -> raise `Not_found
123

124
let local_link (sitemap : Tree, l : String, txt : String) : H:Inline =
125
126
127
128
 try 
  let h = find_local_link ([sitemap],l)  in
  let txt = if txt = "" then h . title else txt in
  <a href=(h . url)>txt
129
 with `Not_found -> raise [ 'Local link not found: ' !l ]
130
 
131
let compute_sitemap ((Page|External) -> Tree)
132
 | <page name=name>[ <title>title (c::(Page|External) | _)* ] & p ->
133
   let children = map c with p -> compute_sitemap p in
134
   { name = name; url = (url_of_page p); title = title; 
135
     children = children; boxes = (boxes_of p) }
136
 | <external name=name href=h title=t>[] ->
137
   { name = name; url = h; title = t; children = []; boxes = [] }
138

139
140
let ul([H:Xli*] -> [H:Xul?]) [] -> [] | l -> [ <ul>l ]

141
let display_sitemap (h : Tree) :  H:Xli =
142
  let ch = map h . children with x -> display_sitemap x in
143
  <li>[ <a href=(h . url)>[ '[' !(h . title) ']' ] !(h . boxes); (ul ch) ]
144

145

146
let boxes_of (Page -> [H:Xul?])
147
<page>[ (items::Item | _)*] & p ->
148
 let toc = transform items with 
149
 | <box title=t link=l>_ -> [ <li>[ <a href=[ !(url_of_page p) '#' !l ]>t ] ] 
150
151
152
 in
 ul toc

153
let link_to (<page>[<title>t ; _ ] & p : Page) : H:Xa =
154
155
156
 let t = match p with
 | <_ new="">_ -> t @ [ <img src="img/new.gif" alt="(new)" style="border:0">[]]
 | _ -> t in
157
 <a href=(url_of_page p)>t
158

159
let small_box (x : H:Flow) : H:Block =
160
 <table cellpadding="2" 
161
162
    style="font-size:11px ; font-family:arial,sans-serif;
           border: solid 2px black; background: #ffffff" width="100%">
163
164
   [ <tr> [<td>x] ]

165
let meta (x : H:Flow) : H:Block =
166
 <table cellpadding="2" 
167
    style="border: solid 1px #b0b0b0; background: #e0e0e0; font-size: 80%"  
168
169
170
    width="100%">
   [ <tr> [<td>x] ]

171
let box_title (x : H:Flow, a : String, t : String) : H:Block =
172
173
 <table cellpadding="5" 
    style="border: solid 2px black; background: #ffffff" width="100%">
174
175
176
177
   [ <tr>[ 
       <td style="background: #fff0f0; color: #0000ff; font: bold 100%
                  helvetica">[<a name=a>t] ] 
     <tr> [<td>x] ]
178

179
180
181
182
183
let box (x : H:Flow) : H:Block =
 <table cellpadding="5" 
    style="border: solid 2px black; background: #ffffff" width="100%">
   [ <tr> [<td>x] ]

184
185
186
187
188
189
let style = "
a:link:hover, a:visited:hover {
  text-decoration: none;
  background: #FFFFD0;
  color: #FF0000;
}
190
191
192
193
194
195
196
197
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;}
198

199
div.abstract { 
200
  font: bold helvetica;
201
202
203
204
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  background: #F0F0F0;
}
205
206

div.note { 
207
  text-align: justify;
208
209
210
211
212
213
  font: bold helvetica;
  margin: 1ex 3em 1ex 3em;
  padding: 1ex 1em 1ex 1em;
  background: #D0E2D2;
}

214
215
216
217
218
219
220
221
div.session
 { 
  font: bold 80% helvetica;
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  border: solid .5px grey;
}

222
div.abstract p { font: sans-serif; }
223
224
"

225
226
type PageO = Page | []

227
228
(* Main transformation function *)

229
(* returns the last page of the descendance *)
230
231
let gen_page (site : String,
              prev : PageO, page : Page, next : PageO, 
232
233
              path : Path, sitemap : Tree) : PageO = 
match page with 
234
<page name=name>[ 
235
 <title>title <banner>banner | <title>(title & banner); items ] ->
236

237
 let footnote_counter = ref Int 0 in 
238
 let footnotes = ref H:Flow [] in
239

240
 let text (t : [InlineText*]) : H:Inlines =
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  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) ]
   | <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 ']' ] ]
256
   | z -> [ z ] 
257
 in
258

259
 let content (t : Content) : H:Flow =
260
  transform t with
261
   | <section title=title>c -> 
262
         [ <p>[ <b style="color: #008000">title ] !(content c) ]
263
   | <paper (r)>[ <title>tit aut::Author* <comment>com <abstract>ab  ]  ->
264
         [ (match r with
265
           | { file = f; old = "" } -> <a class="old" href=f>tit
266
267
           | { file = f } -> <a href=f>tit
           | _ -> <b>tit) '. '
268
           !(authors aut) '. '
269
	   !(text com)
270
271
272
           <div class="abstract">[ 'Abstract:' !(content ab) ]
         ]
   | <slides file=f>[ <title>tit aut::Author* <comment>com ]  ->
273
        [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
274
275
   | <sample highlight="false">s ->
        [ <div class="code">[ <pre>s ] ]
276
   | <sample>s ->
277
        [ <div class="code">[ <pre>(highlight s) ] ]
278
279
280
281
   | <xmlsample highlight="false">s ->
        [ <div class="xmlcode">[ <pre>s ] ]
   | <xmlsample>s ->
        [ <div class="xmlcode">[ <pre>(highlight s) ] ]
282
283
284
285
   | <sessionsample highlight="false">s ->
        [ <div class="session">[ <pre>s ] ]
   | <sessionsample>s ->
        [ <div class="session">[ <pre>(highlight s) ] ]
286
   | <link url=url title=title>com -> 
287
        [ <ul>[ <li>[ <a href=url>title '. ' !(text com) ] ] ]
288
   | <ul>lis -> 
289
        ul (map lis with <li>x -> <li>(content x))
290
   | H:Xtable & x -> 
291
        [ x ]
292
   | <p (attr)>x -> [ <p (attr)>(text x) ]
293
   | <pages-toc (a)>[] ->
294
295
296
297
298
299
      let toc = transform items with 
      | Page & p -> 
        let sects = match a with {|sections=_|} -> boxes_of p | _ -> [] in
        [ <li>[ (link_to p) ; sects ] ]
      | <external href=l title=t>[] -> [ <li>[ <a href=l>t ] ] in
      ul toc
300
   | <boxes-toc (a)>[] ->
301
302
      let sections = match a with { section=_ } -> `true | _ -> `false in
      let short = match a with { short=_ } -> `true | _ -> `false in
303
      let toc = transform items with 
304
305
306
307
      | <box ({title=t; link=l} & ({short=s} | {title=s}))>b ->
        let t = if short then s else t in
        let sects = 
         if sections then
308
          (transform b with <section title=t>_ -> [<br>[] '-' !t])
309
         else [] in
310
311
        [ <li>[ <a href=('#',l)>t !sects ]] in
      ul toc
312
313
314
   | <site-toc>[] ->
        [ <ul>[ (display_sitemap sitemap) ] ]
   | <local-links href=s>[] ->
315
        ul (map (split_comma s) with x -> <li>[ (local_link(sitemap,x,"")) ])
316
317
318
319
320
   | <two-columns>[ <left>x <right>y ] ->
	[ <table width="100%">[ 
            <tr>[ 
              <td valign="top">(content x) 
              <td valign="top">(content y) ] ] ]
321
   | <note title=t>c ->  [ <div class="note">[ <b>[!t ':  '] !(content c) ]]
322
323
324
325
326
   | <note>c ->  [ <div class="note">[ <b>"Note:  " !(content c) ]]
   | <footnotes>[] -> 
       (match !footnotes with 
        | [] -> [] 
        | n -> footnotes := []; [ <br>[] (meta n) ] )
327
328
   | t -> text [ t ]
 in
329

330
(* Preparing left panel *)
331

332
333
334
335
 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">[
336
     <table cellpadding="0" cellspacing="15" 
337
338
            width="200"
            style="font-size:80%; border: 1px dashed black;
339
                   background: #ffcd72">	(* altbg 9aa8ba *)
340
     (map left with x -> <tr>[ <td>[ (small_box (content x)) ] ]) ] in
341

342
 let dpath : H:Inlines = transform path with 
343
  | { url = f; title = t } -> [ <a href=f>t ': '] 
344
 in
345
 let npath = path @ [ { url = (url_of_page page); title = title } ] in
346
 let subpages = transform items with p & Page -> [ p ] in
347
 let (next,last) = gen_page_seq (site,page, subpages, next, npath, sitemap) in
348
 let next = match next with [] -> [] 
349
350
   | <page>[ <title>t; _ ] & p -> 
      [ <a href=(url_of_page p)>[ 
351
          <img width="16" height="16" class="icon" alt="Next page:"
352
               src="img/right.gif">[]
353
354
355
          ' ' !t
        ] ] in
 let prev = match prev with [] -> [] 
356
357
   | <page>[ <title>t; _ ] & p -> 
      [ <a href=(url_of_page p)>[ 
358
          <img width="16" height="16" class="icon"
359
               alt="Previous page:" src="img/left.gif">[]
360
361
          ' ' !t
        ] ] in
362
 let navig = 
363
   if prev = [] then [] else
364
   [ (small_box [
365
     <p>[ !dpath !title ]
366
367
368
369
     <p>[ !prev ' ' !next ] ]) ] in

(* Preparing main panel *)
 let main = transform items with
370
371
   | <box title=t link=l>c -> [ (box_title (content c, l, t)) ]
   | <box>c -> [ (box (content c)) ]
372
373
374
375
   | <footnotes>[] -> 
       (match !footnotes with 
        | [] -> [] 
        | n -> footnotes := []; [ (meta n) ] )
376
   | <meta>c -> [ (meta (content c)) ]
377
 in
378
379
380
381
 let notes = match !footnotes with
   | [] -> []
   | n -> [ (meta n) ] in
 let main = match (navig @ main @ notes @ navig) with
382
383
384
   | [] -> raise "Empty page !"
   | x -> x in

385
 let right : H:Xtd =
386
387
388
389
390
391
392
393
394
395
  <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"
396
          style="border: 1px solid black; background: #fccead">[ (* altbg c8ccd1 *)
397
       <table width="100%" cellpadding="0" cellspacing="17">
398
399
400
401
         (map main with x -> <tr>[ <td>[x] ])
      ] ]
  ] ] in

402
 let html : H:Xhtml =
403
404
 <html>[
  <head>[ 
405
   <title>[ !site ': ' !title ]
406
   <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">[]
407
   <style type="text/css">style
408
  ]
409
  <body style="margin: 0; padding : 0; background: #fcb333">[  (* altbg  4e6e99 *)
410
411
412
   <table cellspacing="10" cellpadding="0" width="100%" border="0">[
    <tr>[ left right ]
   ]
413
  ]
414
415
 ]
 in
416
 let txt : Latin1 = 
417
   [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
418
     '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
419
420
     !(print_xml html) ] in
 let fn = "www/" @ name @ ".html" in
421
 dump_to_file fn txt;
422
 last
423
	
424

425
let gen_page_seq 
426
427
 (site : String,
  prev : PageO, pages : [Page*], next : PageO, 
428
  path : Path, sitemap : Tree) : (PageO, PageO) =
429
430
 match pages with
 | [ p1 p2 ; _ ] & [ _; rest ] -> 
431
432
     let last = gen_page (site,prev,p1,p2, path, sitemap) in
     let (_,last)  = gen_page_seq (site,last, rest, next, path, sitemap) in 
433
     (p1,last)
434
 | [ p ] ->
435
     let last = gen_page (site,prev,p,next, path, sitemap) in (p,last)
436
 | [] -> (next,prev)
437

438

439
;;
440

441
match load_include input with
442
443
 | [ <site>[ <title>(site & String) (p & Page) ] ] -> 
   let _ = gen_page (site,[],p,[], [], compute_sitemap p) in []
444
 | _ -> raise ("Invalid input document " @ input)
445

446