Module: Glot
- Defined in:
- lib/glot.rb,
lib/glot/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'0.1.0'
Class Method Summary collapse
- .extract_reference_locations(content) ⇒ Object
- .find(sym, files) ⇒ Object
- .find_in(sym, hash) ⇒ Object
- .get(str, files) ⇒ Object
- .get_in_hash(str, hash) ⇒ Object
- .prop(content, &block) ⇒ Object
- .reapply_references(content, taggedhash) ⇒ Object
- .set(str, files, val, replace: false) ⇒ Object
- .set_in_hash(str, hash, val, replace: false) ⇒ Object
Class Method Details
.extract_reference_locations(content) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/glot.rb', line 34 def extract_reference_locations(content) references = {} refregex = /(?<objname>:?[a-z0-9\-_]+) *: *&(?<refname>[a-z_]+)/ prop(content) do |tree, line| refregex.match(line) do |m| references[tree.join('.')] = m[:refname] end end references end |
.find(sym, files) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/glot.rb', line 102 def find(sym, files) res = [] files.each do |_k, v| find_in(sym, v) do |x| res << x end end res end |
.find_in(sym, hash) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/glot.rb', line 92 def find_in(sym, hash) hash.deep_traverse do |k, v, _s| if v.is_a?(Hash) if v.key?(sym.to_s) yield path: (k + [sym]).join('.'), v: v[sym.to_s].is_a?(Hash) ? Hash : v[sym.to_s] end end end end |
.get(str, files) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/glot.rb', line 112 def get(str, files) res = {} files.each do |k, v| dstr = str.sub(/^(#{k}\.)/, '') res[k] = get_in_hash(dstr, v[k.to_s]) end res end |
.get_in_hash(str, hash) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/glot.rb', line 74 def get_in_hash(str, hash) if str.include?('.') key = str[/[^\.]+/] inner_hash = hash[key] if !inner_hash nil else get_in_hash(str.sub("#{key}.", ''), inner_hash) end else if hash[str].is_a?(Hash) Hash else hash[str] end end end |
.prop(content, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/glot.rb', line 10 def prop(content, &block) tree = [] indent_counter = /^(?<indent>[ ]+)(?<objname>:?[a-z0-9\-_]+) *:/ content.split("\n").each do |line| im = indent_counter.match(line) if im if im[:indent].length.even? pos = im[:indent].length / 2 if tree.length < pos tree += [im[:objname]] elsif tree.length > pos tree = tree[0...pos] end tree[-1] = im[:objname] else p line end end yield tree, line if block end end |
.reapply_references(content, taggedhash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/glot.rb', line 47 def reapply_references(content, taggedhash) idx_to_ref = {} lines = [] decl_regex = /: *&(?<refnum>[0-9]+)/ ref_regex = /: *\*(?<refnum>[0-9]+)/ prop(content) do |tree, line| theline = line decl_regex.match(line) do |m| idx_to_ref[m[:refnum]] = taggedhash.reference_names[tree.join('.')] theline.sub!(/\&[0-9]+/, "&#{idx_to_ref[m[:refnum]]}") end ref_regex.match(line) do |m| if idx_to_ref[m[:refnum]] theline.sub!(/\*[0-9]+/, "*#{idx_to_ref[m[:refnum]]}") end end lines << theline end lines.join("\n") end |
.set(str, files, val, replace: false) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/glot.rb', line 141 def set(str, files, val, replace: false) files.each do |k, v| dstr = str.sub(/^(#{k}\.)/, '') p v.keys p dstr set_in_hash(dstr, v[k.to_s], val, replace: replace) end end |
.set_in_hash(str, hash, val, replace: false) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/glot.rb', line 121 def set_in_hash(str, hash, val, replace: false) if str.include?('.') key = str[/[^\.]+/] inner_hash = hash[key] if !inner_hash if hash.is_a?(Hash) && (replace || inner_hash.nil?) hash[key] = {} set_in_hash(str.sub("#{key}.", ''), hash[key], val, replace: replace) end else set_in_hash(str.sub("#{key}.", ''), inner_hash, val, replace: replace) end else if hash.is_a?(Hash) && !hash[str].is_a?(Hash) && (hash[str].nil? || replace) hash[str] = val end end end |