site.cd 8.92 KB
Newer Older
1
2
(* This CDuce script produces CDuce web site. *)

3
(** Output types **)
4

5
6
include "xhtml-strict.cd";;  (* XHTML 1 Strict DTD *)
include "xhtml-categ.cd";;   (* Categories (Inline, ...) from this DTD *)
7

8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(** Input types **)

type Page =  <page name=String>[ <title>String <banner>[InlineText*] Item* ];;
type External = <external {|href=String; title=String; name=String |}>[];;

type Item = 
   <box title=String; subtitle=?String; link=String>Content
 | <meta>Content
 | <left>Content
 | Page
 | External;;

type Author = <author>String;;
type Paper = 
  <paper file=?String>[ 
     <title>String Author+ <comment>[InlineText*] <abstract>Content ];;

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

type Link =
  <link url=String; title=String>[ InlineText* ];;

type Content =      
   [ ( <p {||}>[InlineText*]
     | <ul {||}>[<li {||}>Content +]
     | <section title=String>Content
36
     | <sample highlight=?"true"|"false">String
37
38
39
40
41
42
     | Xtable
     | Paper | Slides | Link
     | <boxes-toc>[]
     | <pages-toc>[]
     | <site-toc>[]
     | <local-links href=String>[]
43
     | <two-columns>[ <left>Content <right>Content ]
44
45
46
47
48
49
50
     | InlineText
     )* ];;

type InlineText =
     Char
   | <(`b|`i|`tt|`em) {||}>[InlineText*]
   | <code>String
51
   | <local href=String>String
52
53
54
55
56
57
58
   | Xa | Ximg | Xbr ;;


(** Generic purpose functions **)

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

59
60
let fun load_include (String -> [Any*])
 name ->
61
(*   let _ = print [ 'Loading ' !name '... \n' ] in *)
62
   xtransform [ (load_xml name) ] with 
63
64
   | <include file=(s & String)>[] -> load_include s
   | <include-verbatim file=(s & String)>[] -> load_file s;; 
65

66
(* Highlighting text between {{...}} *)
67

68
69
70
71
let fun highlight (String -> [ (Char | Xvar)* ] )
 | [ '{{' h ::(Char *?) '}}' ; rest ] -> 
          [ <var class="highlight">h; highlight rest ]
 | [ c; rest ] -> [ c; highlight rest ]
72
73
 | [] -> [];;

74
75
(* Split a comma-separated string *)

76
77
78
79
80
let fun split_comma (String -> [String*])
 | [ x::(Char*?) ',' ; rest ] -> (x, split_comma rest)
 | s -> [ s ];;


81
82
83
84
(** Ugly hack to introduce PHP code ...
    The idea is to produce first an XML document with a distinguished element.
    The function patch_css search for the textual representation of this
    element and replace it with the PHP code. **)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

let php_css : String =
[' <?php
$browser = getenv("HTTP_USER_AGENT");
if (preg_match("/MSIE/i", "$browser")) {
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
} elseif (preg_match("/Mozilla/i", "$browser")) {
        $css = "<blink>For better presentation use a more recent version
of your browser, like Netscape 6</blink>";
} if (preg_match("/Mozilla\\/5.0/i", "$browser")) {
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
} elseif  (preg_match("/opera/i", "$browser")) {
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
}
echo "$css";
?> '];;


let fun patch_css (String -> String)
| [ a::_*? '<meta content="css"/>'; rem ] -> a @ php_css @ rem
| s -> s;;



112
(** Internal types **)
113

114
115
116
type Path = [ { url = String; title = String }* ];;
type Tree = { name = String; url = String; title = String; 
              children = [Tree*] } ;;
117

118
119
120
121
let fun url_of_name (String -> String)
   "index" -> "/"
 | s -> s @ ".html";;

122
123
124
125
126
let fun authors ([Author+] -> String)
   | [ <author>a ] -> a
   | [ <author>a1 <author>a2 ] -> a1 @ ", and " @ a2
   | [ <author>a; rem ] -> a @ ", " @ authors rem;;

127
let fun find_local_link (sitemap : [Tree*], l : String) : Tree =
128
129
match sitemap with
 | (h,t) ->
130
   if (h . name = l) then h
131
132
133
134
135
   else 
    (try find_local_link (t,l) with `Not_found -> 
         find_local_link (h . children,l))
 | [] -> raise `Not_found;;

136
137
138
139
140
let fun local_link (sitemap : Tree, l : String, txt : String) : Inline =
 try 
  let h = find_local_link ([sitemap],l)  in
  let txt = if txt = "" then h . title else txt in
  <a href=(h . url)>txt
141
142
143
144
145
 with `Not_found -> raise [ 'Local link not found: ' !l ];;
 
let fun compute_sitemap ((Page|External) -> Tree)
 <page name=name>[ <title>title (c::(Page|External) | _)* ] ->
   let children = map c with p -> compute_sitemap p in
146
   { name = name; url = url_of_name name; title = title; children =children }
147
148
149
150
151
152
153
154
|<external name=name; href=h; title=t>[] ->
   { name = name; url = h; title = t; children = [] };;

let fun display_sitemap (h : Tree) :  Xli =
  let ch = map h . children with x -> display_sitemap x in
  let ch = match ch with [] -> [] | l -> [ <ul>l ] in
  <li>[ <a href=(h . url)>(h . title); ch ];;

155
156
157
let fun link_to (Page -> Xa)
 <page name=n>[<title>t ; _ ] -> <a href=(url_of_name n)>t;;

158
159
(* Main transformation function *)

160
161
let fun gen_page (prev : Page|[], page : Page, next : Page|[], 
                  path : Path, sitemap : Tree) : [] = 
162
163
164
165
match page with
<page name=name>[ <title>title <banner>banner ; items ] ->

 let fun text (t : [InlineText*]) : Inlines =
166
  map t with
167
   | <code>x -> <b>[ <tt>(highlight x) ]
168
   | <local href=l>txt -> local_link (sitemap,l,txt)
169
   | <(tag & (`b|`i|`tt|`em))>x -> <(tag)>(text x)
170
(*   | <a href=url>_ & z -> let [] = print [ 'Link: ' !url '\n'] in z *)
171
172
   | z -> z
 in
173

174
 let fun content (t : Content) : Flow =
175
  transform t with
176
177
   | <section title=title>c -> 
         [ <h4>title !(content c) ]
178
   | <paper (r)>[ <title>tit aut::Author* <comment>com <abstract>ab  ]  ->
179
         [ (match r with { file = f } -> <a href=f>tit | _ -> <b>tit) '. '
180
           !(authors aut) '. '
181
	   !(text com)
182
183
184
           <div class="abstract">[ 'Abstract:' !(content ab) ]
         ]
   | <slides file=f>[ <title>tit aut::Author* <comment>com ]  ->
185
        [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
186
187
   | <sample highlight="false">s ->
        [ <div class="code">[ <pre>s ] ]
188
   | <sample>s ->
189
        [ <div class="code">[ <pre>(highlight s) ] ]
190
191
192
193
194
195
   | <link url=url; title=title>com -> 
        [ <a href=url>title '. ' !(text com) ]
   | <ul>lis -> 
        [ <ul>(map lis with <li>x -> <li>(content x)) ]
   | Xtable & x -> 
        [ x ]
196
   | <p>x -> [ <p>(text x) ]
197
198
199
   | <pages-toc>[] ->
        let toc = 
         transform items with 
200
         | Page & p ->  [ <li>[ (link_to p) ] ]
201
202
203
204
205
206
207
208
209
210
211
212
         | <external href=l; title=t>[] -> [ <li>[ <a href=l>t ] ] in
        (match toc with [] -> [] | lis -> [ <ul>lis ])
   | <boxes-toc>[] ->
        let toc = 
         transform items with 
          <box title=t; link=l>_ -> [ <li>[ <a href=('#',l)>t ] ] in
        (match toc with [] -> [] | lis -> [ <ul>lis ])
   | <site-toc>[] ->
        [ <ul>[ (display_sitemap sitemap) ] ]
   | <local-links href=s>[] ->
         (match (split_comma s) with
           | [] -> []
213
           | l -> let l = map l with x -> <li>[ (local_link(sitemap,x,"")) ]
214
                  in [ <ul>l ])
215
216
217
218
219
   | <two-columns>[ <left>x <right>y ] ->
	[ <table width="100%">[ 
            <tr>[ 
              <td valign="top">(content x) 
              <td valign="top">(content y) ] ] ]
220
221
   | t -> text [ t ]
 in
222

223
224
225
226
227
228
229
230
231
232
233
234
235
236
 let main : Flow = transform items with
  | <box (r)>c ->
     [ <div class="box">[
         <h2>(r . title)
          !(match r with { subtitle = t } -> [<b>t] | _ -> [])
         <a name=r . link>[] 
         !(content c)  ] ]
  | <meta>c -> [ <div class="meta">(content c) ] 
 in
 let navig : Flow = transform items with 
  | <left>c -> [<div class="box">(content c)] 
 in
 let dpath : Inlines = transform path with 
  | { url = f; title = t } -> [ <a href=f>t ' :: '] 
237
 in
238
239
240
241
242
 let npath = path @ [ { url = url_of_name name; title = title } ] in
 let subpages = transform items with p & Page -> [ p ] in
 let next = gen_page_seq (page, subpages, next, npath, sitemap) in
 let next = match next with [] -> [] | p -> [' Next : ' (link_to p)] in
 let prev = match prev with [] -> [] | p -> [' Prev : ' (link_to p)] in
243
 let html : Xhtml =
244
245
 <html>[
  <head>[ 
246
   <title>[ 'CDuce: ' !title ]
247
   <meta content="text/html; charset=iso-8859-1"; http-equiv="Content-Type">[]
248
   <meta content="css">[]  (* Placeholder for PHP code *)
249
250
  ]
  <body>[ 
251
252
253
254
255
   <div class="title">[ 
        <h1>(text banner) 
        <p>[ <b>"You're here: " !dpath !title ]
        <p>[ !prev !next ]
   ]
256
   <div id="Sidelog">navig
257
   <div id="Content">main
258
  ]
259
260
261
 ]
 in
 let txt : String = 
262
   [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
263
264
     '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
     !(patch_css (print_xml html)) ] in
265
266
267
268
269
270
271
272
273
274
275
276
277
 dump_to_file (name @ ".html.php") txt;;

let fun gen_page_seq 
 (prev : Page|[], pages : [Page*], next : Page|[], 
  path : Path, sitemap : Tree) : Page|[] =
 match pages with
 | [ p1 p2 ; _ ] & [ _; rest ] -> 
     let [] = gen_page (prev,p1,p2, path, sitemap) in
     let _  = gen_page_seq (p1, rest, next, path, sitemap) in p1
 | [ p ] ->
     let [] = gen_page (prev,p,next, path, sitemap) in p
 | [] -> next;;

278

279
(* Entry point *)
280

281
282
283
284
285
286
match argv with
 | [ s ] ->
    (match load_include s with
     | [ Page & p ] -> gen_page ([],p,[], [], compute_sitemap p)
     | _ -> raise ("Invalid input document " @ s))
 | _ -> raise "Please specify an input file on the command line";;