texvc_cgi.ml 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. open Netcgi;;
  2. open Netcgi_types;;
  3. open Netcgi_env;;
  4. open Netchannels;;
  5. let cgi = new Netcgi.std_activation ()
  6. let out = cgi # output # output_string
  7. let math = cgi # argument_value ~default:"" "math"
  8. let tmppath = "/home/taw/public_html/wiki/tmp/"
  9. let finalpath = "/home/taw/public_html/wiki/math/"
  10. let finalurl = "http://wroclaw.taw.pl.eu.org/~taw/wiki/math/"
  11. ;;
  12. let h_header = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\""^
  13. " \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"^
  14. "<html><head><title>texvc</title></head><body>"^
  15. "<form method=post action=\"http://wroclaw.taw.pl.eu.org/~taw/cgi-bin/newcodebase/math/texvc_cgi\">"^
  16. "<textarea name='math' rows=10 cols=80>"
  17. let h_middle = "</textarea><br /><input type=submit value=\"Preview\" name='preview'></form>"
  18. let h_footer = "</body></html>\n"
  19. let render tmppath finalpath tree =
  20. let outtex = Texutil.mapjoin Texutil.print tree in
  21. let md5 = Digest.to_hex (Digest.string outtex) in
  22. begin
  23. out "<h3>TeX</h3>";
  24. out outtex; (* <, & and > should be protected *)
  25. (try out ("<h3>HTML</h3>" ^ (Texutil.html_render tree))
  26. with _ -> out "<h3>HTML could not be rendered</h3>");
  27. try Render.render tmppath finalpath outtex md5;
  28. out ("<h3>Image:</h3><img src=\""^finalurl^md5^".png\">")
  29. with Util.FileAlreadyExists -> out ("<h3>Image:</h3><img src=\""^finalurl^md5^".png\">")
  30. | Failure s -> out ("<h3>Other failure: " ^ s ^ "</h3>")
  31. | Render.ExternalCommandFailure "latex" -> out "<h3>latex failed</h3>"
  32. | Render.ExternalCommandFailure "dvips" -> out "<h3>dvips failed</h3>"
  33. | _ -> out "<h3>Other failure</h3>"
  34. end
  35. ;;
  36. cgi#set_header ();;
  37. out h_header;;
  38. out math;;
  39. out h_middle;;
  40. exception LexerException of string
  41. let lexer_token_safe lexbuf =
  42. try Lexer.token lexbuf
  43. with Failure s -> raise (LexerException s)
  44. ;;
  45. if math = ""
  46. then ()
  47. else try
  48. render tmppath finalpath (Parser.tex_expr lexer_token_safe (Lexing.from_string math))
  49. with Parsing.Parse_error -> out "<h3>Parse error</h3>"
  50. | LexerException s -> out "<h3>Lexing failure</h3>"
  51. | Texutil.Illegal_tex_function s -> out ("<h3>Illegal TeX function: " ^ s ^ "</h3>")
  52. | Failure s -> out ("<h3>Other failure: " ^ s ^ "</h3>")
  53. | _ -> out "<h3>Other failure</h3>"
  54. ;;
  55. out h_footer