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 +4 -4
- data/lib/datastory/eval_context.rb +33 -0
- data/lib/datastory/markdown_renderer.rb +7 -14
- data/lib/datastory/version.rb +1 -1
- data/lib/datastory.rb +1 -0
- data/samples/test.md +18 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa61e11b6d796a4492f4a3245328a44504080358
|
4
|
+
data.tar.gz: 7c8d49b6fb6c6297aa5017ce1e6ee0d72438a24c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
17
|
-
#prev_stderr = $stderr
|
17
|
+
eval_output, append = EvalContext.evaluate(code)
|
18
18
|
|
19
|
-
|
20
|
-
|
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
|
data/lib/datastory/version.rb
CHANGED
data/lib/datastory.rb
CHANGED
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.
|
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
|