Special:Export produces a particular output format which is the raw wikitext. Outputt...
[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 let cmd_convert tmpprefix finalpath = "convert -quality 100 -density 120 " ^ tmpprefix ^ ".ps " ^ finalpath ^ " >/dev/null 2>/dev/null"
4
5 exception ExternalCommandFailure of string
6
7 let render tmppath finalpath outtex md5 =
8 let tmpprefix0 = (string_of_int (Unix.getpid ()))^"_"^md5 in
9 let tmpprefix = (tmppath^"/"^tmpprefix0) in
10 let unlink_all () =
11 begin
12 Sys.remove (tmpprefix ^ ".dvi");
13 Sys.remove (tmpprefix ^ ".aux");
14 Sys.remove (tmpprefix ^ ".log");
15 Sys.remove (tmpprefix ^ ".tex");
16 Sys.remove (tmpprefix ^ ".ps");
17 end in
18 let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in
19 begin
20 output_string f (Texutil.get_preface ());
21 output_string f outtex;
22 output_string f (Texutil.get_footer ());
23 close_out f;
24 if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0
25 then (unlink_all (); raise (ExternalCommandFailure "latex"))
26 else if (Sys.command (cmd_dvips tmpprefix) != 0)
27 then (unlink_all (); raise (ExternalCommandFailure "dvips"))
28 else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0)
29 then (unlink_all (); raise (ExternalCommandFailure "convert"))
30 else unlink_all ()
31 end