Class: Dyndoc::JLServer

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

Constant Summary collapse

@@initVerb =

require ‘jl4rb’ Julia.init end

nil

Class Method Summary collapse

Class Method Details

.echo(code, prompt = "julia> ", tab = 2) ⇒ Object



880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
# File 'lib/dyndoc/base/filter/server.rb', line 880

def JLServer.echo(code,prompt="julia> ",tab=2)
	out=""
	res=JLServer.inputsAndOutputs(code)
	## 
	Dyndoc.warn "JLServer",res
   return "Error when executing: "+code unless 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" unless cmd[:input].strip[-1]==";"
		## Dyndoc.warn "output1",out
     if cmd[:error]==""
 			out << ((cmd[:output]=="nothing" or (cmd[:input].strip)[-1]==";")  ? "" : cmd[:output])
       out << ((cmd[:output2]=="nothing" or (cmd[:input].strip)[-1]==";")  ? "" : cmd[:output2]) #cmd[:output2]
 		else
		  out << cmd[:error]
     end
		out << (cmd[:output]=="nothing" ? "" : "\n")
		## Dyndoc.warn "output3",out
	end
	out
end

.echo_verb(txt, mode) ⇒ Object



903
904
905
906
907
908
909
910
911
912
# File 'lib/dyndoc/base/filter/server.rb', line 903

def JLServer.echo_verb(txt,mode)
  txtout=Dyndoc::JLServer.echo(txt).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

.eval(code) ⇒ Object



847
848
849
# File 'lib/dyndoc/base/filter/server.rb', line 847

def JLServer.eval(code)
	Julia.eval("capture_output_julia("+code.strip.inspect.gsub("$","\\$")+")")
end

.initVerbObject



830
831
832
833
834
# File 'lib/dyndoc/base/filter/server.rb', line 830

def JLServer.initVerb
	Julia << "Base.include(@__MODULE__,\""+File.join(Dyndoc.cfg_dir[:gem_path],"share","julia","dyndoc.jl")+"\")"
   Julia << "push!(Libdl.DL_LOAD_PATH,\"/usr/lib\");push!(Libdl.DL_LOAD_PATH,\"/usr/local/lib\")"
 	@@initVerb=true
end

.inputsAndOutputs(code, hash = true) ⇒ Object



836
837
838
839
840
841
842
843
844
845
# File 'lib/dyndoc/base/filter/server.rb', line 836

def JLServer.inputsAndOutputs(code,hash=true)
	JLServer.initVerb unless @@initVerb
   Dyndoc.logger.info("JLServer.inputsAndOutputs: "+code.strip)
 	res=(Julia << 'capture_output_julia('+code.strip.inspect.gsub("$","\\$")+')')
   Dyndoc.logger.info("JLServer.inputsAndOutputs:"+res.inspect)
   res.map!{|input,output,output2,error,error2|
		{:input=>input,:output=>output,:output2=>output2,:error=>error,:error2=>error2}
	} if res and hash
	res
end

.output(code, opts = {}) ⇒ Object



851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/dyndoc/base/filter/server.rb', line 851

def JLServer.output(code,opts={})
	#opts={:print=>true}.merge(opts)
	## Dyndoc.warn "jlserv",code+"|"+Julia.eval(code,:print=>opts[:print]).to_s
	#Julia.eval(code,:print=>opts[:print]).to_s
	
	res=JLServer.inputsAndOutputs(code,false)
	Dyndoc.warn "jlserv.output",[code,res]
	return "" unless res
	res=res.map{|input,output,output2,error,error2|
		#Dyndoc.warn "output",output
		output
	}
	res.length==0 ? "" : res[-1] 
end

.outputs(code, opts = {}) ⇒ Object

may have more than one lines in code



866
867
868
869
870
871
872
873
874
875
876
877
878
# File 'lib/dyndoc/base/filter/server.rb', line 866

def JLServer.outputs(code,opts={}) #may have more than one lines in code
	## Dyndoc.warn "JLServer.outputs opts",opts
	## 
	Dyndoc.warn "JLServer code",code
	res=JLServer.inputsAndOutputs(code,false)
	if opts[:block]
		return "" unless res
		res.map{|input,output,output2,error,error2|
			## Dyndoc.warn "output2",output2
			output
		}.join("\n")
	end
end