From: Antoine Musso Date: Wed, 17 Nov 2010 21:12:52 +0000 (+0000) Subject: some code comments, fix some indentation issues X-Git-Tag: 1.31.0-rc.0~33837 X-Git-Url: http://git.cyclocoop.org/fichier?a=commitdiff_plain;h=3bc84903b2e729e3ee2f4552a7d05224ba860aab;p=lhc%2Fweb%2Fwiklou.git some code comments, fix some indentation issues --- diff --git a/math/render.ml b/math/render.ml index f16735554e..5a02b6715b 100644 --- a/math/render.ml +++ b/math/render.ml @@ -1,7 +1,11 @@ +(* vim: set sw=8 ts=8 et: *) + let cmd_dvips tmpprefix = "dvips -q -R -E " ^ tmpprefix ^ ".dvi -f >" ^ tmpprefix ^ ".ps" let cmd_latex tmpprefix = "latex " ^ tmpprefix ^ ".tex >/dev/null" + (* Putting -transparent white in converts arguments will sort-of give you transperancy *) let cmd_convert tmpprefix finalpath = "convert -quality 100 -density 120 " ^ tmpprefix ^ ".ps " ^ finalpath ^ " >/dev/null 2>/dev/null" + (* Putting -bg Transparent in dvipng's arguments will give full-alpha transparency *) (* Note that IE have problems with such PNGs and need an additional javascript snippet *) (* Putting -bg transparent in dvipng's arguments will give binary transparency *) @@ -15,26 +19,40 @@ let render tmppath finalpath outtex md5 backcolor = let unlink_all () = begin (* Commenting this block out will aid in debugging *) - Sys.remove (tmpprefix ^ ".dvi"); - Sys.remove (tmpprefix ^ ".aux"); - Sys.remove (tmpprefix ^ ".log"); + Sys.remove (tmpprefix ^ ".dvi"); + Sys.remove (tmpprefix ^ ".aux"); + Sys.remove (tmpprefix ^ ".log"); Sys.remove (tmpprefix ^ ".tex"); - if Sys.file_exists (tmpprefix ^ ".ps") - then Sys.remove (tmpprefix ^ ".ps"); + if Sys.file_exists (tmpprefix ^ ".ps") + then Sys.remove (tmpprefix ^ ".ps"); end in + let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in begin - output_string f (Texutil.get_preface ()); - output_string f outtex; - output_string f (Texutil.get_footer ()); - close_out f; - if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0 - then (unlink_all (); raise (ExternalCommandFailure "latex")) - else if (Sys.command (cmd_dvipng tmpprefix (finalpath^"/"^md5^".png") backcolor) != 0) - then (if (Sys.command (cmd_dvips tmpprefix) != 0) - then (unlink_all (); raise (ExternalCommandFailure "dvips")) - else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0) - then (unlink_all (); raise (ExternalCommandFailure "convert")) - else unlink_all ()) - else unlink_all () + (* Assemble final output in file 'f' *) + output_string f (Texutil.get_preface ()); + output_string f outtex; + output_string f (Texutil.get_footer ()); + close_out f; + + (* TODO: document *) + if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0 + then ( + unlink_all (); raise (ExternalCommandFailure "latex") + ) else if (Sys.command (cmd_dvipng tmpprefix (finalpath^"/"^md5^".png") backcolor) != 0) + then ( + if (Sys.command (cmd_dvips tmpprefix) != 0) + then ( + unlink_all (); + raise (ExternalCommandFailure "dvips") + ) else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0) + then ( + unlink_all (); + raise (ExternalCommandFailure "convert") + ) else ( + unlink_all () + ) + ) else ( + unlink_all () + ) end diff --git a/math/texutil.ml b/math/texutil.ml index 7800c65858..cc7f48faa5 100644 --- a/math/texutil.ml +++ b/math/texutil.ml @@ -1,3 +1,4 @@ +(* vim: set sw=8 ts=8 et: *) open Parser open Render_info open Tex @@ -10,6 +11,7 @@ let tex_part = function | MHTMLABLEC (_,t,_,_,_) -> t | HTMLABLE_BIG (t,_) -> t | TEX_ONLY t -> t + let rec render_tex = function TEX_FQ (a,b,c) -> (render_tex a) ^ "_{" ^ (render_tex b) ^ "}^{" ^ (render_tex c) ^ "}" | TEX_DQ (a,b) -> (render_tex a) ^ "_{" ^ (render_tex b) ^ "}" @@ -38,28 +40,40 @@ let rec render_tex = function (* Dynamic loading*) type encoding_t = LATIN1 | LATIN2 | UTF8 +(* module properties *) let modules_ams = ref false let modules_nonascii = ref false let modules_encoding = ref UTF8 let modules_color = ref false +(* wrappers to easily set / reset module properties *) let tex_use_ams () = modules_ams := true let tex_use_nonascii () = modules_nonascii := true let tex_use_color () = modules_color := true -let tex_mod_reset () = (modules_ams := false; modules_nonascii := false; modules_encoding := UTF8; modules_color := false) +let tex_mod_reset () = ( + modules_ams := false; + modules_nonascii := false; + modules_encoding := UTF8; + modules_color := false + ) +(* Return TeX fragment for one of the encodings in (UTF8,LATIN1,LATIN2) *) let get_encoding = function UTF8 -> "\\usepackage{ucs}\n\\usepackage[utf8]{inputenc}\n" | LATIN1 -> "\\usepackage[latin1]{inputenc}\n" | LATIN2 -> "\\usepackage[latin2]{inputenc}\n" +(* TeX fragment inserted before the output *) let get_preface () = "\\nonstopmode\n\\documentclass[12pt]{article}\n" ^ (if !modules_nonascii then get_encoding !modules_encoding else "") ^ (if !modules_ams then "\\usepackage{amsmath}\n\\usepackage{amsfonts}\n\\usepackage{amssymb}\n" else "") ^ (if !modules_color then "\\usepackage[dvips,usenames]{color}\n" else "") ^ "\\usepackage{cancel}\n\\pagestyle{empty}\n\\begin{document}\n$$\n" + +(* TeX fragment appended after the content *) let get_footer () = "\n$$\n\\end{document}\n" +(* Default to UTF8 *) let set_encoding = function "ISO-8859-1" -> modules_encoding := LATIN1 | "iso-8859-1" -> modules_encoding := LATIN1 diff --git a/math/texvc.ml b/math/texvc.ml index 38a7e93b7e..33a14b7be7 100644 --- a/math/texvc.ml +++ b/math/texvc.ml @@ -1,8 +1,12 @@ +(* vim: set sw=8 ts=8 et: *) exception LexerException of string + +(* *) let lexer_token_safe lexbuf = try Lexer.token lexbuf with Failure s -> raise (LexerException s) +(* *) let render tmppath finalpath tree backcolor = let outtex = Util.mapjoin Texutil.render_tex tree in let md5 = Digest.to_hex (Digest.string outtex) in @@ -21,9 +25,29 @@ let render tmppath finalpath tree backcolor = ); Render.render tmppath finalpath outtex md5 backcolor end + +(* TODO: document + * Arguments: + * 1st : + * 2nd : + * 3rd : + * 4th : encoding (Default: UTF-8) + * 5th : color (Default: rgb 1.0 1.0 1.0) + * + * Output one character: + * S : Parsing error + * E : Lexer exception raised + * F : TeX function not recognized + * - : Generic/Default failure code. Might be an invalid argument, + * output file already exist, a problem with an external + * command ... + * *) let _ = Texutil.set_encoding (try Sys.argv.(4) with _ -> "UTF-8"); - 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") + 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") with Parsing.Parse_error -> print_string "S" | LexerException _ -> print_string "E" | Texutil.Illegal_tex_function s -> print_string ("F" ^ s) diff --git a/math/util.ml b/math/util.ml index f045856274..ece0160540 100644 --- a/math/util.ml +++ b/math/util.ml @@ -1,17 +1,26 @@ +(* vim: set sw=8 ts=8 et: *) + +(* TODO document *) let mapjoin f l = (List.fold_left (fun a b -> a ^ (f b)) "" l) + +(* TODO document *) let mapjoine e f = function [] -> "" | h::t -> (List.fold_left (fun a b -> a ^ e ^ (f b)) (f h) t) +(* Exception used by open_out_unless_exists below *) exception FileAlreadyExists + +(* Wrapper which raise an exception when output path already exist *) let open_out_unless_exists path = if Sys.file_exists path then raise FileAlreadyExists else open_out path +(* *) let run_in_other_directory tmppath cmd = let prevdir = Sys.getcwd () in( - Sys.chdir tmppath; - let retval = Sys.command cmd in - (Sys.chdir prevdir; retval) + Sys.chdir tmppath; + let retval = Sys.command cmd in + (Sys.chdir prevdir; retval) )