Class: Dyndoc::RbServer

Inherits:
Object
  • Object
show all
Defined in:
lib/dyndoc/base/filter/server.rb

Class Method Summary collapse

Class Method Details

.capture(code, rbEnvir = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dyndoc/base/filter/server.rb', line 107

def RbServer.capture(code,rbEnvir=nil)

  require 'stringio'
    require 'ostruct'

    begin
   # redirect output to StringIO objects
   oldstdout,oldstderr=$stdout, $stderr
   stdout, stderr = StringIO.new, StringIO.new
   $stdout, $stderr = stdout, stderr

   if rbEnvir.is_a? Binding
    out=rbEnvir.eval(code)
   elsif rbEnvir.is_a? Module
       out=rbEnvir.module_eval(code)
   end
    ensure
   # restore normal output
   $stdout, $stderr = oldstdout,oldstderr
    end
  {input: code, output: out.inspect, stdout: stdout.string, stderr: stderr.string}
end

.echo(code, rbEnvir = nil, prompt = "ruby> ", tab = 2) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dyndoc/base/filter/server.rb', line 144

def RbServer.echo(code,rbEnvir=nil,prompt="ruby> ",tab=2)
  out=""
  res=RbServer.inputsAndOutputs(code,rbEnvir)
  ## Dyndoc.warn "RbServer",res
  res.each do |cmd|
    ## Dyndoc.warn "input",cmd
     out << prompt+ cmd[:input].split("\n").each_with_index.map{|e,i| i==0 ? e : " "*(prompt.length)+e}.join("\n").gsub(/\t/," "*tab)
    out << "\n"
    ## Dyndoc.warn "output1",out
    out << cmd[:stdout]
    out << cmd[:output] || ""
    ## Dyndoc.warn "output2",out
    out << cmd[:stderr]!=""  ? cmd[:stderr] : ""
    out << (cmd[:output]  ? "\n\n" : "")
    ## Dyndoc.warn "output3",out
  end
  out
end

.echo_verb(txt, mode, rbEnvir = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/dyndoc/base/filter/server.rb', line 163

def RbServer.echo_verb(txt,mode,rbEnvir=nil)
    ## Dyndoc.warn "echo_verb:txt",txt
    txtout=Dyndoc::RbServer.echo(txt.strip,rbEnvir).strip
    mode=:default unless Dyndoc::VERB.keys.include? mode
    header= (mode!=:default) and txtout.length>0
    out=""
    out << Dyndoc::VERB[mode][:begin] << "\n" if header
    out << txtout
    out << "\n" << Dyndoc::VERB[mode][:end] << "\n" if header
    out
end

.filter(str, rbEnvir = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dyndoc/base/filter/server.rb', line 32

def RbServer.filter(str,rbEnvir=nil)
  res=str.gsub(/\\?(?i:\#Rb|\#rb|\:Rb|\:rb|\:)#{@@start}[^#{@@stop}]+#{@@stop}/) {|w|
    if w[0,1]=="\\"
 w[1..-1]
    else
 k=(w[1,2].downcase=="rb" ? w[4..-2] : w[2..-2]) #the key
#p "apply:Rb";p k;p w;p rbEnvir
        RbServer.output(k,rbEnvir,:error=> w)
    end
  }
  res
end

.inputsAndOutputs(code, rbEnvir = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dyndoc/base/filter/server.rb', line 130

def RbServer.inputsAndOutputs(code,rbEnvir=nil)
  require 'ripper'
  res = []
  input = ""
  code.each_line do |l|
    input += l
    if Ripper.sexp input
      res << RbServer.capture(input,rbEnvir)
      input = ""
    end
  end
  res
end

.output(code, rbEnvir = nil, opts = {:error=>"ERROR"}) ⇒ Object

def RbServer.output(code,rbEnvir=nil,error=“ERROR”)

begin

#puts “code”;p code out=eval(code,rbEnvir) #puts “out”;p out

rescue
  out=error
end

#p out

  out=out.join(",") if out.is_a? Array
  out.to_s
end


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dyndoc/base/filter/server.rb', line 60

def RbServer.output(code,rbEnvir=nil,opts={:error=>"ERROR"})

 begin
     if rbEnvir.is_a? Binding
    out=rbEnvir.eval(code)
  elsif rbEnvir.is_a? Module
      out=rbEnvir.module_eval(code)
  end
 rescue
  if RUBY_VERSION >= "1.9.3" and rbEnvir.is_a? Binding and rbEnvir.eval("local_variables").include? :childBinding
    begin
      rbEnvir2=rbEnvir.eval("childBinding")
      out=rbEnvir2.eval(code)
      return out
    rescue
    end
  end

      #two solution:
      #in the same spirit as #{}
      # out="\\:{"+code+"}"
      # or more informative for debugging!

      out="\\:{"+code+"}"

      if Dyndoc.cfg_dyn[:ruby_debug] ==:expression
      Dyndoc.warn "WARNING: >>>>>>>>>>>>>>>>>>+\n"+opts[:error]+" in #{rbEnvir}:\n"+code+"\n<<<<<<<<<<<<<<<<<<"
      end


      if Dyndoc.cfg_dyn[:dyndoc_mode]!=:normal and $dyn_logger
##p ["error ruby",code]
      $dyn_logger.write("\nERROR Ruby:\n"+code+"\n")
     end

 rescue SyntaxError
     puts "RbServer syntax error in: "+code
     raise SystemError if Dyndoc.cfg_dyn[:dyndoc_mode]==:normal and  Dyndoc.cfg_dyn[:ruby_debug]!=:none
     if Dyndoc.cfg_dyn[:dyndoc_mode]!=:normal
      $dyn_logger.write("\nERROR Ruby Syntax:\n"+code+"\n")
     end
     out=":{"+code+"}"
  end
#p out
    out
end