site.cd 13.6 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 noindex=?String 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
     | <xmlsample highlight=?"true"|"false">String
43
     | <sessionsample highlight=?"true"|"false">String
44
     | H:Xtable
45
     | Paper | Slides | Link
46
47
     | <boxes-toc sections=?"">[]    (* the presence optional "section" attr produces  *)
     | <pages-toc sections=?"">[]    (* a two-level depth toc to include also sections *) 
48
49
     | <site-toc>[]
     | <local-links href=String>[]
50
     | <two-columns>[ <left>Content <right>Content ]
51
     | <note> Content 
52
     | InlineText
53
     )* ]
54
55
56

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

63
64
65
66
(** Generic purpose functions **)

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

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

74
(* Highlighting text between {{...}} *)
75

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

90
91
(* Split a comma-separated string *)

92
let split_comma (String -> [String*])
93
 | [ x::(Char*?) ',' ; rest ] -> (x, split_comma rest)
94
 | s -> [ s ]
95
96


97
(** Internal types **)
98

99
type Path = [ { url = String; title = String }* ]
100
type Tree = { name = String; url = String; title = String; 
101
              children = [Tree*]; boxes = [H:Xul?] } 
102

103
let url_of_name (String -> String)
104
   "index" -> "/"
105
 | s -> s @ ".html"
106

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

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

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

136
let display_sitemap (h : Tree) :  H:Xli =
137
138
  let ch = map h . children with x -> display_sitemap x in
  let ch = match ch with [] -> [] | l -> [ <ul>l ] in
139
  <li>[ <a href=(h . url)>[ '[' !(h . title) ']' ] !(h . boxes); ch ]
140

141

142
let boxes_of  (Page -> [H:Xul?])
143
     <page name=n>[ (items::Item | _)*] ->
144
145
        let toc = 
         transform items with 
146
147
          | <box noindex=_>_ -> []
          | <box title=t link=l>_ -> [ <li>[ <a href=((url_of_name n)@('#',l))>t ] ] in
148
149
        (match toc with [] -> [] | lis -> [ <ul>lis ])
 
150
let link_to (Page -> H:Xa)
151
152
153
 <page name=n new=_>[<title>t ; _ ] -> 
               <a href=(url_of_name n)>[!t 
                      <img src="img/new.gif" alt="(new)" style="border:0">[]]
154
| <page name=n>[<title>t ; _ ] -> <a href=(url_of_name n)>t
155

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

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

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

let style = "
a:link:hover, a:visited:hover {
  text-decoration: none;
  background: #FFFFD0;
  color: #FF0000;
}
182
183
184
a.old, a.old:hover, a.old:visited:hover {
  text-decoration: line-through;
}
185
186
187
188
189
190
191
p {
  text-align: justify;
  margin: 1ex 1em 0 1em;
}
pre {
  margin: 1ex 1em 0 1em;
}
192
193
194
strong.ocaml{
  color: #333b8e;
}
195
strong.highlight {
196
197
198
199
200
201
202
203
204
205
  color: #FF0000;
}
img.icon {
  border: 0;
}
div.code {
  background: #E0E0E0;
  margin: 0.5ex 0.5em 0 0.5em;
  padding: 0.2ex;
}
206

207
208
209
210
211
div.xmlcode {
  background: #ebefa2;
  margin: 0.5ex 0.5em 0 0.5em;
  padding: 0.2ex;
}
212

213
div.abstract { 
214
  font: bold helvetica;
215
216
217
218
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  background: #F0F0F0;
}
219
220

div.note { 
221
  text-align: justify;
222
223
224
225
226
227
228
  font: bold helvetica;
  margin: 1ex 3em 1ex 3em;
  padding: 1ex 1em 1ex 1em;
  background: #D0E2D2;
}


229
230
231
232
233
234
235
236
div.session
 { 
  font: bold 80% helvetica;
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  border: solid .5px grey;
}

237
div.abstract p { 
238
  font: sans-serif;
239
240
241
}
"

242
243
(* Main transformation function *)

244
(* returns the last page of the descendance *)
245
let gen_page (prev : Page|[], page : Page, next : Page|[], 
246
                  path : Path, sitemap : Tree) : (Page|[]) = 
247
match page with
248
249
<page name=name>[ 
        <title>title <banner>banner | <title>(title & banner); items ] ->
250

251
 let footnote_counter = ref Int 0 in 
252
 let footnotes = ref H:Flow [] in
253

254
 let text (t : [InlineText*]) : H:Inlines =
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  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 ']' ] ]
271
   | z -> [ z ] 
272
 in
273

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

341
(* Preparing left panel *)
342

343
344
345
346
 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">[
347
     <table cellpadding="0" cellspacing="15" 
348
349
            width="200"
            style="font-size:80%; border: 1px dashed black;
350
                   background: #ffcd72">	(* altbg 9aa8ba *)
351
352
     (map left with x -> <tr>[ <td>[ (box (content x)) ] ]) ] in

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

(* Preparing main panel *)
 let main = transform items with
381
382
   | <box (r)>c -> [ (box_title (content c, r . link, r . title)) ]
   | <meta>c -> [ (meta (content c)) ]
383
 in
384
385
386
387
 let notes = match !footnotes with
   | [] -> []
   | n -> [ (meta n) ] in
 let main = match (navig @ main @ notes @ navig) with
388
389
390
   | [] -> raise "Empty page !"
   | x -> x in

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

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

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

443

444
;;
445

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

450