@@ -18,63 +18,94 @@ def self.key_to_str(k)
1818 return k
1919 end
2020
21- def self . value_to_str ( v )
21+ def self . hash_to_str ( h , indent = 0 )
22+ result = [ ]
23+ h . each do |k , v |
24+ case v
25+ when Hash , Mash
26+ result << k + " {"
27+ result << hash_to_str ( v , indent )
28+ else
29+ indent += 4
30+ result << indent ( indent ) + key_value_to_str ( k , v , indent )
31+ indent -= 4
32+ end
33+ end
34+ result . join ( "\n " )
35+ end
36+
37+ def self . value_to_str ( v , indent = 0 )
2238 case v
2339 when String , Symbol , Fixnum , Float
24- "' #{ v } ' "
40+ "\" #{ v } \" "
2541 when Array
26- "[#{ v . map { |e | value_to_str e } . join ( ", " ) } ]"
42+ "[#{ v . map { |e | value_to_str ( e , indent ) } . join ( ", " ) } ]"
2743 when Hash , Mash
28- value_to_str ( v . to_a . flatten )
44+ hash_to_str ( v , indent ) + " \n " + indent ( indent + 4 ) + "}"
2945 when TrueClass , FalseClass
3046 v . to_s
3147 else
3248 v . inspect
3349 end
3450 end
3551
36- def self . key_value_to_str ( k , v )
52+ def self . key_value_to_str ( k , v , indent = 0 )
3753 if !v . nil?
38- key_to_str ( k ) + ' => ' + value_to_str ( v )
54+ #k.inspect + " => " + v.inspect
55+ key_to_str ( k ) + ' => ' + value_to_str ( v , indent )
3956 else
4057 key_to_str ( k )
4158 end
4259 end
4360
44- def self . plugin_to_arr ( plugin , patterns_dir_plugins = nil , patterns_dir = nil ) # , type_to_condition)
61+ def self . plugin_to_arr ( plugin , patterns_dir_plugins = nil , patterns_dir = nil , indent = 0 ) # , type_to_condition)
4562 result = [ ]
4663 plugin . each do |name , hash |
4764# result << ''
4865# result << " if [type] == \"#{hash['type']}\" {" if hash.has_key?('type') and type_to_condition
49- result << ' ' + name . to_s + ' {'
50- result << ' ' + key_value_to_str ( 'patterns_dir' , patterns_dir ) if patterns_dir_plugins . include? ( name . to_s ) && !patterns_dir . nil? && !hash . key? ( 'patterns_dir' )
66+ indent += 4
67+ result << indent ( indent ) + name . to_s + ' {'
68+ result << indent ( indent ) + key_value_to_str ( 'patterns_dir' , patterns_dir , indent ) if patterns_dir_plugins . include? ( name . to_s ) && !patterns_dir . nil? && !hash . key? ( 'patterns_dir' )
5169 hash . sort . each do |k , v |
5270# next if k == 'type' and type_to_condition
53- result << ' ' + key_value_to_str ( k , v )
71+ indent += 4
72+ result << indent ( indent ) + key_value_to_str ( k , v , indent )
73+ indent -= 4
5474 end
55- result << ' }'
75+ result << indent ( indent ) + '}'
76+ indent -= 4
5677# result << ' }' if hash.has_key?('type') and type_to_condition
5778 end
5879 return result . join ( "\n " )
5980 end
6081
6182 public
6283
63- def self . section_to_str ( section , version = nil , patterns_dir = nil )
84+ def self . section_to_str ( section , version = nil , patterns_dir = nil , indent = 0 )
6485 result = [ ]
6586 patterns_dir_plugins = [ 'grok' ]
6687 patterns_dir_plugins << 'multiline' if Gem ::Version . new ( version ) >= Gem ::Version . new ( '1.1.2' ) unless version . nil?
6788# type_to_condition = Gem::Version.new(version) >= Gem::Version.new('1.2.0')
6889 section . each do |segment |
6990 result << ''
7091 if segment . key? ( 'condition' ) || segment . key? ( 'block' )
71- result << ' ' + segment [ 'condition' ] + ' {' if segment [ 'condition' ]
72- result << plugin_to_arr ( segment [ 'block' ] , patterns_dir_plugins , patterns_dir )
73- result << ' }' if segment [ 'condition' ]
92+ indent += 4
93+ result << indent ( indent ) + segment [ 'condition' ] + ' {' if segment [ 'condition' ]
94+ result << plugin_to_arr ( segment [ 'block' ] , patterns_dir_plugins , patterns_dir , indent )
95+ result << indent ( indent ) + '}' if segment [ 'condition' ]
96+ indent -= 4
7497 else
75- result << plugin_to_arr ( segment , patterns_dir_plugins , patterns_dir ) # , type_to_condition)
98+ indent += 4
99+ result << plugin_to_arr ( segment , patterns_dir_plugins , patterns_dir , indent ) # , type_to_condition)
100+ indent -= 4
76101 end
77102 end
78103 return result . join ( "\n " )
79104 end
80105end
106+
107+ def indent ( i )
108+ res = String . new
109+ i . times { res << " " }
110+ res
111+ end
0 commit comments