class Mathjax_Renderer::Renderer
Public Class Methods
new(mathml, image_base_url = nil, options = {})
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 9 def initialize(mathml, image_base_url = nil, options = {}) @mathml = mathml @image_base_url = image_base_url @options = {min_width: 0, extra_style:'', padding:0}.merge options end
Public Instance Methods
html()
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 92 def html return cached(params_hash) if cached? params_hash render! if @html.nil? @html end
image_name()
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 15 def image_name raise ArgumentError if @image_base_url.nil? render! unless File.exist?(image_path) _image_name end
mathjax_ready?(page)
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 37 def mathjax_ready?(page) html = Nokogiri::HTML(page.html) !html.css('.MathJax').empty? && html.css('.MathJax_Processing').empty? && html.css('.MathJax_Processed').empty? end
render!()
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 21 def render! server = RendererServer.new server.ensure_started! url = server.add_content(@mathml, @options[:extra_style], @options[:padding]) require 'phantomjs/poltergeist' require 'phantomjs' Capybara.register_driver :poltergeist do |app| Capybara::Poltergeist::Driver.new(app, :phantomjs => Phantomjs.path) end Capybara.default_driver = :poltergeist Capybara.app_host = "http://localhost:#{server.port}" visit url def mathjax_ready?(page) html = Nokogiri::HTML(page.html) !html.css('.MathJax').empty? && html.css('.MathJax_Processing').empty? && html.css('.MathJax_Processed').empty? end Timeout.timeout(5) do sleep 0.2 until mathjax_ready?(page) end unless @image_base_url.nil? require 'chunky_png' driver = page.driver require 'fileutils' FileUtils.mkpath @image_base_url driver.save_screenshot(image_path) image = ChunkyPNG::Image.from_file(image_path) location = page.driver.evaluate_script <<-EOS function() { var ele = document.querySelector('.MathJax .math'); var rect = ele.getBoundingClientRect(); return [rect.left, rect.top]; }(); EOS size = page.driver.evaluate_script <<-EOS function() { var ele = document.querySelector('.MathJax .math'); var rect = ele.getBoundingClientRect(); return [rect.width, rect.height]; }(); EOS correction = [(@options[:min_width] -(size[0] + 2 * @options[:padding])) / 2,0].max x = location[0] + 1 - @options[:padding]-correction y = location[1] + 1 - @options[:padding] width = [size[0].ceil + 2 * @options[:padding],@options[:min_width]].max height = size[1]+ 2 * @options[:padding] image.crop!(x, y, width, height) image.save(image_path) end result = Nokogiri::HTML(page.html).css('.MathJax .math')[0] put_cache!(params_hash,result) @html = result end
Private Instance Methods
_image_name()
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 110 def _image_name "#{params_hash}.png" end
cache()
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 116 def cache @@cache end
cached(mathml)
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 124 def cached(mathml) cache[mathml] end
cached?(mathml)
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 120 def cached?(mathml) cache.has_key?(mathml) end
image_path()
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 106 def image_path "#{@image_base_url}/#{_image_name}" end
params_hash()
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 102 def params_hash Digest::SHA1.hexdigest(@mathml+@options.to_s) end
put_cache!(mathml,result)
click to toggle source
# File lib/mathjax_renderer/renderer.rb, line 128 def put_cache!(mathml,result) cache[mathml]=result end