Module: Dyndoc::Utils

Defined in:
lib/dyndoc/base/utils.rb,
lib/dyndoc/common/utils.rb

Constant Summary collapse

RAW_TAGS =
{"{#code]"=>"[#code}","{#raw]"=>"[#raw}"}
RAW_LANG =
{"r"=>"R","rb"=>"ruby"}
SAVED_CONTENTS_FILE =
"saved_contents.dyn"
@@raw_code_to_process =

Just used inside do_rb in parse_do.rb file to save rbcode before process_rb!

true
@@saved_content_ls =
[]
@@saved_content_to_be_recreated =
nil

Class Method Summary collapse

Class Method Details

.cfg_file_exists?(tmpl) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/dyndoc/common/utils.rb', line 21

def Utils.cfg_file_exists?(tmpl)
  name=File.join(File.dirname(tmpl),File.basename(tmpl,".*"))
  ([".cfg_dyn",".dyn_cfg"].map{|ext| name+ext}.select{|f| File.exists? f})[0]
end

.clean_bom_utf8!(str) ⇒ Object



82
83
84
# File 'lib/dyndoc/base/utils.rb', line 82

def Utils.clean_bom_utf8!(str)
  str.gsub!("\xEF\xBB\xBF", '')
end

.clean_comment(b) ⇒ Object

TOOLS ###################################



44
45
46
# File 'lib/dyndoc/base/utils.rb', line 44

def Utils.clean_comment(b)
  b.map!{|l| ( l.scan(/([^%]*)(%%.*)/)[0] ? $1.strip : l)}.reject!{|e| e.empty?}
end

.clean_eol(str) ⇒ Object



78
79
80
# File 'lib/dyndoc/base/utils.rb', line 78

def Utils.clean_eol(str)
  str.gsub!("\r\n","\n")
end

.clean_indent(b) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/dyndoc/base/utils.rb', line 48

def Utils.clean_indent(b)
  start,key=b[0].scan(/(\s*)([^\s]*)/)[0]
  if start.length>0
    b.map!{|l|
      ( (l.index start)==0 ? l[start.length,l.length-start.length] : l )
    }
  end
end

.dyndoc_globvar(key, value = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dyndoc/common/utils.rb', line 45

def Utils.dyndoc_globvar(key,value=nil)
	if value
           if [:remove,:rm,:del,:delete].include?  value
               Dyndoc.tmpl_mngr.filterGlobal.envir.remove(key)
           else
		     Dyndoc.tmpl_mngr.filterGlobal.envir[key]= value
           end
	else
		Dyndoc.tmpl_mngr.filterGlobal.envir[key]
	end
end

.dyndoc_raw_text(key = nil) ⇒ Object

find a raw text



157
158
159
160
161
162
163
164
165
# File 'lib/dyndoc/base/utils.rb', line 157

def Utils.dyndoc_raw_text(key=nil)
  if key
    ind=@@raw_key.index{|e| e==key or e=~/^\_\_#{key}/}
    #p (ind ? @@raw_text[ind][0] : nil)
    (ind ? @@raw_text[ind][0] : nil)
  else
    [@@raw_key,@@raw_text]
  end
end

.dyndoc_raw_text!(out, opt = {:clean=>nil,:once=>nil}) ⇒ Object

apply replacement in out



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/dyndoc/base/utils.rb', line 168

def Utils.dyndoc_raw_text!(out,opt={:clean=>nil,:once=>nil})
  @@raw_key.each_index do |i|
    #p [@@raw_key[i],@@raw_text[i]]
    #begin puts @@raw_key[i];p @@raw_text[i];puts @@raw_text[i]; end #if i==1
    @@raw_text[i][0].gsub!('\\\\','\\\\\\\\\\')
    out.gsub!(@@raw_key[i],@@raw_text[i][0]) #if the result is placed into a dyn variable, it can be repeated!
    @@raw_text[i][0]="" if opt[:once] #used only once!!! Ex: used by raw.
  end
  @@raw_text,@@raw_key=[],[] if opt[:clean]
  ##puts "dyndoc_raw:out";puts out
  return out
end

.dyndoc_raw_text_add(raw_text, key = nil, gen_key = true) ⇒ Object

add a raw text



149
150
151
152
153
154
# File 'lib/dyndoc/base/utils.rb', line 149

def Utils.dyndoc_raw_text_add(raw_text,key=nil,gen_key=true)
  @@raw_key << (key=(gen_key ? Utils.dyndoc_raw_text_key(key,raw_text=~/\r?\n/) :  key ))
  @@raw_text << [raw_text] #like a pointer!
  #puts "dyndoc_raw_text:key";p key
  key
end

.dyndoc_raw_text_key(key = nil, multilines = nil) ⇒ Object

multilines is for @verbatim which behaves differently depending on the number of lines in the content when the key replaces the content this solves the problem!



144
145
146
# File 'lib/dyndoc/base/utils.rb', line 144

def Utils.dyndoc_raw_text_key(key=nil,multilines=nil)
  "__"+(key=( key ? key : "" ))+"|"+Utils.uuidgen+"__"+(multilines ? "\n__"+key+"__" : "")
end

.dyndoc_raw_var_content(var, tmplMngr = nil) ⇒ Object



262
263
264
265
266
267
268
269
# File 'lib/dyndoc/base/utils.rb', line 262

def Utils.dyndoc_raw_var_content(var,tmplMngr=nil)
  return ""  unless tmplMngr
  #puts "var_content";p var+".content"
  #p tmplMngr.filter.envir.global["dyn"]
  #p tmplMngr.filterGlobal.envir
  Utils.dyndoc_raw_var_save(var,tmplMngr) if !tmplMngr.filterGlobal.envir[var+".eval"] or Utils.saved_content_to_be_recreated(tmplMngr).include? var
  tmplMngr.filterGlobal.envir[var+".eval"]
end

.dyndoc_raw_var_eval(var, tmplMngr = nil) ⇒ Object

var differs from key since it is saved in filter!



245
246
247
248
249
# File 'lib/dyndoc/base/utils.rb', line 245

def Utils.dyndoc_raw_var_eval(var,tmplMngr=nil) #var differs from key since it is saved in filter!
  return "" unless tmplMngr
  #p var
  return ((@@raw_var_ls.include? var) ? tmplMngr.parse(tmplMngr.filterGlobal.envir[var+".code"]) : "" )
end

.dyndoc_raw_var_lsObject



241
242
243
# File 'lib/dyndoc/base/utils.rb', line 241

def Utils.dyndoc_raw_var_ls
  @@raw_var_ls
end

.dyndoc_raw_var_save(var, tmplMngr = nil) ⇒ Object



251
252
253
254
255
256
257
258
259
260
# File 'lib/dyndoc/base/utils.rb', line 251

def Utils.dyndoc_raw_var_save(var,tmplMngr=nil)
  return  unless tmplMngr
  #puts "raw_var_save_content";p var+".content"
  envir=tmplMngr.filterGlobal.envir
  content=Utils.dyndoc_raw_var_eval(var,tmplMngr)
  #p content
  envir[var+".eval"]=content
  #for next compilation
  Utils.saved_content_add_as_variable(var+".eval",content,tmplMngr.filename)
end

.end_line(key, code) ⇒ Object



71
72
73
74
75
76
# File 'lib/dyndoc/base/utils.rb', line 71

def Utils.end_line(key,code)
  while key[-1,1]=="\\"
    key=(key[0...-1]+(code.shift)).strip
  end
  return [key,code]
end

.escape(str, chars) ⇒ Object



57
58
59
# File 'lib/dyndoc/base/utils.rb', line 57

def Utils.escape(str,chars)
  str.gsub(/#{Regexp.escape(chars[0])}/,chars[1])
end

.escape!(str, chars_set) ⇒ Object



61
62
63
# File 'lib/dyndoc/base/utils.rb', line 61

def Utils.escape!(str,chars_set)
  chars_set.each{|chars| str.gsub!(/#{Regexp.escape(chars[0])}/,chars[1]) }
end

.escape_delim!(str, mode = :last) ⇒ Object



65
66
67
68
69
# File 'lib/dyndoc/base/utils.rb', line 65

def Utils.escape_delim!(str,mode=:last)
  chars_set=(mode==:first ? CHARS_SET_FIRST : CHARS_SET_LAST )
  chars_set.each{|chars| str.gsub!(/#{Regexp.escape(chars[0])}/,chars[1]) }
  return str
end

.format_blocktext(str) ⇒ Object



101
102
103
# File 'lib/dyndoc/base/utils.rb', line 101

def Utils.format_blocktext(str)
  str.gsub(/\n[ \t\r\f]*\|/,"\n").gsub(/\|\n/,"").gsub("<\\n>","\n").gsub("<\\t>","\t")
end

.format_call_without_param(code) ⇒ Object

the scanner converts automatically #toto# in #toto][# and @toto@ in @toto][# this function does the convert when needed for verbatim or code.



127
128
129
# File 'lib/dyndoc/base/utils.rb', line 127

def Utils.format_call_without_param(code)
  code.gsub(/\{(\#|\@)(\w*)\]\[\#\}/,'{\1\2\1}')
end

.format_call_without_param!(code) ⇒ Object



131
132
133
# File 'lib/dyndoc/base/utils.rb', line 131

def Utils.format_call_without_param!(code)
  code.gsub!(/\{(\#|\@)(\w*)\]\[\#\}/,'{\1\2\1}')
end

.is_windows?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/dyndoc/common/utils.rb', line 57

def Utils.is_windows?
	RUBY_PLATFORM =~ /(win|msys|mingw)/
end

.lib_file_exists?(tmpl) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/dyndoc/common/utils.rb', line 26

def Utils.lib_file_exists?(tmpl)
  name=File.join(File.dirname(tmpl),File.basename(tmpl,".*"))
  (["_lib.dyn",".dyn_lib"].map{|ext| name+ext}.select{|f| File.exists? f})[0]
end

.make_doc(txt) ⇒ Object



36
37
38
39
40
41
# File 'lib/dyndoc/base/utils.rb', line 36

def Utils.make_doc(txt)
  out=txt.split("\n")
  ## Ajout  surement du à kile qui rajoutte des "\r" => générant un bug dans R
  out.map!{|e| (e[-1,1]=="\r" ? e[0...-1] : e )}
  return out
end

.mkdir_out_rsrc(tmpl) ⇒ Object



37
38
39
40
41
# File 'lib/dyndoc/common/utils.rb', line 37

def Utils.mkdir_out_rsrc(tmpl)
	require 'fileutils'
	#p File.join(File.dirname(tmpl),File.basename(tmpl,".*")+".dyn_out")
	FileUtils.mkdir_p File.join(File.dirname(tmpl),File.basename(tmpl,".*")+".dyn_out")
end

.out_rsrc_exists?(tmpl) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/dyndoc/common/utils.rb', line 31

def Utils.out_rsrc_exists?(tmpl)
  name=File.join(File.dirname(tmpl),File.basename(tmpl,".*"))
  ## if not a directory it is the zipped version! TODO! 
  ([".dyn_out",".dyn_rsrc"].map{|ext| name+ext}.select{|f| File.exists? f})[0]
end

.parse_dyn_block_for_interactive!(txt) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/dyndoc/base/utils.rb', line 349

def Utils.parse_dyn_block_for_interactive!(txt)
  #Dyndoc.warn "parse_dyn_block_for_atom",txt
  filter=/(?:(\{\#dyn>\])|(\[\#dyn>\}))/m
  txt2=txt.split(filter,-1)
  return if txt2.length==1
  #Dyndoc.warn "parse:txt2",txt2
  code=""
  while txt2.length>1
    if txt2[0]=="{#dyn>]" and txt2[1..-1].include? "[#dyn>}"
      start,tmp,stop=txt2.shift(3)
      ## protect the dyndoc code to delay the evaluation after unprotection (in javascript)
      code << Utils.protect_dyn_block_for_atom(tmp) ##BIZARRE! .inspect)
    else
      code << txt2.shift
    end
  end
  code << txt2.join("") #the remaining code
  #Dyndoc.warn "atom",code
  txt.replace(code)
end

.parse_raw_text!(txt, tmplMngr = nil) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/dyndoc/base/utils.rb', line 197

def Utils.parse_raw_text!(txt,tmplMngr=nil)
  #puts "parse_raw_text:";p txt
  filter=/(?:(\{\#code\]|\{\#raw\])(\s*[\w\.\-_:]+\s*)(\[\#(?:dyn|R|r|ruby|rb)(?:\>|\<)?\]\n?)|(\[\#code\}|\[\#raw\}))/m
  txt2=txt.split(filter,-1)
  return if txt2.length==1
  #Dyndoc.warn "parse:txt2",txt2
  code=""
  while txt2.length>1
    if RAW_TAGS.keys.include? txt2[0] and txt2[1..-1].include? RAW_TAGS[txt2[0]]
      tag,name,lang=txt2.shift(3)
      name=~/\s*([\w\.\-_:]+)\s*/
      name=$1
      lang=~/\[\#(dyn|R|r|ruby|rb)(\>|\<)?\]/
      lang,type=$1,$2
      lang="dyn" unless lang
      lang=RAW_LANG[lang] if RAW_LANG[lang]
      type="<" unless type
      code2=""
      code2 << txt2.shift until txt2[0]==RAW_TAGS[tag]
      #puts "parse_raw_text:name,type,mode,code";p [name,type,lang,code2]
      key=Utils.dyndoc_raw_text_add(code2,name+"-"+lang)
      #puts "parse_raw_text:key added";p key
      code << key if type==">"
      if tmplMngr
        ## puts inside global envir!
        envir=tmplMngr.filterGlobal.envir
        envir[lang+"."+name+".name"]=key
        envir[lang+"."+name+".code"]=@@raw_text[-1]
        ## "content" would be the name of the result after evaluation and saved in <dyn_basename_file>.dyn_out/raw_code.dyn
      end
      @@raw_var_ls << lang+"."+name #
      txt2.shift #last close tag!
    else
      code << txt2.shift
    end
  end
  code << txt2.join("") #the remaining code

  ##OLD: @@raw_key_index=@@raw_key.map{|key| key=~/\_\_(.*)\|(.*)/ ? $1 : nil}.compact
  ##puts "code";p code
  txt.replace(code)

end

.preserve_pattern(line, pattern, chars) ⇒ Object



15
16
17
# File 'lib/dyndoc/base/utils.rb', line 15

def Utils.preserve_pattern(line,pattern,chars)
    line.gsub(pattern){|s| Utils.escape(s,chars)}
end

.protect_blocktext(str, seq = "__STR__") ⇒ Object

To consider first and last whitespaces put before and after an escaped sequence! Before the string is formatted for indentation convenience!



87
88
89
90
91
92
93
# File 'lib/dyndoc/base/utils.rb', line 87

def Utils.protect_blocktext(str,seq="__STR__")
	#str=code+str if str[0,1]==" "
	#str=str+code if str[-1,1]==" "
#puts "protect_blocktext";p str
#puts "format_blocktext";p Utils.format_blocktext(str)
  seq+Utils.format_blocktext(str)+seq
end

.protect_dyn_block_for_atom(txt) ⇒ Object

Added for atom



341
342
343
# File 'lib/dyndoc/base/utils.rb', line 341

def Utils.protect_dyn_block_for_atom(txt)
  txt.gsub("#","<_DIESE_ATOM_>").gsub("@","<_AROBAS_ATOM_>").gsub(":","<_SEMICOLON_ATOM_>").gsub("|","<_BAR_ATOM_>").gsub("$>","<_RVAR_ATOM_>") # => since dyndoc command uses "#" this is very easy way to protect evaluation
end

.protect_extraction(str) ⇒ Object



121
122
123
# File 'lib/dyndoc/base/utils.rb', line 121

def Utils.protect_extraction(str)
  str.gsub(/(?:\#|\:dyn|\#\#|@|#F|#R|#r|\:R|\:r|#Rb|#rb|\:|\:Rb|\:rb)+\{/) {|e| "\\"+e}
end

.protect_format_blocktext(str) ⇒ Object

called in #txt to protect the corresponding blocktext



106
107
108
# File 'lib/dyndoc/base/utils.rb', line 106

def Utils.protect_format_blocktext(str)
   str.gsub(/\n[ \t\r\f]*\|/) {|e| e.gsub(/\|/,"__BLOCKBAR__")}.gsub(/\|\n/,"__BLOCKBAR__\n").gsub("<\\n>","__BLOCKSPACE__").gsub("<\\t>","__BLOCKTAB__")
end

.protect_txt_block(str) ⇒ Object



114
115
116
117
118
119
# File 'lib/dyndoc/base/utils.rb', line 114

def Utils.protect_txt_block(str)
  str2=Utils.protect_format_blocktext(str)
  Utils.escape!(str2,[["{","_{_"],["}","_}_"]])
  Utils.escape_delim!(str2,:first)
  str2
end

.raw_code_to_processObject



191
192
193
# File 'lib/dyndoc/base/utils.rb', line 191

def Utils.raw_code_to_process
  return @@raw_code_to_process
end

.raw_code_to_process=(state = nil) ⇒ Object



187
188
189
# File 'lib/dyndoc/base/utils.rb', line 187

def Utils.raw_code_to_process=(state=nil)
    @@raw_code_to_process=state
end

.saved_content_add_as_variable(var, result, filename) ⇒ Object

var is dyndoc variable



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/dyndoc/base/utils.rb', line 297

def Utils.saved_content_add_as_variable(var,result,filename) #var is dyndoc variable
  ##p filename
  out_rsrc=Dyndoc::Utils.mkdir_out_rsrc(filename)
  unless File.exists? File.join(out_rsrc,SAVED_CONTENTS_FILE)
    File.open(File.join(out_rsrc,SAVED_CONTENTS_FILE),"a") do |f|
      f << "[#%] File automatically generated! Remove it for regenerating it!\n"
    end
  end
  ## if it alread exist, delete it first!
  Utils.saved_content_delete_as_variable(var,filename)
  File.open(File.join(out_rsrc,SAVED_CONTENTS_FILE),"a") do |f|
    f << "[#=]"+var+"["+result+"]\n"
  end
  @@saved_content_ls << var
end

.saved_content_delete_as_variable(var, filename) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/dyndoc/base/utils.rb', line 313

def Utils.saved_content_delete_as_variable(var,filename)
  return unless out_rsrc=Dyndoc::Utils.out_rsrc_exists?(filename)
  return unless File.exists?(saved_contents_file=File.join(out_rsrc,SAVED_CONTENTS_FILE))
  saved_contents=File.read(saved_contents_file)
  ## Normally, no problem since no [#=] inside result!
  saved_contents_new=saved_contents.gsub(/\[\#\=\]\s*#{var}\s*\[.*\]\s*\[\#\=\]/m,"[#=]")
  unless saved_contents==saved_contents_new
    File.open(File.join(out_rsrc,SAVED_CONTENTS_FILE),"w") do |f|
      f << saved_contents_new
    end
    @@saved_content_ls.delete(var)
  end
end

.saved_content_fetch_variables_from_file(filename, tmplMngr = nil) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/dyndoc/base/utils.rb', line 273

def Utils.saved_content_fetch_variables_from_file(filename,tmplMngr=nil)
  return unless tmplMngr
  #p filename
  return unless out_rsrc=Dyndoc::Utils.out_rsrc_exists?(filename)
  return unless File.exists?(saved_contents_file=File.join(out_rsrc,SAVED_CONTENTS_FILE)) ##normally, autogenerated!
  #p out_rsrc
  ## fetch the contents by reading and parsing unsing the global filter!
  #puts "saved_contents_file";p saved_contents_file
  code="{#document][#main]"+File.read(saved_contents_file)+"[#}"
  tmplMngr.parse(code,tmplMngr.filterGlobal)
  #puts "fetch var:dyn";p tmplMngr.filter.envir.global["dyn"]
end

.saved_content_get(var, tmplMngr = nil, force = nil) ⇒ Object



331
332
333
334
335
336
337
338
# File 'lib/dyndoc/base/utils.rb', line 331

def Utils.saved_content_get(var,tmplMngr=nil,force=nil)
  return unless tmplMngr
  if force
    return (Utils.saved_content_to_be_recreated(tmplMngr).include? var) ? nil : tmplMngr.filterGlobal.envir[var]
  else
    return tmplMngr.filterGlobal.envir[var]
  end
end

.saved_content_lsObject



327
328
329
# File 'lib/dyndoc/base/utils.rb', line 327

def Utils.saved_content_ls
  return @@saved_content_ls
end

.saved_content_to_be_recreated(tmplMngr) ⇒ Object



289
290
291
292
293
294
295
# File 'lib/dyndoc/base/utils.rb', line 289

def Utils.saved_content_to_be_recreated(tmplMngr)
  unless @@saved_content_to_be_recreated
    user_input=tmplMngr.filterGlobal.envir["_.EVAL"]
    @@saved_content_to_be_recreated=(user_input ? user_input.strip.split(",") : [])
  end
  @@saved_content_to_be_recreated
end

.silence_warnings(val = nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/dyndoc/base/utils.rb', line 29

def Utils.silence_warnings(val=nil)
  old_verbose, $VERBOSE = $VERBOSE, val
  yield
ensure
  $VERBOSE = old_verbose
end

.split_code(code, pattern = /@\{.*\}/, chars = ["|","@@@"]) ⇒ Object

used in [#var] and [#set] protect “|” inside @toto(…)



21
22
23
24
25
26
27
# File 'lib/dyndoc/base/utils.rb', line 21

def Utils.split_code(code,pattern=/@\{.*\}/,chars=["|","@@@"])
  inst=Utils.preserve_pattern(code,pattern,chars)
  inst,*b=inst.split(/\n\-{3,}\n/)
  b=inst.strip.split("\n").map{|e| e.split(chars[0])}.flatten+b if inst
  b.map!{|l| Utils.preserve_pattern(l,pattern,chars.reverse)}
  return b
end

.split_code_by_sep(code, sep = "|") ⇒ Object

variable sep!



11
12
13
# File 'lib/dyndoc/base/utils.rb', line 11

def Utils.split_code_by_sep(code,sep="|")
  return code.split("\n").map{|e| e.split(sep)}.flatten
end

.unprotect_blocktext(str, seq = "__STR__") ⇒ Object

When considered, remove the escaped sequence!



96
97
98
99
# File 'lib/dyndoc/base/utils.rb', line 96

def Utils.unprotect_blocktext(str,seq="__STR__")
#puts "unprotect blocktext";p str
  str.gsub(seq,"")
end

.unprotect_dyn_block_for_atom(txt) ⇒ Object



345
346
347
# File 'lib/dyndoc/base/utils.rb', line 345

def Utils.unprotect_dyn_block_for_atom(txt)
  txt.gsub("<_DIESE_ATOM_>","#").gsub("<_AROBAS_ATOM_>","@").gsub("<_SEMICOLON_ATOM_>",":").gsub("<_BAR_ATOM_>","|").gsub("<_RVAR_ATOM_>","$>") # => since dyndoc command uses "#" this is very easy way to protect evaluation
end

.unprotect_format_blocktext(str) ⇒ Object



110
111
112
# File 'lib/dyndoc/base/utils.rb', line 110

def Utils.unprotect_format_blocktext(str)
  str.gsub("__BLOCKBAR__","|").gsub("__BLOCKSPACE__","<\\n>").gsub("__BLOCKTAB__","<\\t>").gsub("__RAW__","")
end

.uuidgenObject



135
136
137
# File 'lib/dyndoc/base/utils.rb', line 135

def Utils.uuidgen
  `uuidgen`.strip
end