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


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

7
8
9
10
11
12
13
14

let fun load_include (String -> [Any*])
 name ->
   let _ = print [ 'Loading ' !name '... \n' ] in
   xtransform [ (load_xml name) ] with 
     <include file=(s & String)>[] -> load_include s;; 


15
16
17
18
let fun highlight (String -> [ (Char | Xvar)* ] )
 | [ '{{' h ::(Char *?) '}}' ; rest ] -> 
          [ <var class="highlight">h; highlight rest ]
 | [ c; rest ] -> [ c; highlight rest ]
19
20
21
 | [] -> [];;


22
type SitePage = 
23
   Page
24
25
 | <external {|href=String; title=String|}>[];;
type Site = <site>[ SitePage* ];;
26

27
type Page = 
28
  <page output=String>[
29
30
31
    <title>String
    <banner>[InlineText*]
    <navig>[ NavigBox* ] <main>[ Box* ] ];;
32
33
34

type Author = <author>String;;
type Paper = 
35
36
37
38
39
40
  <paper file=?String>[ 
    <title>String 
    Author+ 
    <comment>[InlineText*] 
    <abstract>Content ];;

41
type Slides = 
42
43
44
45
  <slides file=String>[ 
   <title>String 
   Author+ 
   <comment>[InlineText*] ];;
46
47

type Link =
48
  <link url=String; title=String>[ InlineText* ];;
49
50
51
52
53

type Content =      
   [ ( <p {||}>[InlineText*]
     | <ul {||}>[<li {||}>Content +]
     | <section title=String>Content
54
     | <sample>String
55
     | Xtable
56
57
58
     | Paper | Slides | Link 
     | <include-verbatim file=String>[]
     | InlineText )* ];;
59
60
61

type InlineText =
     Char
62
63
   | <(`b|`i|`tt|`em) {||}>[InlineText*]
   | <duce>String
64
   | Xa
65
   | Ximg | Xbr ;;
66
67
68
69
70
71
72
73
74
75

type Box = <box title=String; subtitle=?String; link=String>Content
         | <meta>Content;;
type NavigBox = <box>Content | <toc>[];;

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

76
let fun text (t : [InlineText*]) : Inlines =
77
  map t with
78
   | <duce>x -> <b>[ <tt>(highlight x) ]
79
   | <(tag & (`b|`i|`tt|`em))>x -> <(tag)>(text x)
80
   | z -> z;;
81

82
83
let fun content (t : Content) : Flow =
  transform t with
84
85
   | <section title=title>c -> 
         [ <h4>title !(content c) ]
86
   | <paper (r)>[ <title>tit aut::Author* <comment>com <abstract>ab  ]  ->
87
         [ (match r with { file = f } -> <a href=f>tit | _ -> <b>tit) '. '
88
           !(authors aut) '. '
89
	   !(text com)
90
91
92
           <div class="abstract">[ 'Abstract:' !(content ab) ]
         ]
   | <slides file=f>[ <title>tit aut::Author* <comment>com ]  ->
93
        [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
94
   | <include-verbatim file=f>[] ->
95
        [ <div class="code">[ <pre>(load_file f) ] ]
96
   | <sample>s ->
97
        [ <div class="code">[ <pre>(highlight s) ] ]
98
99
100
101
102
103
   | <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 ]
104
105
   | <p>x -> [ <p>(text x) ]
   | x -> text [ x ];;
106
107
108
109

let fun main2html (Box -> Flow)
  <box (r)>c ->
   [ <div class="box">[
110
       <h2>(r . title)
111
        !(match r with { subtitle = t } -> [<b>t] | _ -> [])
112
       <a name=r . link>[] 
113
114
115
116
       !(content c)  ] ]
| <meta>c -> [ <div class="meta">(content c) ];;


117
118
119
120
121
(* 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. *)

122
123
124
125
let php_css : String =
[' <?php
$browser = getenv("HTTP_USER_AGENT");
if (preg_match("/MSIE/i", "$browser")) {
126
127
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
128
129
130
} elseif (preg_match("/Mozilla/i", "$browser")) {
        $css = "<blink>For better presentation use a more recent version
of your browser, like Netscape 6</blink>";
131
132
133
} if (preg_match("/Mozilla\\/5.0/i", "$browser")) {
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
134
} elseif  (preg_match("/opera/i", "$browser")) {
135
136
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
137
138
139
140
141
142
143
144
145
}
echo "$css";
?> '];;


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

146
147
148
149
150
151
152
153
154
155
156
157
158
let fun page2html (Page -> Xhtml)
<page>[ <title>title <banner>banner <navig>navig <main>main ] ->
 let toc = 
   transform main with <box title=t; link=l>_ -> [ <li>[ <a href=('#',l)>t ] ] in
 let toc = match toc with [] -> [] | lis -> [ <ul>lis ] in
 let navig : Flow = transform navig with
  | <box>c -> [ <div class="box">(content c) ]
  | <toc>[] -> [ <div class="box">toc ]
 in
 <html>[
  <head>[ 
   <title>title
   <meta content="text/html; charset=iso-8859-1"; http-equiv="Content-Type">[]
159
   <meta content="css">[]  (* Placeholder for PHP code *)
160
161
  ]
  <body>[ 
162
   <div class="title">[ <h1>(text banner) ]
163
164
165
166
167
   <div id="Sidelog">navig
   <div id="Content">(transform main with b -> main2html b)
  ]
 ];;

168
type P = (String,<title>String);;
169

170
let fun make_plan (l : [ P+ ]) : Page =
171
<page output="plan.php">[ 
172
173
174
175
176
177
178
179
180
181
  <title>"CDuce site"
  <banner>"CDuce site"
  <navig>[ <box>[ <a href="/">"Home" ] ]
  <main>[ 
    <box title="Pages"; link="pages">[
      <ul>(map l with (file,<title>t) -> <li>[<a href=file>t]) 
    ]
    <meta>[ 'This page was automatically generated by a CDuce program.' ]
  ]
];;
182

183
184
185
let fun do_page(Page -> P)
 <page output=outf>[ tit & <title>_; _ ] & page ->
    let _ = print [ 'Generating html... ' ] in
186
    let html : String = 
187
       [ (* '<?xml version="1.0" encoding="iso-8859-1"?>' *)
188
189
         '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
         '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
190
         !(patch_css (print_xml (page2html page))) ] in
191
    let _ = print [ 'Saving to ' !outf '...\n' ] in
192
    let _ = dump_to_file outf html in
193
    (outf, tit);;
194
 
195
let site =     
196
197
 match load_include "site.xml" with
 | [ Site & <site>s ] ->
198
     let ts = map s with 
199
             | Page & p -> do_page p
200
201
             | <external href=url; title=t>_ -> (url,<title>t) in
     let _ = print [ 'Create plan... ' ] in
202
     let plan = make_plan (ts @ [("plan.php", <title>"CDuce site")]) in
203
204
     let _ = do_page plan in
     []
205
 | _ -> raise "Invalid site.xml";;