site.cd 13.4 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=?"">[ <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
 | <meta>Content
 | <left>Content
23
 | <footnotes>[]
24
 | Page
25
 | External
26

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

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

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

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

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

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

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

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

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

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

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

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


99
(** Internal types **)
100

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

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

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

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

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

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

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

144

145
146
147
148
149
150
151
152
153
154
155
156
157
let boxes_of (Page -> [H:Xul?])
<page name=n>[ (items::Item | _)*] ->
 let toc = transform items with 
 | <box noindex="">_ -> []
 | <box title=t link=l>_ -> [ <li>[ <a href=[ !(url_of_name n) '#' !l ]>t ] ] 
 in
 ul toc

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

159
let 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
184

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

194
div.abstract { 
195
  font: bold helvetica;
196
197
198
199
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  background: #F0F0F0;
}
200
201

div.note { 
202
  text-align: justify;
203
204
205
206
207
208
  font: bold helvetica;
  margin: 1ex 3em 1ex 3em;
  padding: 1ex 1em 1ex 1em;
  background: #D0E2D2;
}

209
210
211
212
213
214
215
216
div.session
 { 
  font: bold 80% helvetica;
  margin: 1ex 1em 1ex 1em;
  padding: 1ex 1em 1ex 1em;
  border: solid .5px grey;
}

217
div.abstract p { font: sans-serif; }
218
219
"

220
221
type PageO = Page | []

222
223
(* Main transformation function *)

224
(* returns the last page of the descendance *)
225
226
227
let gen_page (prev : PageO, page : Page, next : PageO, 
              path : Path, sitemap : Tree) : PageO = 
match page with 
228
<page name=name>[ 
229
 <title>title <banner>banner | <title>(title & banner); items ] ->
230

231
 let footnote_counter = ref Int 0 in 
232
 let footnotes = ref H:Flow [] in
233

234
 let text (t : [InlineText*]) : H:Inlines =
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  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 ']' ] ]
250
   | z -> [ z ] 
251
 in
252

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

321
(* Preparing left panel *)
322

323
324
325
326
 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">[
327
     <table cellpadding="0" cellspacing="15" 
328
329
            width="200"
            style="font-size:80%; border: 1px dashed black;
330
                   background: #ffcd72">	(* altbg 9aa8ba *)
331
332
     (map left with x -> <tr>[ <td>[ (box (content x)) ] ]) ] in

333
 let dpath : H:Inlines = transform path with 
334
  | { url = f; title = t } -> [ <a href=f>t ': '] 
335
 in
336
 let npath = path @ [ { url = (url_of_name name); title = title } ] in
337
 let subpages = transform items with p & Page -> [ p ] in
338
 let (next,last) = gen_page_seq (page, subpages, next, npath, sitemap) in
339
340
341
 let next = match next with [] -> [] 
   | <page name=n>[ <title>t; _ ] -> 
      [ <a href=(url_of_name n)>[ 
342
          <img width="16" height="16" class="icon" alt="Next page:"
343
               src="img/right.gif">[]
344
345
346
347
348
          ' ' !t
        ] ] in
 let prev = match prev with [] -> [] 
   | <page name=n>[ <title>t; _ ] -> 
      [ <a href=(url_of_name n)>[ 
349
          <img width="16" height="16" class="icon"
350
               alt="Previous page:" src="img/left.gif">[]
351
352
          ' ' !t
        ] ] in
353
 let navig = 
354
   if prev = [] then [] else
355
   [ (box [
356
     <p>[ !dpath !title ]
357
358
359
360
     <p>[ !prev ' ' !next ] ]) ] in

(* Preparing main panel *)
 let main = transform items with
361
   | <box (r)>c -> [ (box_title (content c, r . link, r . title)) ]
362
363
364
365
   | <footnotes>[] -> 
       (match !footnotes with 
        | [] -> [] 
        | n -> footnotes := []; [ (meta n) ] )
366
   | <meta>c -> [ (meta (content c)) ]
367
 in
368
369
370
371
 let notes = match !footnotes with
   | [] -> []
   | n -> [ (meta n) ] in
 let main = match (navig @ main @ notes @ navig) with
372
373
374
   | [] -> raise "Empty page !"
   | x -> x in

375
 let right : H:Xtd =
376
377
378
379
380
381
382
383
384
385
  <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"
386
          style="border: 1px solid black; background: #fccead">[ (* altbg c8ccd1 *)
387
       <table width="100%" cellpadding="0" cellspacing="17">
388
389
390
391
         (map main with x -> <tr>[ <td>[x] ])
      ] ]
  ] ] in

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

415
let gen_page_seq 
416
417
 (prev : PageO, pages : [Page*], next : PageO, 
  path : Path, sitemap : Tree) : (PageO, PageO) =
418
419
 match pages with
 | [ p1 p2 ; _ ] & [ _; rest ] -> 
420
421
422
     let last = gen_page (prev,p1,p2, path, sitemap) in
     let (_,last)  = gen_page_seq (last, rest, next, path, sitemap) in 
     (p1,last)
423
 | [ p ] ->
424
     let last = gen_page (prev,p,next, path, sitemap) in (p,last)
425
 | [] -> (next,prev)
426

427

428
;;
429

430
match load_include input with
431
 | [ Page & p ] -> let _ = gen_page ([],p,[], [], compute_sitemap p) in []
432
 | _ -> raise ("Invalid input document " @ input)
433

434