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
41 input_base_uri: string;
42 output_base_uri: string;
47 requires: (string * string) list;
51 let default_script = {
52 name = ""; contents = []
55 let default_scripts = 2
57 let load_registry registry =
58 let suffix = ".conf.xml" in
60 if Filename.check_suffix registry suffix then registry
61 else registry ^ suffix
63 Printf.eprintf "reading configuration %s ...\n" registry; flush stderr;
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
73 | None -> List.rev files
74 | Some l -> aux (trim l :: files)
77 let _ = Unix.close_process_in ich in
78 {st with files = files}
81 let map file = (Filename.basename file, file) in
82 let requires = List.rev_map map st.files in
83 {st with requires = requires}
86 let default_registry = "transcript" in
87 load_registry default_registry
90 load_registry registry;
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";
103 scripts = Array.make default_scripts default_script
105 prerr_endline "reading file names ...";
106 let st = set_files st in
107 let st = set_requires st in
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)
116 match get_index name 0, get_index "" 0 with
117 | Some i, _ | _, Some i -> i
118 | None, None -> failwith "not enought script entries"
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}
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]
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]
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;
144 st.scripts.(i) <- default_script
147 let init name = set_heading st name; set_baseuri st name in
148 let partition = function
150 | T.Notation _ -> false
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))
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
166 let items = V8Parser.items V8Lexer.token lexbuf in
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
175 prerr_endline (Printexc.to_string e); close_in ich
177 init st.package; List.iter (produce st) st.files; commit st st.package