Sha256: 3ae65f7e26cdb45ccb17bfbe88461de1abeb87a2413aa572931ae86c1ee9d9f8

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

require 'fileutils'
require 'base64'
require 'tempfile'

module Mathematical
  class Render
    DEFAULT_OPTS = {
      :ppi => 72.0,
      :zoom => 1.0,
      :base64 => false
    }

    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

    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

private

    def xml_header
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    end

    def svg_to_base64(contents)
      "data:image/svg+xml;base64,#{Base64.strict_encode64(contents)}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mathematical-0.1.2 lib/mathematical/render.rb
mathematical-0.1.1 lib/mathematical/render.rb
mathematical-0.1.0 lib/mathematical/render.rb