iruby 0.6.0 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ubuntu.yml +9 -2
- data/CHANGES.md +232 -0
- data/README.md +17 -2
- data/iruby.gemspec +1 -0
- data/lib/iruby.rb +3 -0
- data/lib/iruby/backend.rb +11 -0
- data/lib/iruby/command.rb +2 -6
- data/lib/iruby/display.rb +178 -62
- data/lib/iruby/kernel.rb +99 -10
- data/lib/iruby/session.rb +1 -0
- data/lib/iruby/utils.rb +12 -1
- data/lib/iruby/version.rb +1 -1
- data/test/helper.rb +5 -2
- data/test/iruby/backend_test.rb +12 -0
- data/test/iruby/display_test.rb +185 -0
- data/test/iruby/kernel_test.rb +33 -1
- data/test/iruby/mime_test.rb +7 -0
- data/test/iruby/multi_logger_test.rb +0 -3
- data/test/iruby/utils_test.rb +25 -0
- metadata +22 -4
- data/CHANGES +0 -181
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea24babb0bbbb88c3a3bacfbb2ca4438903d9cffa1e6609976e066cfa3e72ff5
|
4
|
+
data.tar.gz: 428513fab78f77f82632f3d66cbd2dc6a81344ea781c213164af038530ecf90e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d451a1d683ef25e3f1beeb235db1a2c082199b1a3196b3fa3c1468c505f2d7406931cdc03f678ee0b792f8b7fdf7ffe75979e9f5c2e1470c83a9c8cc1f7fc05
|
7
|
+
data.tar.gz: 2ff9d61912037029eb3b6c087d129cee9b4473f825b51a492e165ee40e0370c46bc2b41159f8f8510f0e083c1c2bb200c46b6fa1ea18c5d11f4e12bf5c61416b
|
@@ -1,8 +1,14 @@
|
|
1
1
|
name: CI
|
2
2
|
|
3
3
|
on:
|
4
|
-
|
5
|
-
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request:
|
8
|
+
types:
|
9
|
+
- opened
|
10
|
+
- synchronize
|
11
|
+
- reopened
|
6
12
|
|
7
13
|
jobs:
|
8
14
|
test:
|
@@ -35,6 +41,7 @@ jobs:
|
|
35
41
|
|
36
42
|
- name: Install requirements on ubuntu
|
37
43
|
run: |
|
44
|
+
sudo apt update
|
38
45
|
sudo apt install -y --no-install-recommends \
|
39
46
|
libczmq-dev \
|
40
47
|
python3 \
|
data/CHANGES.md
ADDED
@@ -0,0 +1,232 @@
|
|
1
|
+
# 0.7.3 (2021-07-08)
|
2
|
+
|
3
|
+
## Bug Fixes
|
4
|
+
|
5
|
+
* Do not call default renderers when to_iruby_mimebundle is available (@mrkn)
|
6
|
+
|
7
|
+
# 0.7.2 (2021-06-23)
|
8
|
+
|
9
|
+
## Bug Fixes
|
10
|
+
|
11
|
+
* Fix IRuby.table for Ruby >= 2.7 https://github.com/SciRuby/iruby/pull/305 (@topofocus)
|
12
|
+
* Fix PlainBackend to include modules https://github.com/SciRuby/iruby/issues/303 (@UliKuch, @mrkn)
|
13
|
+
|
14
|
+
# 0.7.1 (2021-06-21)
|
15
|
+
|
16
|
+
## Enhancements
|
17
|
+
|
18
|
+
* Add support of `to_iruby_mimebundle` format method https://github.com/SciRuby/iruby/pull/304 (@mrkn)
|
19
|
+
|
20
|
+
## Bug Fixes
|
21
|
+
|
22
|
+
* Prevent unintentional display the result of Session#send (@mrkn)
|
23
|
+
* Update display formatter for Gruff::Base to prevent warning (@mrkn)
|
24
|
+
|
25
|
+
# 0.7.0 (2021-05-28)
|
26
|
+
|
27
|
+
## Enhancements
|
28
|
+
|
29
|
+
* The default backend is changed to IRB (@mrkn)
|
30
|
+
* Add IRuby::Kernel#switch_backend! method (@mrkn)
|
31
|
+
|
32
|
+
## Bug Fixes
|
33
|
+
|
34
|
+
* Fix the handling of image/svg+xml https://github.com/SciRuby/iruby/pull/300, https://github.com/SciRuby/iruby/pull/301 (@kojix2)
|
35
|
+
|
36
|
+
# 0.6.1 (2021-05-26)
|
37
|
+
|
38
|
+
## Bug Fixes
|
39
|
+
|
40
|
+
* Follow the messages and hooks orders during execute_request processing (@mrkn)
|
41
|
+
|
42
|
+
# 0.6.0 (2021-05-25)
|
43
|
+
|
44
|
+
## Bug Fixes
|
45
|
+
|
46
|
+
* Fix the handling of application/javascript https://github.com/SciRuby/iruby/issues/292, https://github.com/SciRuby/iruby/pull/294 (@kylekyle, @mrkn)
|
47
|
+
|
48
|
+
## Enhancements
|
49
|
+
|
50
|
+
* Add the `initialized` event in `IRuby::Kernel` class https://github.com/SciRuby/iruby/pull/168, https://github.com/SciRuby/iruby/pull/296 (@Yuki-Inoue, @mrkn)
|
51
|
+
* Add the following four events https://github.com/SciRuby/iruby/pull/295 (@mrkn):
|
52
|
+
* `pre-execute` -- occurs before every code execution
|
53
|
+
* `pre-run-cell` -- occurs before every non-silent code execution
|
54
|
+
* `post-execute` -- occurs after every code execution
|
55
|
+
* `post-run-cell` -- occurs after every non-silent code execution
|
56
|
+
* Replace Bond with IRB in PlainBackend https://github.com/SciRuby/iruby/pull/276, https://github.com/SciRuby/iruby/pull/297 (@cfis, @mrkn)
|
57
|
+
|
58
|
+
# 0.5.0 (2021-03-25)
|
59
|
+
|
60
|
+
## Bug Fixes
|
61
|
+
|
62
|
+
* Fix Jupyter console crashes issue https://github.com/SciRuby/iruby/pull/210 (@kojix2)
|
63
|
+
* Fix syntax highlighting issue on Jpyter Lab https://github.com/SciRuby/iruby/issues/224 (@kojix2)
|
64
|
+
* Fix interoperability issue with ruby-git https://github.com/SciRuby/iruby/pull/139 (@habemus-papadum)
|
65
|
+
* Fix the issue of `$stderr.write` that cannot handle multiple arguments https://github.com/SciRuby/iruby/issues/206 (@kojix2)
|
66
|
+
* Remove a buggy `inspect_request` implementation https://github.com/SciRuby/iruby/pull/119 (@LunarLanding)
|
67
|
+
* Fix uninitialized constant `Fiddle` caused in initialization phase https://github.com/SciRuby/iruby/issues/264 (@MatthewSteen, @kjoix2)
|
68
|
+
* Fix the issue on displaying a table https://github.com/SciRuby/iruby/pull/281 (@ankane)
|
69
|
+
|
70
|
+
## Enhancements
|
71
|
+
|
72
|
+
* Add `IRuby.clear_output` method https://github.com/SciRuby/iruby/pull/220 (@kojix2)
|
73
|
+
* Make backtrace on exception simplify and more appropriate for code in a cell https://github.com/SciRuby/iruby/pull/249 (@zheng-yongping)
|
74
|
+
* Make syntax error message more appropriate https://github.com/SciRuby/iruby/pull/251 (@zheng-yongping)
|
75
|
+
* Remove top-level `In` and `Out` constants https://github.com/SciRuby/iruby/pull/229 (@kojix2)
|
76
|
+
* Use text/plain for the default format of `Numo::NArray` objects https://github.com/SciRuby/iruby/pull/255 (@kojix2)
|
77
|
+
* Use ffi-rzmq as the default ZeroMQ adapter https://github.com/SciRuby/iruby/pull/256 (@kojix2)
|
78
|
+
* Drop rbczmq support https://github.com/SciRuby/iruby/pull/260 (@rstammer)
|
79
|
+
* Add ruby-vips image support https://github.com/SciRuby/iruby/pull/279 (@ankane)
|
80
|
+
* Replace mimemagic with mime-types https://github.com/SciRuby/iruby/pull/291 (@mrkn)
|
81
|
+
|
82
|
+
# 0.4.0 (2019-07-31)
|
83
|
+
|
84
|
+
(TBD)
|
85
|
+
|
86
|
+
# 0.3 (2017-03-26)
|
87
|
+
|
88
|
+
## Bug Fixes
|
89
|
+
|
90
|
+
* Disable Jupyter keyboard manager for all popups made using IRuby.popup (@kylekyle).
|
91
|
+
* Fix Iruby/Input date values bug that set date fields to whatever the last date value was (@kylekyle).
|
92
|
+
* Fix a bug where time strings put into prompter would give an 'out of range' error (@kylekyle).
|
93
|
+
|
94
|
+
## Enhancements
|
95
|
+
|
96
|
+
* Improvements to IRuby dependency detection using `Bundler::Dependencies#specs` (@kou).
|
97
|
+
* Use less memory forcing pry to store only the last 3 commands in memory (@kylekyle).
|
98
|
+
* Use bigger z-index that is used accross all browsers (@kylekyle).
|
99
|
+
* Ability to input date values as DateTime objects in IRuby/Input (@kylekyle).
|
100
|
+
* Add option to have check boxes checked by default (@kylekyle).
|
101
|
+
* Option for multi-select in drop down menus in the prompter (@kylekyle).
|
102
|
+
* Add support for multiple widgets using `IRuby::Input::Multiple` (@kylekyle).
|
103
|
+
* Calender icon for date selector icon (@kylekyle).
|
104
|
+
* Add support for Numo/NArray (@zalt50).
|
105
|
+
* Text now only completes after a space (@zalt50).
|
106
|
+
* Remove the DONTWAIT flag when receiving a message (@cloud-oak).
|
107
|
+
* Add support for CZTop (@kou).
|
108
|
+
|
109
|
+
# 0.2.9 (2016-05-02)
|
110
|
+
|
111
|
+
## Bug Fixes
|
112
|
+
|
113
|
+
* Fix an error where a NoMethodError was being raised where a table rendered using an Array of Hashes has more than `maxcols` columns. (@CGamesPlay)
|
114
|
+
* Patch PryBackend to throw unterminated string and unexpected end-of-file syntax errors (@kylekyle)
|
115
|
+
|
116
|
+
## Enhnacements
|
117
|
+
|
118
|
+
* Add an IRuby::Input class which provides widgets for getting inputs from users. (@kylekyle)
|
119
|
+
* Add data_uri dependency (@kylekyle)
|
120
|
+
* Added a clear_output display function (@mrkn)
|
121
|
+
* Doc fixes for installation (@kozo2, @generall)
|
122
|
+
|
123
|
+
# 0.2.8 (2015-12-06)
|
124
|
+
|
125
|
+
* Add compatibility with ffi-rzmq
|
126
|
+
* Windows support
|
127
|
+
|
128
|
+
# 0.2.7 (2015-07-02)
|
129
|
+
|
130
|
+
* Fix problem with autoloaded constants in Display, problem with sciruby gem
|
131
|
+
|
132
|
+
# 0.2.6 (2015-06-21)
|
133
|
+
|
134
|
+
* Check registered kernel and Gemfile to prevent gem loading problems
|
135
|
+
* Support to_tex method for the rendering
|
136
|
+
|
137
|
+
# 0.2.5 (2015-06-07)
|
138
|
+
|
139
|
+
* Fix #29, empty signatures
|
140
|
+
* Move iruby utils to IRuby::Utils module
|
141
|
+
* Add IRuby.tex alias for IRuby.latex
|
142
|
+
* Remove example notebooks from gem
|
143
|
+
|
144
|
+
# 0.2.4 (2015-06-02)
|
145
|
+
|
146
|
+
* Better exception handling
|
147
|
+
* Fix ctrl-C issue #17
|
148
|
+
* Fix timeout issue #19
|
149
|
+
|
150
|
+
# 0.2.3 (2015-05-31)
|
151
|
+
|
152
|
+
* Fix notebook indentation
|
153
|
+
* Fix tab completion for multiple lines
|
154
|
+
|
155
|
+
# 0.2.2 (2015-05-26)
|
156
|
+
|
157
|
+
* Support history variables In, Out, _, _i etc
|
158
|
+
* Internal refactoring and minor bugfixes
|
159
|
+
|
160
|
+
# 0.2.1 (2015-05-26)
|
161
|
+
|
162
|
+
* Copy Ruby logo to kernel specification
|
163
|
+
|
164
|
+
# 0.2.0 (2015-05-25)
|
165
|
+
|
166
|
+
* Dropped IPython2 support
|
167
|
+
* Dropped Ruby < 2.0.0 support
|
168
|
+
* Supports and requires now IPython3/Jupyter
|
169
|
+
* Switch from ffi-rzmq to rbczmq
|
170
|
+
* Added IRuby::Conn (experimental, to be used by widgets)
|
171
|
+
* iruby register/unregister commands to register IRuby kernel in Jupyter
|
172
|
+
|
173
|
+
# 0.1.13 (2014-08-19)
|
174
|
+
|
175
|
+
* Improved IRuby.table, supports :maxrows and :maxcols
|
176
|
+
* IRuby#javascript workaround (https://github.com/ipython/ipython/issues/6259)
|
177
|
+
|
178
|
+
# 0.1.12 (2014-08-01)
|
179
|
+
|
180
|
+
* IRuby#table add option maxrows
|
181
|
+
* powerful display system with format and datatype registry, see #25
|
182
|
+
* Add IRuby#javascript
|
183
|
+
* Add IRuby#svg
|
184
|
+
|
185
|
+
# 0.1.11 (2014-07-08)
|
186
|
+
|
187
|
+
* Push binding if pry binding stack is empty
|
188
|
+
|
189
|
+
# 0.1.10 (2014-07-08)
|
190
|
+
|
191
|
+
* Fix #19 (pp)
|
192
|
+
* Handle exception when symlink cannot be created
|
193
|
+
* Fix dependencies and Pry backend
|
194
|
+
|
195
|
+
# 0.1.9 (2014-02-28)
|
196
|
+
|
197
|
+
* Check IPython version
|
198
|
+
|
199
|
+
# 0.1.7/0.1.8
|
200
|
+
|
201
|
+
* Bugfixes #11, #12, #13
|
202
|
+
|
203
|
+
# 0.1.6 (2013-10-11)
|
204
|
+
|
205
|
+
* Print Matrix and GSL::Matrix as LaTeX
|
206
|
+
* Add check for Pry version
|
207
|
+
|
208
|
+
# 0.1.5 (2013-10-03)
|
209
|
+
|
210
|
+
* Implement a rich display system
|
211
|
+
* Fix error output
|
212
|
+
|
213
|
+
# 0.1.4 (2013-10-03)
|
214
|
+
|
215
|
+
* Extract display handler from kernel
|
216
|
+
* Always return a text/plain response
|
217
|
+
|
218
|
+
# 0.1.3 (2013-10-03)
|
219
|
+
|
220
|
+
* Implement missing request handlers
|
221
|
+
* Detect if Bundler is running and set kernel_cmd appropriately
|
222
|
+
* Improve Pry integration
|
223
|
+
* Add support for the gems gnuplot, gruff, rmagick and mini_magick
|
224
|
+
|
225
|
+
# 0.1.2 (2013-10-02)
|
226
|
+
|
227
|
+
* Support for Pry added
|
228
|
+
* Launch `iruby console` if plain `iruby` is started
|
229
|
+
|
230
|
+
# 0.1.0 (2013-10-02)
|
231
|
+
|
232
|
+
* Cleanup and rewrite of some parts
|
data/README.md
CHANGED
@@ -31,8 +31,8 @@ If both ffi-rzmq and cztop are installed, ffi-rzmq is used. If you prefer cztop,
|
|
31
31
|
export IRUBY_SESSION_ADAPTER="cztop"
|
32
32
|
```
|
33
33
|
|
34
|
-
*
|
35
|
-
* If you want to install the
|
34
|
+
* [Pry][Pry], if you want to use [Pry][Pry] instead of IRB for the code execution backend
|
35
|
+
* If you want to install the development version of IRuby from the source code, try [specific_install](https://github.com/rdp/specific_install).
|
36
36
|
|
37
37
|
```
|
38
38
|
gem specific_install https://github.com/SciRuby/iruby
|
@@ -148,6 +148,19 @@ If you have already used IRuby with a different version, you need to generate a
|
|
148
148
|
$ iruby register --force
|
149
149
|
```
|
150
150
|
|
151
|
+
## Backends
|
152
|
+
|
153
|
+
There are two backends: PlainBackend and PryBackend.
|
154
|
+
|
155
|
+
* PlainBackend is the default backend. It uses [IRB](https://github.com/ruby/irb).
|
156
|
+
* PryBackend uses [Pry][Pry].
|
157
|
+
|
158
|
+
You can switch the backend to PryBackend by running the code below.
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
IRuby::Kernel.instance.switch_backend!(:pry)
|
162
|
+
```
|
163
|
+
|
151
164
|
## Notebooks
|
152
165
|
|
153
166
|
Take a look at the [example notebook](http://nbviewer.ipython.org/urls/raw.github.com/SciRuby/sciruby-notebooks/master/getting_started.ipynb)
|
@@ -167,3 +180,5 @@ In February 2021, [IRuby became the canonical repository](https://github.com/Sci
|
|
167
180
|
Copyright (c) IRuby contributors and the Ruby Science Foundation.
|
168
181
|
|
169
182
|
Licensed under the [MIT](LICENSE) license.
|
183
|
+
|
184
|
+
[Pry]: https://github.com/pry/pry
|
data/iruby.gemspec
CHANGED
data/lib/iruby.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
|
+
require 'fileutils'
|
1
2
|
require 'mime/types'
|
2
3
|
require 'multi_json'
|
3
4
|
require 'securerandom'
|
4
5
|
require 'openssl'
|
5
6
|
require 'tempfile'
|
6
7
|
require 'set'
|
8
|
+
require 'stringio'
|
7
9
|
|
8
10
|
require 'iruby/version'
|
9
11
|
require 'iruby/jupyter'
|
10
12
|
require 'iruby/event_manager'
|
13
|
+
require 'iruby/logger'
|
11
14
|
require 'iruby/kernel'
|
12
15
|
require 'iruby/backend'
|
13
16
|
require 'iruby/ostream'
|
data/lib/iruby/backend.rb
CHANGED
@@ -40,6 +40,7 @@ module IRuby
|
|
40
40
|
require 'irb/completion'
|
41
41
|
IRB.setup(nil)
|
42
42
|
@main = TOPLEVEL_BINDING.eval("self").dup
|
43
|
+
init_main_object(@main)
|
43
44
|
@workspace = IRB::WorkSpace.new(@main)
|
44
45
|
@irb = IRB::Irb.new(@workspace)
|
45
46
|
@eval_path = @irb.context.irb_path
|
@@ -58,6 +59,16 @@ module IRuby
|
|
58
59
|
def complete(code)
|
59
60
|
IRB::InputCompletor::CompletionProc.call(code)
|
60
61
|
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def init_main_object(main)
|
66
|
+
wrapper_module = Module.new
|
67
|
+
main.extend(wrapper_module)
|
68
|
+
main.define_singleton_method(:include) do |*args|
|
69
|
+
wrapper_module.include(*args)
|
70
|
+
end
|
71
|
+
end
|
61
72
|
end
|
62
73
|
|
63
74
|
class PryBackend
|
data/lib/iruby/command.rb
CHANGED
@@ -1,7 +1,4 @@
|
|
1
|
-
require 'iruby
|
2
|
-
|
3
|
-
require 'fileutils'
|
4
|
-
require 'multi_json'
|
1
|
+
require 'iruby'
|
5
2
|
|
6
3
|
module IRuby
|
7
4
|
class Command
|
@@ -92,7 +89,6 @@ Try `ipython help` for more information.
|
|
92
89
|
end
|
93
90
|
|
94
91
|
def run_kernel
|
95
|
-
require 'iruby/logger'
|
96
92
|
IRuby.logger = MultiLogger.new(*Logger.new(STDOUT))
|
97
93
|
STDOUT.sync = true # FIXME: This can make the integration test.
|
98
94
|
|
@@ -131,7 +127,7 @@ Try `ipython help` for more information.
|
|
131
127
|
check_registered_kernel
|
132
128
|
check_bundler {|e| STDERR.puts "Could not load bundler: #{e.message}" }
|
133
129
|
|
134
|
-
|
130
|
+
Process.exec('ipython', *@args)
|
135
131
|
end
|
136
132
|
|
137
133
|
def check_registered_kernel
|
data/lib/iruby/display.rb
CHANGED
@@ -1,10 +1,30 @@
|
|
1
|
+
require "set"
|
2
|
+
|
1
3
|
module IRuby
|
2
4
|
module Display
|
5
|
+
DEFAULT_MIME_TYPE_FORMAT_METHODS = {
|
6
|
+
"text/html" => :to_html,
|
7
|
+
"text/markdown" => :to_markdown,
|
8
|
+
"image/svg+xml" => :to_svg,
|
9
|
+
"image/png" => :to_png,
|
10
|
+
"appliation/pdf" => :to_pdf,
|
11
|
+
"image/jpeg" => :to_jpeg,
|
12
|
+
"text/latex" => [:to_latex, :to_tex],
|
13
|
+
# NOTE: Do not include the entry of "application/json" because
|
14
|
+
# all objects can respond to `to_json` due to json library
|
15
|
+
# "application/json" => :to_json,
|
16
|
+
"application/javascript" => :to_javascript,
|
17
|
+
nil => :to_iruby,
|
18
|
+
"text/plain" => :inspect
|
19
|
+
}.freeze
|
20
|
+
|
3
21
|
class << self
|
22
|
+
# @private
|
4
23
|
def convert(obj, options)
|
5
24
|
Representation.new(obj, options)
|
6
25
|
end
|
7
26
|
|
27
|
+
# @private
|
8
28
|
def display(obj, options = {})
|
9
29
|
obj = convert(obj, options)
|
10
30
|
options = obj.options
|
@@ -20,23 +40,58 @@ module IRuby
|
|
20
40
|
raise 'Invalid mime type' unless exact_mime.include?('/')
|
21
41
|
end
|
22
42
|
|
23
|
-
data =
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
43
|
+
data = if obj.respond_to?(:to_iruby_mimebundle)
|
44
|
+
render_mimebundle(obj, exact_mime, fuzzy_mime)
|
45
|
+
else
|
46
|
+
{}
|
47
|
+
end
|
48
|
+
|
49
|
+
# Render by additional formatters
|
50
|
+
render_by_registry(data, obj, exact_mime, fuzzy_mime)
|
51
|
+
|
52
|
+
# Render by to_xxx methods
|
53
|
+
default_renderers = if obj.respond_to?(:to_iruby_mimebundle)
|
54
|
+
# Do not use Hash#slice for Ruby < 2.5
|
55
|
+
{"text/plain" => DEFAULT_MIME_TYPE_FORMAT_METHODS["text/plain"]}
|
56
|
+
else
|
57
|
+
DEFAULT_MIME_TYPE_FORMAT_METHODS
|
58
|
+
end
|
59
|
+
default_renderers.each do |mime, methods|
|
60
|
+
next if mime.nil? && !data.empty? # for to_iruby
|
61
|
+
|
62
|
+
next if mime && data.key?(mime) # do not overwrite
|
63
|
+
|
64
|
+
method = Array(methods).find {|m| obj.respond_to?(m) }
|
65
|
+
next if method.nil?
|
66
|
+
|
67
|
+
result = obj.send(method)
|
68
|
+
case mime
|
69
|
+
when nil # to_iruby
|
70
|
+
case result
|
71
|
+
when nil
|
72
|
+
# do nothing
|
73
|
+
next
|
74
|
+
when Array
|
75
|
+
mime, result = result
|
76
|
+
else
|
77
|
+
warn(("Ignore the result of to_iruby method of %p because " +
|
78
|
+
"it does not return a pair of mime-type and formatted representation") % obj)
|
79
|
+
next
|
80
|
+
end
|
81
|
+
end
|
82
|
+
data[mime] = result
|
83
|
+
end
|
30
84
|
|
31
85
|
# As a last resort, interpret string representation of the object
|
32
86
|
# as the given mime type.
|
33
|
-
if exact_mime && data.
|
87
|
+
if exact_mime && !data.key?(exact_mime)
|
34
88
|
data[exact_mime] = protect(exact_mime, obj)
|
35
89
|
end
|
36
90
|
|
37
91
|
data
|
38
92
|
end
|
39
93
|
|
94
|
+
# @private
|
40
95
|
def clear_output(wait = false)
|
41
96
|
IRuby::Kernel.instance.session.send(:publish, :clear_output, wait: wait)
|
42
97
|
end
|
@@ -47,18 +102,34 @@ module IRuby
|
|
47
102
|
ascii?(mime) ? data.to_s : [data.to_s].pack('m0')
|
48
103
|
end
|
49
104
|
|
105
|
+
# Each of the following mime types must be a text type,
|
106
|
+
# but mime-types library tells us it is a non-text type.
|
107
|
+
FORCE_TEXT_TYPES = Set[
|
108
|
+
"application/javascript",
|
109
|
+
"image/svg+xml"
|
110
|
+
].freeze
|
111
|
+
|
50
112
|
def ascii?(mime)
|
51
|
-
|
52
|
-
when "application/javascript"
|
53
|
-
# Special case for application/javascript.
|
54
|
-
# This needs because mime-types tells us application/javascript a non-text type.
|
113
|
+
if FORCE_TEXT_TYPES.include?(mime)
|
55
114
|
true
|
56
115
|
else
|
57
116
|
MIME::Type.new(mime).ascii?
|
58
117
|
end
|
59
118
|
end
|
60
119
|
|
61
|
-
def
|
120
|
+
private def render_mimebundle(obj, exact_mime, fuzzy_mime)
|
121
|
+
data = {}
|
122
|
+
include_mime = [exact_mime].compact
|
123
|
+
formats, metadata = obj.to_iruby_mimebundle(include: include_mime)
|
124
|
+
formats.each do |mime, value|
|
125
|
+
if fuzzy_mime.nil? || mime.include?(fuzzy_mime)
|
126
|
+
data[mime] = value
|
127
|
+
end
|
128
|
+
end
|
129
|
+
data
|
130
|
+
end
|
131
|
+
|
132
|
+
private def render_by_registry(data, obj, exact_mime, fuzzy_mime)
|
62
133
|
# Filter matching renderer by object type
|
63
134
|
renderer = Registry.renderer.select { |r| r.match?(obj) }
|
64
135
|
|
@@ -79,6 +150,8 @@ module IRuby
|
|
79
150
|
# Return first render result which has the right mime type
|
80
151
|
renderer.each do |r|
|
81
152
|
mime, result = r.render(obj)
|
153
|
+
next if data.key?(mime)
|
154
|
+
|
82
155
|
if mime && result && (!exact_mime || exact_mime == mime) && (!fuzzy_mime || mime.include?(fuzzy_mime))
|
83
156
|
data[mime] = protect(mime, result)
|
84
157
|
break
|
@@ -89,6 +162,19 @@ module IRuby
|
|
89
162
|
end
|
90
163
|
end
|
91
164
|
|
165
|
+
private def render_by_to_iruby(data, obj)
|
166
|
+
if obj.respond_to?(:to_iruby)
|
167
|
+
result = obj.to_iruby
|
168
|
+
mime, rep = case result
|
169
|
+
when Array
|
170
|
+
result
|
171
|
+
else
|
172
|
+
[nil, result]
|
173
|
+
end
|
174
|
+
data[mime] = rep
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
92
178
|
class Representation
|
93
179
|
attr_reader :object, :options
|
94
180
|
|
@@ -111,6 +197,60 @@ module IRuby
|
|
111
197
|
end
|
112
198
|
end
|
113
199
|
|
200
|
+
class FormatMatcher
|
201
|
+
def initialize(&block)
|
202
|
+
@block = block
|
203
|
+
end
|
204
|
+
|
205
|
+
def call(obj)
|
206
|
+
@block.(obj)
|
207
|
+
end
|
208
|
+
|
209
|
+
def inspect
|
210
|
+
"#{self.class.name}[%p]" % @block
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
class RespondToFormatMatcher < FormatMatcher
|
215
|
+
def initialize(name)
|
216
|
+
super() {|obj| obj.respond_to?(name) }
|
217
|
+
@name = name
|
218
|
+
end
|
219
|
+
|
220
|
+
attr_reader :name
|
221
|
+
|
222
|
+
def inspect
|
223
|
+
"#{self.class.name}[respond_to?(%p)]" % name
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
class TypeFormatMatcher < FormatMatcher
|
228
|
+
def initialize(class_block)
|
229
|
+
super() do |obj|
|
230
|
+
begin
|
231
|
+
self.klass === obj
|
232
|
+
# We have to rescue all exceptions since constant autoloading could fail with a different error
|
233
|
+
rescue Exception
|
234
|
+
false
|
235
|
+
end
|
236
|
+
end
|
237
|
+
@class_block = class_block
|
238
|
+
end
|
239
|
+
|
240
|
+
def klass
|
241
|
+
@class_block.()
|
242
|
+
end
|
243
|
+
|
244
|
+
def inspect
|
245
|
+
klass = begin
|
246
|
+
@class_block.()
|
247
|
+
rescue Exception
|
248
|
+
@class_block
|
249
|
+
end
|
250
|
+
"#{self.class.name}[%p]" % klass
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
114
254
|
class Renderer
|
115
255
|
attr_reader :match, :mime, :priority
|
116
256
|
|
@@ -138,37 +278,22 @@ module IRuby
|
|
138
278
|
@renderer ||= []
|
139
279
|
end
|
140
280
|
|
141
|
-
SUPPORTED_MIMES = %w[
|
142
|
-
text/plain
|
143
|
-
text/html
|
144
|
-
text/latex
|
145
|
-
application/json
|
146
|
-
application/javascript
|
147
|
-
image/png
|
148
|
-
image/jpeg
|
149
|
-
image/svg+xml
|
150
|
-
]
|
151
|
-
|
152
281
|
def match(&block)
|
153
|
-
@match = block
|
282
|
+
@match = FormatMatcher.new(&block)
|
154
283
|
priority 0
|
155
284
|
nil
|
156
285
|
end
|
157
286
|
|
158
287
|
def respond_to(name)
|
159
|
-
match
|
288
|
+
@match = RespondToFormatMatcher.new(name)
|
289
|
+
priority 0
|
290
|
+
nil
|
160
291
|
end
|
161
292
|
|
162
293
|
def type(&block)
|
163
|
-
match
|
164
|
-
|
165
|
-
|
166
|
-
# We have to rescue all exceptions since constant autoloading could fail with a different error
|
167
|
-
rescue Exception
|
168
|
-
rescue #NameError
|
169
|
-
false
|
170
|
-
end
|
171
|
-
end
|
294
|
+
@match = TypeFormatMatcher.new(block)
|
295
|
+
priority 0
|
296
|
+
nil
|
172
297
|
end
|
173
298
|
|
174
299
|
def priority(p)
|
@@ -266,13 +391,24 @@ module IRuby
|
|
266
391
|
end
|
267
392
|
end
|
268
393
|
|
394
|
+
format_magick_image = ->(obj) do
|
395
|
+
format = obj.format || 'PNG'
|
396
|
+
[
|
397
|
+
format == 'PNG' ? 'image/png' : 'image/jpeg',
|
398
|
+
obj.to_blob {|i| i.format = format }
|
399
|
+
]
|
400
|
+
end
|
401
|
+
|
269
402
|
match do |obj|
|
270
403
|
defined?(Magick::Image) && Magick::Image === obj ||
|
271
404
|
defined?(MiniMagick::Image) && MiniMagick::Image === obj
|
272
405
|
end
|
406
|
+
format 'image', &format_magick_image
|
407
|
+
|
408
|
+
type { Gruff::Base }
|
273
409
|
format 'image' do |obj|
|
274
|
-
|
275
|
-
|
410
|
+
image = obj.to_image
|
411
|
+
format_magick_image.(obj.to_image)
|
276
412
|
end
|
277
413
|
|
278
414
|
match do |obj|
|
@@ -289,39 +425,19 @@ module IRuby
|
|
289
425
|
end
|
290
426
|
end
|
291
427
|
|
292
|
-
type {
|
293
|
-
format 'image/png', &:to_blob
|
294
|
-
|
295
|
-
respond_to :to_html
|
296
|
-
format 'text/html', &:to_html
|
297
|
-
|
298
|
-
respond_to :to_latex
|
299
|
-
format 'text/latex', &:to_latex
|
300
|
-
|
301
|
-
respond_to :to_tex
|
302
|
-
format 'text/latex', &:to_tex
|
303
|
-
|
304
|
-
respond_to :to_javascript
|
305
|
-
format 'text/javascript', &:to_javascript
|
306
|
-
|
307
|
-
respond_to :to_svg
|
428
|
+
type { Rubyvis::Mark }
|
308
429
|
format 'image/svg+xml' do |obj|
|
309
|
-
obj.render
|
430
|
+
obj.render
|
310
431
|
obj.to_svg
|
311
432
|
end
|
312
433
|
|
313
|
-
respond_to :to_iruby
|
314
|
-
format(&:to_iruby)
|
315
|
-
|
316
434
|
match { |obj| obj.respond_to?(:path) && obj.method(:path).arity == 0 && File.readable?(obj.path) }
|
317
435
|
format do |obj|
|
318
436
|
mime = MIME::Types.of(obj.path).first.to_s
|
319
|
-
|
437
|
+
if mime && DEFAULT_MIME_TYPE_FORMAT_METHODS.key?(mime)
|
438
|
+
[mime, File.read(obj.path)]
|
439
|
+
end
|
320
440
|
end
|
321
|
-
|
322
|
-
type { Object }
|
323
|
-
priority(-1000)
|
324
|
-
format 'text/plain', &:inspect
|
325
441
|
end
|
326
442
|
end
|
327
443
|
end
|