Un-comment deletion of TeX temporary files after rendering is finished
[lhc/web/wiklou.git] / math / render.ml
1 let cmd_dvips tmpprefix = "dvips -R -E " ^ tmpprefix ^ ".dvi -o -"
2 let cmd_latex tmpprefix = "latex " ^ tmpprefix ^ ".tex >/dev/null"
3 let cmd_convert finalpath = "convert -quality 100 -density 120 ps:- " ^ finalpath ^ " >/dev/null 2>/dev/null"
4
5 exception ExternalCommandFailure of string
6
7 let render tmppath finalpath outtex md5 =
8 let tmpprefix = (tmppath^"/"^(string_of_int (Unix.getpid ()))^"_"^md5) in
9 let unlink_all () =
10 begin
11 Sys.remove (tmpprefix ^ ".dvi");
12 Sys.remove (tmpprefix ^ ".aux");
13 Sys.remove (tmpprefix ^ ".log");
14 Sys.remove (tmpprefix ^ ".tex")
15 end in
16 let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in
17 begin
18 output_string f (Texutil.get_preface ());
19 output_string f outtex;
20 output_string f (Texutil.get_footer ());
21 close_out f;
22 if Util.run_in_other_directory tmppath (cmd_latex tmpprefix) != 0
23 then (unlink_all (); raise (ExternalCommandFailure "latex"))
24 else if (Sys.command ((cmd_dvips tmpprefix) ^ " | " ^ (cmd_convert (finalpath^"/"^md5^".png"))) != 0)
25 then (unlink_all (); raise (ExternalCommandFailure ("dvips")))
26 else unlink_all ()
27 end