Module: Ontology

Defined in:
lib/ontology.rb,
lib/ontology/leaf.rb,
lib/ontology/write_file.rb,
lib/ontology/yaml_utils.rb,
lib/ontology/error_message.rb,
lib/ontology/make_glossary.rb,
lib/ontology/load_config_yaml.rb

Defined Under Namespace

Classes: Leaf

Constant Summary collapse

Lang_block =
Proc.new {|hash, lang|  if hash.keys.include?(:any) then hash[:any] end}
Main_block =
Proc.new {|hash, arg|  if hash.keys.include?(:title) and arg == :order then hash[:title] end}
Def_List =
[Main_block, Lang_block]

Instance Method Summary collapse

Instance Method Details

#any_language_ize(leaf, key_as_symbol) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/ontology/yaml_utils.rb', line 66

def any_language_ize(leaf, key_as_symbol)
  leaf.each{|key, val| if key == key_as_symbol and val.class == String then
    h = Hash.new(&Lang_block)
    h[:any] = val
    leaf[key] = h
  end
  }
end

#build_glossary(gloss_symbol) ⇒ Object

?? test_file = ‘/Users/mathias/Server/Projets/mflu_nanoc/mflu/work-progs/glossary/secondary-currency.md’



107
108
109
110
111
112
113
114
115
116
# File 'lib/ontology/make_glossary.rb', line 107

def build_glossary(gloss_symbol)
  gloss_dir = $data_symb[:ontology][gloss_symbol][:source]
  Dir.foreach(gloss_dir) {  |f|
    unless (f == '.') or (f == '..')
      full_file_name = Base_dir + '/' + gloss_dir +'/' + f 
      #puts full_file_name
      treat_glossary_file full_file_name, gloss_symbol #?? if full_file_name == test_file
    end
  }
end

#check_config_yamlObject



9
10
11
12
# File 'lib/ontology/load_config_yaml.rb', line 9

def check_config_yaml
  error_message_abort("The configuration file #{$options[:config_file]} has been found, however it does not have an ontology section. Ontology cannot work, Aborting !") unless $data_symb[:ontology]
  error_message_abort("No languages defined under ontology: languages: . Ontology cannot work, Aborting !")  if $data_symb[:ontology][:languages].nil?
end

#decompose_as_multi_lang_md(str) ⇒ Object



41
42
43
44
45
46
# File 'lib/ontology/make_glossary.rb', line 41

def decompose_as_multi_lang_md(str)
  list_of_language_parts = str.scan(/\[(\w+)\](.*?)(?=(\[(\w+)\]|\z))/m)
#    puts "decompose_as_multi_lang_md", str, '!!!!!!!!!!!!!!!!!!!!!!', list_of_language_parts, '!!!!!!!!!!!!!', list_of_language_parts.length
  raise "not multi-language" if list_of_language_parts.length == 0
  list_of_language_parts.inject(Hash.new(&Lang_block)){|result, arg| result[arg[0].to_sym] = arg[1]; result}
end

#decompose_as_yaml_conf(str) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/ontology/make_glossary.rb', line 30

def decompose_as_yaml_conf(str)
#    begin  ??????????????????????????????????????
#      puts "before!!!!!!!!!!!!!!decompose_as_yaml_conf #{str}"
    wrk_yaml = YAML.load(str)
#    rescue
    puts "****!!!!!!!!!!!!!!decompose_as_yaml_conf #{str}"
#    end
  raise "not a yaml"  unless wrk_yaml.class == Hash
  to_symbol(wrk_yaml, Def_List.clone)
end

#error_message(msg, exception = nil) ⇒ Object



11
12
13
14
15
16
# File 'lib/ontology/error_message.rb', line 11

def error_message(msg, exception = nil)
  STDERR.puts msg
  if exception
    raise exception
  end
end

#error_message_abort(msg, exception = nil) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/ontology/error_message.rb', line 3

def error_message_abort(msg, exception = nil)
  STDERR.puts msg
  if exception
    raise exception
  end
  exit 1
end

#generate_leaf_init_struct(file_as_str) ⇒ Object



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
# File 'lib/ontology/make_glossary.rb', line 61

def generate_leaf_init_struct(file_as_str)
  elt_ant = nil
  iterate_yaml_part(file_as_str) {|str_elt|
    begin
      begin
        x_elt_ant = decompose_as_yaml_conf(str_elt)
        unless elt_ant.nil?
          #puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!yield"
          yield elt_ant
          elt_ant = nil
        end
        elt_ant = x_elt_ant
        elt_ant[:content] = []
      rescue
        begin
          mark_down = decompose_as_multi_lang_md(str_elt)
        rescue 
          mark_down = Hash.new(&Lang_block)
          mark_down[:any] = str_elt
        end
        elt_ant[:content] << mark_down
      end
    rescue
      STDERR.puts "Aborting:"
      puts file_as_str
      exit 1
    end
  }
  unless elt_ant.nil?
    yield elt_ant
  end
end

#iterate_yaml_part(file_as_str) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ontology/make_glossary.rb', line 48

def iterate_yaml_part(file_as_str)
#    list_of_yaml_files = file_as_str.scan(/^---$(.*?)(?=(^---$|\z))/m)  initial version 
#    list_of_yaml_files = file_as_str.scan(/^---[\f\t ]*$(.*?)(?=(^---[\f\t ]*$|\z))/m)     better

  list_of_yaml_files = file_as_str.scan(/^---[\f\t ]*$(.*?)(?=(^---[\f\t ]*$|\z))/m)

  #puts '***', list_of_yaml_files.class, list_of_yaml_files.length, list_of_yaml_files.inspect
  list_of_yaml_files.each {|elt|
    #puts "test ===  #{/\A[\f\t \r]*\z/m  =~ elt[0]} , #{elt[0]}"
    yield elt[0] unless /\A[\s\t\r\n\f]*\z/m  =~ elt[0]
  }
end

#load_config_pars(str) ⇒ Object



3
4
5
6
7
8
# File 'lib/ontology/load_config_yaml.rb', line 3

def load_config_pars(str)
  data = YAML.load(str)  
  $data_symb = to_symbol(data)
  symbolize_for_this_key($data_symb, :default_language)
  check_config_yaml
end

#symbolize_for_this_key(tree, this_key) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ontology/yaml_utils.rb', line 53

def symbolize_for_this_key(tree, this_key)
  if tree.class == Hash
    tree.each {|key, val| 
      if key == this_key and val.class == String then 
        tree[this_key] = val.to_sym
      end
      if val.class == Hash then
        symbolize_for_this_key(val, this_key)
      end
    }
  end
end

#to_symbol(x, hash_default_list = nil) ⇒ Object

to_symbol converts a hash (of hash’s) in the same strcuture replacing string keys by symbol. The routine is recursive



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ontology/yaml_utils.rb', line 21

def to_symbol(x, hash_default_list = nil)
  if x.class == Hash
    if ! hash_default_list.nil? && hash_default_list.class == Array && hash_default_list.length > 0
      default_function = hash_default_list[0]
      new_default_list = hash_default_list[1, 100]
      unless new_default_list.length > 0
        new_default_list = nil
      end
      hash_used = Hash.new(&default_function)
    else
      hash_used = Hash.new()
      new_default_list = nil
    end
    result = x.inject(hash_used) {|result, arg| 
      if arg.last.class == Hash || arg.last.class == Array
        result[arg.first.to_sym] = to_symbol(arg.last, new_default_list); 
      else
        result[arg.first.to_sym] = arg.last; 
      end
      result} 
  elsif x.class == Array
    begin
      result = x.inject([]) {|res, elt| raise '' unless elt.class == String; res << elt.to_sym}
    rescue
      result = x
    end
  else
    result = x
  end
  result
end

#treat_glossary_file(fname, gloss_symb) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/ontology/make_glossary.rb', line 94

def treat_glossary_file(fname, gloss_symb)
  file = File.open(fname, 'rb')
  content = file.read
  content.force_encoding("UTF-8")
  
  generate_leaf_init_struct(content){|hash_struct|
    leaf = Leaf.new(hash_struct, gloss_symb)
    leaf.export_markdowns
  } 
end

#type_of_leaf_part(str) ⇒ Object

type_of_leaf_part returns one of the following:

  • :is_yaml_conf

  • :is_multi_lang_md

  • :is_md

This choice is done by successive tests



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ontology/make_glossary.rb', line 16

def type_of_leaf_part(str)
  begin 
    decompose_as_yaml_conf(str)
    return :is_yaml_conf
  rescue
    begin
      decompose_as_multi_lang_md(str)
      return :is_multi_lang_md
    rescue
      return :is_md
    end
  end
end

#write_md_file(name, content) ⇒ Object



4
5
6
7
8
9
# File 'lib/ontology/write_file.rb', line 4

def write_md_file(name, content)
#    puts "#{name} with content #{content} will be written"
  f = File.open(name, 'wb')
  f.write(content)
  f.close
end