dyndoc-ruby-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +7 -0
  2. data/dyndoc/.DS_Store +0 -0
  3. data/dyndoc/Dyn/.postload +1 -0
  4. data/dyndoc/Dyn/.preload +1 -0
  5. data/dyndoc/Dyn/After.dyn +3 -0
  6. data/dyndoc/Dyn/Base.dyn +18 -0
  7. data/dyndoc/Dyn/Minimum.dyn +20 -0
  8. data/dyndoc/Model/Html/DefaultPost_html.dyn +5 -0
  9. data/dyndoc/Model/Html/DefaultPre_html.dyn +15 -0
  10. data/dyndoc/Model/Odt/DefaultPost_content.xml +3 -0
  11. data/dyndoc/Model/Odt/DefaultPost_tmpl_content.xml +3 -0
  12. data/dyndoc/Model/Odt/DefaultPre_content.xml +8 -0
  13. data/dyndoc/Model/Odt/DefaultPre_tmpl_content.xml +8 -0
  14. data/dyndoc/Model/Tex/ContentPost_tmpl.tex +1 -0
  15. data/dyndoc/Model/Tex/ContentPre_tmpl.tex +6 -0
  16. data/dyndoc/Model/Tex/DefaultPost_tmpl.tex +73 -0
  17. data/dyndoc/Model/Tex/DefaultPre_tmpl.tex +16 -0
  18. data/dyndoc/Model/Ttm/DefaultPost_tmpl.ttm +3 -0
  19. data/dyndoc/Model/Ttm/DefaultPre_tmpl.ttm +16 -0
  20. data/dyndoc/Std/Array.dyn +96 -0
  21. data/dyndoc/Std/Composer.dyn +38 -0
  22. data/dyndoc/Std/DevTag.dyn +101 -0
  23. data/dyndoc/Std/DynVar.dyn +23 -0
  24. data/dyndoc/Std/File.dyn +55 -0
  25. data/dyndoc/Std/Git.dyn +34 -0
  26. data/dyndoc/Std/List.dyn +46 -0
  27. data/dyndoc/Std/OOP.dyn +32 -0
  28. data/dyndoc/Std/Signal.dyn +18 -0
  29. data/dyndoc/Std/String.dyn +22 -0
  30. data/dyndoc/Std/Styles.dyn +1 -0
  31. data/dyndoc/Std/Test.dyn +282 -0
  32. data/dyndoc/Std/Utils.dyn +42 -0
  33. data/dyndoc/Std/Verb.dyn +5 -0
  34. data/dyndoc/Style/Notation/Math.dyn +7 -0
  35. data/dyndoc/Style/Notation/Stat/Classic.dyn +8 -0
  36. data/dyndoc/Style/Notation/Stat/Cqls.dyn +44 -0
  37. data/dyndoc/Style/Text/Std.dyn +5 -0
  38. data/dyndoc/Style/Text/StdAlias.dyn +3 -0
  39. data/dyndoc/Style/Text/StdTex.dyn +17 -0
  40. data/dyndoc/Style/Text/Txt.dyn +1 -0
  41. data/dyndoc/Tex/.postload +1 -0
  42. data/dyndoc/Tex/.preload +1 -0
  43. data/dyndoc/Tex/10pt_tmpl.tex +1 -0
  44. data/dyndoc/Tex/11pt_tmpl.tex +1 -0
  45. data/dyndoc/Tex/12pt_tmpl.tex +1 -0
  46. data/dyndoc/Tex/Article_tmpl.tex +1 -0
  47. data/dyndoc/Tex/Beamer.dyn_tex +35 -0
  48. data/dyndoc/Tex/BeamerHandout_tmpl.tex +2 -0
  49. data/dyndoc/Tex/Book_tmpl.tex +1 -0
  50. data/dyndoc/Tex/DefaultFirst_tmpl.tex +1 -0
  51. data/dyndoc/Tex/DefaultLast_tmpl.tex +8 -0
  52. data/dyndoc/Tex/Default_tmpl.tex +9 -0
  53. data/dyndoc/Tex/First_tmpl.tex +8 -0
  54. data/dyndoc/Tex/Fr_tmpl.tex +17 -0
  55. data/dyndoc/Tex/Header_tmpl.tex +3 -0
  56. data/dyndoc/Tex/InclGraph_tmpl.tex +19 -0
  57. data/dyndoc/Tex/InclSpatProc_tmpl.tex +70 -0
  58. data/dyndoc/Tex/InclSumMI_tmpl.tex +4 -0
  59. data/dyndoc/Tex/LargeA4_tmpl.tex +5 -0
  60. data/dyndoc/Tex/Last_tmpl.tex +1 -0
  61. data/dyndoc/Tex/Option_tmpl.tex +8 -0
  62. data/dyndoc/Tex/Png_tmpl.tex +1 -0
  63. data/dyndoc/Tex/RefCite2_tmpl.tex +16 -0
  64. data/dyndoc/Tex/RefCite_tmpl.tex +16 -0
  65. data/dyndoc/Tex/Report_tmpl.tex +1 -0
  66. data/dyndoc/Tex/Theorem_tmpl.tex +14 -0
  67. data/dyndoc/Tools/.DS_Store +0 -0
  68. data/dyndoc/Tools/Atom.dyn/index.dyn +42 -0
  69. data/dyndoc/Tools/AtomDyndocker.dyn/index.dyn +43 -0
  70. data/dyndoc/Tools/R/Fig.dyn +144 -0
  71. data/dyndoc/Tools/R/Tools.dyn +344 -0
  72. data/dyndoc/Tools/Tex/Beamer.dyn +204 -0
  73. data/dyndoc/Tools/Tex/BeamerSlide.dyn_tex +199 -0
  74. data/dyndoc/Tools/Tex/Pgf.dyn +115 -0
  75. data/dyndoc/Tools/Tex/Table.dyn +278 -0
  76. data/dyndoc/Tools/Tex/Tools.dyn +37 -0
  77. data/dyndoc/Tools/Tex/Verb.dyn +77 -0
  78. data/dyndoc/Tools/Web/.DS_Store +0 -0
  79. data/dyndoc/Tools/Web/Ace.dyn +54 -0
  80. data/dyndoc/Tools/Web/Code.dyn +129 -0
  81. data/dyndoc/Tools/Web/DHtmlX.dyn +39 -0
  82. data/dyndoc/Tools/Web/DataTable.dyn_html +354 -0
  83. data/dyndoc/Tools/Web/Html.dyn +286 -0
  84. data/dyndoc/Tools/Web/Html/JQuery.dyn +123 -0
  85. data/dyndoc/Tools/Web/Html/Styles.dyn +4 -0
  86. data/dyndoc/Tools/Web/JQueryTools.dyn +87 -0
  87. data/dyndoc/Tools/Web/Layout.dyn +86 -0
  88. data/dyndoc/Tools/Web/Layout/CQLS.dyn +6 -0
  89. data/dyndoc/Tools/Web/Layout/LJK.dyn +41 -0
  90. data/dyndoc/Tools/Web/TabBar.dyn +37 -0
  91. data/dyndoc/Tools/Web/Ttm.dyn +20 -0
  92. data/dyndoc/Tools/Web/Txtl.dyn +14 -0
  93. data/lib/dyndoc-core.rb +59 -0
  94. data/lib/dyndoc/base/envir.rb +541 -0
  95. data/lib/dyndoc/base/filter/call.rb +127 -0
  96. data/lib/dyndoc/base/filter/filter_mngr.rb +637 -0
  97. data/lib/dyndoc/base/filter/server.rb +882 -0
  98. data/lib/dyndoc/base/filters.rb +3 -0
  99. data/lib/dyndoc/base/helpers.rb +9 -0
  100. data/lib/dyndoc/base/helpers/core.rb +32 -0
  101. data/lib/dyndoc/base/helpers/parser.rb +188 -0
  102. data/lib/dyndoc/base/scanner.rb +886 -0
  103. data/lib/dyndoc/base/tags.rb +4 -0
  104. data/lib/dyndoc/base/tags/keys_mngr.rb +401 -0
  105. data/lib/dyndoc/base/tags/part_tag.rb +194 -0
  106. data/lib/dyndoc/base/tags/tag_mngr.rb +125 -0
  107. data/lib/dyndoc/base/tags/user_tag.rb +216 -0
  108. data/lib/dyndoc/base/tmpl.rb +6 -0
  109. data/lib/dyndoc/base/tmpl/eval.rb +581 -0
  110. data/lib/dyndoc/base/tmpl/extension.rb +337 -0
  111. data/lib/dyndoc/base/tmpl/manager.rb +450 -0
  112. data/lib/dyndoc/base/tmpl/oop.rb +57 -0
  113. data/lib/dyndoc/base/tmpl/parse_do.rb +2446 -0
  114. data/lib/dyndoc/base/tmpl/rbenvir.rb +54 -0
  115. data/lib/dyndoc/base/utils.rb +367 -0
  116. data/lib/dyndoc/common/dynArray.rb +234 -0
  117. data/lib/dyndoc/common/file.rb +52 -0
  118. data/lib/dyndoc/common/init.rb +2 -0
  119. data/lib/dyndoc/common/tilt.rb +149 -0
  120. data/lib/dyndoc/common/utils.rb +61 -0
  121. data/lib/dyndoc/common/uv.rb +163 -0
  122. data/lib/dyndoc/init/config.rb +296 -0
  123. data/lib/dyndoc/init/home.rb +9 -0
  124. data/lib/dyndoc/plugins/tex.rb +4 -0
  125. data/lib/dyndoc/plugins/tex/beamer.rb +208 -0
  126. data/lib/dyndoc/plugins/tex/tex_eval.rb +69 -0
  127. data/lib/dyndoc/plugins/tex/tex_parse_do.rb +25 -0
  128. data/lib/dyndoc/plugins/tex/tex_user_tag.rb +32 -0
  129. data/share/R/dyndocMsys2.R +5 -0
  130. data/share/R/dyndocTools.R +9 -0
  131. data/share/R/test.R +14 -0
  132. data/share/R/tools/dynArray.R +34 -0
  133. data/share/R/tools/dynCapture.R +84 -0
  134. data/share/R/tools/dynMsys2.R +54 -0
  135. data/share/julia/dynArray.jl +93 -0
  136. data/share/julia/dyndoc.jl +110 -0
  137. data/share/julia/ruby.jl +37 -0
  138. metadata +202 -0
@@ -0,0 +1,882 @@
1
+ # coding: utf-8
2
+
3
+ module Dyndoc
4
+
5
+ VERB={:tex=>{
6
+ :begin=>"\\begin{Verbatim}[frame=leftline,fontfamily=tt,fontshape=n,numbers=left]",
7
+ :end=>"\\end{Verbatim}"
8
+ },
9
+ :ttm=>{
10
+ :begin=>"\\begin{verbatim}",
11
+ :end=>"\\end{verbatim}"
12
+ },
13
+ :txtl=>{
14
+ :begin=>"<pre><code style=\"color:yellow;background-color:black\">",
15
+ :end=>"</code></pre>"
16
+ },
17
+ :tm=>{
18
+ :begin=>"<\\verbatim>__TM__",
19
+ :end=>"__TM__</verbatim>"
20
+ },
21
+ :raw=>{
22
+ :begin=>"",
23
+ :end=>""
24
+ }
25
+ }
26
+ VERB[:html]=VERB[:txtl]
27
+
28
+ class RbServer
29
+
30
+ @@start,@@stop="\\{","\\}"
31
+
32
+ def RbServer.filter(str,rbEnvir=nil)
33
+ res=str.gsub(/\\?(?i:\#Rb|\#rb|\:Rb|\:rb|\:)#{@@start}[^#{@@stop}]+#{@@stop}/) {|w|
34
+ if w[0,1]=="\\"
35
+ w[1..-1]
36
+ else
37
+ k=(w[1,2].downcase=="rb" ? w[4..-2] : w[2..-2]) #the key
38
+ #p "apply:Rb";p k;p w;p rbEnvir
39
+ RbServer.output(k,rbEnvir,:error=> w)
40
+ end
41
+ }
42
+ res
43
+ end
44
+
45
+ =begin
46
+ def RbServer.output(code,rbEnvir=nil,error="ERROR")
47
+ begin
48
+ #puts "code";p code
49
+ out=eval(code,rbEnvir)
50
+ #puts "out";p out
51
+ rescue
52
+ out=error
53
+ end
54
+ #p out
55
+ out=out.join(",") if out.is_a? Array
56
+ out.to_s
57
+ end
58
+ =end
59
+
60
+ def RbServer.output(code,rbEnvir=nil,opts={:error=>"ERROR"})
61
+
62
+ begin
63
+ if rbEnvir.is_a? Binding
64
+ out=rbEnvir.eval(code)
65
+ elsif rbEnvir.is_a? Module
66
+ out=rbEnvir.module_eval(code)
67
+ end
68
+ rescue
69
+ if RUBY_VERSION >= "1.9.3" and rbEnvir.is_a? Binding and rbEnvir.eval("local_variables").include? :childBinding
70
+ begin
71
+ rbEnvir2=rbEnvir.eval("childBinding")
72
+ out=rbEnvir2.eval(code)
73
+ return out
74
+ rescue
75
+ end
76
+ end
77
+
78
+ #two solution:
79
+ #in the same spirit as #{}
80
+ # out="\\:{"+code+"}"
81
+ # or more informative for debugging!
82
+
83
+ out="\\:{"+code+"}"
84
+
85
+ if Dyndoc.cfg_dyn[:ruby_debug] ==:expression
86
+ Dyndoc.warn "WARNING: >>>>>>>>>>>>>>>>>>+\n"+opts[:error]+" in #{rbEnvir}:\n"+code+"\n<<<<<<<<<<<<<<<<<<"
87
+ end
88
+
89
+
90
+ if Dyndoc.cfg_dyn[:dyndoc_mode]!=:normal and $dyn_logger
91
+ ##p ["error ruby",code]
92
+ $dyn_logger.write("\nERROR Ruby:\n"+code+"\n")
93
+ end
94
+
95
+ rescue SyntaxError
96
+ puts "RbServer syntax error in: "+code
97
+ raise SystemError if Dyndoc.cfg_dyn[:dyndoc_mode]==:normal and Dyndoc.cfg_dyn[:ruby_debug]!=:none
98
+ if Dyndoc.cfg_dyn[:dyndoc_mode]!=:normal
99
+ $dyn_logger.write("\nERROR Ruby Syntax:\n"+code+"\n")
100
+ end
101
+ out=":{"+code+"}"
102
+ end
103
+ #p out
104
+ out
105
+ end
106
+
107
+ def RbServer.capture(code,rbEnvir=nil)
108
+
109
+ require 'stringio'
110
+ require 'ostruct'
111
+
112
+ begin
113
+ # redirect output to StringIO objects
114
+ oldstdout,oldstderr=$stdout, $stderr
115
+ stdout, stderr = StringIO.new, StringIO.new
116
+ $stdout, $stderr = stdout, stderr
117
+
118
+ if rbEnvir.is_a? Binding
119
+ out=rbEnvir.eval(code)
120
+ elsif rbEnvir.is_a? Module
121
+ out=rbEnvir.module_eval(code)
122
+ end
123
+ ensure
124
+ # restore normal output
125
+ $stdout, $stderr = oldstdout,oldstderr
126
+ end
127
+ {input: code, output: out.inspect, stdout: stdout.string, stderr: stderr.string}
128
+ end
129
+
130
+ def RbServer.inputsAndOutputs(code,rbEnvir=nil)
131
+ require 'ripper'
132
+ res = []
133
+ input = ""
134
+ code.each_line do |l|
135
+ input += l
136
+ if Ripper.sexp input
137
+ res << RbServer.capture(input,rbEnvir)
138
+ input = ""
139
+ end
140
+ end
141
+ res
142
+ end
143
+
144
+ def RbServer.echo(code,rbEnvir=nil,prompt="ruby> ",tab=2)
145
+ out=""
146
+ res=RbServer.inputsAndOutputs(code,rbEnvir)
147
+ ## Dyndoc.warn "RbServer",res
148
+ res.each do |cmd|
149
+ ## Dyndoc.warn "input",cmd
150
+ out << prompt+ cmd[:input].split("\n").each_with_index.map{|e,i| i==0 ? e : " "*(prompt.length)+e}.join("\n").gsub(/\t/," "*tab)
151
+ out << "\n"
152
+ ## Dyndoc.warn "output1",out
153
+ out << cmd[:stdout]
154
+ out << cmd[:output] || ""
155
+ ## Dyndoc.warn "output2",out
156
+ out << cmd[:stderr]!="" ? cmd[:stderr] : ""
157
+ out << (cmd[:output] ? "\n\n" : "")
158
+ ## Dyndoc.warn "output3",out
159
+ end
160
+ out
161
+ end
162
+
163
+ def RbServer.echo_verb(txt,mode,rbEnvir=nil)
164
+ ## Dyndoc.warn "echo_verb:txt",txt
165
+ txtout=Dyndoc::RbServer.echo(txt.strip,rbEnvir).strip
166
+ mode=:default unless Dyndoc::VERB.keys.include? mode
167
+ header= (mode!=:default) and txtout.length>0
168
+ out=""
169
+ out << Dyndoc::VERB[mode][:begin] << "\n" if header
170
+ out << txtout
171
+ out << "\n" << Dyndoc::VERB[mode][:end] << "\n" if header
172
+ out
173
+ end
174
+
175
+ end
176
+
177
+ class RServer
178
+
179
+ require 'tempfile'
180
+
181
+ def RServer.R4rb(mode,cli=nil) #mode= :R2rb or Rserve
182
+ return unless [:R2rb,:Rserve].include? mode
183
+ require 'R4rb'
184
+ ##puts "Rserver.R4rb: mode=#{mode}"
185
+ Object.module_eval("R4rb_is #{mode}")
186
+ ##puts "Rserver.R4rb: R4rb";p Object.const_get(:R4rb)
187
+ R4rb.init
188
+ Rserve.client(cli) if cli
189
+ ##puts "Rserve.cli";p Rserve.cli
190
+ end
191
+
192
+ if $dyndoc_rserv
193
+ ##puts "$dyndoc_rserv";p $dyndoc_rserv
194
+ RServer.R4rb :Rserve,$dyndoc_rserv
195
+ R4rb_status? if $dyndoc_server_hostname
196
+ end
197
+
198
+ def RServer.echo_verb(txt,mode,env="Global",opts={prompt: ""})
199
+ txtout=Dyndoc::RServer.echo(txt,env,opts[:prompt]).strip
200
+ mode=:default unless Dyndoc::VERB.keys.include? mode
201
+ header= (mode!=:default) and txtout.length>0
202
+ out=""
203
+ out << Dyndoc::VERB[mode][:begin] << "\n" if header
204
+ out << txtout
205
+ out << "\n" << Dyndoc::VERB[mode][:end] << "\n" if header
206
+ out
207
+ end
208
+
209
+ def RServer.echo(block,env="Global",prompt="")
210
+ Utils.clean_eol(block)
211
+ txtout=""
212
+ optout=nil #options for the output
213
+ hide=0
214
+ passe=0
215
+ opt = nil
216
+ code=""
217
+ ##Dyndoc.warn "block",block
218
+ block.each_line{|l|
219
+ l2=l.chomp
220
+ inst=l2.delete(" ").split("|")[0]
221
+ if inst
222
+ inst=inst.split(":")
223
+ ## number of lines to apply
224
+ nb = 1 #default
225
+ nb=inst[1].to_i if inst.length>1
226
+ ## instr to exec
227
+ inst=inst[0].downcase
228
+ else
229
+ inst="line"
230
+ end
231
+ ## options
232
+ opt=l2.split("|")
233
+ if opt.length>1
234
+ opt2=opt[1..-1]
235
+ ## of the form key=value like Hash
236
+ opt2.map!{|elt| elt.split("=")}
237
+ opt={}
238
+ opt2.each{|elt| opt[elt[0].downcase.delete(" ")]=elt[1]}
239
+ else
240
+ opt=nil
241
+ end
242
+ case inst
243
+ when "line"
244
+ txtout << "\n"
245
+ when "##!eval"
246
+ passe= nb.to_i #this is a copy
247
+ when "##out"
248
+ optout=opt
249
+ when "##hide"
250
+ hide = nb.to_i
251
+ else
252
+ txtout=txtout.force_encoding("utf-8")
253
+ txtout << ( code.length==0 ? prompt+"> " : "+ ") << l2 << "\n" if hide==0
254
+ if passe==0 and l2[0,1]!="#"
255
+ ## redirect R output
256
+ code << l << "\n" ##ajout de "\n" grace à Pierre (le 15/12/05) pour bug: "1:10 #toto" -> pas de sortie car parse erreur n2!!!
257
+ case @@mode
258
+ when :capture_normal
259
+ evalOk=(R4rb << ".output<<-capture.output({"+RServer.code_envir(code,env)+"})")
260
+ when :capture_cqls
261
+ evalOk=(R4rb << ".output<<-capture.output.cqls({"+RServer.code_envir(code,env)+"})")
262
+ end
263
+
264
+ ##Dyndoc.warn "evalOk",code,evalOk
265
+ if evalOk
266
+ txt=(@@out < '.output' ) ##.join("\n").split(/\n/)
267
+ code=""
268
+ else
269
+ txt=@@out=[]
270
+ end
271
+ if optout and optout.keys.include? "short"
272
+ short=optout["short"].split(",")
273
+ short[0]=short[0].to_i
274
+ short[2]=short[2].to_i
275
+ ## Dyndoc.warn "short",[short,txt]
276
+ (0...short[0]).each{|i| txtout << txt[i].force_encoding("utf-8") << "\n"}
277
+ txtout << short[1] << "\n"
278
+ le = txt.length
279
+ ((le-short[2])...le).each{|i| txtout << txt[i].force_encoding("utf-8") << "\n"}
280
+ else
281
+ txt=txt.map{|e| e.force_encoding("utf-8")}
282
+ txtout << txt.join("\n")
283
+ txtout += "\n" if @@out.length>0
284
+ ##txt.each{|l| txtout << l <<"\n"}
285
+ end
286
+ end
287
+ optout=nil
288
+ hide -= 1 if hide>0
289
+ passe -=1 if passe>0
290
+ end
291
+ }
292
+ return txtout
293
+ end
294
+
295
+ def RServer.echo_blocks(block,prompt={:normal=>'> ',:continue=>'+ '},env="Global")
296
+ Utils.clean_eol(block)
297
+ inputs,outputs=[],[]
298
+ input,output="",""
299
+ optout=nil #options for the output
300
+ hide=0
301
+ passe=0
302
+ opt = nil
303
+ code=""
304
+ block.each_line{|l|
305
+ l2=l.chomp
306
+ inst=l2.delete(" ").split("|")[0]
307
+ if inst
308
+ inst=inst.split(":")
309
+ ## number of lines to apply
310
+ nb = 1 #default
311
+ nb=inst[1].to_i if inst.length>1
312
+ ## instr to exec
313
+ inst=inst[0].downcase
314
+ else
315
+ inst="line"
316
+ end
317
+ ## options
318
+ opt=l2.split("|")
319
+ if opt.length>1
320
+ opt2=opt[1..-1]
321
+ ## of the form key=value like Hash
322
+ opt2.map!{|elt| elt.split("=")}
323
+ opt={}
324
+ opt2.each{|elt| opt[elt[0].downcase.delete(" ")]=elt[1]}
325
+ else
326
+ opt=nil
327
+ end
328
+ case inst
329
+ when "##!eval"
330
+ passe= nb.to_i #this is a copy
331
+ when "##out"
332
+ optout=opt
333
+ when "##hide"
334
+ hide = nb.to_i
335
+ else
336
+ if hide==0
337
+ input << ( code.length==0 ? prompt[:normal] : prompt[:continue]) if prompt
338
+ input << l2 << "\n"
339
+ end
340
+ if passe==0 and l2[0,1]!="#"
341
+ ## redirect R output
342
+ code << l << "\n" ##ajout de "\n" grace à Pierre (le 15/12/05) pour bug: "1:10 #toto" -> pas de sortie car parse erreur n2!!!
343
+ evalOk=(R4rb << ".output<<-capture.output({"+RServer.code_envir(code,env)+"})")
344
+ if evalOk
345
+ txt=(@@out < '.output' ) ##.join("\n").split(/\n/)
346
+ code=""
347
+ else
348
+ txt=@@out=[]
349
+ end
350
+ if optout and optout.keys.include? "short"
351
+ short=optout["short"].split(",")
352
+ short[0]=short[0].to_i
353
+ short[2]=short[2].to_i
354
+ (0...short[0]).each{|i| output << txt[i] << "\n"}
355
+ output << short[1] << "\n"
356
+ le = txt.length
357
+ ((le-short[2])...le).each{|i| output << txt[i] << "\n"}
358
+ else
359
+ output << txt.join("\n")
360
+ output += "\n" if @@out.length>0
361
+ end
362
+ inputs << input
363
+ outputs << output.gsub(/^[\n]*/,"")
364
+ input,output="",""
365
+ end
366
+ optout=nil
367
+ hide -= 1 if hide>0
368
+ passe -=1 if passe>0
369
+ end
370
+ }
371
+ return {:in=>inputs,:out=>outputs}
372
+ end
373
+
374
+
375
+ @@mode=:capture_cqls #or :capture_protected or capture_normal or capture_local
376
+
377
+ def RServer.mode=(val)
378
+ @@mode= val
379
+ end
380
+
381
+ def RServer.mode
382
+ @@mode
383
+ end
384
+
385
+ @@device_cmd,@@device="png","png"
386
+
387
+ def RServer.device(dev="pdf")
388
+ case dev
389
+ when "pdf"
390
+ @@device_cmd,@@device="pdf","pdf" #(%{capabilities()["cairo"]}.to_R ? "cairo_pdf" : "pdf"),"pdf"
391
+ when "png"
392
+ @@device_cmd,@@device="png","png"
393
+ end
394
+ end
395
+
396
+ #def RServer.input_semi_colon(block)
397
+ # block.map{|e| e.chomp!;((e.include? ";") ? (ee=e.split(";");["##!eval",e,"##hide:#{ee.length}"]+ee) : e )}.compact.join("\n")
398
+ #end
399
+
400
+ def RServer.inputsAndOutputs(block,id="",optRDevice="",prompt={:normal=>'',:continue=>''},env="Global")
401
+ Utils.clean_eol(block)
402
+ envLoc=env
403
+ optRDevice=(@@device=="png" ? "width=10,height=10,units=\"cm\",res=128" : "width=5,height=5,onefile=FALSE") if optRDevice.empty?
404
+ ##R4rb << "require(dyndoc)" if @@mode==:capture_cqls
405
+ results=[]
406
+ input,output="",""
407
+ optout,optpasse=nil,nil #options for the output
408
+ hide,passe,passeNb=0,0,0
409
+ echo,echoLines=0,[]
410
+ opt = nil
411
+ code=""
412
+ # add R device
413
+ imgdir=($dyn_rsrc ? File.join($dyn_rsrc,"img") : "/tmp/Rserver-img"+rand(1000000).to_s)
414
+
415
+ imgfile=File.join(imgdir,"tmpImgFile"+id.to_s+"-")
416
+ cptImg=0
417
+ imgCopy=[]
418
+
419
+ FileUtils.mkdir_p imgdir unless File.directory? imgdir
420
+ Dir[imgfile+"*"].each{|f| FileUtils.rm_f(f)}
421
+ #p Dir[imgfile+"*"]
422
+
423
+
424
+ #Dyndoc.warn "fig command:", "#{@@device_cmd}(\"#{imgfile}%d.#{@@device}\",#{optRDevice})"
425
+ R4rb << "#{@@device_cmd}(\"#{imgfile}%d.#{@@device}\",#{optRDevice})"
426
+ #block=RServer.input_semi_colon(block)
427
+ # the following is equivalent to each_line!
428
+ block.each_line{|l|
429
+ l2=l.chomp
430
+ #Dyndoc.warn :l2,l2
431
+ inst=l2.delete(" ").split("|")[0]
432
+ #Dyndoc.warn "inst",inst
433
+ if inst and inst[0,2]=="##"
434
+ #if inst
435
+ inst=inst.split(":")
436
+ ## number of lines to apply
437
+ nb = 1 #default
438
+ nb=inst[1].to_i if inst.length>1
439
+ ## instr to exec
440
+ inst=inst[0].downcase
441
+ #else
442
+ # inst="line"
443
+ #end
444
+ ## options
445
+ opt=l2.split("|")
446
+ if opt.length>1
447
+ opt2=opt[1..-1]
448
+ ## of the form key=value like Hash
449
+ opt2.map!{|elt| elt.split("=")}
450
+ #p opt2
451
+ opt={}
452
+ opt2.each{|elt| opt[elt[0].downcase.delete(" ")]=elt[1..-1].join("=")}
453
+ else
454
+ opt=nil
455
+ end
456
+ #Dyndoc.warn "opt",opt
457
+ else
458
+ inst="line"
459
+ opt=nil
460
+ end
461
+
462
+ if echo>0
463
+ echo -= 1
464
+ echoLines << l2
465
+ next
466
+ end
467
+
468
+ if echo==0 and !echoLines.empty? and !results.empty?
469
+ results[-1][:output] << "\n" unless results[-1][:output].empty?
470
+ results[-1][:output] << echoLines.join("\n")
471
+ echoLines=[]
472
+ end
473
+
474
+ #Dyndoc.warn :inst, inst
475
+ case inst
476
+ when "##echo" ##need to be added
477
+ echo=nb.to_i
478
+ when "##!eval"
479
+ passe= nb.to_i #this is a copy
480
+ passeNb=nb.to_i #to remember the original nb
481
+ optpasse=opt
482
+ when "##out"
483
+ optout=opt
484
+ when "##hide"
485
+ hide = nb.to_i
486
+ #Dyndoc.warn :hide,hide
487
+ when "##fig"
488
+ if opt and opt["img"] and !opt["img"].empty?
489
+ imgName=File.basename(opt["img"].strip,".*")
490
+ imgName+=".#{@@device}" #unless imgName=~/\.#{@@device}$/
491
+ imgName=File.join(imgdir,imgName)
492
+
493
+ imgCopy << {:in => imgfile+cptImg.to_s+".#{@@device}",:out=>imgName}
494
+ opt.delete("img")
495
+ else
496
+ imgName=imgfile+cptImg.to_s+".#{@@device}"
497
+ end
498
+ puts "DYN ERROR!!! no fig allowed after empty R output!!!" unless results[-1]
499
+ results[-1][:img]={:name=>imgName}
500
+ results[-1][:img][:opt]=opt if opt and !opt.empty?
501
+ #could not copy file now!!!!
502
+ when "##add"
503
+ results[-1][:add]=opt
504
+ else
505
+ #Dyndoc.warn :hide?, [hide,passe,@@mode]
506
+ if hide==0
507
+ promptMode=(code.length==0 ? :normal : :continue )
508
+ input << prompt[promptMode] if prompt
509
+ #puts "before";p l;p envLoc
510
+ l2,envLoc=RServer.find_envir(l2,envLoc)
511
+ #Dyndoc.warn "after",l,envLoc
512
+ input << l2 << "\n"
513
+ #Dyndoc.warn :input3, input
514
+ end
515
+ if passe==0 and l2[0,1]!="#"
516
+ ## redirect R output
517
+ code << l2 << "\n" ##ajout de "\n" grace à Pierre (le 15/12/05) pour bug: "1:10 #toto" -> pas de sortie car parse erreur n2!!!
518
+ case @@mode
519
+ when :capture_cqls
520
+ ##TODO: instead of only splitting check that there is no
521
+ ## or ask the user to use another character instead of ";" printed as is in the input!
522
+ codes=code.split(";")
523
+ #Dyndoc.warn :codes, codes
524
+ evalOk=(R4rb << ".output <<- ''")
525
+ codes.each{|cod|
526
+ evalOk &= (R4rb << (tmp=".output <<- c(.output,capture.output.cqls({"+RServer.code_envir(cod,envLoc)+"}))"))
527
+ #Dyndoc.warn "tmp",tmp
528
+ }
529
+ when :capture_protected
530
+ ##TODO: instead of only splitting check that there is no
531
+ ## or ask the user to use another character instead of ";" printed as is in the input!
532
+ codes=code.split(";")
533
+ evalOk=(R4rb << ".output <<- ''")
534
+ codes.each{|cod|
535
+ evalOk &= (R4rb << (tmp=".output <<- c(.output,capture.output.protected({"+RServer.code_envir(cod,envLoc)+"}))"))
536
+ #Dyndoc.warn "tmp",tmp
537
+ }
538
+ when :capture_normal
539
+ codes=code.split(";")
540
+ evalOk=(R4rb << ".output <<- ''")
541
+ codes.each{|cod|
542
+ evalOk &= (R4rb << (tmp=".output <<- c(.output,capture.output({"+RServer.code_envir(cod,envLoc)+"}))"))
543
+ }
544
+ when :sink #Ne marche pas à cause du sink!!!
545
+ evalOk=(R4rb << (tmp=%{
546
+ zz <- textConnection(".output", "w")
547
+ sink(zz)
548
+ local({
549
+ #{code}
550
+ },.GlobalEnv$.env4dyn$#{envLoc}
551
+ )
552
+ sink()
553
+ close(zz)
554
+ print(.output)
555
+ }))
556
+ #Dyndoc.warn "tmp",tmp
557
+ when :capture_local
558
+ codes=code.split(";")
559
+ evalOk=(R4rb << ".output <<- ''")
560
+ codes.each{|cod|
561
+ cod=".output <<- c(.output,capture.output({local({"+cod+"},.GlobalEnv$.env4dyn$#{envLoc})}))"
562
+ #Dyndoc.warn cod
563
+ evalOk &= (R4rb << cod )
564
+ }
565
+ end
566
+ cptImg += 1 if File.exists? imgfile+(cptImg+1).to_s+".#{@@device}"
567
+ #p evalOk;p code;R4rb << "print(geterrmessage())";R4rb << "if(exists(\".output\") ) print(.output)"
568
+ if evalOk
569
+ txt=(@@out < '.output' ) ##.join("\n").split(/\n/)
570
+ code=""
571
+ else
572
+ txt=@@out=[]
573
+ end
574
+ if optout and optout.keys.include? "short"
575
+ short=optout["short"].split(",")
576
+ short[0]=short[0].to_i
577
+ short[2]=short[2].to_i
578
+ (0...short[0]).each{|i| output << txt[i] << "\n"}
579
+ output << short[1] << "\n"
580
+ le = txt.length
581
+ ((le-short[2])...le).each{|i| output << txt[i] << "\n"}
582
+ else
583
+ output << txt.join("\n")
584
+ output += "\n" if @@out.length>0
585
+ end
586
+ #Dyndoc.warn :inputAndOutput,[input,output]
587
+
588
+ input=RServer.formatInput(input).force_encoding("utf-8")
589
+ #Dyndoc.warn :input,[input,output]
590
+ output=RServer.formatOutput(output).force_encoding("utf-8")
591
+ #Dyndoc.warn :output2,[input,output]
592
+ #Dyndoc.warn :state, {:hide=>hide,:passe=>passe}
593
+ #if hide==0
594
+ result={}
595
+ result[:input]= (hide==0 ? input : "")
596
+ result[:prompt]= (hide==0 ? promptMode : :none)
597
+ result[:output]=output.gsub(/^[\n]*/,"")
598
+ results << result unless (result[:input]+result[:output]).empty?
599
+ #end
600
+ input,output="",""
601
+
602
+ end
603
+ if passe==0 and l2[0,1]=="#"
604
+ result={}
605
+ result[:input]=RServer.formatInput(input).force_encoding("utf-8")
606
+ result[:prompt]=promptMode
607
+ result[:output]=""
608
+ results << result
609
+ input,output="",""
610
+ end
611
+ if passe>=1
612
+ result={}
613
+ result[:input]=RServer.formatInput(input).force_encoding("utf-8")
614
+ result[:prompt]= ( passe == passeNb ? :normal : :continue )#promptMode
615
+ result[:output]= ((optpasse and optpasse["print"]) ? optpasse["print"] : output)
616
+ #Dyndoc.warn :result,result
617
+ results << result
618
+ input,output="",""
619
+ end
620
+ optout=nil
621
+ hide -= 1 if hide>0
622
+ passe -=1 if passe>0
623
+ #Dyndoc.warn :hide2,hide
624
+ end
625
+ }
626
+ R4rb << "dev.off()"
627
+ imgCopy.each{|e|
628
+ FileUtils.mkdir_p File.dirname(e[:out]) unless File.exist? File.dirname(e[:out])
629
+ if File.exists? e[:in]
630
+ FileUtils.mv(e[:in],e[:out])
631
+ else
632
+ Dyndoc.warn "WARNING! #{e[:in]} does not exists for #{e[:out]}"
633
+ Dyndoc.warn "RServer:imgCopy",imgCopy
634
+ Dyndoc.warn "imgDir",Dir[imgdir+"/*"]
635
+ end
636
+ }
637
+ #TODO: remove all the file newly created!
638
+ #Dyndoc.warn :results, results
639
+ return results
640
+ end
641
+
642
+ @@out=[]
643
+
644
+ @@start,@@stop="\\{","\\}"
645
+
646
+ def RServer.formatOutput(out)
647
+ #out2=out.gsub(/\\n/,'\textbackslash{n}')
648
+ out.gsub("{",'\{').gsub("}",'\}').gsub("~",'\boldmath\ensuremath{\mathtt{\sim}}')
649
+ end
650
+
651
+ def RServer.formatInput(out)
652
+ out2=out.gsub(/\\n/,'\textbackslash{n}')
653
+ ## {\texttildelow}
654
+ # unless out2=~/\\\w*\{.*\}/
655
+ # out2.gsub("{",'\{').gsub("}",'\}')
656
+ # else
657
+ # out2
658
+ # end
659
+ #Dyndoc.warn :formatInput, [out,out2]
660
+ out2,out3=out2.split("#") unless out2.empty?
661
+ #Dyndoc.warn :formatInput2, [out,out2,out3]
662
+ out2=out2.gsub(/(?<!\\textbackslash)\{/,'\{').gsub(/(?<!\\textbackslash\{n)\}/,'\}')
663
+ #Dyndoc.warn :formatInput3, [out,out2]
664
+ out2=out2+"#"+out3 if out3
665
+ #Dyndoc.warn :formatInput4, [out,out2,out3]
666
+ return out2.gsub("~",'\boldmath\ensuremath{\mathtt{\sim}}')
667
+ end
668
+
669
+
670
+ def RServer.filter(str)
671
+ ## modified (28/5/04) (old : /\#R\{.+\}/ => {\#R{ok}} does not work since "ok}" was selected !!
672
+ res=str.gsub(/\\?(?i:\#|\:)[rR]#{@@start}[^#{@@stop}]+#{@@stop}/) {|w|
673
+ if w[0,1]=="\\"
674
+ w[1..-1]
675
+ else
676
+ code=w[3..-2] #the key
677
+ RServer.output(code,w[1,1]=="r")
678
+ end
679
+ }
680
+ res
681
+ end
682
+
683
+ def RServer.init_envir
684
+ ##Dyndoc.warn "Rserver.init_envir!!!",Rserve.client
685
+ "if(!exists(\".env4dyn\",envir=.GlobalEnv)) {.GlobalEnv$.env4dyn<-new.env(parent=.GlobalEnv);.GlobalEnv$.env4dyn$Global<-.GlobalEnv}".to_R
686
+ ##Dyndoc.warn "Global?",RServer.exist?("Global")
687
+ end
688
+
689
+ def RServer.exist?(env)
690
+ #puts "Rserver.exist? .env4dyn"
691
+ #"print(ls(.GlobalEnv$.env4dyn))".to_R
692
+ "\"#{env}\" %in% ls(.GlobalEnv$.env4dyn)".to_R
693
+ end
694
+
695
+ def RServer.new_envir(env,parent="Global")
696
+ #puts "New env #{env} in #{parent}"
697
+ ".GlobalEnv$.env4dyn$#{env}<-new.env(parent=.GlobalEnv$.env4dyn$#{parent})".to_R
698
+ end
699
+
700
+ def RServer.local_code_envir(code,env="Global")
701
+ "local({"+code+"},.GlobalEnv$.env4dyn$#{env})"
702
+ end
703
+
704
+ def RServer.code_envir(code,env="Global")
705
+ "evalq({"+code+"},.GlobalEnv$.env4dyn$#{env})"
706
+ end
707
+
708
+ def RServer.eval_envir(code,env="Global")
709
+ #R4rb << "evalq({"+code+"},.GlobalEnv$.env4dyn$#{env})" ##-> replaced by
710
+ R4rb << RServer.code_envir(code,env)
711
+ end
712
+
713
+ def RServer.find_envir(code,env)
714
+ #p code
715
+ #p env
716
+ codeSaved=code.clone
717
+ code2=code.split(/(\s*[\w\.\_]*\s*:)/)
718
+ #p code2
719
+ if code2[0] and code2[0].empty?
720
+ env=code2[1][0...-1].strip
721
+ code=code2[2..-1].join("")
722
+ end
723
+ env2=env.clone
724
+ env=Dyndoc.vars[env+".Renvir"] unless RServer.exist?(env)
725
+ unless RServer.exist?(env)
726
+ puts "Warning! environment #{env2} does not exist!"
727
+ code=codeSaved
728
+ env="Global"
729
+ end
730
+ return [code,env]
731
+ end
732
+
733
+ def RServer.output(code,env="Global",pretty=nil)
734
+ code,env=RServer.find_envir(code,env)
735
+ code="{"+code+"}"
736
+ #Dyndoc.warn "RServer.output",code
737
+ #Dyndoc.warn "without",code,(@@out < "evalq("+code+",.env4dyn$"+env+")"),"done"
738
+ code="prettyNum("+code+")" if pretty
739
+ #Dyndoc.warn "with",code,(@@out < "evalq("+code+",.env4dyn$"+env+")"),"done"
740
+
741
+ ## code="evalq("+code+",envir=.GlobalEnv$.env4dyn$"+env+")" ##-> replaced by
742
+ code=RServer.code_envir(code,env)
743
+ #Dyndoc.warn "RServer.output->",code,(@@out < code)
744
+ (@@out < code) #.join(', ')
745
+ end
746
+
747
+ def RServer.safe_output(code,env="Global",opts={}) #pretty=nil,capture=nil)
748
+ #Dyndoc.warn "opts",opts
749
+ code,env=RServer.find_envir(code,env)
750
+ invisible=code.split("\n")[-1].strip =~ /^print\s*\(/
751
+ code="{"+code+"}"
752
+ #Dyndoc.warn "RServer.output",code
753
+ #Dyndoc.warn "without",code,(@@out < "evalq("+code+",.env4dyn$"+env+")"),"done"
754
+ code="prettyNum("+code+")" if opts[:pretty]
755
+ #Dyndoc.warn "with",code,(@@out < "evalq("+code+",.env4dyn$"+env+")"),"done"
756
+
757
+ ## code="evalq("+code+",envir=.GlobalEnv$.env4dyn$"+env+")" ##-> replaced by
758
+ code=RServer.code_envir(code,env)
759
+ #Dyndoc.warn "RServer.output->",code,(@@out < code)
760
+ #Dyndoc.warn "RServer.safe_output: capture",capture,code
761
+ if opts[:capture] or opts[:blockR]
762
+ ## IMPORTANT; this is here to ensure that a double output is avoided at the end if the last instruction is a print
763
+ code = "invisible("+code+")" if invisible
764
+ code+=";invisible()" if opts[:blockR]
765
+ #Dyndoc.warn "safe_output",code
766
+ res=(@@out < "capture.output.cqls({"+code+"})")
767
+ #Dyndoc.warn "res", res
768
+ res=res.join("\n")
769
+ else
770
+ res=(@@out < "{.result_try_code<-try({"+code+"},silent=TRUE);if(inherits(.result_try_code,'try-error')) 'try-error' else .result_try_code}") #.join(', ')
771
+ end
772
+ res
773
+ end
774
+
775
+ #more useful than echo_tex!!!
776
+ def RServer.rout(code,env="Global")
777
+ out="> "+code
778
+ code="capture.output({"+code+"})"
779
+ ## code="evalq("+code+",.GlobalEnv$.env4dyn$"+env+")" ##-> replaced by
780
+ code=RServer.code_envir(code,env)
781
+ #puts "Rserver.rout";p code
782
+ return (@@out< code).join("\n")
783
+ end
784
+
785
+ def RServer.init_filter
786
+ dyndocTools="~/dyndoc" #Same as DYNDOCROOT as default
787
+ # DYNDOCTOOLS is possibly defined inside the launcher of DyndocStudio (Mac and linux).
788
+ dyndocTools=ENV["DYNDOCTOOLS"] if ENV["DYNDOCTOOLS"] and File.exists? ENV["DYNDOCTOOLS"]
789
+ ## if RUBY_ENGINE (ruby,jruby,rbx) defined (actually, not defined for 1.8.7)
790
+ if Object.constants.map{|e| e.to_s}.include? "RUBY_ENGINE"
791
+ version = RbConfig::CONFIG["exec_prefix"].split(File::Separator)[-1]
792
+ R4rb << ".libPaths('"+dyndocTools+"/R/library/"+RUBY_ENGINE+"/"+version+"')"
793
+ R4rb << "print(.libPaths())"
794
+ end
795
+ R4rb << "if(!(Sys.getenv('R_LIBS_USER') %in% .libPaths())) .libPaths(Sys.getenv('R_LIBS_USER'))"
796
+ ## R4rb << "require(dyndoc)" => put in TemplateManager.initR dyndocTools.R
797
+ R4rb << "require(rb4R)"
798
+ end
799
+
800
+ end
801
+
802
+ class JLServer
803
+
804
+ # def JLServer.init(mode=:default) #mode=maybe zmq (to investigate)
805
+ # require 'jl4rb'
806
+ # Julia.init
807
+ # end
808
+ @@initVerb=nil
809
+
810
+ def JLServer.initVerb
811
+ Julia << "include(\""+File.join(Dyndoc.cfg_dir[:gem_path],"share","julia","dyndoc.jl")+"\")"
812
+ @@initVerb=true
813
+ end
814
+
815
+ def JLServer.inputsAndOutputs(code,hash=true)
816
+ JLServer.initVerb unless @@initVerb
817
+ res=(Julia << 'capture_julia('+code.strip.inspect+')')
818
+ ## Dyndoc.warn "JLServer.inputsAndOutputs",res
819
+ res.map!{|input,output,output2,error,error2|
820
+ {:input=>input,:output=>output,:output2=>output2,:error=>error,:error2=>error2}
821
+ } if hash
822
+ res
823
+ end
824
+
825
+ def JLServer.eval(code)
826
+ Julia.eval(code)
827
+ end
828
+
829
+ def JLServer.output(code,opts={})
830
+ opts={:print=>true}.merge(opts)
831
+ ## Dyndoc.warn "jlserv",code+"|"+Julia.eval(code,:print=>opts[:print]).to_s
832
+ Julia.eval(code,:print=>opts[:print]).to_s
833
+ end
834
+
835
+ def JLServer.outputs(code,opts={}) #may have more than one lines in code
836
+ ## Dyndoc.warn "JLServer.outputs opts",opts
837
+ ## Dyndoc.warn "JLServer code",code
838
+ if opts[:block]
839
+ res=JLServer.inputsAndOutputs(code,false)
840
+ return "" unless res
841
+ res.map{|input,output,output2,error,error2|
842
+ ## Dyndoc.warn "output2",output2
843
+ output2
844
+ }.join("\n")
845
+ else
846
+ JLServer.eval(code)
847
+ end
848
+ end
849
+
850
+ def JLServer.echo(code,prompt="julia> ",tab=2)
851
+ out=""
852
+ res=JLServer.inputsAndOutputs(code)
853
+ ## Dyndoc.warn "JLServer",res
854
+ res.each do |cmd|
855
+ ## Dyndoc.warn "input",cmd
856
+ out << prompt+ cmd[:input].split("\n").each_with_index.map{|e,i| i==0 ? e : " "*(prompt.length)+e}.join("\n").gsub(/\t/," "*tab)
857
+ out << "\n"
858
+ ## Dyndoc.warn "output1",out
859
+ out << cmd[:output2]
860
+ out << (cmd[:output]=="nothing" ? "" : cmd[:output])
861
+ ## Dyndoc.warn "output2",out
862
+ out << cmd[:error]!="" ? cmd[:error] : ""
863
+ out << (cmd[:output]=="nothing" ? "" : "\n\n")
864
+ ## Dyndoc.warn "output3",out
865
+ end
866
+ out
867
+ end
868
+
869
+ def JLServer.echo_verb(txt,mode)
870
+ txtout=Dyndoc::JLServer.echo(txt).strip
871
+ mode=:default unless Dyndoc::VERB.keys.include? mode
872
+ header= (mode!=:default) and txtout.length>0
873
+ out=""
874
+ out << Dyndoc::VERB[mode][:begin] << "\n" if header
875
+ out << txtout
876
+ out << "\n" << Dyndoc::VERB[mode][:end] << "\n" if header
877
+ out
878
+ end
879
+
880
+ end
881
+
882
+ end