Sha256: 621c973df58d363e1011bc285ea5199d51cc3c3c001b9bbe7f550e4324b2d8d1
Contents?: true
Size: 1.35 KB
Versions: 4
Compression:
Stored size: 1.35 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 = {}) @config = DEFAULT_OPTS.merge(opts) @processer = Mathematical::Process.new(@config) 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 ParseError, DocumentCreationError, DocumentReadError => 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
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
mathematical-0.2.3 | lib/mathematical/render.rb |
mathematical-0.2.2 | lib/mathematical/render.rb |
mathematical-0.2.1 | lib/mathematical/render.rb |
mathematical-0.2.0 | lib/mathematical/render.rb |