mathjax-renderer 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/README.md +2 -4
- data/features/options.feature +2 -2
- data/features/step_definitions/image_steps.rb +6 -3
- data/features/support/env.rb +1 -1
- data/lib/mathjax_renderer/renderer.rb +59 -46
- data/lib/mathjax_renderer/version.rb +1 -1
- data/mathjax-renderer.gemspec +2 -3
- metadata +6 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e95c5e5f34072effcd51928ee50b719c620e67e
|
4
|
+
data.tar.gz: edd581776d9b4e52240cd966d99c2fcad8b78a65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 553117673179eb5420f9a52c40bfef53a9cce7a4619ac8e6552ffbd4213bfd304e9aab7f05bae788b0cc181e7b75f9614f89b87750425f256ac0de0cc611a13f
|
7
|
+
data.tar.gz: ebed43efddc15f38bb6a303b32ddad25e567506ee761f4e8266675dc8a3a7b54b90fd88fee3cda7abf604befe7172b5828d8007dab427599eef90c72a08e56c7
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -7,6 +7,8 @@ generating images from MathJax expressions which can be inserted to the site ins
|
|
7
7
|
expression, thus making them visible to readers. Also, there is an option to generate the HTML,
|
8
8
|
so you can remove the client-side scripts.
|
9
9
|
|
10
|
+
You can find some background in this blog post: https://advancedweb.hu/2015/03/17/mathjax-processing-on-the-server-side/
|
11
|
+
|
10
12
|
## Installation
|
11
13
|
|
12
14
|
Add this line to your application's Gemfile:
|
@@ -29,10 +31,6 @@ mathjax-renderer can generate the HTML from running the MathJax scripts, and it
|
|
29
31
|
generate an image from the result. It uses a global cache, so it is fast for the second time for the
|
30
32
|
same input.
|
31
33
|
|
32
|
-
### Requirements
|
33
|
-
|
34
|
-
mathjax-renderer uses Chrome with Xvfb, so they need to be installed (sorry, not gonna work on Windows).
|
35
|
-
|
36
34
|
### Generating the HTML
|
37
35
|
|
38
36
|
To generated the HTML, just call:
|
data/features/options.feature
CHANGED
@@ -18,6 +18,6 @@ Feature: Padding, min width and additional styles can be set for image generatio
|
|
18
18
|
"""
|
19
19
|
And the additional styles are
|
20
20
|
"""
|
21
|
-
|
21
|
+
body{background-color:blue;}
|
22
22
|
"""
|
23
|
-
Then the generated image is mostly blue
|
23
|
+
Then the generated image is mostly blue
|
@@ -62,7 +62,7 @@ Then(/^it's width is (\d+)px$/) do |arg1|
|
|
62
62
|
end
|
63
63
|
|
64
64
|
Given(/^the additional styles are$/) do |string|
|
65
|
-
renderer = Mathjax_Renderer::Renderer.new(@expression, $tmpdir,
|
65
|
+
renderer = Mathjax_Renderer::Renderer.new(@expression, $tmpdir, extra_style:string)
|
66
66
|
@image = ChunkyPNG::Image.from_file("#{$tmpdir}/#{renderer.image_name}")
|
67
67
|
end
|
68
68
|
|
@@ -70,11 +70,14 @@ Then(/^the generated image is mostly blue$/) do
|
|
70
70
|
blue = 0
|
71
71
|
@image.width.times do |x|
|
72
72
|
@image.height.times do |y|
|
73
|
-
|
73
|
+
b = ChunkyPNG::Color.b(@image[x,y])
|
74
|
+
g = ChunkyPNG::Color.g(@image[x,y])
|
75
|
+
r = ChunkyPNG::Color.r(@image[x,y])
|
76
|
+
blue += 1 if b == 255 && g == 0 && r == 0
|
74
77
|
end
|
75
78
|
end
|
76
79
|
|
77
80
|
all_pixels = @image.width * @image.height
|
78
81
|
|
79
82
|
expect(blue).to be >= all_pixels / 2
|
80
|
-
end
|
83
|
+
end
|
data/features/support/env.rb
CHANGED
@@ -2,7 +2,6 @@ module Mathjax_Renderer
|
|
2
2
|
class Renderer
|
3
3
|
require 'capybara'
|
4
4
|
require 'capybara/dsl'
|
5
|
-
require 'headless'
|
6
5
|
require 'timeout'
|
7
6
|
|
8
7
|
include Capybara::DSL
|
@@ -23,61 +22,71 @@ module Mathjax_Renderer
|
|
23
22
|
server = RendererServer.new
|
24
23
|
server.ensure_started!
|
25
24
|
|
26
|
-
url = server.add_content(@mathml, @options[:extra_style])
|
25
|
+
url = server.add_content(@mathml, @options[:extra_style], @options[:padding])
|
27
26
|
|
28
|
-
|
27
|
+
require 'phantomjs/poltergeist'
|
28
|
+
require 'phantomjs'
|
29
|
+
Capybara.register_driver :poltergeist do |app|
|
30
|
+
Capybara::Poltergeist::Driver.new(app, :phantomjs => Phantomjs.path)
|
31
|
+
end
|
32
|
+
Capybara.default_driver = :poltergeist
|
33
|
+
Capybara.app_host = "http://localhost:#{server.port}"
|
29
34
|
|
30
|
-
|
31
|
-
Capybara::Selenium::Driver.new(app, :browser => :chrome, :args => ['no-sandbox','no-default-browser-check','no-first-run','disable-default-apps'])
|
32
|
-
end
|
35
|
+
visit url
|
33
36
|
|
34
|
-
|
35
|
-
|
37
|
+
def mathjax_ready?(page)
|
38
|
+
html = Nokogiri::HTML(page.html)
|
39
|
+
!html.css('.MathJax').empty? &&
|
40
|
+
html.css('.MathJax_Processing').empty? &&
|
41
|
+
html.css('.MathJax_Processed').empty?
|
42
|
+
end
|
36
43
|
|
37
|
-
|
44
|
+
Timeout.timeout(5) do
|
45
|
+
sleep 0.1 until mathjax_ready?(page)
|
46
|
+
end
|
38
47
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
html.css('.MathJax_Processing').empty? &&
|
43
|
-
html.css('.MathJax_Processed').empty?
|
44
|
-
end
|
48
|
+
unless @image_base_url.nil?
|
49
|
+
require 'chunky_png'
|
50
|
+
driver = page.driver
|
45
51
|
|
46
|
-
|
47
|
-
|
48
|
-
end
|
52
|
+
require 'fileutils'
|
53
|
+
FileUtils.mkpath @image_base_url
|
49
54
|
|
50
|
-
|
51
|
-
require 'chunky_png'
|
52
|
-
driver = page.driver
|
55
|
+
driver.save_screenshot(image_path)
|
53
56
|
|
54
|
-
|
55
|
-
FileUtils.mkpath @image_base_url
|
57
|
+
image = ChunkyPNG::Image.from_file(image_path)
|
56
58
|
|
57
|
-
|
59
|
+
location = page.driver.evaluate_script <<-EOS
|
60
|
+
function() {
|
61
|
+
var ele = document.querySelector('.MathJax .math');
|
62
|
+
var rect = ele.getBoundingClientRect();
|
63
|
+
return [rect.left, rect.top];
|
64
|
+
}();
|
65
|
+
EOS
|
58
66
|
|
59
|
-
|
67
|
+
size = page.driver.evaluate_script <<-EOS
|
68
|
+
function() {
|
69
|
+
var ele = document.querySelector('.MathJax .math');
|
70
|
+
var rect = ele.getBoundingClientRect();
|
71
|
+
return [rect.width, rect.height];
|
72
|
+
}();
|
73
|
+
EOS
|
60
74
|
|
61
|
-
|
75
|
+
correction = [(@options[:min_width] -(size[0] + 2 * @options[:padding])) / 2,0].max
|
62
76
|
|
63
|
-
|
77
|
+
x = location[0] + 1 - @options[:padding]-correction
|
78
|
+
y = location[1] + 1 - @options[:padding]
|
79
|
+
width = [size[0].ceil + 2 * @options[:padding],@options[:min_width]].max
|
80
|
+
height = size[1]+ 2 * @options[:padding]
|
64
81
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
82
|
+
image.crop!(x, y, width, height)
|
83
|
+
image.save(image_path)
|
84
|
+
end
|
85
|
+
result = Nokogiri::HTML(page.html).css('.MathJax .math')[0]
|
69
86
|
|
70
|
-
|
71
|
-
image.save(image_path)
|
72
|
-
end
|
73
|
-
result = Nokogiri::HTML(page.html).css('.MathJax')[0]
|
87
|
+
put_cache!(params_hash,result)
|
74
88
|
|
75
|
-
|
76
|
-
|
77
|
-
page.driver.quit
|
78
|
-
|
79
|
-
@html = result
|
80
|
-
end
|
89
|
+
@html = result
|
81
90
|
end
|
82
91
|
|
83
92
|
def html
|
@@ -125,11 +134,11 @@ module Mathjax_Renderer
|
|
125
134
|
require 'concurrent/atomic/atomic_boolean'
|
126
135
|
require 'digest'
|
127
136
|
|
128
|
-
def add_content(content, extra_style = '')
|
137
|
+
def add_content(content, extra_style = '', padding = 0)
|
129
138
|
digest = Digest::SHA1.hexdigest(content)
|
130
139
|
path = "/#{digest}.html"
|
131
140
|
server.mount_proc path do |_, res|
|
132
|
-
res.body = response(content, extra_style)
|
141
|
+
res.body = response(content, extra_style, padding)
|
133
142
|
end
|
134
143
|
|
135
144
|
path
|
@@ -139,7 +148,7 @@ module Mathjax_Renderer
|
|
139
148
|
server.config[:Port]
|
140
149
|
end
|
141
150
|
|
142
|
-
def response(content, extra_style)
|
151
|
+
def response(content, extra_style, padding)
|
143
152
|
"
|
144
153
|
<html><head>
|
145
154
|
<script type='text/x-mathjax_renderer-config'>
|
@@ -151,7 +160,11 @@ module Mathjax_Renderer
|
|
151
160
|
<script type='text/javascript'
|
152
161
|
src='javascripts/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
|
153
162
|
<style>body{display: flex;justify-content: center;}
|
154
|
-
#{extra_style}
|
163
|
+
#{extra_style}
|
164
|
+
.MathJax{
|
165
|
+
left: #{padding}px;
|
166
|
+
top: #{padding}px;
|
167
|
+
}</style>
|
155
168
|
</head><body>#{content}</body></html>"
|
156
169
|
end
|
157
170
|
|
@@ -217,4 +230,4 @@ module Mathjax_Renderer
|
|
217
230
|
end
|
218
231
|
|
219
232
|
end
|
220
|
-
end
|
233
|
+
end
|
data/mathjax-renderer.gemspec
CHANGED
@@ -27,9 +27,8 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_dependency "rails-assets-MathJax"
|
28
28
|
spec.add_dependency "nokogiri"
|
29
29
|
spec.add_dependency "concurrent-ruby"
|
30
|
-
spec.add_dependency "selenium-webdriver"
|
31
30
|
spec.add_dependency "capybara"
|
32
|
-
spec.add_dependency "chromedriver-helper"
|
33
31
|
spec.add_dependency "chunky_png"
|
34
|
-
|
32
|
+
spec.add_dependency "phantomjs"
|
33
|
+
spec.add_dependency "poltergeist"
|
35
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sashee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,20 +108,6 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: selenium-webdriver
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
requirements:
|
115
|
-
- - ">="
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :runtime
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
requirements:
|
122
|
-
- - ">="
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
version: '0'
|
125
111
|
- !ruby/object:Gem::Dependency
|
126
112
|
name: capybara
|
127
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -137,7 +123,7 @@ dependencies:
|
|
137
123
|
- !ruby/object:Gem::Version
|
138
124
|
version: '0'
|
139
125
|
- !ruby/object:Gem::Dependency
|
140
|
-
name:
|
126
|
+
name: chunky_png
|
141
127
|
requirement: !ruby/object:Gem::Requirement
|
142
128
|
requirements:
|
143
129
|
- - ">="
|
@@ -151,7 +137,7 @@ dependencies:
|
|
151
137
|
- !ruby/object:Gem::Version
|
152
138
|
version: '0'
|
153
139
|
- !ruby/object:Gem::Dependency
|
154
|
-
name:
|
140
|
+
name: phantomjs
|
155
141
|
requirement: !ruby/object:Gem::Requirement
|
156
142
|
requirements:
|
157
143
|
- - ">="
|
@@ -165,7 +151,7 @@ dependencies:
|
|
165
151
|
- !ruby/object:Gem::Version
|
166
152
|
version: '0'
|
167
153
|
- !ruby/object:Gem::Dependency
|
168
|
-
name:
|
154
|
+
name: poltergeist
|
169
155
|
requirement: !ruby/object:Gem::Requirement
|
170
156
|
requirements:
|
171
157
|
- - ">="
|
@@ -221,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
221
207
|
version: '0'
|
222
208
|
requirements: []
|
223
209
|
rubyforge_project:
|
224
|
-
rubygems_version: 2.4.
|
210
|
+
rubygems_version: 2.4.5
|
225
211
|
signing_key:
|
226
212
|
specification_version: 4
|
227
213
|
summary: MathJax expression renderer
|