From ff36f17dcd88bb97cda02ae561147a871417a222 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Thu, 25 Mar 2021 14:05:37 +0900 Subject: [PATCH 1/2] Add a test case to examine mime type handling --- test/iruby/mime_test.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test/iruby/mime_test.rb diff --git a/test/iruby/mime_test.rb b/test/iruby/mime_test.rb new file mode 100644 index 0000000..81a4419 --- /dev/null +++ b/test/iruby/mime_test.rb @@ -0,0 +1,32 @@ +class IRubyTest::MimeTest < IRubyTest::TestBase + sub_test_case("IRuby::Display") do + def test_display_with_mime_type + html = "Bold Text" + + obj = Object.new + obj.define_singleton_method(:to_s) { html } + + res = IRuby::Display.display(obj, mime: "text/html") + assert_equal({ plain: obj.inspect, html: html }, + { plain: res["text/plain"], html: res["text/html"] }) + end + end + + sub_test_case("Rendering a file") do + def setup + @html = "Bold Text" + Dir.mktmpdir do |tmpdir| + @file = File.join(tmpdir, "test.html") + File.write(@file, @html) + yield + end + end + + def test_display + File.open(@file, "rb") do |f| + res = IRuby::Display.display(f) + assert_equal(@html, res["text/html"]) + end + end + end +end From 230b9162cadb7e93659772624e85cb1fd9ca338e Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Thu, 25 Mar 2021 14:16:41 +0900 Subject: [PATCH 2/2] Replace mimemagic with mime-types This change is to avoid troubles due to the licensing issue of mimemagic. See https://github.com/minad/mimemagic/issues/97 for the detail. --- iruby.gemspec | 2 +- lib/iruby.rb | 2 +- lib/iruby/display.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/iruby.gemspec b/iruby.gemspec index 9fee5a0..cfebb8d 100644 --- a/iruby.gemspec +++ b/iruby.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |s| s.add_dependency 'bond', '~> 0.5' s.add_dependency 'data_uri', '~> 0.1' s.add_dependency 'ffi-rzmq' - s.add_dependency 'mimemagic', '~> 0.3' + s.add_dependency 'mime-types', '>= 3.3.1' s.add_dependency 'multi_json', '~> 1.11' s.add_development_dependency 'pycall', '>= 1.2.1' diff --git a/lib/iruby.rb b/lib/iruby.rb index bd68b1a..480cf8c 100644 --- a/lib/iruby.rb +++ b/lib/iruby.rb @@ -1,4 +1,4 @@ -require 'mimemagic' +require 'mime/types' require 'multi_json' require 'securerandom' require 'openssl' diff --git a/lib/iruby/display.rb b/lib/iruby/display.rb index f312a55..340a437 100644 --- a/lib/iruby/display.rb +++ b/lib/iruby/display.rb @@ -44,7 +44,7 @@ def clear_output(wait = false) private def protect(mime, data) - MimeMagic.new(mime).text? ? data.to_s : [data.to_s].pack('m0') + MIME::Type.new(mime).ascii? ? data.to_s : [data.to_s].pack('m0') end def render(data, obj, exact_mime, fuzzy_mime) @@ -304,7 +304,7 @@ def format(mime = nil, &block) match { |obj| obj.respond_to?(:path) && obj.method(:path).arity == 0 && File.readable?(obj.path) } format do |obj| - mime = MimeMagic.by_path(obj.path).to_s + mime = MIME::Types.of(obj.path).first.to_s [mime, File.read(obj.path)] if SUPPORTED_MIMES.include?(mime) end