Skip to content

Commit 5c7c26a

Browse files
authored
Add support for charty (#18)
* ✨ Add support for charty * 🎨 Check nil or extname and add gem deps
1 parent 66663f0 commit 5c7c26a

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

exe/rubydown

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'kramdown'
44
require 'erb'
55
require 'stringio'
6-
6+
require 'base64'
77
require 'optparse'
88

99
require 'rubydown'
@@ -62,6 +62,9 @@ class EvalDoc < Kramdown::Document
6262
case
6363
when eval_result.respond_to?(:to_html)
6464
Kramdown::Document.new(eval_result.to_html, input: :html).root
65+
when eval_result.instance_of?(File) && File.extname(eval_result.path) == ".png"
66+
img_b64 = Base64.encode64(File.binread(eval_result.path))
67+
Kramdown::Document.new("<img src='data:image/png;base64,#{img_b64}' />", input: :html).root
6568
else
6669
text_result_el(stdout_string + "=> #{eval_result}")
6770
end

lib/rubydown.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'numo/gnuplot'
66
require 'base64'
77
require 'tempfile'
8+
require 'charty'
89

910

1011
class Numo::NArray
@@ -29,6 +30,17 @@ def to_html
2930
end
3031
end
3132

33+
Charty::RenderContext.prepend Module.new {
34+
def render(filename=nil)
35+
super
36+
if filename.nil?
37+
p "The image could not be rendered. In the environment without IRuby, the file name for the charty render method is required."
38+
else
39+
File.open(filename)
40+
end
41+
end
42+
}
43+
3244
module Rubydown
3345
class RbMarkPlot < Numo::Gnuplot
3446
def plot(*args)

rubydown.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Gem::Specification.new do |spec|
4040
spec.add_dependency "numo-narray", "~> 0.9.1"
4141
spec.add_dependency "numo-gnuplot", "~> 0.2.4"
4242
spec.add_dependency "rbplotly", "~> 0.1.2"
43+
spec.add_dependency "charty", "0.1.1.dev"
44+
spec.add_dependency "matplotlib", "~> 1.0.0"
4345

4446
spec.add_development_dependency "bundler", "~> 1.17"
4547
spec.add_development_dependency "rake", "~> 10.0"

0 commit comments

Comments
 (0)