lib/mathematical/render.rb in mathematical-0.4.2 vs lib/mathematical/render.rb in mathematical-0.5.0

- old
+ new

@@ -2,40 +2,49 @@ require 'base64' require 'tempfile' module Mathematical class Render + include Corrections + + FORMAT_TYPES = %w(svg png mathml) + DEFAULT_OPTS = { :ppi => 72.0, :zoom => 1.0, :base64 => false, - :maxsize => 0 + :maxsize => 0, + :format => "svg" } def initialize(opts = {}) @config = DEFAULT_OPTS.merge(opts) + raise(TypeError, "maxsize must be an integer!") unless @config[:maxsize].is_a? Fixnum raise(TypeError, "maxsize cannot be less than 0!") if @config[:maxsize] < 0 + raise(TypeError, "format must be a string!") unless @config[:format].is_a? String + raise(TypeError, "format type must be one of the following formats: #{FORMAT_TYPES.join(', ')}") unless FORMAT_TYPES.include?(@config[:format]) + @processer = Mathematical::Process.new(@config) end def render(maths) raise(TypeError, "text must be a string!") unless maths.is_a? String + maths = maths.strip raise(ArgumentError, "text must be in itex format (`$...$` or `$$...$$`)!") unless maths =~ /\A\${1,2}/ - # seems to be a bug in itex@1.5.1 where the "Vertical spacing and page breaks in multiline display" (\\) - # do not work, and yield an "unknown character" error - maths.gsub!(/\\\\/, "\\\\\\\\") + maths = apply_corrections(maths) - # `{align}` *should* be valid, according to AMS-Latex, but it seems itex@1.5.1 does not like it. - maths.gsub!(/\\begin\{align\}/, "\\begin{aligned}") - maths.gsub!(/\\end\{align\}/, "\\end{aligned}") - begin - raise RuntimeError unless svg_hash = @processer.process(maths) - svg_hash["svg"] = svg_hash["svg"][xml_header.length..-1] # remove starting <?xml...> tag - svg_hash["svg"] = svg_to_base64(svg_hash["svg"]) if @config[:base64] - svg_hash + raise RuntimeError unless data_hash = @processer.process(maths) + case @config[:format] + when "svg" + data_hash["svg"] = data_hash["svg"][xml_header.length..-1] # remove starting <?xml...> tag + data_hash["svg"] = svg_to_base64(data_hash["svg"]) if @config[:base64] + data_hash + when "png", "mathml" + data_hash + end rescue ParseError, DocumentCreationError, DocumentReadError => e # an error in the C code, probably a bad TeX parse $stderr.puts "#{e.message}: #{maths}" maths end end