module Dyndoc::Utils

Constants

RAW_LANG
RAW_TAGS
SAVED_CONTENTS_FILE

Public Class Methods

cfg_file_exist?(tmpl) click to toggle source
# File lib/dyndoc/common/utils.rb, line 21
    def Utils.cfg_file_exist?(tmpl)
  name=File.join(File.dirname(tmpl),File.basename(tmpl,".*"))
  ([".cfg_dyn",".dyn_cfg"].map{|ext| name+ext}.select{|f| File.exist? f})[0]
end
clean_bom_utf8!(str) click to toggle source
# File lib/dyndoc/base/utils.rb, line 82
def Utils.clean_bom_utf8!(str)
  str.gsub!("\xEF\xBB\xBF", '')
end
clean_comment(b) click to toggle source

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

# 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) click to toggle source
# File lib/dyndoc/base/utils.rb, line 78
def Utils.clean_eol(str)
  str.gsub!("\r\n","\n")
end
clean_indent(b) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

find a raw text

# 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}) click to toggle source

apply replacement in out

# 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) click to toggle source

add a raw text

# 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) click to toggle source

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!

# 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) click to toggle source
# 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) click to toggle source
# 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_ls() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# File lib/dyndoc/base/utils.rb, line 131
def Utils.format_call_without_param!(code)
  code.gsub!(/\{(\#|\@)(\w*)\]\[\#\}/,'{\1\2\1}')
end
is_windows?() click to toggle source
# File lib/dyndoc/common/utils.rb, line 57
def Utils.is_windows?
        RUBY_PLATFORM =~ /(win|msys|mingw)/
end
lib_file_exist?(tmpl) click to toggle source
# File lib/dyndoc/common/utils.rb, line 26
def Utils.lib_file_exist?(tmpl)
  name=File.join(File.dirname(tmpl),File.basename(tmpl,".*"))
  (["_lib.dyn",".dyn_lib"].map{|ext| name+ext}.select{|f| File.exist? f})[0]
end
make_doc(txt) click to toggle source
# 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) click to toggle source
# 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_exist?(tmpl) click to toggle source
# File lib/dyndoc/common/utils.rb, line 31
def Utils.out_rsrc_exist?(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.exist? f})[0]
end
parse_dyn_block_for_interactive!(txt) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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__") click to toggle source

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

# 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) click to toggle source

Added for atom

# 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) click to toggle source
# 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) click to toggle source

called in txt to protect the corresponding blocktext

# 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) click to toggle source
# 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_process() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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.exist? 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) click to toggle source
# 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_exist?(filename)
  return unless File.exist?(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) click to toggle source
# 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_exist?(filename)
  return unless File.exist?(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) click to toggle source
# 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_ls() click to toggle source
# File lib/dyndoc/base/utils.rb, line 327
def Utils.saved_content_ls
  return @@saved_content_ls
end
saved_content_to_be_recreated(tmplMngr) click to toggle source
# 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) { || ... } click to toggle source
# 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=["|","@@@"]) click to toggle source

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

# 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="|") click to toggle source

variable sep!

# 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__") click to toggle source

When considered, remove the escaped sequence!

# 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) click to toggle source
# 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) click to toggle source
# 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
uuidgen() click to toggle source
# File lib/dyndoc/base/utils.rb, line 135
def Utils.uuidgen
  `uuidgen`.strip
end