mathjax-renderer 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/lib/mathjax/renderer.rb +201 -0
- data/lib/mathjax/renderer/version.rb +5 -0
- data/mathjax-renderer.gemspec +32 -0
- metadata +192 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 165ff0794cd3f86f3bc5ced82d3a664c3158a001
|
4
|
+
data.tar.gz: 5dbba1a656af3cca9a23ed24ca2ad71080badfa1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8ad8df450bf98d2634868769ed0731f5f0d925b7ce4538769f840f924dd1d17a49d0e722d6bc0e7f49de959b46f64d1f87738d2da9d846e0213c81aee54f0141
|
7
|
+
data.tar.gz: a12f8a668b433844cdc54490a06021176fa0393e14e49fbda7a43d15f76ee13e8166668fe2ed6a3222db061b802672eec82a17f3fa0984cc82724b88805aefb4
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 sashee
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Mathjax::Renderer
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mathjax-renderer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mathjax-renderer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/mathjax-renderer/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,201 @@
|
|
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
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mathjax/renderer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mathjax-renderer"
|
8
|
+
spec.version = Mathjax::Renderer::VERSION
|
9
|
+
spec.authors = ["sashee"]
|
10
|
+
spec.email = ["gsashee@gmail.com"]
|
11
|
+
spec.summary = %q{Summary}
|
12
|
+
spec.description = %q{Description}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_dependency "rails-assets-MathJax"
|
25
|
+
spec.add_dependency "nokogiri"
|
26
|
+
spec.add_dependency "concurrent-ruby"
|
27
|
+
spec.add_dependency "selenium-webdriver"
|
28
|
+
spec.add_dependency "capybara"
|
29
|
+
spec.add_dependency "chromedriver-helper"
|
30
|
+
spec.add_dependency "chunky_png"
|
31
|
+
spec.add_dependency "headless"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mathjax-renderer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sashee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails-assets-MathJax
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
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: nokogiri
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: concurrent-ruby
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: selenium-webdriver
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: capybara
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: chromedriver-helper
|
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
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: chunky_png
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: headless
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Description
|
154
|
+
email:
|
155
|
+
- gsashee@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".gitignore"
|
161
|
+
- Gemfile
|
162
|
+
- LICENSE.txt
|
163
|
+
- README.md
|
164
|
+
- Rakefile
|
165
|
+
- lib/mathjax/renderer.rb
|
166
|
+
- lib/mathjax/renderer/version.rb
|
167
|
+
- mathjax-renderer.gemspec
|
168
|
+
homepage: ''
|
169
|
+
licenses:
|
170
|
+
- MIT
|
171
|
+
metadata: {}
|
172
|
+
post_install_message:
|
173
|
+
rdoc_options: []
|
174
|
+
require_paths:
|
175
|
+
- lib
|
176
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
|
+
requirements:
|
183
|
+
- - ">="
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: '0'
|
186
|
+
requirements: []
|
187
|
+
rubyforge_project:
|
188
|
+
rubygems_version: 2.4.3
|
189
|
+
signing_key:
|
190
|
+
specification_version: 4
|
191
|
+
summary: Summary
|
192
|
+
test_files: []
|