mathjax-renderer 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/features/generate_html.feature +21 -0
- data/features/generate_image.feature +19 -0
- data/features/options.feature +23 -0
- data/features/step_definitions/html_steps.rb +11 -0
- data/features/step_definitions/image_steps.rb +80 -0
- data/features/support/env.rb +9 -0
- data/lib/mathjax_renderer/renderer.rb +207 -0
- data/lib/mathjax_renderer/version.rb +3 -0
- data/lib/renderer.rb +5 -0
- data/mathjax-renderer.gemspec +5 -2
- metadata +45 -4
- data/lib/mathjax/renderer/version.rb +0 -5
- data/lib/mathjax/renderer.rb +0 -201
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a82f09525f6f7743b239d3a7ff05682b4e28fdda
|
4
|
+
data.tar.gz: 80664ffb9aecc94634e6e6a12127a68f73e0efd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9c9e9dc0fccac290d8e784d238e89377295611d40f6af689381d67f656ac64f225de97b00de6eb47e106a3d48a1cc8ea371b8af8e4febc2af3fa9c85fab1816
|
7
|
+
data.tar.gz: b2c9514dc87c99ee7d1e9d5fe129788e37f8ee5e85d9bcfde088237605b43776ae7a9d97c3bc941a32173bf13b2845f4123554fac614e5d392fd76ac6595a3fa
|
data/Gemfile
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
Feature: The renderer can generate the HTML content that displays the mathml
|
2
|
+
|
3
|
+
Scenario: The renderer generates the HTML from MathML
|
4
|
+
When the renderer is invoked with
|
5
|
+
"""
|
6
|
+
<math xmlns="http://www.w3.org/1998/Math/MathML">
|
7
|
+
<mi>a</mi><msup><mi>x</mi><mn>2</mn></msup>
|
8
|
+
<mo>+</mo> <mi>b</mi><mi>x</mi>
|
9
|
+
<mo>+</mo> <mi>c</mi> <mo>=</mo> <mn>0</mn>
|
10
|
+
</math>
|
11
|
+
"""
|
12
|
+
Then the result text is "ax2+bx+c=0"
|
13
|
+
And the contains more than 10 HTML tags
|
14
|
+
|
15
|
+
Scenario: The renderer generates HTML from TeX
|
16
|
+
When the renderer is invoked with
|
17
|
+
"""
|
18
|
+
\(ax^2 + bx + c = 0\)
|
19
|
+
"""
|
20
|
+
Then the result text is "ax2+bx+c=0"
|
21
|
+
And the contains more than 10 HTML tags
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Feature: The renderer can render PNG images from math
|
2
|
+
|
3
|
+
Scenario: When an image path is supplied, the generated image is put there
|
4
|
+
Given the image path is set
|
5
|
+
When the renderer is invoked with a valid expression
|
6
|
+
Then the image is generated in the directory
|
7
|
+
|
8
|
+
Scenario: When there is no image path supplied, then an image is not generated
|
9
|
+
Given the image path is not set
|
10
|
+
When getting the image path
|
11
|
+
Then an Exception occurs
|
12
|
+
|
13
|
+
Scenario: The generated image contains the expression
|
14
|
+
Given an image is generated for the formulae
|
15
|
+
"""
|
16
|
+
\(ax^2 + bx + c = 0\)
|
17
|
+
"""
|
18
|
+
Then it's width is at least 120px
|
19
|
+
And it's height is at least 10px
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Feature: Padding, min width and additional styles can be set for image generation
|
2
|
+
|
3
|
+
Scenario: If a padding is set, then the generated image is bigger
|
4
|
+
When an image is generated with 10px padding
|
5
|
+
Then it's size increases by 20px in every dimension
|
6
|
+
|
7
|
+
Scenario: If min width is set, then no image will be smaller than that
|
8
|
+
When an image is generated with 300px min width for
|
9
|
+
"""
|
10
|
+
\(ax^2 + bx + c = 0\)
|
11
|
+
"""
|
12
|
+
Then it's width is 300px
|
13
|
+
|
14
|
+
Scenario: If additional styles are supplied, they will be applied to the image
|
15
|
+
Given an image is generated for the formulae
|
16
|
+
"""
|
17
|
+
\(ax^2 + bx + c = 0\)
|
18
|
+
"""
|
19
|
+
And the additional styles are
|
20
|
+
"""
|
21
|
+
body{background-color:blue;}
|
22
|
+
"""
|
23
|
+
Then the generated image is mostly blue
|
@@ -0,0 +1,11 @@
|
|
1
|
+
When(/^the renderer is invoked with$/) do |string|
|
2
|
+
@renderer = Mathjax_Renderer::Renderer.new(string)
|
3
|
+
end
|
4
|
+
|
5
|
+
Then(/^the result text is "(.*?)"$/) do |arg1|
|
6
|
+
expect(@renderer.html.inner_text).to eq arg1
|
7
|
+
end
|
8
|
+
|
9
|
+
Then(/^the contains more than (\d+) HTML tags$/) do |arg1|
|
10
|
+
expect(@renderer.html.xpath('.//*').size).to be >= arg1.to_i
|
11
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
Given(/^the image path is set$/) do
|
2
|
+
@img_path = $tmpdir
|
3
|
+
end
|
4
|
+
|
5
|
+
When(/^the renderer is invoked with a valid expression$/) do
|
6
|
+
@renderer = Mathjax_Renderer::Renderer.new('\(ax^2 + bx + c = 0\)', @img_path)
|
7
|
+
end
|
8
|
+
|
9
|
+
Then(/^the image is generated in the directory$/) do
|
10
|
+
img_name = @renderer.image_name
|
11
|
+
expect(File.exists? @img_path + '/' +img_name).to be
|
12
|
+
end
|
13
|
+
|
14
|
+
Given(/^the image path is not set$/) do
|
15
|
+
@img_path = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
Then(/^an Exception occurs$/) do
|
19
|
+
expect(@exception).to be
|
20
|
+
end
|
21
|
+
|
22
|
+
When(/^getting the image path$/) do
|
23
|
+
begin
|
24
|
+
@renderer.image_name
|
25
|
+
rescue => @exception
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Given(/^an image is generated for the formulae$/) do |string|
|
30
|
+
@expression = string
|
31
|
+
renderer = Mathjax_Renderer::Renderer.new(@expression, $tmpdir)
|
32
|
+
@image = ChunkyPNG::Image.from_file("#{$tmpdir}/#{renderer.image_name}")
|
33
|
+
end
|
34
|
+
|
35
|
+
Then(/^it's width is at least (\d+)px$/) do |arg1|
|
36
|
+
expect(@image.width).to be >= arg1.to_i
|
37
|
+
end
|
38
|
+
|
39
|
+
Then(/^it's height is at least (\d+)px$/) do |arg1|
|
40
|
+
expect(@image.height).to be >= arg1.to_i
|
41
|
+
end
|
42
|
+
|
43
|
+
When(/^an image is generated with (\d+)px padding$/) do |arg1|
|
44
|
+
original_renderer = Mathjax_Renderer::Renderer.new('\(ax^2 + bx + c = 0\)', $tmpdir)
|
45
|
+
padded_renderer = Mathjax_Renderer::Renderer.new('\(ax^2 + bx + c = 0\)', $tmpdir, padding: arg1.to_i)
|
46
|
+
@original_image = ChunkyPNG::Image.from_file("#{$tmpdir}/#{original_renderer.image_name}")
|
47
|
+
@padded_image = ChunkyPNG::Image.from_file("#{$tmpdir}/#{padded_renderer.image_name}")
|
48
|
+
end
|
49
|
+
|
50
|
+
Then(/^it's size increases by (\d+)px in every dimension$/) do |arg1|
|
51
|
+
expect(@padded_image.width).to eq @original_image.width + arg1.to_i
|
52
|
+
expect(@padded_image.height).to eq @original_image.height + arg1.to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
When(/^an image is generated with (\d+)px min width for$/) do |arg1, string|
|
56
|
+
renderer = Mathjax_Renderer::Renderer.new(string, $tmpdir, min_width:arg1.to_i)
|
57
|
+
@image = ChunkyPNG::Image.from_file("#{$tmpdir}/#{renderer.image_name}")
|
58
|
+
end
|
59
|
+
|
60
|
+
Then(/^it's width is (\d+)px$/) do |arg1|
|
61
|
+
expect(@image.width).to eq arg1.to_i
|
62
|
+
end
|
63
|
+
|
64
|
+
Given(/^the additional styles are$/) do |string|
|
65
|
+
renderer = Mathjax_Renderer::Renderer.new(@expression, $tmpdir, additional_styles:string)
|
66
|
+
@image = ChunkyPNG::Image.from_file("#{$tmpdir}/#{renderer.image_name}")
|
67
|
+
end
|
68
|
+
|
69
|
+
Then(/^the generated image is mostly blue$/) do
|
70
|
+
blue = 0
|
71
|
+
@image.width.times do |x|
|
72
|
+
@image.height.times do |y|
|
73
|
+
blue+=1 if ChunkyPNG::Color.b(@image[1,1])==255
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
all_pixels = @image.width * @image.height
|
78
|
+
|
79
|
+
expect(blue).to be >= all_pixels / 2
|
80
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
module Mathjax_Renderer
|
2
|
+
class Renderer
|
3
|
+
require 'capybara'
|
4
|
+
require 'capybara/dsl'
|
5
|
+
require 'headless'
|
6
|
+
|
7
|
+
include Capybara::DSL
|
8
|
+
|
9
|
+
def initialize(mathml, image_base_url = nil, options = {})
|
10
|
+
@mathml = mathml
|
11
|
+
@image_base_url = image_base_url
|
12
|
+
@options = {min_width: 0, extra_style:'', padding:0}.merge options
|
13
|
+
end
|
14
|
+
|
15
|
+
def image_name
|
16
|
+
raise ArgumentError if @image_base_url.nil?
|
17
|
+
render! unless File.exist?(image_path)
|
18
|
+
_image_name
|
19
|
+
end
|
20
|
+
|
21
|
+
def render!
|
22
|
+
server = RendererServer.new
|
23
|
+
server.ensure_started!
|
24
|
+
|
25
|
+
added_margin = [@options[:padding],@options[:min_width]].max
|
26
|
+
|
27
|
+
url = server.add_content(@mathml, added_margin, @options[:extra_style])
|
28
|
+
|
29
|
+
Headless.ly do
|
30
|
+
|
31
|
+
Capybara.register_driver :chrome do |app|
|
32
|
+
Capybara::Selenium::Driver.new(app, :browser => :chrome)
|
33
|
+
end
|
34
|
+
|
35
|
+
Capybara.default_driver = :chrome
|
36
|
+
Capybara.app_host = "http://localhost:#{server.port}"
|
37
|
+
|
38
|
+
visit url
|
39
|
+
|
40
|
+
def mathjax_ready?(page)
|
41
|
+
html = Nokogiri::HTML(page.html)
|
42
|
+
!html.css('.MathJax').empty? && html.css('.MathJax_Processing').empty? && html.css('.MathJax_Processed').empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
sleep 0.1 until mathjax_ready?(page)
|
46
|
+
|
47
|
+
unless @image_base_url.nil?
|
48
|
+
require 'chunky_png'
|
49
|
+
driver = page.driver
|
50
|
+
|
51
|
+
require 'fileutils'
|
52
|
+
FileUtils.mkpath @image_base_url
|
53
|
+
|
54
|
+
driver.save_screenshot(image_path)
|
55
|
+
|
56
|
+
el= page.find('.MathJax .math').native
|
57
|
+
|
58
|
+
image = ChunkyPNG::Image.from_file(image_path)
|
59
|
+
|
60
|
+
correction = [(@options[:min_width] -(el.size.width + 2 * @options[:padding])) / 2,0].max
|
61
|
+
|
62
|
+
x = el.location.x + 1 - @options[:padding]-correction
|
63
|
+
y = el.location.y + 1 - @options[:padding]
|
64
|
+
width = [el.size.width + 2 * @options[:padding],@options[:min_width]].max
|
65
|
+
height = el.size.height+ 2 * @options[:padding]
|
66
|
+
|
67
|
+
image.crop!(x, y, width, height)
|
68
|
+
image.save(image_path)
|
69
|
+
end
|
70
|
+
result = Nokogiri::HTML(page.html).css('.MathJax')[0]
|
71
|
+
|
72
|
+
put_cache!(params_hash,result)
|
73
|
+
|
74
|
+
page.driver.quit
|
75
|
+
|
76
|
+
@html = result
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def html
|
81
|
+
return cached(params_hash) if cached? params_hash
|
82
|
+
|
83
|
+
render! if @html.nil?
|
84
|
+
|
85
|
+
@html
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def params_hash
|
91
|
+
Digest::SHA1.hexdigest(@mathml+@options.to_s)
|
92
|
+
end
|
93
|
+
|
94
|
+
def image_path
|
95
|
+
"#{@image_base_url}/#{_image_name}"
|
96
|
+
end
|
97
|
+
|
98
|
+
def _image_name
|
99
|
+
"#{params_hash}.png"
|
100
|
+
end
|
101
|
+
|
102
|
+
@@cache={}
|
103
|
+
|
104
|
+
def cache
|
105
|
+
@@cache
|
106
|
+
end
|
107
|
+
|
108
|
+
def cached?(mathml)
|
109
|
+
cache.has_key?(mathml)
|
110
|
+
end
|
111
|
+
|
112
|
+
def cached(mathml)
|
113
|
+
cache[mathml]
|
114
|
+
end
|
115
|
+
|
116
|
+
def put_cache!(mathml,result)
|
117
|
+
cache[mathml]=result
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class RendererServer
|
122
|
+
require 'concurrent/atomic/atomic_boolean'
|
123
|
+
require 'digest'
|
124
|
+
|
125
|
+
def add_content(content, added_margin, extra_style = '')
|
126
|
+
digest = Digest::SHA1.hexdigest(content)
|
127
|
+
path = "/#{digest}.html"
|
128
|
+
server.mount_proc path do |_, res|
|
129
|
+
res.body = response(content, added_margin, extra_style)
|
130
|
+
end
|
131
|
+
|
132
|
+
path
|
133
|
+
end
|
134
|
+
|
135
|
+
def port
|
136
|
+
server.config[:Port]
|
137
|
+
end
|
138
|
+
|
139
|
+
def response(content, added_margin, extra_style)
|
140
|
+
"
|
141
|
+
<html><head>
|
142
|
+
<script type='text/x-mathjax_renderer-config'>
|
143
|
+
MathJax.Hub.Config({
|
144
|
+
messageStyle: 'none',
|
145
|
+
showMathMenu:false
|
146
|
+
});
|
147
|
+
</script>
|
148
|
+
<script type='text/javascript'
|
149
|
+
src='javascripts/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
|
150
|
+
<style>.MathJax{position:absolute!important;margin:#{added_margin}px!important;}
|
151
|
+
#{extra_style}</style>
|
152
|
+
</head><body>#{content}</body></html>"
|
153
|
+
end
|
154
|
+
|
155
|
+
def ensure_started!
|
156
|
+
if start!
|
157
|
+
Thread.start do
|
158
|
+
require 'webrick'
|
159
|
+
mathjax_dir = Gem::Specification.find_by_name("rails-assets-MathJax").gem_dir
|
160
|
+
|
161
|
+
self.server = WEBrick::HTTPServer.new(:Port => 0, :DocumentRoot => "#{mathjax_dir}/app/assets",:AccessLog => [], :Logger => WEBrick::Log::new('/dev/null', 7))
|
162
|
+
|
163
|
+
server.mount '/javascripts/MathJax/fonts', WEBrick::HTTPServlet::FileHandler, "#{mathjax_dir}/app/assets/fonts/MathJax/fonts"
|
164
|
+
|
165
|
+
begin
|
166
|
+
server.start
|
167
|
+
ensure
|
168
|
+
server.shutdown
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
def server_started?
|
173
|
+
require 'net/http'
|
174
|
+
uri = URI("http://localhost:#{port}/javascripts/MathJax/MathJax.js")
|
175
|
+
|
176
|
+
req = Net::HTTP::Get.new(uri)
|
177
|
+
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
|
178
|
+
http.read_timeout = 1
|
179
|
+
http.request(req)
|
180
|
+
}
|
181
|
+
|
182
|
+
res.is_a?(Net::HTTPSuccess)
|
183
|
+
rescue
|
184
|
+
false
|
185
|
+
end
|
186
|
+
|
187
|
+
sleep 0.1 until server_started?
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
|
192
|
+
def server
|
193
|
+
@@server
|
194
|
+
end
|
195
|
+
|
196
|
+
def server=(server)
|
197
|
+
@@server = server
|
198
|
+
end
|
199
|
+
|
200
|
+
@@started=Concurrent::AtomicBoolean.new
|
201
|
+
|
202
|
+
def start!
|
203
|
+
@@started.make_true
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
end
|
data/lib/renderer.rb
ADDED
data/mathjax-renderer.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require '
|
4
|
+
require 'mathjax_renderer/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "mathjax-renderer"
|
8
|
-
spec.version =
|
8
|
+
spec.version = Mathjax_Renderer::VERSION
|
9
9
|
spec.authors = ["sashee"]
|
10
10
|
spec.email = ["gsashee@gmail.com"]
|
11
11
|
spec.summary = %q{Summary}
|
@@ -21,6 +21,9 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
|
24
|
+
spec.add_development_dependency "cucumber"
|
25
|
+
spec.add_development_dependency "rspec-expectations"
|
26
|
+
|
24
27
|
spec.add_dependency "rails-assets-MathJax"
|
25
28
|
spec.add_dependency "nokogiri"
|
26
29
|
spec.add_dependency "concurrent-ruby"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mathjax-renderer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sashee
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: cucumber
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-expectations
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: rails-assets-MathJax
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -162,8 +190,15 @@ files:
|
|
162
190
|
- LICENSE.txt
|
163
191
|
- README.md
|
164
192
|
- Rakefile
|
165
|
-
-
|
166
|
-
-
|
193
|
+
- features/generate_html.feature
|
194
|
+
- features/generate_image.feature
|
195
|
+
- features/options.feature
|
196
|
+
- features/step_definitions/html_steps.rb
|
197
|
+
- features/step_definitions/image_steps.rb
|
198
|
+
- features/support/env.rb
|
199
|
+
- lib/mathjax_renderer/renderer.rb
|
200
|
+
- lib/mathjax_renderer/version.rb
|
201
|
+
- lib/renderer.rb
|
167
202
|
- mathjax-renderer.gemspec
|
168
203
|
homepage: ''
|
169
204
|
licenses:
|
@@ -189,4 +224,10 @@ rubygems_version: 2.4.3
|
|
189
224
|
signing_key:
|
190
225
|
specification_version: 4
|
191
226
|
summary: Summary
|
192
|
-
test_files:
|
227
|
+
test_files:
|
228
|
+
- features/generate_html.feature
|
229
|
+
- features/generate_image.feature
|
230
|
+
- features/options.feature
|
231
|
+
- features/step_definitions/html_steps.rb
|
232
|
+
- features/step_definitions/image_steps.rb
|
233
|
+
- features/support/env.rb
|
data/lib/mathjax/renderer.rb
DELETED
@@ -1,201 +0,0 @@
|
|
1
|
-
require "mathjax/renderer/version"
|
2
|
-
|
3
|
-
module Mathjax
|
4
|
-
module Renderer
|
5
|
-
class MathJaxRenderer
|
6
|
-
require 'capybara'
|
7
|
-
require 'capybara/dsl'
|
8
|
-
require 'headless'
|
9
|
-
|
10
|
-
include Capybara::DSL
|
11
|
-
|
12
|
-
def initialize(image_base_url, mathml, options = {})
|
13
|
-
@mathml = mathml
|
14
|
-
@image_base_url = image_base_url
|
15
|
-
@options = {min_width: 0, extra_style:'', padding:0}.merge options
|
16
|
-
end
|
17
|
-
|
18
|
-
def image_name
|
19
|
-
render! unless File.exist?(image_path)
|
20
|
-
_image_name
|
21
|
-
end
|
22
|
-
|
23
|
-
def render!
|
24
|
-
server = MathJaxServer.new
|
25
|
-
server.ensure_started!
|
26
|
-
|
27
|
-
url = server.add_content(@mathml, @options[:extra_style])
|
28
|
-
|
29
|
-
Headless.ly do
|
30
|
-
|
31
|
-
Capybara.register_driver :chrome do |app|
|
32
|
-
Capybara::Selenium::Driver.new(app, :browser => :chrome)
|
33
|
-
end
|
34
|
-
|
35
|
-
Capybara.default_driver = :chrome
|
36
|
-
Capybara.app_host = "http://localhost:#{server.port}"
|
37
|
-
|
38
|
-
visit url
|
39
|
-
|
40
|
-
def mathjax_ready?(page)
|
41
|
-
html = Nokogiri::HTML(page.html)
|
42
|
-
!html.css('.MathJax_Display').empty? && html.css('.MathJax_Processing').empty? && html.css('.MathJax_Processed').empty?
|
43
|
-
end
|
44
|
-
|
45
|
-
sleep 0.1 until mathjax_ready?(page)
|
46
|
-
|
47
|
-
require 'chunky_png'
|
48
|
-
driver = page.driver
|
49
|
-
|
50
|
-
require 'fileutils'
|
51
|
-
FileUtils.mkpath @image_base_url
|
52
|
-
|
53
|
-
driver.save_screenshot(image_path)
|
54
|
-
|
55
|
-
el= page.find('.MathJax_Display .math').native
|
56
|
-
|
57
|
-
image = ChunkyPNG::Image.from_file(image_path)
|
58
|
-
|
59
|
-
correction = [(@options[:min_width] -(el.size.width + 2 * @options[:padding])) /2,0].max
|
60
|
-
|
61
|
-
image.crop!(el.location.x + 1 - @options[:padding]-correction, el.location.y + 1 - @options[:padding], el.size.width + 2 * @options[:padding] + (2 * correction), el.size.height+ 2 * @options[:padding])
|
62
|
-
image.save(image_path)
|
63
|
-
|
64
|
-
result = Nokogiri::HTML(page.html).css('.MathJax_Display')[0]
|
65
|
-
|
66
|
-
put_cache!(params_hash,result)
|
67
|
-
|
68
|
-
page.driver.quit
|
69
|
-
|
70
|
-
@html = result
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def html
|
75
|
-
return cached(params_hash) if cached? params_hash
|
76
|
-
|
77
|
-
render! if @html.nil?
|
78
|
-
|
79
|
-
@html
|
80
|
-
end
|
81
|
-
|
82
|
-
private
|
83
|
-
|
84
|
-
def params_hash
|
85
|
-
Digest::SHA1.hexdigest(@mathml+@options.to_s)
|
86
|
-
end
|
87
|
-
|
88
|
-
def image_path
|
89
|
-
"#{@image_base_url}/#{_image_name}"
|
90
|
-
end
|
91
|
-
|
92
|
-
def _image_name
|
93
|
-
"#{params_hash}.png"
|
94
|
-
end
|
95
|
-
|
96
|
-
@@cache={}
|
97
|
-
|
98
|
-
def cache
|
99
|
-
@@cache
|
100
|
-
end
|
101
|
-
|
102
|
-
def cached?(mathml)
|
103
|
-
cache.has_key?(mathml)
|
104
|
-
end
|
105
|
-
|
106
|
-
def cached(mathml)
|
107
|
-
cache[mathml]
|
108
|
-
end
|
109
|
-
|
110
|
-
def put_cache!(mathml,result)
|
111
|
-
cache[mathml]=result
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
class MathJaxServer
|
116
|
-
require 'concurrent/atomic/atomic_boolean'
|
117
|
-
require 'digest'
|
118
|
-
|
119
|
-
def add_content(content, extra_style = '')
|
120
|
-
digest = Digest::SHA1.hexdigest(content)
|
121
|
-
path = "/#{digest}.html"
|
122
|
-
server.mount_proc path do |_, res|
|
123
|
-
res.body = response(content, extra_style)
|
124
|
-
end
|
125
|
-
|
126
|
-
path
|
127
|
-
end
|
128
|
-
|
129
|
-
def port
|
130
|
-
server.config[:Port]
|
131
|
-
end
|
132
|
-
|
133
|
-
def response(content, extra_style)
|
134
|
-
"
|
135
|
-
<html><head>
|
136
|
-
<script type='text/x-mathjax-config'>
|
137
|
-
MathJax.Hub.Config({
|
138
|
-
messageStyle: 'none',
|
139
|
-
showMathMenu:false
|
140
|
-
});
|
141
|
-
</script>
|
142
|
-
<script type='text/javascript'
|
143
|
-
src='javascripts/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
|
144
|
-
<style>#{extra_style}</style>
|
145
|
-
</head><body>#{content}</body></html>"
|
146
|
-
end
|
147
|
-
|
148
|
-
def ensure_started!
|
149
|
-
if start!
|
150
|
-
Thread.start do
|
151
|
-
require 'webrick'
|
152
|
-
mathjax_dir = Gem::Specification.find_by_name("rails-assets-MathJax").gem_dir
|
153
|
-
|
154
|
-
self.server = WEBrick::HTTPServer.new(:Port => 0, :DocumentRoot => "#{mathjax_dir}/app/assets",:AccessLog => [], :Logger => WEBrick::Log::new('/dev/null', 7))
|
155
|
-
|
156
|
-
server.mount '/javascripts/MathJax/fonts', WEBrick::HTTPServlet::FileHandler, "#{mathjax_dir}/app/assets/fonts/MathJax/fonts"
|
157
|
-
|
158
|
-
begin
|
159
|
-
server.start
|
160
|
-
ensure
|
161
|
-
server.shutdown
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
def server_started?
|
166
|
-
require 'net/http'
|
167
|
-
uri = URI("http://localhost:#{port}/javascripts/MathJax/MathJax.js")
|
168
|
-
|
169
|
-
req = Net::HTTP::Get.new(uri)
|
170
|
-
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
|
171
|
-
http.read_timeout = 1
|
172
|
-
http.request(req)
|
173
|
-
}
|
174
|
-
|
175
|
-
res.is_a?(Net::HTTPSuccess)
|
176
|
-
rescue
|
177
|
-
false
|
178
|
-
end
|
179
|
-
|
180
|
-
sleep 0.1 until server_started?
|
181
|
-
end
|
182
|
-
|
183
|
-
private
|
184
|
-
|
185
|
-
def server
|
186
|
-
@@server
|
187
|
-
end
|
188
|
-
|
189
|
-
def server=(server)
|
190
|
-
@@server = server
|
191
|
-
end
|
192
|
-
|
193
|
-
@@started=Concurrent::AtomicBoolean.new
|
194
|
-
|
195
|
-
def start!
|
196
|
-
@@started.make_true
|
197
|
-
end
|
198
|
-
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|