1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 module R = Helm_registry
30 module HG = Http_getter
43 input_package: string;
44 output_package: string;
45 input_base_uri: string;
46 output_base_uri: string;
50 output_type: T.output_kind;
51 includes: (string * string) list;
52 coercions: (string * string) list;
54 requires: (string * string) list;
58 let default_script = {
59 name = ""; is_ma = false; contents = []
62 let default_scripts = 2
64 let load_registry registry =
65 let suffix = ".conf.xml" in
67 if Filename.check_suffix registry suffix then registry
68 else registry ^ suffix
70 Printf.eprintf "reading configuration %s ...\n" registry; flush stderr;
74 let eof ich = try Some (input_line ich) with End_of_file -> None in
75 let trim l = Filename.chop_extension (Str.string_after l 2) in
76 let cmd = Printf.sprintf "cd %s && find -name *%s" st.input_path st.input_type in
77 let ich = Unix.open_process_in cmd in
80 | None -> List.rev files
81 | Some l -> aux (trim l :: files)
84 let _ = Unix.close_process_in ich in
85 {st with files = files}
88 let map file = (Filename.basename file, file) in
89 let requires = List.rev_map map st.files in
90 {st with requires = requires}
93 let transcript_dir = Filename.dirname Sys.argv.(0) in
94 let default_registry = Filename.concat transcript_dir "transcript" in
95 let matita_registry = Filename.concat !O.cwd "matita" in
96 load_registry default_registry;
97 load_registry matita_registry;
102 let get_pairs = R.get_list (R.pair id id) in
103 let get_output_type key =
104 match R.get_string key with
105 | "procedural" -> T.Procedural
106 | "declarative" -> T.Declarative
107 | _ -> failwith "unknown output type"
109 load_registry registry;
111 heading_path = R.get_string "transcript.heading_path";
112 heading_lines = R.get_int "transcript.heading_lines";
113 input_package = R.get_string "package.input_name";
114 output_package = R.get_string "package.output_name";
115 input_base_uri = R.get_string "package.input_base_uri";
116 output_base_uri = R.get_string "package.output_base_uri";
117 input_path = R.get_string "package.input_path";
118 output_path = R.get_string "package.output_path";
119 input_type = R.get_string "package.input_type";
120 output_type = get_output_type "package.output_type";
121 includes = get_pairs "package.include";
122 coercions = get_pairs "package.coercion";
125 scripts = Array.make default_scripts default_script
128 heading_path = Filename.concat !O.cwd st.heading_path;
129 output_path = Filename.concat !O.cwd st.output_path;
131 prerr_endline "reading file names ...";
132 let st = set_files st in
133 let st = set_requires st in
136 let get_index st name =
137 let rec get_index name i =
138 if i >= Array.length st.scripts then None else
139 if st.scripts.(i).name = name then Some i else
140 get_index name (succ i)
142 match get_index name 0, get_index "" 0 with
143 | Some i, _ | _, Some i -> i
144 | None, None -> failwith "not enought script entries"
147 let i = get_index st name in
148 let script = st.scripts.(i) in
149 st.scripts.(i) <- {script with is_ma = true}
151 let set_items st name items =
152 let i = get_index st name in
153 let script = st.scripts.(i) in
154 let contents = List.rev_append items script.contents in
155 st.scripts.(i) <- {script with name = name; contents = contents}
157 let set_heading st name =
158 let heading = st.heading_path, st.heading_lines in
159 set_items st name [T.Heading heading]
161 let require st name inc =
162 set_items st name [T.Include inc]
164 let get_coercion st str =
165 try List.assoc str st.coercions with Not_found -> ""
168 List.fold_left Filename.concat "" (List.rev path)
170 let make_prefix path =
171 String.concat "__" (List.rev path) ^ "__"
173 let make_script_name st script name =
174 let ext = if script.is_ma then ".ma" else ".mma" in
175 Filename.concat st.output_path (name ^ ext)
178 let i = get_index st name in
179 let script = st.scripts.(i) in
180 let path = Filename.concat st.output_path (Filename.dirname name) in
181 let name = make_script_name st script name in
182 let cmd = Printf.sprintf "mkdir -p %s" path in
183 let _ = Sys.command cmd in
184 let och = open_out name in
185 G.commit st.output_type och script.contents;
187 st.scripts.(i) <- default_script
190 let init name = set_heading st name in
191 let partition = function
192 | T.Coercion (false, _)
193 | T.Notation (false, _) -> false
196 let produce st name =
197 let in_base_uri = Filename.concat st.input_base_uri name in
198 let out_base_uri = Filename.concat st.output_base_uri name in
199 let filter path = function
200 | T.Inline (b, k, obj, p, f) ->
202 if b then Filename.concat (make_path path) obj, make_prefix path
205 let s = obj ^ G.string_of_inline_kind k in
206 path, Some (T.Inline (b, k, Filename.concat in_base_uri s, p, f))
209 try path, Some (T.Include (List.assoc s st.requires))
210 with Not_found -> path, None
212 | T.Coercion (b, obj) ->
213 let str = get_coercion st obj in
214 if str <> "" then path, Some (T.Coercion (b, str)) else
215 let base_uri = if b then out_base_uri else in_base_uri in
216 let s = obj ^ G.string_of_inline_kind T.Con in
217 path, Some (T.Coercion (b, Filename.concat base_uri s))
218 | T.Section (b, id, _) as item ->
219 let path = if b then id :: path else List.tl path in
221 | item -> path, Some item
223 let set_includes st name =
224 try require st name (List.assoc name st.includes)
227 Printf.eprintf "processing file name: %s ...\n" name; flush stderr;
228 let file = Filename.concat st.input_path (name ^ st.input_type) in
229 let ich = open_in file in
230 let lexbuf = Lexing.from_channel ich in
232 let items = V8Parser.items V8Lexer.token lexbuf in
234 let _, rev_items = X.list_rev_map_filter_fold filter [] items in
235 let items = List.rev rev_items in
236 let local_items, global_items = List.partition partition items in
237 let comment = T.Line (Printf.sprintf "From %s" name) in
238 if global_items <> [] then
239 set_items st st.input_package (comment :: global_items);
240 init name; require st name st.input_package; set_includes st name;
241 set_items st name local_items; commit st name
243 prerr_endline (Printexc.to_string e); close_in ich
245 is_ma st st.input_package;
246 init st.input_package; require st st.input_package "preamble";
247 List.iter (produce st) st.files;
248 commit st st.input_package