Class: Mathematical::Render
- Inherits:
-
Object
- Object
- Mathematical::Render
- Defined in:
- lib/mathematical/render.rb
Constant Summary collapse
- DEFAULT_OPTS =
{ :ppi => 72.0, :zoom => 1.0, :base64 => false }
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Render
constructor
A new instance of Render.
- #render(maths) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Render
Returns a new instance of Render.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mathematical/render.rb', line 13 def initialize(opts = {}) raise(TypeError, "opts must be a hash!") unless opts.is_a? Hash @config = DEFAULT_OPTS.merge(opts) begin @processer = Mathematical::Process.new(@config) rescue TypeError => e # some error in the C code raise end end |
Instance Method Details
#render(maths) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mathematical/render.rb', line 24 def render(maths) raise(TypeError, "text must be a string!") unless maths.is_a? String raise(ArgumentError, "text must be in itex format (`$...$` or `$$...$$`)!") unless maths =~ /\A\${1,2}/ # TODO: figure out how to write SVGs without the tempfile tempfile = Tempfile.new('mathematical-temp.svg') begin raise RuntimeError unless @processer.process(maths, tempfile.path) svg_content = File.open(tempfile.path, 'r') { |image_file| image_file.read } svg_content = svg_content[xml_header.length..-1] # remove starting <?xml...> tag @config[:base64] ? svg_to_base64(svg_content) : svg_content rescue RuntimeError => e # an error in the C code, probably a bad TeX parse $stderr.puts "#{e.message}: #{maths}" maths end end |