Index: trunk/phase3/math/util.ml |
— | — | @@ -1,17 +1,26 @@ |
| 2 | +(* vim: set sw=8 ts=8 et: *) |
| 3 | + |
| 4 | +(* TODO document *) |
2 | 5 | let mapjoin f l = (List.fold_left (fun a b -> a ^ (f b)) "" l) |
| 6 | + |
| 7 | +(* TODO document *) |
3 | 8 | let mapjoine e f = function |
4 | 9 | [] -> "" |
5 | 10 | | h::t -> (List.fold_left (fun a b -> a ^ e ^ (f b)) (f h) t) |
6 | 11 | |
| 12 | +(* Exception used by open_out_unless_exists below *) |
7 | 13 | exception FileAlreadyExists |
| 14 | + |
| 15 | +(* Wrapper which raise an exception when output path already exist *) |
8 | 16 | let open_out_unless_exists path = |
9 | 17 | if Sys.file_exists path |
10 | 18 | then raise FileAlreadyExists |
11 | 19 | else open_out path |
12 | 20 | |
| 21 | +(* *) |
13 | 22 | let run_in_other_directory tmppath cmd = |
14 | 23 | let prevdir = Sys.getcwd () in( |
15 | | - Sys.chdir tmppath; |
16 | | - let retval = Sys.command cmd in |
17 | | - (Sys.chdir prevdir; retval) |
| 24 | + Sys.chdir tmppath; |
| 25 | + let retval = Sys.command cmd in |
| 26 | + (Sys.chdir prevdir; retval) |
18 | 27 | ) |
Index: trunk/phase3/math/render.ml |
— | — | @@ -1,7 +1,11 @@ |
| 2 | +(* vim: set sw=8 ts=8 et: *) |
| 3 | + |
2 | 4 | let cmd_dvips tmpprefix = "dvips -q -R -E " ^ tmpprefix ^ ".dvi -f >" ^ tmpprefix ^ ".ps" |
3 | 5 | let cmd_latex tmpprefix = "latex " ^ tmpprefix ^ ".tex >/dev/null" |
| 6 | + |
4 | 7 | (* Putting -transparent white in converts arguments will sort-of give you transperancy *) |
5 | 8 | let cmd_convert tmpprefix finalpath = "convert -quality 100 -density 120 " ^ tmpprefix ^ ".ps " ^ finalpath ^ " >/dev/null 2>/dev/null" |
| 9 | + |
6 | 10 | (* Putting -bg Transparent in dvipng's arguments will give full-alpha transparency *) |
7 | 11 | (* Note that IE have problems with such PNGs and need an additional javascript snippet *) |
8 | 12 | (* Putting -bg transparent in dvipng's arguments will give binary transparency *) |
— | — | @@ -15,26 +19,40 @@ |
16 | 20 | let unlink_all () = |
17 | 21 | begin |
18 | 22 | (* Commenting this block out will aid in debugging *) |
19 | | - Sys.remove (tmpprefix ^ ".dvi"); |
20 | | - Sys.remove (tmpprefix ^ ".aux"); |
21 | | - Sys.remove (tmpprefix ^ ".log"); |
| 23 | + Sys.remove (tmpprefix ^ ".dvi"); |
| 24 | + Sys.remove (tmpprefix ^ ".aux"); |
| 25 | + Sys.remove (tmpprefix ^ ".log"); |
22 | 26 | Sys.remove (tmpprefix ^ ".tex"); |
23 | | - if Sys.file_exists (tmpprefix ^ ".ps") |
24 | | - then Sys.remove (tmpprefix ^ ".ps"); |
| 27 | + if Sys.file_exists (tmpprefix ^ ".ps") |
| 28 | + then Sys.remove (tmpprefix ^ ".ps"); |
25 | 29 | end in |
| 30 | + |
26 | 31 | let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in |
27 | 32 | begin |
28 | | - output_string f (Texutil.get_preface ()); |
29 | | - output_string f outtex; |
30 | | - output_string f (Texutil.get_footer ()); |
31 | | - close_out f; |
32 | | - if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0 |
33 | | - then (unlink_all (); raise (ExternalCommandFailure "latex")) |
34 | | - else if (Sys.command (cmd_dvipng tmpprefix (finalpath^"/"^md5^".png") backcolor) != 0) |
35 | | - then (if (Sys.command (cmd_dvips tmpprefix) != 0) |
36 | | - then (unlink_all (); raise (ExternalCommandFailure "dvips")) |
37 | | - else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0) |
38 | | - then (unlink_all (); raise (ExternalCommandFailure "convert")) |
39 | | - else unlink_all ()) |
40 | | - else unlink_all () |
| 33 | + (* Assemble final output in file 'f' *) |
| 34 | + output_string f (Texutil.get_preface ()); |
| 35 | + output_string f outtex; |
| 36 | + output_string f (Texutil.get_footer ()); |
| 37 | + close_out f; |
| 38 | + |
| 39 | + (* TODO: document *) |
| 40 | + if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0 |
| 41 | + then ( |
| 42 | + unlink_all (); raise (ExternalCommandFailure "latex") |
| 43 | + ) else if (Sys.command (cmd_dvipng tmpprefix (finalpath^"/"^md5^".png") backcolor) != 0) |
| 44 | + then ( |
| 45 | + if (Sys.command (cmd_dvips tmpprefix) != 0) |
| 46 | + then ( |
| 47 | + unlink_all (); |
| 48 | + raise (ExternalCommandFailure "dvips") |
| 49 | + ) else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0) |
| 50 | + then ( |
| 51 | + unlink_all (); |
| 52 | + raise (ExternalCommandFailure "convert") |
| 53 | + ) else ( |
| 54 | + unlink_all () |
| 55 | + ) |
| 56 | + ) else ( |
| 57 | + unlink_all () |
| 58 | + ) |
41 | 59 | end |
Index: trunk/phase3/math/texvc.ml |
— | — | @@ -1,8 +1,12 @@ |
| 2 | +(* vim: set sw=8 ts=8 et: *) |
2 | 3 | exception LexerException of string |
| 4 | + |
| 5 | +(* *) |
3 | 6 | let lexer_token_safe lexbuf = |
4 | 7 | try Lexer.token lexbuf |
5 | 8 | with Failure s -> raise (LexerException s) |
6 | 9 | |
| 10 | +(* *) |
7 | 11 | let render tmppath finalpath tree backcolor = |
8 | 12 | let outtex = Util.mapjoin Texutil.render_tex tree in |
9 | 13 | let md5 = Digest.to_hex (Digest.string outtex) in |
— | — | @@ -21,9 +25,29 @@ |
22 | 26 | ); |
23 | 27 | Render.render tmppath finalpath outtex md5 backcolor |
24 | 28 | end |
| 29 | + |
| 30 | +(* TODO: document |
| 31 | + * Arguments: |
| 32 | + * 1st : |
| 33 | + * 2nd : |
| 34 | + * 3rd : |
| 35 | + * 4th : encoding (Default: UTF-8) |
| 36 | + * 5th : color (Default: rgb 1.0 1.0 1.0) |
| 37 | + * |
| 38 | + * Output one character: |
| 39 | + * S : Parsing error |
| 40 | + * E : Lexer exception raised |
| 41 | + * F : TeX function not recognized |
| 42 | + * - : Generic/Default failure code. Might be an invalid argument, |
| 43 | + * output file already exist, a problem with an external |
| 44 | + * command ... |
| 45 | + * *) |
25 | 46 | let _ = |
26 | 47 | Texutil.set_encoding (try Sys.argv.(4) with _ -> "UTF-8"); |
27 | | - try render Sys.argv.(1) Sys.argv.(2) (Parser.tex_expr lexer_token_safe (Lexing.from_string Sys.argv.(3))) (try Sys.argv.(5) with _ -> "rgb 1.0 1.0 1.0") |
| 48 | + try render Sys.argv.(1) Sys.argv.(2) ( |
| 49 | + Parser.tex_expr lexer_token_safe ( |
| 50 | + Lexing.from_string Sys.argv.(3)) |
| 51 | + ) (try Sys.argv.(5) with _ -> "rgb 1.0 1.0 1.0") |
28 | 52 | with Parsing.Parse_error -> print_string "S" |
29 | 53 | | LexerException _ -> print_string "E" |
30 | 54 | | Texutil.Illegal_tex_function s -> print_string ("F" ^ s) |
Index: trunk/phase3/math/texutil.ml |
— | — | @@ -1,3 +1,4 @@ |
| 2 | +(* vim: set sw=8 ts=8 et: *) |
2 | 3 | open Parser |
3 | 4 | open Render_info |
4 | 5 | open Tex |
— | — | @@ -10,6 +11,7 @@ |
11 | 12 | | MHTMLABLEC (_,t,_,_,_) -> t |
12 | 13 | | HTMLABLE_BIG (t,_) -> t |
13 | 14 | | TEX_ONLY t -> t |
| 15 | + |
14 | 16 | let rec render_tex = function |
15 | 17 | TEX_FQ (a,b,c) -> (render_tex a) ^ "_{" ^ (render_tex b) ^ "}^{" ^ (render_tex c) ^ "}" |
16 | 18 | | TEX_DQ (a,b) -> (render_tex a) ^ "_{" ^ (render_tex b) ^ "}" |
— | — | @@ -38,28 +40,40 @@ |
39 | 41 | (* Dynamic loading*) |
40 | 42 | type encoding_t = LATIN1 | LATIN2 | UTF8 |
41 | 43 | |
| 44 | +(* module properties *) |
42 | 45 | let modules_ams = ref false |
43 | 46 | let modules_nonascii = ref false |
44 | 47 | let modules_encoding = ref UTF8 |
45 | 48 | let modules_color = ref false |
46 | 49 | |
| 50 | +(* wrappers to easily set / reset module properties *) |
47 | 51 | let tex_use_ams () = modules_ams := true |
48 | 52 | let tex_use_nonascii () = modules_nonascii := true |
49 | 53 | let tex_use_color () = modules_color := true |
50 | | -let tex_mod_reset () = (modules_ams := false; modules_nonascii := false; modules_encoding := UTF8; modules_color := false) |
| 54 | +let tex_mod_reset () = ( |
| 55 | + modules_ams := false; |
| 56 | + modules_nonascii := false; |
| 57 | + modules_encoding := UTF8; |
| 58 | + modules_color := false |
| 59 | + ) |
51 | 60 | |
| 61 | +(* Return TeX fragment for one of the encodings in (UTF8,LATIN1,LATIN2) *) |
52 | 62 | let get_encoding = function |
53 | 63 | UTF8 -> "\\usepackage{ucs}\n\\usepackage[utf8]{inputenc}\n" |
54 | 64 | | LATIN1 -> "\\usepackage[latin1]{inputenc}\n" |
55 | 65 | | LATIN2 -> "\\usepackage[latin2]{inputenc}\n" |
56 | 66 | |
| 67 | +(* TeX fragment inserted before the output *) |
57 | 68 | let get_preface () = "\\nonstopmode\n\\documentclass[12pt]{article}\n" ^ |
58 | 69 | (if !modules_nonascii then get_encoding !modules_encoding else "") ^ |
59 | 70 | (if !modules_ams then "\\usepackage{amsmath}\n\\usepackage{amsfonts}\n\\usepackage{amssymb}\n" else "") ^ |
60 | 71 | (if !modules_color then "\\usepackage[dvips,usenames]{color}\n" else "") ^ |
61 | 72 | "\\usepackage{cancel}\n\\pagestyle{empty}\n\\begin{document}\n$$\n" |
| 73 | + |
| 74 | +(* TeX fragment appended after the content *) |
62 | 75 | let get_footer () = "\n$$\n\\end{document}\n" |
63 | 76 | |
| 77 | +(* Default to UTF8 *) |
64 | 78 | let set_encoding = function |
65 | 79 | "ISO-8859-1" -> modules_encoding := LATIN1 |
66 | 80 | | "iso-8859-1" -> modules_encoding := LATIN1 |