finishing moving of code from inex.php to includes/Wiki.php
[lhc/web/wiklou.git] / math / render.ml
1 let cmd_dvips tmpprefix = "dvips -R -E " ^ tmpprefix ^ ".dvi -f >" ^ tmpprefix ^ ".ps"
2 let cmd_latex tmpprefix = "latex " ^ tmpprefix ^ ".tex >/dev/null"
3 (* Putting -transparent white in converts arguments will sort-of give you transperancy *)
4 let cmd_convert tmpprefix finalpath = "convert -quality 100 -density 120 " ^ tmpprefix ^ ".ps " ^ finalpath ^ " >/dev/null 2>/dev/null"
5
6 exception ExternalCommandFailure of string
7
8 let render tmppath finalpath outtex md5 =
9 let tmpprefix0 = (string_of_int (Unix.getpid ()))^"_"^md5 in
10 let tmpprefix = (tmppath^"/"^tmpprefix0) in
11 let unlink_all () =
12 begin
13 (* Commenting this block out will aid in debugging *)
14 Sys.remove (tmpprefix ^ ".dvi");
15 Sys.remove (tmpprefix ^ ".aux");
16 Sys.remove (tmpprefix ^ ".log");
17 Sys.remove (tmpprefix ^ ".tex");
18 Sys.remove (tmpprefix ^ ".ps");
19 end in
20 let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in
21 begin
22 output_string f (Texutil.get_preface ());
23 output_string f outtex;
24 output_string f (Texutil.get_footer ());
25 close_out f;
26 if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0
27 then (unlink_all (); raise (ExternalCommandFailure "latex"))
28 else if (Sys.command (cmd_dvips tmpprefix) != 0)
29 then (unlink_all (); raise (ExternalCommandFailure "dvips"))
30 else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0)
31 then (unlink_all (); raise (ExternalCommandFailure "convert"))
32 else unlink_all ()
33 end