datastory 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87dea24700cc2bdc46984ddd4cf9a7567b0b3ebb
4
- data.tar.gz: 69aa1f62365fab941aa2c284856f6b5d5f71d74a
3
+ metadata.gz: fa61e11b6d796a4492f4a3245328a44504080358
4
+ data.tar.gz: 7c8d49b6fb6c6297aa5017ce1e6ee0d72438a24c
5
5
  SHA512:
6
- metadata.gz: c314b77af442cbe21b47dec8a654e83eab5e70e60cf977d3d3950621b18aaea1330da3abc2aa0fcf6910f59e37392cc6143f55df81046eee2d9b32a012c74b9e
7
- data.tar.gz: b261f9dd9db34c7e2b0444701284115d169fe74c9419210f79ed28c2a91282d949319d04cafae7c4fc3384fa0819a8e79e402d0ff235d30dfadab7dde323e406
6
+ metadata.gz: 9bcb0fffae195e371304c45fd36f6115d7dda2665d6b892d5a5efd985a3b9e982abaaa30f5632d1ffa8fda724311a0867a263994d915dc70abf698abb1f012d1
7
+ data.tar.gz: d0011f9eaaafe5fafe43e956b0ba5bcd67114f38647bf15d9ca0b98c9e5580cdd094ed61757030142a441e135e3a44b7ed378bd1e44db31c6e6455ad146be592
@@ -0,0 +1,33 @@
1
+ module DataStory
2
+ class EvalContext
3
+
4
+ # Returns the binding for this context.
5
+ def self.evaluate(code)
6
+ @binding ||= binding
7
+ @append = ""
8
+
9
+ prev_stdout = $stdout
10
+ #prev_stderr = $stderr
11
+
12
+ out = StringIO.new
13
+ $stdout = out
14
+ #$stderr = out
15
+
16
+ eval(code, @binding)
17
+
18
+ $stdout = prev_stdout
19
+ #$stderr = prev_stderr
20
+
21
+ to_return = [out.string, @append.clone]
22
+ @append = nil
23
+
24
+ to_return
25
+ end
26
+
27
+ # Public: Appends html to the document.
28
+ def self.append(content)
29
+ @append ||= ""
30
+ @append << content
31
+ end
32
+ end
33
+ end
@@ -1,4 +1,5 @@
1
1
  require "redcarpet"
2
+ require "cgi"
2
3
 
3
4
  module DataStory
4
5
  class MarkdownRenderer < Redcarpet::Render::HTML
@@ -9,25 +10,17 @@ module DataStory
9
10
 
10
11
  def block_code(code, language)
11
12
  output = <<-EOF
12
- <pre><code class="#{language}">#{code}</code></pre>
13
+ <pre><code class="#{language}">#{CGI.escapeHTML(code)}</code></pre>
13
14
  EOF
14
15
 
15
16
  if language == "ruby-datastory" || language == "ruby-datastory-silent"
16
- prev_stdout = $stdout
17
- #prev_stderr = $stderr
17
+ eval_output, append = EvalContext.evaluate(code)
18
18
 
19
- out = StringIO.new
20
- $stdout = out
21
- #$stderr = out
22
-
23
- eval(code, @binding)
24
-
25
- $stdout = prev_stdout
26
- #$stderr = prev_stderr
27
-
28
- if out.string.length > 0 && language != "ruby-datastory-silent"
29
- output += "<p>Output:</p><pre>#{out.string}</pre>"
19
+ if eval_output.length > 0 && language != "ruby-datastory-silent"
20
+ output += "<p>Output:</p><pre>#{CGI.escapeHTML(eval_output)}</pre>"
30
21
  end
22
+
23
+ output += append
31
24
  end
32
25
 
33
26
  output
@@ -1,3 +1,3 @@
1
1
  module DataStory
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/datastory.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require_relative "datastory/version"
2
2
  require_relative "datastory/markdown_renderer"
3
3
  require_relative "datastory/data_uri"
4
+ require_relative "datastory/eval_context"
4
5
 
5
6
  module DataStory
6
7
  class Renderer
data/samples/test.md CHANGED
@@ -48,4 +48,21 @@ Gnuplot.open do |gp|
48
48
  end
49
49
  ```
50
50
 
51
- ![Graph of Stuff](tmp/sin_wave.gif)
51
+ ![Graph of Stuff](tmp/sin_wave.gif)
52
+
53
+ The following code uses the `append` method to append html output to the document.
54
+
55
+ ``` ruby-datastory
56
+ append "<table>"
57
+ append "<tr>"
58
+ 12.times do |i|
59
+ append "<td>#{i}</td>"
60
+ end
61
+ append "</tr>"
62
+ append "<tr>"
63
+ 12.times do
64
+ append "<td>#{rand}</td>"
65
+ end
66
+ append "</tr>"
67
+ append "</table>"
68
+ ```
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datastory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Johnson
@@ -69,6 +69,7 @@ files:
69
69
  - datastory.gemspec
70
70
  - lib/datastory.rb
71
71
  - lib/datastory/data_uri.rb
72
+ - lib/datastory/eval_context.rb
72
73
  - lib/datastory/markdown_renderer.rb
73
74
  - lib/datastory/version.rb
74
75
  - samples/test.md