]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/binaries/transcript/engine.ml
transcript updated
[helm.git] / helm / software / components / binaries / transcript / engine.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 module R = Helm_registry
27 module X = HExtlib
28 module T = Types
29 module G = Grafite
30
31 type script = {
32    name: string;
33    contents: T.items
34 }
35
36 type status = {
37    helm_dir: string;
38    heading_path: string;
39    heading_lines: int;
40    package: string;
41    input_base_uri: string;
42    output_base_uri: string;
43    input_path: string;
44    output_path: string;
45    script_ext: string;
46    files: string list;
47    requires: (string * string) list;
48    scripts: script array
49 }
50
51 let default_script = { 
52    name = ""; contents = []
53 }
54
55 let default_scripts = 2
56
57 let load_registry registry =
58    let suffix = ".conf.xml" in
59    let registry = 
60       if Filename.check_suffix registry suffix then registry
61       else registry ^ suffix
62    in
63    Printf.eprintf "reading configuration %s ...\n" registry; flush stderr;
64    R.load_from registry
65
66 let set_files st =
67    let eof ich = try Some (input_line ich) with End_of_file -> None in
68    let trim l = Filename.chop_extension (Str.string_after l 2) in
69    let cmd = Printf.sprintf "cd %s && find -name *%s" st.input_path st.script_ext in
70    let ich = Unix.open_process_in cmd in
71    let rec aux files =
72       match eof ich with
73          | None   -> List.rev files
74          | Some l -> aux (trim l :: files)
75    in 
76    let files = aux [] in
77    let _ = Unix.close_process_in ich in
78    {st with files = files}
79
80 let set_requires st =
81    let map file = (Filename.basename file, file) in
82    let requires = List.rev_map map st.files in
83    {st with requires = requires}
84
85 let init () = 
86    let default_registry = "transcript" in
87    load_registry default_registry
88
89 let make registry =
90    load_registry registry;
91    let st = {
92       helm_dir = R.get_string "transcript.helm_dir";
93       heading_path = R.get_string "transcript.heading_path";
94       heading_lines = R.get_int "transcript.heading_lines";
95       package = R.get_string "package.name";
96       input_base_uri = R.get_string "package.input_base_uri";
97       output_base_uri = R.get_string "package.output_base_uri";
98       input_path = R.get_string "package.input_path";
99       output_path = R.get_string "package.output_path";
100       script_ext = R.get_string "package.script_type";
101       files = [];
102       requires = [];
103       scripts = Array.make default_scripts default_script
104    } in
105    prerr_endline "reading file names ...";
106    let st = set_files st in
107    let st = set_requires st in
108    st
109
110 let get_index st name = 
111    let rec get_index name i =
112       if i >= Array.length st.scripts then None else 
113       if st.scripts.(i).name = name then Some i else 
114       get_index name (succ i)
115    in
116    match get_index name 0, get_index "" 0 with
117       | Some i, _ | _, Some i -> i
118       | None, None            -> failwith "not enought script entries"
119
120 let set_items st name items =
121    let i = get_index st name in
122    let script = st.scripts.(i) in
123    let contents = List.rev_append items script.contents in
124    st.scripts.(i) <- {name = name; contents = contents}
125    
126 let set_heading st name =
127    let heading = Filename.concat st.helm_dir st.heading_path, st.heading_lines in
128    set_items st name [T.Heading heading] 
129    
130 let set_baseuri st name =
131    let baseuri = Filename.concat st.output_base_uri name in
132    set_items st name [T.BaseUri baseuri]
133    
134 let commit st name =
135    let i = get_index st name in
136    let script = st.scripts.(i) in
137    let path = Filename.concat st.output_path (Filename.dirname name) in
138    let name = Filename.concat st.output_path (name ^ ".ma") in
139    let cmd = Printf.sprintf "mkdir -p %s" path in
140    let _ = Sys.command cmd in
141    let och = open_out name in
142    G.commit och script.contents;
143    close_out och;
144    st.scripts.(i) <-  default_script
145    
146 let produce st =
147    let init name = set_heading st name; set_baseuri st name in
148    let partition = function
149       | T.Coercion _ 
150       | T.Notation _ -> false
151       | _            -> true
152    in
153    let produce st name =
154       let base_uri = Filename.concat st.input_base_uri name in
155       let filter = function
156          | T.Inline (k, obj) -> 
157             let s = obj ^ G.string_of_inline_kind k in
158             Some (T.Inline (k, Filename.concat base_uri s))
159          | item              -> Some item
160       in
161       Printf.eprintf "processing file name: %s ...\n" name; flush stderr; 
162       let file = Filename.concat st.input_path (name ^ st.script_ext) in
163       let ich = open_in file in
164       let lexbuf = Lexing.from_channel ich in
165       try 
166          let items = V8Parser.items V8Lexer.token lexbuf in
167          close_in ich;
168          let items = List.rev (X.list_rev_map_filter filter items) in
169          let local_items, global_items = List.partition partition items in
170          let comment = T.Line (Printf.sprintf "From %s" name) in 
171          if global_items <> [] then 
172             set_items st st.package (comment :: global_items);
173          init name; set_items st name local_items; commit st name
174       with e -> 
175          prerr_endline (Printexc.to_string e); close_in ich 
176    in
177    init st.package; List.iter (produce st) st.files; commit st st.package