1 (* Extraction from ROF XML *)
8 namespace ns2 = "http://cdm-fr.fr/2012/CDM-frSchema"
9 namespace ns3 = "http://cdm-fr.fr/2012/CDM"
12 type ws = ' ' | '\n' | '\t'
13 type content = <entry id=String ref=String title=String >[ output? ]
14 type output = <document class=String>[ ws* <title>String ws* (content | ws)* ]
21 (cmp : 'a -> 'a -> Int)
22 (elems : ['a*]) : ['a*] =
27 (acc1 : ['a*]) (acc2 : ['a*]) : (['a*], ['a*]) =
31 if cmp e pivot <= 0 then split pivot ll (e,acc1) acc2
32 else split pivot ll acc1 (e,acc2)
36 | [e; ll ] -> let (l1, l2) = split e ll [] [] in (quicksort cmp l1) @ (e,(quicksort cmp l2))
40 let cmp_entry (e1 : content) (e2 : content) : Int =
42 (<_ title=t1 ..>_, <_ title=t2 ..>_) -> if t1 << t2 then -1 else if t1 >> t2 then 1 else 0
45 let level_of_int ( Int -> `h1 | `h2 | `h3 | `h4 | `h5 | `h6 )
54 let output_to_html (o : output) (dec : String) (level : Int) : xhtml:div =
57 <document class="course">[ ws* <title>t _ * ] ->
58 <div class="course">[ <span>t <br>[] '\n']
60 | <document class=c >[ ws* <title>t (cl :: content |_)* ] ->
61 <div class=c> [ '\n' !dec ' ' <(level_of_int level)>t '\n' ;
63 e -> entry_to_html e (' ', dec) (level + 1)) @ [
66 (* | String & u -> print_utf8 (u @ "\n"); raise "Impossible" *)
69 let entry_to_html (e : content) (dec : String) (level : Int) :
70 [ (Char | xhtml:div | xhtml:br | xhtml:a )* ] =
71 print_utf8 "Inside entry\n";
73 <entry ref=base_ref title=t ..>[] -> [ !dec <a href=(base_ref @ ".xhtml")>t <br>[] '\n' ]
75 | <entry (_)>[ out ] -> print_utf8 "After matching in entry\n";
78 match (out :? Any) with
79 (* [Any*] -> print_utf8 ("!!!1 " @ (string_of out)) *)
80 | output -> print_utf8 ("!!!2 " @ (string_of out))
83 (*let _ = u in*) (* check this bug in CDuce ! *)
84 [ '\n'<div >[ '\n' !dec (output_to_html out dec (level)) '\n' ] '\n' ]
91 let mk_page ((o & <document ..>[ws* <title>t _*]) : output) (style : String) : xhtml:html =
92 xhtml:create <head>[<title>t
93 <link href="style_common.css" type="text/css" rel="stylesheet">[]
94 <link href=style type="text/css" rel="stylesheet">[]
96 <body>[ (output_to_html o "" 1) ]
100 let first_program_id (doc : AnyXml) : String =
101 match [doc] // <ns2:relatedPrograms ..>_ with
102 [ <_ ..>[ _* <ns2:relationInfo userDefined=(String&id) ..>_ _* ] _*] -> id
103 | _ -> raise "Invalid ROF document"
106 let todo_ids = ref ([(Latin1, output)*]) []
108 let push (x : (Latin1,output)) : [] =
109 todo_ids := (x, !todo_ids)
113 let name_from_descr (l : String) (suff : String): Latin1 =
116 [ ws* (x::Char)*? ws* ] -> x
119 transform l @ "_" @ suff with
121 | x & ('A'--'Z' | 'a'--'z' | '0'--'9') -> [x]
126 let find_by_id (l : [ ('a & <_ id=String ..>_)* ]) (id : String) : ('a & <_ id=String ..>_) =
128 [] -> raise "Not found"
129 | ((<_ id=x ..>_ & y), rest) -> if x = id then y else find_by_id rest id
133 let getContentById (doc : AnyXml) (id : String) : (Latin1,output) =
136 <ns3:CDM ..>[ (l::<(`ns3:program|`ns3:course) id=String ..>_ | _)* ] -> find_by_id l id
137 | _ -> raise "Invalid ROF document"
140 (<ns3:program ..>[ _* <ns2:programName ..>[ _* <ns2:text ..>(String&t) _*]
141 _* (<ns3:programStructure userDefined="ObjetsFils" ..>l | (l:=[]))
142 _* ] & (course:=`false))
143 | (<ns3:course ..>[ _* <ns3:courseName ..>(String&t) _* ] & (l:= []) & (course:=`true))
145 (name_from_descr t id, <document class=(if course then "course" else "box")>[ '\n'
149 |`ns3:refProgram) ref=(String&r) >_ ->
150 print_utf8 ("Looking for id: " @ r @"\n");
151 let (sonref,son) = getContentById doc r in
152 let descr = match son with <document ..>[ _* <title>tt _*] -> tt in
154 [<entry id=r ref=sonref title=descr >[ son ] '\n'])
157 | _ -> raise ("Invalid ROF document (id: " @ id @ ")")
163 let main (_ :[]) : [] =
165 [ (file & Latin1) ((outdir & Latin1) ) _* ] ->
166 (let doc = load_xml file in
167 let id = first_program_id doc in
168 let (file,out) = getContentById doc id in
169 let out = match out with
170 <document ..>[ (x::ws)* <title>t ; l ] ->
171 <document class="box">[ !x <title>t !(quicksort cmp_entry (transform l with <entry (y)>_ -> [<entry (y)>[]])) ]
175 let h = mk_page out "style_parcours_main.css" in
176 xhtml:serialize h `xhtml (outdir @ "/" @ file @".xhtml")
178 transform !todo_ids with
180 let h = mk_page out "style_parcours.css" in
181 xhtml:serialize h `xhtml (outdir @ "/" @ file @".xhtml")
183 | _ -> raise "Invalid command line arguments"