summaryrefslogtreecommitdiff
path: root/math
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2011-06-22 11:28:20 +0200
committerPierre Schmitz <pierre@archlinux.de>2011-06-22 11:28:20 +0200
commit9db190c7e736ec8d063187d4241b59feaf7dc2d1 (patch)
tree46d1a0dee7febef5c2d57a9f7b972be16a163b3d /math
parent78677c7bbdcc9739f6c10c75935898a20e1acd9e (diff)
update to MediaWiki 1.17.0
Diffstat (limited to 'math')
-rw-r--r--math/Makefile47
-rw-r--r--math/README2
-rw-r--r--math/lexer.mll1
-rw-r--r--math/render.ml54
-rw-r--r--math/texutil.ml30
-rw-r--r--math/texvc.ml26
-rw-r--r--math/util.ml15
7 files changed, 136 insertions, 39 deletions
diff --git a/math/Makefile b/math/Makefile
index 47c40f96..804f0857 100644
--- a/math/Makefile
+++ b/math/Makefile
@@ -1,3 +1,5 @@
+.PHONY: clean all
+
OBJ=render_info.cmo tex.cmo texutil.cmo parser.cmo lexer.cmo texvc.cmo \
render_info.cmx tex.cmx texutil.cmx parser.cmx lexer.cmx texvc.cmx \
lexer.cmi parser.cmi render_info.cmi tex.cmi texutil.cmi texvc.cmi \
@@ -10,15 +12,41 @@ texvc_tex.o texvc_tex.cmi texvc_tex html.cmi html.cmo html.cmx \
html.o mathml.cmi mathml.cmo mathml.cmx mathml.o
CGIPATH=-I /usr/lib/ocaml/cgi -I /usr/lib/ocaml/netstring -I /usr/lib/ocaml/pcre
+COMMON_NATIVE_OBJ =util.cmx parser.cmx html.cmx mathml.cmx texutil.cmx lexer.cmx
+COMMON_BYTECODE_OBJ=util.cmo parser.cmo html.cmo mathml.cmo texutil.cmo lexer.cmo
+
all: texvc texvc_test texvc_tex
-texvc.bc: util.cmo parser.cmo html.cmo mathml.cmo texutil.cmo render.cmo lexer.cmo texvc.cmo
- ocamlc -o $@ unix.cma $^
-texvc: util.cmx parser.cmx html.cmx mathml.cmx texutil.cmx render.cmx lexer.cmx texvc.cmx
+cgi: texvc_cgi.cmo texvc_cgi
+clean:
+ rm -f $(OBJ)
+
+# Native versions
+texvc: $(COMMON_NATIVE_OBJ) render.cmx texvc.cmx
ocamlopt -o $@ unix.cmxa $^
-texvc_test: util.cmx parser.cmx html.cmx mathml.cmx texutil.cmx lexer.cmx texvc_test.cmx
+texvc_test: $(COMMON_NATIVE_OBJ) lexer.cmx texvc_test.cmx
ocamlopt -o $@ $^
-texvc_tex: util.cmx parser.cmx html.cmx mathml.cmx texutil.cmx lexer.cmx texvc_tex.cmx
+texvc_tex: $(COMMON_NATIVE_OBJ) texvc_tex.cmx
ocamlopt -o $@ $^
+
+# Bytecode version
+texvc.bc: $(COMMON_BYTECODE_OBJ) render.cmo texvc.cmo
+ ocamlc -o $@ unix.cma $^
+
+# CGI related targets:
+texvc_cgi.cmo: texvc_cgi.ml
+ ocamlc -c $(CGIPATH) $<
+texvc_cgi: util.cmo parser.cmo texutil.cmo render.cmo lexer.cmo texvc_cgi.cmo
+ ocamlc -o $@ unix.cma $(CGIPATH) pcre.cma netstring.cma cgi.cma $^
+ chmod g-w $@
+
+#
+# Pattern rules
+#
+
+# .ml source .mli interface
+# .cmi compiled interface
+# .cmo object .cma library object
+# .cmx object file .cmxa library object file
%.ml: %.mll
ocamllex $<
%.mli %.ml: %.mly
@@ -29,13 +57,8 @@ texvc_tex: util.cmx parser.cmx html.cmx mathml.cmx texutil.cmx lexer.cmx texvc_t
ocamlopt -c $<
%.cmi: %.mli
ocamlc -c $<
-texvc_cgi.cmo: texvc_cgi.ml
- ocamlc -c $(CGIPATH) $<
-texvc_cgi: util.cmo parser.cmo texutil.cmo render.cmo lexer.cmo texvc_cgi.cmo
- ocamlc -o $@ unix.cma $(CGIPATH) pcre.cma netstring.cma cgi.cma $^
- chmod g-w $@
-clean:
- rm -f $(OBJ)
+
+# Various dependencies
html.cmo: render_info.cmi tex.cmi util.cmo html.cmi
html.cmx: render_info.cmi tex.cmi util.cmx html.cmi
diff --git a/math/README b/math/README
index d0e21648..5b99d4c2 100644
--- a/math/README
+++ b/math/README
@@ -84,7 +84,7 @@ texvc output format is like this:
== Troubleshooting ==
-Unforunately, many error conditions with rasterization are not well reported.
+Unfortunately, many error conditions with rasterization are not well reported.
texvc will return as though everything is successful, and the only obvious
sign of problems for the user is a big X on a wiki page where an equation
should be.
diff --git a/math/lexer.mll b/math/lexer.mll
index 4dd31e0e..1702084c 100644
--- a/math/lexer.mll
+++ b/math/lexer.mll
@@ -69,6 +69,7 @@ rule token = parse
| "\\#" { LITERAL (HTMLABLE (FONT_UFH,"\\#","#")) }
| "\\%" { LITERAL (HTMLABLE (FONT_UFH,"\\%","%")) }
| "\\$" { LITERAL (HTMLABLE (FONT_UFH,"\\$","$")) }
+ | "\\&" { LITERAL (HTMLABLEC (FONT_RM,"\\&","&amp;")) }
| "&" { NEXT_CELL }
| "\\\\" { NEXT_ROW }
| "\\begin{matrix}" { Texutil.tex_use_ams(); BEGIN__MATRIX }
diff --git a/math/render.ml b/math/render.ml
index f1673555..5a02b671 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 ad4fce10..cc7f48fa 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
@@ -80,7 +94,7 @@ let find = function
| "\\Gamma" -> LITERAL (HTMLABLEC (FONT_UF, "\\Gamma ", "&Gamma;"))
| "\\delta" -> LITERAL (HTMLABLEC (FONT_UF, "\\delta ", "&delta;"))
| "\\Delta" -> LITERAL (HTMLABLEC (FONT_UF, "\\Delta ", "&Delta;"))
- | "\\epsilon" -> LITERAL (HTMLABLEC (FONT_UF, "\\epsilon ", "&epsilon;"))
+ | "\\epsilon" -> LITERAL (TEX_ONLY "\\epsilon ")
| "\\Epsilon" -> (tex_use_ams (); LITERAL (HTMLABLEC (FONT_UF,
"\\mathrm{E}", "&Epsilon;")))
| "\\varepsilon" -> LITERAL (TEX_ONLY "\\varepsilon ")
@@ -119,7 +133,7 @@ let find = function
| "\\Rho" -> (tex_use_ams (); LITERAL (HTMLABLEC (FONT_UF,
"\\mathrm{P}", "&Rho;")))
| "\\varrho" -> LITERAL (TEX_ONLY "\\varrho ")
- | "\\sim" -> LITERAL (HTMLABLEC (FONT_UF, "\\sim ", "&tilde;"))
+ | "\\sim" -> LITERAL (HTMLABLEC (FONT_UF, "\\sim ", "&sim;"))
| "\\sigma" -> LITERAL (HTMLABLEC (FONT_UF, "\\sigma ", "&sigma;"))
| "\\Sigma" -> LITERAL (HTMLABLEC (FONT_UF, "\\Sigma ", "&Sigma;"))
| "\\varsigma" -> LITERAL (TEX_ONLY "\\varsigma ")
@@ -128,7 +142,7 @@ let find = function
"\\mathrm{T}", "&Tau;")))
| "\\upsilon" -> LITERAL (HTMLABLEC (FONT_UF, "\\upsilon ", "&upsilon;"))
| "\\Upsilon" -> LITERAL (HTMLABLEC (FONT_UF, "\\Upsilon ", "&Upsilon;"))
- | "\\phi" -> LITERAL (HTMLABLEC (FONT_UF, "\\phi ", "&phi;"))
+ | "\\phi" -> LITERAL (TEX_ONLY "\\phi ")
| "\\Phi" -> LITERAL (HTMLABLEC (FONT_UF, "\\Phi ", "&Phi;"))
| "\\varphi" -> LITERAL (TEX_ONLY "\\varphi ")
| "\\chi" -> LITERAL (HTMLABLEC (FONT_UF, "\\chi ", "&chi;"))
@@ -409,6 +423,10 @@ let find = function
| "\\mod" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UFH,"\\mod ", "mod")))
| "\\Diamond" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UF, "\\Diamond ", "&loz;")))
| "\\dotsb" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UF, "\\dotsb ", "&sdot;&sdot;&sdot;")))
+ | "\\dotsc" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UF, "\\dotsc ", "...")))
+ | "\\dotsi" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UF, "\\dotsi ", "&sdot;&sdot;&sdot;")))
+ | "\\dotsm" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UF, "\\dotsm ", "&sdot;&sdot;&sdot;")))
+ | "\\dotso" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UF, "\\dotso ", "...")))
| "\\reals" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UFH,"\\mathbb{R}", "<b>R</b>")))
| "\\Reals" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UFH,"\\mathbb{R}", "<b>R</b>")))
| "\\R" -> (tex_use_ams (); LITERAL (HTMLABLE (FONT_UFH,"\\mathbb{R}", "<b>R</b>")))
@@ -508,12 +526,15 @@ let find = function
| "\\over" -> FUN_INFIXh ("\\over ", fun num den -> Html.html_render num, "<hr style=\"{background: black}\"/>", Html.html_render den)
| "\\sqrt" -> FUN_AR1 "\\sqrt "
| "\\cancel" -> FUN_AR1 "\\cancel "
+ | "\\bcancel" -> FUN_AR1 "\\bcancel "
+ | "\\xcancel" -> FUN_AR1 "\\xcancel "
| "\\cancelto" -> FUN_AR2 "\\cancelto "
| "\\pmod" -> FUN_AR1hl ("\\pmod ", ("(mod ", ")"))
| "\\bmod" -> FUN_AR1hl ("\\bmod ", ("mod ", ""))
| "\\emph" -> FUN_AR1 "\\emph "
| "\\texttt" -> FUN_AR1 "\\texttt "
| "\\textbf" -> FUN_AR1 "\\textbf "
+ | "\\textsf" -> FUN_AR1 "\\textsf "
| "\\textit" -> FUN_AR1hf ("\\textit ", FONTFORCE_IT)
| "\\textrm" -> FUN_AR1hf ("\\textrm ", FONTFORCE_RM)
| "\\rm" -> DECLh ("\\rm ", FONTFORCE_RM)
@@ -726,6 +747,7 @@ let find = function
| "\\mathsf" -> (tex_use_ams (); FUN_AR1 "\\mathsf ")
| "\\mathcal" -> (tex_use_ams (); FUN_AR1 "\\mathcal ")
| "\\mathbb" -> (tex_use_ams (); FUN_AR1 "\\mathbb ")
+ | "\\mathtt" -> (tex_use_ams (); FUN_AR1 "\\mathtt ")
| "\\mathfrak" -> (tex_use_ams (); FUN_AR1 "\\mathfrak ")
| "\\operatorname" -> (tex_use_ams (); FUN_AR1 "\\operatorname ")
| "\\text" -> raise (Failure "malformatted \\text")
diff --git a/math/texvc.ml b/math/texvc.ml
index 38a7e93b..33a14b7b 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 f0458562..ece01605 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)
)