site.cd 5.9 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
type SitePage = 
   <page {|input=String; output=String|}>[]
 | <external {|href=String; title=String|}>[];;
type Site = <site>[ SitePage* ];;
11

12
13
14
15
16
type Page = 
  <page>[
    <title>String
    <banner>[InlineText*]
    <navig>[ NavigBox* ] <main>[ Box* ] ];;
17
18
19

type Author = <author>String;;
type Paper = 
20
21
22
23
24
25
  <paper file=?String>[ 
    <title>String 
    Author+ 
    <comment>[InlineText*] 
    <abstract>Content ];;

26
type Slides = 
27
28
29
30
  <slides file=String>[ 
   <title>String 
   Author+ 
   <comment>[InlineText*] ];;
31
32

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

type Content =      
   [ ( <p {||}>[InlineText*]
     | <ul {||}>[<li {||}>Content +]
     | <section title=String>Content
39
     | Xtable
40
41
42
     | Paper | Slides | Link 
     | <include-verbatim file=String>[]
     | InlineText )* ];;
43
44
45

type InlineText =
     Char
46
   | <(`b|`i|`tt) {||}>[InlineText*]
47
   | <duce>[InlineText*]
48
   | Xa
49
   | Ximg | Xbr ;;
50
51
52
53
54
55
56
57
58
59

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;;

60
let fun text (t : [InlineText*]) : Inlines =
61
62
63
64
  map t with
   | <duce>x -> <b>[ <tt>(text x) ]
   | <(tag & (`b|`i|`tt))>x -> <(tag)>(text x)
   | z -> z;;
65

66
67
let fun content (t : Content) : Flow =
  transform t with
68
69
   | <section title=title>c -> 
         [ <h4>title !(content c) ]
70
   | <paper (r)>[ <title>tit aut::Author* <comment>com <abstract>ab  ]  ->
71
         [ (match r with { file = f } -> <a href=f>tit | _ -> <b>tit) '. '
72
           !(authors aut) '. '
73
	   !(text com)
74
75
76
           <div class="abstract">[ 'Abstract:' !(content ab) ]
         ]
   | <slides file=f>[ <title>tit aut::Author* <comment>com ]  ->
77
        [ <a href=f>tit '. ' !(authors aut) '. ' !(text com) ]
78
   | <include-verbatim file=f>[] ->
79
80
81
82
83
84
85
        [ <div class="code">[ <pre>(load_file f) ] ]
   | <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 ]
86
87
   | <p>x -> [ <p>(text x) ]
   | x -> text [ x ];;
88
89
90
91

let fun main2html (Box -> Flow)
  <box (r)>c ->
   [ <div class="box">[
92
       <h2>(r . title)
93
        !(match r with { subtitle = t } -> [<b>t] | _ -> [])
94
       <a name=r . link>[] 
95
96
97
98
       !(content c)  ] ]
| <meta>c -> [ <div class="meta">(content c) ];;


99
100
101
102
103
(* 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. *)

104
105
106
107
let php_css : String =
[' <?php
$browser = getenv("HTTP_USER_AGENT");
if (preg_match("/MSIE/i", "$browser")) {
108
109
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
110
111
112
} elseif (preg_match("/Mozilla/i", "$browser")) {
        $css = "<blink>For better presentation use a more recent version
of your browser, like Netscape 6</blink>";
113
114
115
} if (preg_match("/Mozilla\\/5.0/i", "$browser")) {
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
116
} elseif  (preg_match("/opera/i", "$browser")) {
117
118
        $css = "<link rel=\\"stylesheet\\" href=\\"cduce.css\\"
type=\\"text/css\\">";
119
120
121
122
123
124
125
126
127
}
echo "$css";
?> '];;


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

128
129
130
131
132
133
134
135
136
137
138
139
140
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">[]
141
   <meta content="css">[]  (* Placeholder for PHP code *)
142
143
  ]
  <body>[ 
144
   <div class="title">[ <h1>(text banner) ]
145
146
147
148
149
   <div id="Sidelog">navig
   <div id="Content">(transform main with b -> main2html b)
  ]
 ];;

150
type P = (String,<title>String);;
151

152
let fun make_plan (l : [ P+ ]) : Page =
153
154
155
156
157
158
159
160
161
162
163
<page>[ 
  <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.' ]
  ]
];;
164
165
166

let fun do_page((Page,String) -> [])
 (page,outf) ->
167
168
    let _ = print [ 'Generating html ... ' ] in
    let html : String = 
169
       [ (* '<?xml version="1.0" encoding="iso-8859-1"?>' *)
170
171
         '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
         '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
172
         !(patch_css (print_xml (page2html page))) ] in
173
174
    let _ = print [ 'Saving to ' !outf '...\n' ] in
    dump_to_file outf html;;
175
176
177
178
179
180
181
182
183
184
 
let fun do_file((String,String) -> P)
 (inf,outf) -> 
    let _ = print [ 'Loading ' !inf '... ' ] in
    let page = match load_xml inf with 
      | Page & p -> p
      | _ -> raise ("Invalid input document: " @ inf) in
    let _ = do_page (page,outf) in
    let tit = match [page]/<title>_ with [t] -> t in
    (outf, tit);;
185
186


187
188
189
190
let site =     
 let _ = print [ 'Loading site.xml ...\n' ] in
 match load_xml "site.xml" with
 | Site & <site>s ->
191
192
193
194
     let ts = map s with 
             | <page input=inf; output=outf>_ -> do_file(inf,outf)
             | <external href=url; title=t>_ -> (url,<title>t) in
     let _ = print [ 'Create plan... ' ] in
195
196
     let plan = make_plan (ts @ [("plan.php", <title>"CDuce site")]) in
     do_page(plan,"plan.php")
197
 | _ -> raise "Invalid site.xml";;