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
39 input_package: string;
40 output_package: string;
41 input_base_uri: string;
42 output_base_uri: string;
46 output_type: T.output_kind;
47 coercions: (string * string) list;
49 requires: (string * string) list;
53 let default_script = {
54 name = ""; contents = []
57 let default_scripts = 2
59 let load_registry registry =
60 let suffix = ".conf.xml" in
62 if Filename.check_suffix registry suffix then registry
63 else registry ^ suffix
65 Printf.eprintf "reading configuration %s ...\n" registry; flush stderr;
69 let eof ich = try Some (input_line ich) with End_of_file -> None in
70 let trim l = Filename.chop_extension (Str.string_after l 2) in
71 let cmd = Printf.sprintf "cd %s && find -name *%s" st.input_path st.input_type in
72 let ich = Unix.open_process_in cmd in
75 | None -> List.rev files
76 | Some l -> aux (trim l :: files)
79 let _ = Unix.close_process_in ich in
80 {st with files = files}
83 let map file = (Filename.basename file, file) in
84 let requires = List.rev_map map st.files in
85 {st with requires = requires}
88 let transcript_dir = Filename.dirname Sys.argv.(0) in
89 let default_registry = Filename.concat transcript_dir "transcript" in
90 load_registry default_registry
92 let make cwd registry =
94 let get_coercions = R.get_list (R.pair id id) in
95 let get_output_type key =
96 match R.get_string key with
97 | "procedural" -> T.Procedural
98 | "declarative" -> T.Declarative
99 | _ -> failwith "unknown output type"
101 load_registry registry;
103 heading_path = R.get_string "transcript.heading_path";
104 heading_lines = R.get_int "transcript.heading_lines";
105 input_package = R.get_string "package.input_name";
106 output_package = R.get_string "package.output_name";
107 input_base_uri = R.get_string "package.input_base_uri";
108 output_base_uri = R.get_string "package.output_base_uri";
109 input_path = R.get_string "package.input_path";
110 output_path = R.get_string "package.output_path";
111 input_type = R.get_string "package.input_type";
112 output_type = get_output_type "package.output_type";
113 coercions = get_coercions "package.coercion";
116 scripts = Array.make default_scripts default_script
119 heading_path = Filename.concat cwd st.heading_path;
120 output_path = Filename.concat cwd st.output_path;
122 prerr_endline "reading file names ...";
123 let st = set_files st in
124 let st = set_requires st in
127 let get_index st name =
128 let rec get_index name i =
129 if i >= Array.length st.scripts then None else
130 if st.scripts.(i).name = name then Some i else
131 get_index name (succ i)
133 match get_index name 0, get_index "" 0 with
134 | Some i, _ | _, Some i -> i
135 | None, None -> failwith "not enought script entries"
137 let set_items st name items =
138 let i = get_index st name in
139 let script = st.scripts.(i) in
140 let contents = List.rev_append items script.contents in
141 st.scripts.(i) <- {name = name; contents = contents}
143 let set_heading st name =
144 let heading = st.heading_path, st.heading_lines in
145 set_items st name [T.Heading heading]
147 let set_baseuri st name =
148 let baseuri = Filename.concat st.output_base_uri name in
149 set_items st name [T.BaseUri baseuri]
151 let require st name inc =
152 set_items st name [T.Include inc]
154 let get_coercion st str =
155 try List.assoc str st.coercions with Not_found -> ""
158 List.fold_left Filename.concat "" (List.rev path)
160 let make_prefix path =
161 String.concat "__" (List.rev path) ^ "__"
164 let i = get_index st name in
165 let script = st.scripts.(i) in
166 let path = Filename.concat st.output_path (Filename.dirname name) in
167 let name = Filename.concat st.output_path (name ^ ".ma") in
168 let cmd = Printf.sprintf "mkdir -p %s" path in
169 let _ = Sys.command cmd in
170 let och = open_out name in
171 G.commit st.output_type och script.contents;
173 st.scripts.(i) <- default_script
176 let init name = set_heading st name; set_baseuri st name in
177 let partition = function
178 | T.Coercion (false, _)
179 | T.Notation (false, _) -> false
182 let produce st name =
183 let in_base_uri = Filename.concat st.input_base_uri name in
184 let out_base_uri = Filename.concat st.output_base_uri name in
185 let filter path = function
186 | T.Inline (b, k, obj, p) ->
188 if b then Filename.concat (make_path path) obj, make_prefix path
191 let s = obj ^ G.string_of_inline_kind k in
192 path, Some (T.Inline (b, k, Filename.concat in_base_uri s, p))
195 try path, Some (T.Include (List.assoc s st.requires))
196 with Not_found -> path, None
198 | T.Coercion (b, obj) ->
199 let str = get_coercion st obj in
200 if str <> "" then path, Some (T.Coercion (b, str)) else
201 let base_uri = if b then out_base_uri else in_base_uri in
202 let s = obj ^ G.string_of_inline_kind T.Con in
203 path, Some (T.Coercion (b, Filename.concat base_uri s))
204 | T.Section (b, id, _) as item ->
205 let path = if b then id :: path else List.tl path in
207 | item -> path, Some item
209 Printf.eprintf "processing file name: %s ...\n" name; flush stderr;
210 let file = Filename.concat st.input_path (name ^ st.input_type) in
211 let ich = open_in file in
212 let lexbuf = Lexing.from_channel ich in
214 let items = V8Parser.items V8Lexer.token lexbuf in
216 let _, rev_items = X.list_rev_map_filter_fold filter [] items in
217 let items = List.rev rev_items in
218 let local_items, global_items = List.partition partition items in
219 let comment = T.Line (Printf.sprintf "From %s" name) in
220 if global_items <> [] then
221 set_items st st.input_package (comment :: global_items);
222 init name; require st name st.input_package;
223 set_items st name local_items; commit st name
225 prerr_endline (Printexc.to_string e); close_in ich
227 init st.input_package; require st st.input_package "preamble";
228 List.iter (produce st) st.files;
229 commit st st.input_package