site.cd 14.1 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
     | <ol {||}>[<li {||}>Content *]
43
     | <section title=String>Content
44
     | <sample highlight=?"true"|"false">String
45
     | <xmlsample highlight=?"true"|"false">String
46
     | <sessionsample highlight=?"true"|"false">String
47
     | H:Xtable
48
     | Paper | Slides | Link
49
50
     | <boxes-toc short=?"" sections=?"">[]
     | <pages-toc sections=?"">[]
51
52
     | <site-toc>[]
     | <local-links href=String>[]
53
     | <two-columns>[ <left>Content <right>Content ]
54
     | <note title=?String> Content
55
     | <footnotes>[]
56
     | InlineText
57
     )* ]
58
59
60

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

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

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

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

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

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

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

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


101
(** Internal types **)
102

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

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

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

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

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

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

142
143
let ol([H:Xli*] -> [H:Xol?]) [] -> [] | l -> [ <ol>l ]

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

148

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

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

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

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

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

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

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

202
div.abstract { 
203
  font: bold helvetica;
204
205
206
207
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  background: #F0F0F0;
}
208
209

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

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

225
div.abstract p { font: sans-serif; }
226
227
"

228
229
type PageO = Page | []

230
231
(* Main transformation function *)

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

240
 let footnote_counter = ref Int 0 in 
241
 let footnotes = ref H:Flow [] in
242

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

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

335
(* Preparing left panel *)
336

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

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

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

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

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

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

443

444
;;
445

446
match load_include input with
447
448
 | [ <site>[ <title>(site & String) (p & Page) ] ] -> 
   let _ = gen_page (site,[],p,[], [], compute_sitemap p) in []
449
 | _ -> raise ("Invalid input document " @ input)
450

451