dyndoc-ruby-core 1.2.6 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -119,7 +119,7 @@ module Dyndoc
119
119
  end
120
120
 
121
121
  def Utils.protect_extraction(str)
122
- str.gsub(/(?:\#|\#\#|@|#F|#R|#r|\:R|\:r|#Rb|#rb|\:|\:Rb|\:rb)+\{/) {|e| "\\"+e}
122
+ str.gsub(/(?:\#|\:dyn|\#\#|@|#F|#R|#r|\:R|\:r|#Rb|#rb|\:|\:Rb|\:rb)+\{/) {|e| "\\"+e}
123
123
  end
124
124
 
125
125
  ## the scanner converts automatically {#toto#} in {#toto][#} and {@toto@} in {@toto][#}
@@ -1,9 +1,12 @@
1
- # include("ruby.jl")
1
+
2
2
 
3
3
  module Dyndoc
4
4
 
5
- import Base.setindex!,Base.getindex,Base.IO,Base.show,Base.showarray
6
- importall Ruby
5
+
6
+ using Main.Ruby
7
+
8
+ import Base.setindex!,Base.getindex,Base.IO,Base.show #,Base.showarray
9
+ import Main.Ruby.start,Main.Ruby.stop,Main.Ruby.run,Main.Ruby.alive
7
10
 
8
11
  export DynVector,DynArray,getindex,setindex!,show,Vector,sync,getkey
9
12
 
@@ -11,11 +14,11 @@ export DynVector,DynArray,getindex,setindex!,show,Vector,sync,getkey
11
14
  # when change on the vector occurs
12
15
 
13
16
 
14
- type DynVector
17
+ mutable struct DynVector
15
18
  ary::Vector
16
19
  key::String
17
20
 
18
- DynVector(a::Vector,k::String)=(x=new();x.ary=a;x.key=k;x)
21
+ #DynVector(a::Vector,k::String)=(x=new();x.ary=a;x.key=k;x)
19
22
  end
20
23
 
21
24
  function getindex(dynvect::DynVector,i::Integer)
@@ -31,13 +34,12 @@ function setindex!(dynvect::DynVector,value,i::Integer)
31
34
  end
32
35
  end
33
36
 
34
- show(io::IO,dynvect::DynVector)=showarray(io,dynvect.ary)
37
+ show(io::IO,dynvect::DynVector)=show(io,dynvect.ary)
35
38
 
36
39
  # gather all the julia vectors connected to dyndoc.
37
40
 
38
- type DynArray
41
+ mutable struct DynArray
39
42
  vars::Dict
40
-
41
43
  DynArray()=(x=new();x.vars=Dict();x)
42
44
  end
43
45
 
@@ -1,87 +1,55 @@
1
- #cmd="a=1\n(a)\nfor i in 1:3\nprintln(i)\nend"
2
-
3
- # # Unused! See capture_julia
4
- # function capture_cmd(cmd::AbstractString)
5
- # add,cmd0=true,AbstractString[]
6
- # res=Any[] #Dict{AbstractString,Any}()
7
- # for l=split(cmd,"\n")
8
- # #println("l => ",l)
9
- # push!(cmd0,l)
10
- # pcmd0=Base.parse_input_line(join(cmd0,"\n"))
11
- # #print(join(cmd0,"\n")*":");println(pcmd0)
12
- # add = typeof(pcmd0)==Expr && pcmd0.head == :continue
13
- # if !add
14
- # #print("ici:")
15
- # #println(Base.eval(pcmd0))
16
- # push!(res,(join(cmd0,"\n"),eval(pcmd0)))
17
- # cmd0=AbstractString[]
18
- # end
19
- # #println(res)
20
- # end
21
- # res
22
- # end
23
-
24
- module DyndocSandbox
25
- importall Ruby
26
- importall Dyndoc
1
+ module Dyndoc2Sandbox
2
+ import Main.Ruby.run,Main.Ruby.alive
3
+ import Main.Dyndoc.DynVector,Main.Dyndoc.DynArray,Main.Dyndoc.getindex,Main.Dyndoc.setindex!,Main.Dyndoc.show,Main.Dyndoc.Vector,Main.Dyndoc.sync,Main.Dyndoc.getkey
4
+ using InteractiveUtils
5
+ end
27
6
 
28
- # Replace OUTPUT_STREAM references so we can capture output.
29
- OUTPUT_STREAM = IOBuffer()
30
- print(x) = Base.print(OUTPUT_STREAM, x)
31
- println(x) = Base.println(OUTPUT_STREAM, x)
32
7
 
33
- # Output
34
- MIME_OUTPUT = Array(Tuple, 0)
35
- emit(mime, data) = push!(MIME_OUTPUT, (mime, data))
8
+ function replace_dyndoc2sandbox(txt)
9
+ replace(replace(txt,"Main.Dyndoc2Sandbox." => ""),"Main.Dyndoc2Sandbox" => "Main")
36
10
  end
37
11
 
38
- function get_stdout_iobuffer()
39
- #seek(DyndocSandbox.OUTPUT_STREAM, 0)
40
- #jl4rb_out =
41
- takebuf_string(DyndocSandbox.OUTPUT_STREAM)
42
- #truncate(DyndocSandbox.OUTPUT_STREAM, 0)
43
- #jl4rb_out
12
+ function echo_repl_julia(res)
13
+ # buf = IOBuffer();
14
+ # td = TextDisplay(buf);
15
+ # display(td, res);
16
+ # takebuf_string(buf)
17
+ #println(typeof(res))
18
+ replace_dyndoc2sandbox(repr("text/plain",res))
44
19
  end
45
20
 
46
- function get_stderr_iobuffer()
47
- #jl4rb_out = takebuf_string(STDERR.buffer)
48
- #jl4rb_out
49
- ## THIS FAILS WHEN DYNDOC DAEMONIZED SO AUTOMATIC EMPTY RESULT for now
50
- ## MAYBE TO DELETE SOON!
51
- ""
21
+ ## Rmk: The process is based on what is done in weave.jl (src/run.jl)
22
+ getstdout() = stdout #Base.STDOUT
23
+ function capture_output_expr(expr)
24
+ #oldSTDOUT = STDOUT
25
+ oldSTDOUT = getstdout()
26
+ out = nothing
27
+ obj = nothing
28
+ rw, wr = redirect_stdout()
29
+ reader = @async read(rw, String) # @async readstring(rw)
30
+ try
31
+ obj = Core.eval(Dyndoc2Sandbox, expr)
32
+ obj != nothing && display(obj)
33
+ #catch E
34
+ # throw_errors && throw(E)
35
+ # display(E)
36
+ # @warn("ERROR: $(typeof(E)) occurred, including output in Weaved document")
37
+
38
+ finally
39
+ redirect_stdout(oldSTDOUT)
40
+ close(wr)
41
+ out = fetch(reader) #wait(reader)
42
+ close(rw)
43
+ end
44
+ return (obj, out)
52
45
  end
53
46
 
54
- # export weave
55
- # module DyndocSandbox
56
- # # Copied from Gadfly.jl/src/weave.jl
57
- # # Replace OUTPUT_STREAM references so we can capture output.
58
- # OUTPUT_STREAM = IOString()
59
- # print(x) = Base.print(OUTPUT_STREAM, x)
60
- # println(x) = Base.println(OUTPUT_STREAM, x)
61
-
62
- # function eval(expr)
63
- # result = try
64
- # Base.eval(DyndocSandbox, expr)
65
- # seek(DyndocSandbox.OUTPUT_STREAM, 0)
66
- # output = takebuf_string(DyndocSandbox.OUTPUT_STREAM)
67
- # truncate(DyndocSandbox.OUTPUT_STREAM, 0)
68
- # output
69
- # catch e
70
- # io = IOBuffer()
71
- # print(io, "ERROR: ")
72
- # Base.error_show(io, e)
73
- # tmp = bytestring(io)
74
- # close(io)
75
- # tmp
76
- # end
77
- # result
78
- # end
79
- # end
80
-
81
- function capture_julia(cmd::AbstractString)
47
+ function capture_output_julia(cmd::AbstractString)
82
48
  add,cmd0=true,AbstractString[]
83
49
  res=Any[] #Dict{AbstractString,Any}()
84
50
  #println(cmd)
51
+ #cmd=replace(cmd,r"\$","\$")
52
+
85
53
  for l=split(cmd,"\n")
86
54
  #println("l => ",l)
87
55
  push!(cmd0,l)
@@ -91,17 +59,18 @@ function capture_julia(cmd::AbstractString)
91
59
  if !add
92
60
  #print("ici:")
93
61
  #println(Base.eval(pcmd0))
94
- result,error = "",""
62
+ result,error,out = "","",""
95
63
  try
96
- result=eval(DyndocSandbox, pcmd0)
64
+ result,out=capture_output_expr(pcmd0)
97
65
  catch e
98
- #io = IOBuffer()
99
- #print(io, "ERROR: ")
100
- #Base.error_show(io, e)
101
- error = "Error: $(string(e))"
102
- #close(io)
103
- end
104
- push!(res,(join(cmd0,"\n"),string(result),get_stdout_iobuffer(),error,get_stderr_iobuffer()))
66
+ #io = IOBuffer()
67
+ #print(io, "ERROR: ")
68
+ #Base.error_show(io, e)
69
+ error = "ERROR: $(sprint(showerror,e))"
70
+ #close(io)
71
+ end
72
+ println(l)
73
+ push!(res,(join(cmd0,"\n"), echo_repl_julia(result),replace_dyndoc2sandbox(out),replace_dyndoc2sandbox(error),""))
105
74
  cmd0=AbstractString[]
106
75
  end
107
76
  #println(res)
@@ -0,0 +1,192 @@
1
+ #cmd="a=1\n(a)\nfor i in 1:3\nprintln(i)\nend"
2
+
3
+ # # Unused! See capture_julia
4
+ # function capture_cmd(cmd::AbstractString)
5
+ # add,cmd0=true,AbstractString[]
6
+ # res=Any[] #Dict{AbstractString,Any}()
7
+ # for l=split(cmd,"\n")
8
+ # #println("l => ",l)
9
+ # push!(cmd0,l)
10
+ # pcmd0=Base.parse_input_line(join(cmd0,"\n"))
11
+ # #print(join(cmd0,"\n")*":");println(pcmd0)
12
+ # add = typeof(pcmd0)==Expr && pcmd0.head == :continue
13
+ # if !add
14
+ # #print("ici:")
15
+ # #println(Base.eval(pcmd0))
16
+ # push!(res,(join(cmd0,"\n"),eval(pcmd0)))
17
+ # cmd0=AbstractString[]
18
+ # end
19
+ # #println(res)
20
+ # end
21
+ # res
22
+ # end
23
+
24
+ module DyndocSandbox
25
+ import Main.Ruby.start,Main.Ruby.stop,Main.Ruby.run,Main.Ruby.alive
26
+ import Main.Dyndoc.DynVector,Main.Dyndoc.DynArray,Main.Dyndoc.getindex,Main.Dyndoc.setindex!,Main.Dyndoc.show,Main.Dyndoc.Vector,Main.Dyndoc.sync,Main.Dyndoc.getkey
27
+
28
+ # Replace OUTPUT_STREAM references so we can capture output.
29
+ OUTPUT_STREAM = IOBuffer()
30
+ print(x) = Base.print(OUTPUT_STREAM, x)
31
+ println(x) = Base.println(OUTPUT_STREAM, x)
32
+
33
+ # Output
34
+ MIME_OUTPUT = Array(Tuple, 0)
35
+ emit(mime, data) = push!(MIME_OUTPUT, (mime, data))
36
+ end
37
+
38
+ function get_stdout_iobuffer()
39
+ #seek(DyndocSandbox.OUTPUT_STREAM, 0)
40
+ #jl4rb_out =
41
+ takebuf_string(DyndocSandbox.OUTPUT_STREAM)
42
+ #truncate(DyndocSandbox.OUTPUT_STREAM, 0)
43
+ #jl4rb_out
44
+ end
45
+
46
+ function get_stderr_iobuffer()
47
+ #jl4rb_out = takebuf_string(STDERR.buffer)
48
+ #jl4rb_out
49
+ ## THIS FAILS WHEN DYNDOC DAEMONIZED SO AUTOMATIC EMPTY RESULT for now
50
+ ## MAYBE TO DELETE SOON!
51
+ ""
52
+ end
53
+
54
+ # export weave
55
+ # module DyndocSandbox
56
+ # # Copied from Gadfly.jl/src/weave.jl
57
+ # # Replace OUTPUT_STREAM references so we can capture output.
58
+ # OUTPUT_STREAM = IOString()
59
+ # print(x) = Base.print(OUTPUT_STREAM, x)
60
+ # println(x) = Base.println(OUTPUT_STREAM, x)
61
+
62
+ # function eval(expr)
63
+ # result = try
64
+ # Base.eval(DyndocSandbox, expr)
65
+ # seek(DyndocSandbox.OUTPUT_STREAM, 0)
66
+ # output = takebuf_string(DyndocSandbox.OUTPUT_STREAM)
67
+ # truncate(DyndocSandbox.OUTPUT_STREAM, 0)
68
+ # output
69
+ # catch e
70
+ # io = IOBuffer()
71
+ # print(io, "ERROR: ")
72
+ # Base.error_show(io, e)
73
+ # tmp = bytestring(io)
74
+ # close(io)
75
+ # tmp
76
+ # end
77
+ # result
78
+ # end
79
+ # end
80
+
81
+
82
+ function capture_julia(cmd::AbstractString)
83
+ add,cmd0=true,AbstractString[]
84
+ res=Any[] #Dict{AbstractString,Any}()
85
+ #println(cmd)
86
+ #cmd=replace(cmd,r"\$","\$")
87
+ # for standard redirection
88
+ ##OUT: (outRead, outWrite) = redirect_stdout()
89
+ for l=split(cmd,"\n")
90
+ #println("l => ",l)
91
+ push!(cmd0,l)
92
+ pcmd0=Base.parse_input_line(join(cmd0,"\n"))
93
+ #print(join(cmd0,"\n")*":");println(pcmd0)
94
+ add = typeof(pcmd0)==Expr && pcmd0.head == :incomplete
95
+ if !add
96
+ #print("ici:")
97
+ #println(Base.eval(pcmd0))
98
+ result,error = "",""
99
+ try
100
+ result=eval(DyndocSandbox, pcmd0)
101
+ catch e
102
+ #io = IOBuffer()
103
+ #print(io, "ERROR: ")
104
+ #Base.error_show(io, e)
105
+ error = "Error: $(string(e))"
106
+ #close(io)
107
+ end
108
+
109
+ ##OUT: data_out = readavailable(outRead)
110
+
111
+ ##OUT: push!(res,(join(cmd0,"\n"),echo_repl_julia(result),get_stdout_iobuffer(),data_out,error,get_stderr_iobuffer()))
112
+ push!(res,(join(cmd0,"\n"),echo_repl_julia(result),get_stdout_iobuffer(),error,get_stderr_iobuffer()))
113
+ cmd0=AbstractString[]
114
+ end
115
+ #println(res)
116
+ end
117
+ ##OUT: close(outWrite);close(outRead)
118
+ res
119
+ end
120
+
121
+
122
+ module Dyndoc2Sandbox
123
+ import Main.Ruby.run,Main.Ruby.alive
124
+ import Main.Dyndoc.DynVector,Main.Dyndoc.DynArray,Main.Dyndoc.getindex,Main.Dyndoc.setindex!,Main.Dyndoc.show,Main.Dyndoc.Vector,Main.Dyndoc.sync,Main.Dyndoc.getkey
125
+ end
126
+
127
+ function echo_repl_julia(res)
128
+ buf = IOBuffer();
129
+ td = TextDisplay(buf);
130
+ display(td, res);
131
+ takebuf_string(buf)
132
+ end
133
+
134
+ ## Rmk: The process is based on what is done in weave.jl (src/run.jl)
135
+ getstdout() = stdout #Base.STDOUT
136
+ function capture_output_expr(expr)
137
+ #oldSTDOUT = STDOUT
138
+ oldSTDOUT = getstdout()
139
+ out = nothing
140
+ obj = nothing
141
+ rw, wr = redirect_stdout()
142
+ reader = @async read(rw, String) # @async readstring(rw)
143
+ try
144
+ obj = eval(Dyndoc2Sandbox, expr)
145
+ obj != nothing && display(obj)
146
+ catch E
147
+ throw_errors && throw(E)
148
+ display(E)
149
+ @warn("ERROR: $(typeof(E)) occurred, including output in Weaved document")
150
+
151
+ finally
152
+ redirect_stdout(oldSTDOUT)
153
+ close(wr)
154
+ out = wait(reader)
155
+ close(rw)
156
+ end
157
+ return (obj, out)
158
+ end
159
+
160
+ function capture_output_julia(cmd::AbstractString)
161
+ add,cmd0=true,AbstractString[]
162
+ res=Any[] #Dict{AbstractString,Any}()
163
+ #println(cmd)
164
+ #cmd=replace(cmd,r"\$","\$")
165
+
166
+ for l=split(cmd,"\n")
167
+ #println("l => ",l)
168
+ push!(cmd0,l)
169
+ pcmd0=Base.parse_input_line(join(cmd0,"\n"))
170
+ #print(join(cmd0,"\n")*":");println(pcmd0)
171
+ add = typeof(pcmd0)==Expr && pcmd0.head == :incomplete
172
+ if !add
173
+ #print("ici:")
174
+ #println(Base.eval(pcmd0))
175
+ result,error,out = "","",""
176
+ try
177
+ result,out=capture_output_expr(pcmd0)
178
+ catch e
179
+ #io = IOBuffer()
180
+ #print(io, "ERROR: ")
181
+ #Base.error_show(io, e)
182
+ error = "Error: $(string(e))"
183
+ #close(io)
184
+ end
185
+
186
+ push!(res,(join(cmd0,"\n"),echo_repl_julia(result),out,error,""))
187
+ cmd0=AbstractString[]
188
+ end
189
+ #println(res)
190
+ end
191
+ res
192
+ end
@@ -0,0 +1,127 @@
1
+
2
+ ## Inspired from the excellent weave.jl
3
+
4
+ using Compat
5
+
6
+ #Contains report global properties
7
+ mutable struct DyndocReport <: AbstractDisplay
8
+ #cwd::AbstractString
9
+ #basename::AbstractString
10
+ #formatdict::Dict{Symbol,Any}
11
+ #pending_code::AbstractString
12
+ #cur_result::AbstractString
13
+ #rich_output::AbstractString
14
+ fignum::Int
15
+ figures::Array{AbstractString}
16
+ #term_state::Symbol
17
+ #cur_chunk
18
+ mimetypes::Array{AbstractString}
19
+ # first_plot::Bool
20
+ #header_script::String
21
+ #throw_errors::Bool
22
+ end
23
+
24
+ #function Report(cwd, basename, formatdict, mimetypes, throw_errors)
25
+ # Report(cwd, basename, formatdict, "", "", "", 1, AbstractString[], :text, nothing,
26
+ # mimetypes, true, "", throw_errors)
27
+ #end
28
+
29
+
30
+ #Default mimetypes in order, can be overridden for some inside `run method` formats
31
+ const default_mime_types = ["image/svg+xml", "image/png", "text/html", "text/plain"]
32
+ #const default_mime_types = ["image/png", "image/svg+xml", "text/html", "text/plain"]
33
+ #From IJulia as a reminder
34
+ #const supported_mime_types = [ "text/html", "text/latex", "image/svg+xml", "image/png", "image/jpeg", "text/plain", "text/markdown" ]
35
+
36
+ function Base.display(report::DyndocReport, data)
37
+ #Set preferred mimetypes for report based on format
38
+ for m in report.mimetypes
39
+ if showable(m, data)
40
+ try
41
+ if !istextmime(m)
42
+ Compat.invokelatest(display, report, m, data)
43
+ elseif report.cur_chunk.options[:term]
44
+ Compat.invokelatest(display, report, "text/plain", data)
45
+ else
46
+ Compat.invokelatest(display, report, m, data)
47
+ end
48
+ catch e
49
+ throw(e)
50
+ @warn("Failed to display data in \"$m\" format")
51
+ continue
52
+ end
53
+ break
54
+ end
55
+ end
56
+ end
57
+
58
+ function Base.display(report::DyndocReport, m::MIME"image/png", data)
59
+ figname = add_figure(report, data, m, ".png")
60
+ end
61
+
62
+ function Base.display(report::DyndocReport, m::MIME"image/svg+xml", data)
63
+ figname = add_figure(report, data, m, ".svg")
64
+ end
65
+
66
+ function Base.display(report::DyndocReport, m::MIME"application/pdf", data)
67
+ figname = add_figure(report, data, m, ".pdf")
68
+ end
69
+
70
+ #Text is written to stdout, called from "term" mode chunks
71
+ function Base.display(report::DyndocReport, m::MIME"text/plain", data)
72
+ io = PipeBuffer()
73
+ show(IOContext(io, :limit => true), m, data)
74
+ flush(io)
75
+ s = read(io, String)
76
+ close(io)
77
+ println(s)
78
+ end
79
+
80
+ function Base.display(report::DyndocReport, m::MIME"text/plain", data::Exception)
81
+ println("Error: " * sprint(showerror, data))
82
+ end
83
+
84
+ function Base.display(report::DyndocReport, m::MIME"text/html", data::Exception)
85
+ report.rich_output = sprint(show, m, data)
86
+ end
87
+
88
+ function Base.show(io, m::MIME"text/html", data::Exception)
89
+ println(io ,"<pre class=\"julia-error\">")
90
+ println(io, Markdown.htmlesc("ERROR: " * sprint(showerror, data)))
91
+ println(io ,"</pre>")
92
+ end
93
+
94
+ #Catch "rich_output"
95
+ function Base.display(report::DyndocReport, m::MIME"text/html", data)
96
+ s = repr(m, data)
97
+ report.rich_output *= "\n" * s
98
+ end
99
+
100
+ #Catch "rich_output"
101
+ function Base.display(report::DyndocReport, m::MIME"text/markdown", data)
102
+ s = repr(m, data)
103
+ report.rich_output *= "\n" * s
104
+ end
105
+
106
+ function Base.display(report::DyndocReport, m::MIME"text/latex", data)
107
+ s = repr(m, data)
108
+ report.rich_output *= "\n" * s
109
+ end
110
+
111
+ """Add saved figure name to results and return the name"""
112
+ function add_figure(report::DyndocReport, data, m, ext)
113
+ chunk = report.cur_chunk
114
+ full_name, rel_name = get_figname(report, chunk, ext = ext)
115
+
116
+ open(full_name, "w") do io
117
+ if ext == ".pdf"
118
+ write(io, repr(m, data))
119
+ else
120
+ show(io, m, data)
121
+ end
122
+ end
123
+
124
+ push!(report.figures, rel_name)
125
+ report.fignum += 1
126
+ return full_name
127
+ end