r76906 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76905‎ | r76906 | r76907 >
Date:21:12, 17 November 2010
Author:hashar
Status:ok
Tags:
Comment:
some code comments, fix some indentation issues
Modified paths:
  • /trunk/phase3/math/render.ml (modified) (history)
  • /trunk/phase3/math/texutil.ml (modified) (history)
  • /trunk/phase3/math/texvc.ml (modified) (history)
  • /trunk/phase3/math/util.ml (modified) (history)

Diff [purge]

Index: trunk/phase3/math/util.ml
@@ -1,17 +1,26 @@
 2+(* vim: set sw=8 ts=8 et: *)
 3+
 4+(* TODO document *)
25 let mapjoin f l = (List.fold_left (fun a b -> a ^ (f b)) "" l)
 6+
 7+(* TODO document *)
38 let mapjoine e f = function
49 [] -> ""
510 | h::t -> (List.fold_left (fun a b -> a ^ e ^ (f b)) (f h) t)
611
 12+(* Exception used by open_out_unless_exists below *)
713 exception FileAlreadyExists
 14+
 15+(* Wrapper which raise an exception when output path already exist *)
816 let open_out_unless_exists path =
917 if Sys.file_exists path
1018 then raise FileAlreadyExists
1119 else open_out path
1220
 21+(* *)
1322 let run_in_other_directory tmppath cmd =
1423 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)
1827 )
Index: trunk/phase3/math/render.ml
@@ -1,7 +1,11 @@
 2+(* vim: set sw=8 ts=8 et: *)
 3+
24 let cmd_dvips tmpprefix = "dvips -q -R -E " ^ tmpprefix ^ ".dvi -f >" ^ tmpprefix ^ ".ps"
35 let cmd_latex tmpprefix = "latex " ^ tmpprefix ^ ".tex >/dev/null"
 6+
47 (* Putting -transparent white in converts arguments will sort-of give you transperancy *)
58 let cmd_convert tmpprefix finalpath = "convert -quality 100 -density 120 " ^ tmpprefix ^ ".ps " ^ finalpath ^ " >/dev/null 2>/dev/null"
 9+
610 (* Putting -bg Transparent in dvipng's arguments will give full-alpha transparency *)
711 (* Note that IE have problems with such PNGs and need an additional javascript snippet *)
812 (* Putting -bg transparent in dvipng's arguments will give binary transparency *)
@@ -15,26 +19,40 @@
1620 let unlink_all () =
1721 begin
1822 (* 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");
2226 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");
2529 end in
 30+
2631 let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in
2732 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+ )
4159 end
Index: trunk/phase3/math/texvc.ml
@@ -1,8 +1,12 @@
 2+(* vim: set sw=8 ts=8 et: *)
23 exception LexerException of string
 4+
 5+(* *)
36 let lexer_token_safe lexbuf =
47 try Lexer.token lexbuf
58 with Failure s -> raise (LexerException s)
69
 10+(* *)
711 let render tmppath finalpath tree backcolor =
812 let outtex = Util.mapjoin Texutil.render_tex tree in
913 let md5 = Digest.to_hex (Digest.string outtex) in
@@ -21,9 +25,29 @@
2226 );
2327 Render.render tmppath finalpath outtex md5 backcolor
2428 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+ * *)
2546 let _ =
2647 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")
2852 with Parsing.Parse_error -> print_string "S"
2953 | LexerException _ -> print_string "E"
3054 | 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: *)
23 open Parser
34 open Render_info
45 open Tex
@@ -10,6 +11,7 @@
1112 | MHTMLABLEC (_,t,_,_,_) -> t
1213 | HTMLABLE_BIG (t,_) -> t
1314 | TEX_ONLY t -> t
 15+
1416 let rec render_tex = function
1517 TEX_FQ (a,b,c) -> (render_tex a) ^ "_{" ^ (render_tex b) ^ "}^{" ^ (render_tex c) ^ "}"
1618 | TEX_DQ (a,b) -> (render_tex a) ^ "_{" ^ (render_tex b) ^ "}"
@@ -38,28 +40,40 @@
3941 (* Dynamic loading*)
4042 type encoding_t = LATIN1 | LATIN2 | UTF8
4143
 44+(* module properties *)
4245 let modules_ams = ref false
4346 let modules_nonascii = ref false
4447 let modules_encoding = ref UTF8
4548 let modules_color = ref false
4649
 50+(* wrappers to easily set / reset module properties *)
4751 let tex_use_ams () = modules_ams := true
4852 let tex_use_nonascii () = modules_nonascii := true
4953 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+ )
5160
 61+(* Return TeX fragment for one of the encodings in (UTF8,LATIN1,LATIN2) *)
5262 let get_encoding = function
5363 UTF8 -> "\\usepackage{ucs}\n\\usepackage[utf8]{inputenc}\n"
5464 | LATIN1 -> "\\usepackage[latin1]{inputenc}\n"
5565 | LATIN2 -> "\\usepackage[latin2]{inputenc}\n"
5666
 67+(* TeX fragment inserted before the output *)
5768 let get_preface () = "\\nonstopmode\n\\documentclass[12pt]{article}\n" ^
5869 (if !modules_nonascii then get_encoding !modules_encoding else "") ^
5970 (if !modules_ams then "\\usepackage{amsmath}\n\\usepackage{amsfonts}\n\\usepackage{amssymb}\n" else "") ^
6071 (if !modules_color then "\\usepackage[dvips,usenames]{color}\n" else "") ^
6172 "\\usepackage{cancel}\n\\pagestyle{empty}\n\\begin{document}\n$$\n"
 73+
 74+(* TeX fragment appended after the content *)
6275 let get_footer () = "\n$$\n\\end{document}\n"
6376
 77+(* Default to UTF8 *)
6478 let set_encoding = function
6579 "ISO-8859-1" -> modules_encoding := LATIN1
6680 | "iso-8859-1" -> modules_encoding := LATIN1

Status & tagging log