]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitaFilesystem.ml
Removed filename input box from index.html (just browse the library now).
[helm.git] / matitaB / matita / matitaFilesystem.ml
1 (* Copyright (C) 2004-2011, 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 SvnError of string;;
27
28 let exec_process cmd =
29   let (stdout, stdin, stderr) as chs = Unix.open_process_full cmd [||] in
30   let outlines = ref [] in
31   let errlines = ref [] in
32   (try
33      while true do
34        outlines := input_line stdout :: !outlines;
35      done;
36      assert false
37    with End_of_file -> 
38    (try
39      while true do
40        errlines := input_line stderr :: !errlines;
41      done;
42      assert false
43     with End_of_file -> 
44      match (Unix.close_process_full chs) with
45      | Unix.WEXITED errno -> 
46         let output = "std out =\n" ^ String.concat "\n" (List.rev !outlines) in
47         let errors = "std err =\n" ^ String.concat "\n" (List.rev !errlines) in
48         errno, output ^ "\n\n" ^ errors
49      | _ -> assert false))
50
51 let checkout user =
52   let rt_dir = Helm_registry.get "matita.rt_base_dir" in
53   let repo = Helm_registry.get "matita.weblib" in
54
55   let errno, outstr = exec_process 
56     ("svn co " ^ repo ^ " " ^ rt_dir ^ "/users/" ^ user ^ "/")
57   in
58   if errno = 0 then ()
59   else raise (SvnError outstr)
60
61 let html_of_library uid =
62   let i = ref 0 in
63   let newid () = incr i; ("node" ^ string_of_int !i) in
64
65   let basedir = (Helm_registry.get "matita.rt_base_dir") ^ "/users/" ^ uid in
66
67   let branch text acc =
68     let id = newid () in
69     let name = Filename.basename text in
70     let name = if name <> "." then name else "cic:/matita" in
71     "<span class=\"trigger\" onClick=\"showBranch('" ^ id ^ "')\">\n" ^
72     "<img src=\"treeview/closed.gif\" id=\"I" ^ id ^ "\"/>\n" ^
73     name ^ "<br/></span>\n" ^
74     "<span class=\"branch\" id=\"" ^ id ^ "\">\n" ^
75     acc ^ "\n</span>"
76   in
77   let leaf text =
78     "<img src=\"treeview/doc.gif\"/>\n" ^
79     "<a href=\"javascript:retrieveFile('" ^ text ^ "')\">" ^ 
80      (Filename.basename text) ^ "</a><br/>"
81   in
82
83   let rec aux path =
84     let lpath filename = path ^ "/" ^ filename in
85     let gpath filename = basedir ^ "/" ^ path ^ "/" ^ filename in
86     let dirlist = 
87       List.filter (fun x -> String.sub x 0 1 <> ".") 
88         (Array.to_list (Sys.readdir (basedir ^ "/" ^ path))) in
89     let subdirs = List.filter (fun x -> Sys.is_directory (gpath x)) dirlist in
90     let scripts = 
91       List.filter (fun x -> 
92         try
93           let i = String.rindex x '.' in
94           let len = String.length x - i in
95           not (Sys.is_directory (gpath x)) && 
96           (String.sub x 0 1 <> ".") && (String.sub x i len = ".ma")
97         with Not_found | Invalid_argument _ -> false) dirlist in
98     let subdirtags = 
99       String.concat "\n" (List.map (fun x -> aux (lpath x)) subdirs) in
100     let scripttags =
101       String.concat "\n" 
102        (List.map (fun x -> leaf (lpath x)) scripts)
103     in
104     branch path (subdirtags ^ "\n" ^ scripttags)
105   in
106
107   let res = aux "." in
108   prerr_endline "BEGIN TREE";prerr_endline res;prerr_endline "END TREE";
109   res
110 ;;