]> matita.cs.unibo.it Git - helm.git/blob - matita/components/library/librarian.ml
bcb84fd6791f9f142defb474dd33f6c8c83958cd
[helm.git] / matita / components / library / librarian.ml
1 (* Copyright (C) 2005, 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://helm.cs.unibo.it/
24  *)
25
26 exception FileNotFound of string
27 exception NoRootFor of string
28
29 let absolutize path =
30  let path = 
31    if String.length path > 0 && path.[0] <> '/' then
32      Sys.getcwd () ^ "/" ^ path
33    else 
34      path
35  in
36    HExtlib.normalize_path path
37 ;;
38
39
40 let find_root path =
41   let path = absolutize path in
42   let paths = List.rev (Str.split (Str.regexp "/") path) in
43   let rec build = function
44     | _he::tl as l -> ("/" ^ String.concat "/" (List.rev l) ^ "/") :: build tl
45     | [] -> ["/"]
46   in
47   let paths = List.map HExtlib.normalize_path (build paths) in
48   try HExtlib.find_in paths "root"
49   with Failure "find_in" -> 
50     raise (NoRootFor (path ^ " (" ^ String.concat ", " paths ^ ")"))
51 ;;
52   
53 let ensure_trailing_slash s = 
54   if s = "" then "/" else
55   if s.[String.length s-1] <> '/' then s^"/" else s
56 ;;
57
58 let remove_trailing_slash s = 
59   if s = "" then "" else
60   let len = String.length s in
61   if s.[len-1] = '/' then String.sub s 0 (len-1) else s
62 ;;
63
64 let load_root_file rootpath =
65   let data = HExtlib.input_file rootpath in
66   let lines = Str.split (Str.regexp "\n") data in
67   let clean s = 
68     Pcre.replace ~pat:"[ \t]+" ~templ:" "
69       (Pcre.replace ~pat:"^ *" (Pcre.replace ~pat:" *$" s))
70   in
71   List.map 
72     (fun l -> 
73       match Str.split (Str.regexp "=") l with
74       | [k;v] -> clean k, Http_getter_misc.strip_trailing_slash (clean v)
75       | _ -> raise (Failure ("Malformed root file: " ^ rootpath)))
76     lines
77 ;;
78
79 let find_root_for ~include_paths file = 
80   let include_paths = "" :: Sys.getcwd () :: include_paths in
81    let find_path_for file =
82       try HExtlib.find_in include_paths file
83       with Failure "find_in" -> 
84        HLog.error ("We are in: " ^ Sys.getcwd ());
85        HLog.error ("Unable to find: "^file^"\nPaths explored:");
86        List.iter (fun x -> HLog.error (" - "^x)) include_paths;
87        raise (FileNotFound file)
88    in
89    let path = find_path_for file in   
90    let path = absolutize path in
91 (*     HLog.debug ("file "^file^" resolved as "^path);  *)
92    let rootpath, root, buri = 
93      try
94        let mburi = Helm_registry.get "matita.baseuri" in
95        match Str.split (Str.regexp " ") mburi with
96        | [root; buri] when HExtlib.is_prefix_of root path -> 
97            ":registry:", root, buri
98        | _ -> raise (Helm_registry.Key_not_found "matita.baseuri")
99      with Helm_registry.Key_not_found "matita.baseuri" -> 
100        let rootpath = find_root path in
101        let buri = List.assoc "baseuri" (load_root_file rootpath) in
102        rootpath, Filename.dirname rootpath, buri
103    in
104 (*     HLog.debug ("file "^file^" rooted by "^rootpath^"");  *)
105    let uri = Http_getter_misc.strip_trailing_slash buri in
106    if String.length uri < 5 || String.sub uri 0 5 <> "cic:/" then
107      HLog.error (rootpath ^ " sets an incorrect baseuri: " ^ buri);
108    ensure_trailing_slash root, remove_trailing_slash uri, path
109  
110 ;;
111
112 let mk_baseuri root extra =
113   let chop name = 
114 (* FG: there is no reason why matita should edit just matita files
115        matita's editor is good on its own and has interesting facilities
116        including predefined virtuals
117     assert(Filename.check_suffix name ".ma" ||
118       Filename.check_suffix name ".mma");
119 *)    
120     try Filename.chop_extension name
121     with Invalid_argument "Filename.chop_extension" -> name
122   in
123    remove_trailing_slash (HExtlib.normalize_path (root ^ "/" ^ chop extra))
124 ;;
125
126 let baseuri_of_script ~include_paths file = 
127   let root, buri, path = find_root_for ~include_paths file in
128   let path = HExtlib.normalize_path path in
129   let root = HExtlib.normalize_path root in
130   let lpath = Str.split (Str.regexp "/") path in
131   let lroot = Str.split (Str.regexp "/") root in
132   let rec substract l1 l2 =
133     match l1, l2 with
134     | h1::tl1,h2::tl2 when h1 = h2 -> substract tl1 tl2
135     | l,[] -> l
136     | _ -> assert false
137   in
138   let extra_buri = substract lpath lroot in
139   let extra = String.concat "/" extra_buri in
140    root,
141    mk_baseuri buri extra,
142    path,
143    extra
144 ;;
145
146 let find_roots_in_dir dir =
147   HExtlib.find ~test:(fun f ->
148     Filename.basename f = "root" &&
149     try (Unix.stat f).Unix.st_kind = Unix.S_REG 
150     with Unix.Unix_error _ -> false)
151     dir
152 ;;
153
154 (* scheme uri part as defined in URI Generic Syntax (RFC 3986) *)
155 let uri_scheme_rex = Pcre.regexp "^[[:alpha:]][[:alnum:]\-+.]*:"
156
157 let is_uri str = Pcre.pmatch ~rex:uri_scheme_rex str