2010-02-22 15:52:52 +01:00
|
|
|
# Allow the metal piece to run in isolation
|
2010-02-22 07:05:52 +01:00
|
|
|
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
|
|
|
|
|
|
|
|
require 'stringsupport'
|
|
|
|
|
|
|
|
class Itex
|
|
|
|
def self.call(env)
|
|
|
|
if env["PATH_INFO"] =~ /^\/itex/
|
|
|
|
[200, {"Content-Type" => "application/xml"}, [response(env)]]
|
|
|
|
else
|
|
|
|
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.response(env)
|
|
|
|
@params = Rack::Request.new(env).params
|
2010-02-22 15:52:52 +01:00
|
|
|
tex = (@params['tex'] || '').purify
|
|
|
|
case @params['display']
|
|
|
|
when 'block'
|
|
|
|
filter = :block_filter
|
|
|
|
else
|
|
|
|
filter = :inline_filter
|
|
|
|
end
|
|
|
|
return "<math xmlns='http://www.w3.org/1998/Math/MathML' display='" +
|
|
|
|
filter.to_s[/(.*?)_filter/] + "'/>" if tex.strip == ''
|
2010-02-22 07:05:52 +01:00
|
|
|
estart = "<math xmlns='http://www.w3.org/1998/Math/MathML' display='inline'><merror><mtext>"
|
|
|
|
eend = "</mtext></merror></math>"
|
|
|
|
begin
|
|
|
|
require 'itextomml'
|
2010-02-23 02:13:46 +01:00
|
|
|
@itex2mml_parser ||= Itex2MML::Parser.new
|
|
|
|
@itex2mml_parser.send(filter, tex).to_utf8
|
2010-02-22 07:05:52 +01:00
|
|
|
rescue LoadError
|
|
|
|
estart + "Please install the itex2MML Ruby bindings." + eend
|
|
|
|
rescue Itex2MML::Error => e
|
|
|
|
estart + e.to_s + eend
|
|
|
|
rescue
|
|
|
|
estart + "Unknown Error" + eend
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|