ykutils 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -1
  3. data/.rubocop.yml +2 -1
  4. data/.rubocop_todo.yml +181 -85
  5. data/Gemfile +11 -11
  6. data/Gemfile.lock +17 -12
  7. data/bin/erubix +1 -1
  8. data/bin/makegrid +4 -4
  9. data/lib/ykutils/erubyx.rb +30 -19
  10. data/lib/ykutils/fileoputils.rb +2 -2
  11. data/lib/ykutils/gridlist.rb +49 -49
  12. data/lib/ykutils/lines.rb +143 -143
  13. data/lib/ykutils/lsutils.rb +71 -71
  14. data/lib/ykutils/nginxconfig.rb +40 -41
  15. data/lib/ykutils/nginxconfigfiles.rb +31 -31
  16. data/lib/ykutils/nkfutil.rb +148 -147
  17. data/lib/ykutils/nkfutil19.rb +9 -11
  18. data/lib/ykutils/osutil.rb +16 -16
  19. data/lib/ykutils/retcodex.rb +1 -3
  20. data/lib/ykutils/stext.rb +427 -425
  21. data/lib/ykutils/stextx.rb +32 -32
  22. data/lib/ykutils/stringutils.rb +111 -111
  23. data/lib/ykutils/treemanager.rb +33 -33
  24. data/lib/ykutils/treemanagera.rb +21 -21
  25. data/lib/ykutils/version.rb +1 -1
  26. data/lib/ykutils/xlines.rb +8 -8
  27. data/lib/ykutils/yamlop.rb +227 -227
  28. data/lib/ykutils/yamlxop.rb +35 -35
  29. data/test_data/4servers.erb +7 -7
  30. data/test_data/a.bat +5 -5
  31. data/test_data/a_ncn.bat +12 -12
  32. data/test_data/v103-3-189-127/a.northern-cross.net/base.yml +28 -28
  33. data/test_data/v103-3-189-127/a.northern-cross.net/value_host.yml +1 -1
  34. data/test_data/v103-3-189-127/b.northern-cross.net/base.yml +28 -28
  35. data/test_data/v103-3-189-127/b.northern-cross.net/value_host.yml +1 -1
  36. data/test_data/v103-3-189-127/c.northern-cross.net/base.yml +28 -28
  37. data/test_data/v103-3-189-127/c.northern-cross.net/value_host.yml +1 -1
  38. data/test_data/v103-3-189-127/d.northern-cross.info/base.yml +28 -28
  39. data/test_data/v103-3-189-127/d.northern-cross.info/value_host.yml +1 -1
  40. data/test_data/v103-3-189-127/d.northern-cross.net/base.yml +28 -28
  41. data/test_data/v103-3-189-127/d.northern-cross.net/value_host.yml +1 -1
  42. data/test_data/v103-3-189-127/e.northern-cross.info/base.yml +28 -28
  43. data/test_data/v103-3-189-127/e.northern-cross.info/value_host.yml +1 -1
  44. data/test_data/v103-3-189-127/e.northern-cross.net/base.yml +28 -28
  45. data/test_data/v103-3-189-127/e.northern-cross.net/value_host.yml +1 -1
  46. data/test_data/v103-3-189-127/f.northern-cross.info/base.yml +28 -28
  47. data/test_data/v103-3-189-127/f.northern-cross.info/value_host.yml +1 -1
  48. data/test_data/v103-3-189-127/f.northern-cross.net/base.yml +28 -28
  49. data/test_data/v103-3-189-127/f.northern-cross.net/value_host.yml +1 -1
  50. data/test_data/v103-3-189-127/t_server_1.erb +7 -7
  51. data/test_data/v103-3-189-127/template.erb +21 -21
  52. data/test_data/v103-3-189-127/template_ssl.erb +22 -22
  53. data/test_data/v103-3-189-127/template_ssl_www.erb +12 -12
  54. data/test_data/v103-3-189-127/template_www.erb +24 -24
  55. data/test_data/v103-3-189-127/value.yml +3 -3
  56. data/test_data/v103-3-189-127/value_ssl.yml +5 -5
  57. data/ykutils.gemspec +45 -43
  58. metadata +8 -9
  59. data/_config.yml +0 -1
data/lib/ykutils/stext.rb CHANGED
@@ -1,425 +1,427 @@
1
- # -*- coding utf-8 -*-
2
-
3
- require "ykutils/debugutils"
4
- require "ykutils/lines"
5
- require "ykutils/specfileop"
6
- require "ykutils/datastructop"
7
- require "filex"
8
-
9
- module Ykutils
10
- class StructuredText
11
- attr_reader :main_ary, :main_hash, :item_ary
12
-
13
- include SpecFileOp
14
- include DataStructOp
15
- include DebugUtils
16
-
17
- def initialize(debug = false)
18
- @text = nil
19
- @fname = nil
20
- @text_ary = []
21
-
22
- debug_utils_init
23
- set_debug(debug)
24
- end
25
-
26
- def load_analyze(fname, subfname = nil)
27
- # puts_current_method
28
- # puts "-0 fname=#{fname}"
29
-
30
- @fname = fname
31
- @text_ary = load(fname)
32
- # @text_ary.each do |x| puts x end
33
- analyze(@text_ary, subfname)
34
- end
35
-
36
- def load(fname)
37
- load_plain_text_file(fname).collect { |it| it.chomp }
38
- end
39
-
40
- def analyze(line_ary, subfname = nil); end
41
-
42
- def dump_to_file(fname)
43
- file = FileX.open(fname, "w") do |file|
44
- @text_ary.each do |l|
45
- file.write(l + "\n")
46
- end
47
- end
48
- rescue StandardError => e
49
- pp e
50
- pp e.backtrace
51
- @valid = false
52
- end
53
- end
54
-
55
- class StructuredTextForFtpOp < StructuredText
56
- def analyze(line_ary, fname = nil)
57
- lines = FtpOpLines.new(line_ary)
58
- lines.output_f(fname)
59
-
60
- analyze_sub(lines)
61
- end
62
- end
63
-
64
- class StructuredTextForAccount < StructuredText
65
- attr_reader :main_host_ary, :main_host_hash, :main_domain_ary, :main_domain_hash, :main_sep_ary,
66
- :main_sep_hash, :main_host_item_ary
67
-
68
- def initialize
69
- @main_host_item_ary = []
70
-
71
- @main_sep_ary = []
72
- @main_sep_hash = {}
73
-
74
- @main_host_ary = []
75
- @main_host_hash = {}
76
- 1
77
- @main_domain_ary = []
78
- @main_domain_hash = {}
79
- end
80
-
81
- def analyze(line_ary, fname = nil)
82
- lines = AccountLines.new(line_ary)
83
- lines.output_f(fname)
84
-
85
- analyze_sub(lines)
86
- end
87
-
88
- def analyze_sub(lines)
89
- host = StructuredTextForAccountOneLayerHost.new
90
- domain = StructuredTextForAccountOneLayerDomain.new
91
-
92
- while line = lines.get_line
93
- case line["STATUS"]
94
- when AccountLines::HOST_ACCOUNT_START, AccountLines::HOST_ACCOUNT, AccountLines::HOST_ACCOUNT_END
95
- key, value = line["CONTENT"].split(":") if line["CONTENT"]
96
- key.strip! if key
97
- value.strip! if value
98
- host.analyze(line["STATUS"], line, key, value, @main_host_ary, @main_host_hash)
99
- when AccountLines::DOMAIN_ACCOUNT_START, AccountLines::DOMAIN_ACCOUNT, AccountLines::DOMAIN_ACCOUNT_END
100
- key, value = line["CONTENT"].split(":") if line["CONTENT"]
101
- key.strip! if key
102
- value.strip! if value
103
- domain.analyze(line["STATUS"], line, key, value, @main_domain_ary, @main_domain_hash)
104
- when AccountLines::SEPARATOR
105
- key, value = line["CONTENT"].split(":") if line["CONTENT"]
106
-
107
- @main_sep_ary << key
108
- @main_sep_hash[key] = { "TITLE" => key }
109
- else
110
- puts("Error")
111
- puts(line["CONTENT"])
112
- end
113
- end
114
- @main_host_item_ary |= host.item_ary
115
- end
116
-
117
- def sort_by(other)
118
- diff_main_host_ary = @main_host_ary - other.main_host_ary
119
- diff_main_domain_ary = @main_domain_ary - other.main_domain_ary
120
-
121
- n_main_sep_ary = []
122
- n_main_sep_hash = {}
123
- n_main_host_ary = []
124
- n_main_host_hash = {}
125
- n_main_domain_ary = []
126
- n_main_domain_hash = {}
127
-
128
- if other.main_sep_ary.size > 0
129
- @main_sep_ary |= other.main_sep_ary
130
- @main_sep_hash |= n_main_sep_hash
131
- end
132
-
133
- main_host_item_ary = other.main_host_item_ary | @main_host_item_ary
134
-
135
- other.main_host_ary.each do |it|
136
- sort_by_sub(it, @main_host_hash, main_host_item_ary, n_main_host_hash, n_main_host_ary)
137
- end
138
-
139
- diff_main_host_ary.each do |it|
140
- sort_by_sub(it, @main_host_hash, main_host_item_ary, n_main_host_hash, n_main_host_ary)
141
- end
142
-
143
- other.main_domain_ary.each do |it|
144
- sort_by_sub_for_array(it, @main_domain_hash, n_main_domain_hash, n_main_domain_ary)
145
- end
146
-
147
- diff_main_domain_ary.each do |it|
148
- sort_by_sub_for_array(it, @main_domain_hash, n_main_domain_hash, n_main_domain_ary)
149
- end
150
-
151
- @main_host_ary = n_main_host_ary
152
- @main_host_hash = n_main_host_hash
153
- @main_domain_ary = n_main_domain_ary
154
- @main_domain_hash = n_main_domain_hash
155
- end
156
-
157
- def sort_by_sub(it, item_hash, item_ary, n_hash, n_ary)
158
- h = item_hash[it]
159
- if h
160
- n_ary << it
161
- title = h["TITLE"]
162
- hash = h["CONTENT"]
163
- ary = []
164
- if hash
165
- item_ary.each do |ai|
166
- v = hash[ai]
167
- ary << ai if v
168
- end
169
- end
170
- n_hash[it] = { "TITLE" => title, "META" => ary, "CONTENT" => hash }
171
- end
172
- end
173
-
174
- def sort_by_sub_for_array(it, item_hash, n_hash, n_ary)
175
- h = item_hash[it]
176
- if h
177
- n_ary << it
178
- title = h["TITLE"]
179
- ary = h["META"]
180
- hash = h["CONTENT"]
181
-
182
- n_hash[it] = { "TITLE" => title, "META" => ary, "CONTENT" => hash }
183
- end
184
- end
185
-
186
- def dump(file = nil)
187
- dump_ary = []
188
- dump_ary += dump_sub(@main_sep_ary, @main_sep_hash)
189
- dump_ary += dump_sub(@main_host_ary, @main_host_hash)
190
- dump_ary += dump_sub_for_array(@main_domain_ary, @main_domain_hash)
191
-
192
- str = dump_ary.join("\n")
193
-
194
- if file
195
- begin
196
- file.write(str)
197
- file.write("\n")
198
- rescue StandardError => e
199
- pp e
200
- pp e.backtrace
201
- @valid = false
202
- end
203
- end
204
- str
205
- end
206
-
207
- def dump_sub(ary, hash)
208
- dump_ary = []
209
- ary.each do |it|
210
- h = hash[it]
211
- next unless h
212
-
213
- item_ary = h["META"]
214
- title = h["TITLE"]
215
- item_hash = h["CONTENT"]
216
- dump_ary << title
217
-
218
- next unless item_ary
219
-
220
- item_ary.each do |subit|
221
- dump_ary << item_hash[subit] if item_hash[subit]
222
- end
223
- end
224
- dump_ary
225
- end
226
-
227
- def dump_sub_for_array(ary, hash)
228
- dump_ary = []
229
- ary.each do |it|
230
- dump_ary += hash[it]["CONTENT"]
231
- end
232
- dump_ary
233
- end
234
- end
235
-
236
- class StructuredTextForAccountOneLayer
237
- attr_accessor :item_ary
238
-
239
- def initialize
240
- @item_ary = []
241
- @ary = []
242
- @hash = {}
243
- @title = ""
244
- end
245
- end
246
-
247
- class StructuredTextForAccountOneLayerDomain < StructuredTextForAccountOneLayer
248
- def initialize
249
- super
250
- @line_ary = []
251
- end
252
-
253
- def analyze(status, line, key, _value, main_line_ary, main_line_hash)
254
- case status
255
- when AccountLines::DOMAIN_ACCOUNT_START
256
- key.sub!(/^==/, "")
257
- @title = line["CONTENT"]
258
- @line_ary << line["CONTENT"]
259
- when AccountLines::DOMAIN_ACCOUNT
260
- @line_ary << line["CONTENT"]
261
- when AccountLines::DOMAIN_ACCOUNT_END
262
- main_line_ary << @title
263
- main_line_hash[@title] = { "TITLE" => @title, "CONTENT" => @line_ary }
264
- @title = ""
265
- @line_ary = []
266
- end
267
- end
268
- end
269
-
270
- class StructuredTextForAccountOneLayerHost < StructuredTextForAccountOneLayer
271
- def initialize
272
- super
273
- end
274
-
275
- def analyze(status, line, key, _value, main_ary, main_hash)
276
- case status
277
- when AccountLines::HOST_ACCOUNT_START
278
- @hash[key] = line["CONTENT"]
279
- @title = line["CONTENT"]
280
- when AccountLines::HOST_ACCOUNT
281
- @ary << key
282
- @hash[key] = line["CONTENT"]
283
- when AccountLines::HOST_ACCOUNT_END
284
- main_ary << @title
285
- main_hash[@title] = { "TITLE" => @title, "CONTENT" => @hash, "META" => @ary }
286
-
287
- @item_ary |= @ary
288
- @ary = []
289
- @hash = {}
290
- @title = ""
291
- end
292
- end
293
- end
294
-
295
- class StructuredTextForSimple < StructuredText
296
- class Item
297
- attr_reader :name, :contents
298
-
299
- def initialize(name)
300
- @name = name
301
- @contents = []
302
- end
303
-
304
- def add(content)
305
- @contents << content
306
- end
307
- end
308
-
309
- def initialize
310
- @item_ary = {}
311
- end
312
-
313
- def get_event
314
- value = nil
315
- line = @line_ary.shift
316
-
317
- if line
318
- content = line.strip
319
- if content.length == 0
320
- ret = :EMPTY_LINE
321
- value = ""
322
- elsif content =~ /^-(.*)/
323
- title = Regexp.last_match(1)
324
- if title.length > 0
325
- ret = :TITLE_LINE
326
- value = title
327
- else
328
- ret = :EMPTY_TITLE_LINE
329
- value = ""
330
- end
331
- else
332
- ret = :NON_EMPTY_LINE
333
- value = content
334
- end
335
- else
336
- ret = :EOF
337
- end
338
-
339
- [ret, value]
340
- end
341
-
342
- def make_next_state_table
343
- @next_state = {}
344
- @next_state[:NONE] = {}
345
- @next_state[:NONE][:EMPTY_LINE] = :NONE
346
- @next_state[:NONE][:TITLE_LINE] = :ITEM
347
- @next_state[:NONE][:EMPTY_TITLE_LINE] = :END
348
- @next_state[:NONE][:NON_EMPTY_LINE] = :BAD
349
- @next_state[:ITEM] = {}
350
- @next_state[:ITEM][:EMPTY_LINE] = :ITEM
351
- @next_state[:ITEM][:TITLE_LINE] = :ITEM
352
- @next_state[:ITEM][:EMPTY_TITLE_LINE] = :END
353
- @next_state[:ITEM][:NON_EMPTY_LINE] = :ITEM
354
- @next_state[:END] = {}
355
- @next_state[:END][:EMPTY_LINE] = :END
356
- @next_state[:END][:TITLE_LINE] = :BAD
357
- @next_state[:END][:EMPTY_TITLE_LINE] = :BAD
358
- @next_state[:END][:NON_EMPTY_LINE] = :END
359
- end
360
-
361
- def get_next_state(state, event)
362
- if state == :BAD
363
- :BAD
364
- else
365
- @next_state[state][event]
366
- end
367
- end
368
-
369
- def analyze(line_ary, _subfname = nil)
370
- @line_ary = line_ary
371
- make_next_state_table
372
- # :NONE
373
- # :ITEM
374
- # :END
375
- # :BAD
376
-
377
- # :EMPTY_LINE
378
- # :TITLE_LINE
379
- # :EMPTY_TITLE_LINE
380
- # :NON_EMPTY_LINE
381
- state = :NONE
382
- event = get_event
383
-
384
- @item_ary = []
385
- item = nil
386
- while state != :BAD and event[0] != :EOF
387
- case state
388
- when :NONE
389
- case event[0]
390
- when :EMPTY_LINE
391
- when :TITLE_LINE
392
- @item_ary << (item = Item.new(event[1]))
393
- when :EMPTY_TITLE_LINE
394
- @item_ary << (item = Item.new(""))
395
- when :NON_EMPTY_LINE
396
- item.add(event[1])
397
- end
398
- when :ITEM
399
- case event[0]
400
- when :EMPTY_LINE
401
- when :TITLE_LINE
402
- @item_ary << (item = Item.new(event[1]))
403
- when :EMPTY_TITLE_LINE
404
- @item_ary << (item = Item.new(""))
405
- when :NON_EMPTY_LINE
406
- item.add(event[1])
407
- end
408
- else
409
- case event[0]
410
- when :EMPTY_LINE
411
- when :TITLE_LINE
412
- when :EMPTY_TITLE_LINE
413
- when :NON_EMPTY_LINE
414
- item.add(event[1])
415
- end
416
- end
417
-
418
- state = get_next_state(state, event[0])
419
- event = get_event
420
- end
421
-
422
- @item_ary
423
- end
424
- end
425
- end
1
+ # -*- coding utf-8 -*-
2
+
3
+ require "ykutils/debugutils"
4
+ require "ykutils/lines"
5
+ require "ykutils/specfileop"
6
+ require "ykutils/datastructop"
7
+ require "filex"
8
+
9
+ module Ykutils
10
+ class StructuredText
11
+ attr_reader :main_ary, :main_hash, :item_ary
12
+
13
+ include SpecFileOp
14
+ include DataStructOp
15
+ include DebugUtils
16
+
17
+ def initialize(debug = false)
18
+ @text = nil
19
+ @fname = nil
20
+ @text_ary = []
21
+
22
+ debug_utils_init
23
+ set_debug(debug)
24
+ end
25
+
26
+ def load_analyze(fname, subfname = nil)
27
+ # puts_current_method
28
+ # puts "-0 fname=#{fname}"
29
+
30
+ @fname = fname
31
+ @text_ary = load(fname)
32
+ # @text_ary.each do |x| puts x end
33
+ analyze(@text_ary, subfname)
34
+ end
35
+
36
+ def load(fname)
37
+ load_plain_text_file(fname).collect { |it| it.chomp }
38
+ end
39
+
40
+ def analyze(line_ary, subfname = nil); end
41
+
42
+ def dump_to_file(fname)
43
+ file = FileX.open(fname, "w") do |file|
44
+ @text_ary.each do |l|
45
+ file.write(l + "\n")
46
+ end
47
+ end
48
+ rescue StandardError => e
49
+ pp e
50
+ pp e.backtrace
51
+ @valid = false
52
+ end
53
+ end
54
+
55
+ class StructuredTextForFtpOp < StructuredText
56
+ def analyze(line_ary, fname = nil)
57
+ lines = FtpOpLines.new(line_ary)
58
+ lines.output_f(fname)
59
+
60
+ analyze_sub(lines)
61
+ end
62
+ end
63
+
64
+ class StructuredTextForAccount < StructuredText
65
+ attr_reader :main_host_ary, :main_host_hash, :main_domain_ary, :main_domain_hash, :main_sep_ary,
66
+ :main_sep_hash, :main_host_item_ary
67
+
68
+ def initialize
69
+ super()
70
+ @main_host_item_ary = []
71
+
72
+ @main_sep_ary = []
73
+ @main_sep_hash = {}
74
+
75
+ @main_host_ary = []
76
+ @main_host_hash = {}
77
+
78
+ @main_domain_ary = []
79
+ @main_domain_hash = {}
80
+ end
81
+
82
+ def analyze(line_ary, fname = nil)
83
+ lines = AccountLines.new(line_ary)
84
+ lines.output_f(fname)
85
+
86
+ analyze_sub(lines)
87
+ end
88
+
89
+ def analyze_sub(lines)
90
+ host = StructuredTextForAccountOneLayerHost.new
91
+ domain = StructuredTextForAccountOneLayerDomain.new
92
+
93
+ while (line = lines.get_line)
94
+ case line["STATUS"]
95
+ when AccountLines::HOST_ACCOUNT_START, AccountLines::HOST_ACCOUNT, AccountLines::HOST_ACCOUNT_END
96
+ key, value = line["CONTENT"].split(":") if line["CONTENT"]
97
+ key.strip! if key
98
+ value.strip! if value
99
+ host.analyze(line["STATUS"], line, key, value, @main_host_ary, @main_host_hash)
100
+ when AccountLines::DOMAIN_ACCOUNT_START, AccountLines::DOMAIN_ACCOUNT, AccountLines::DOMAIN_ACCOUNT_END
101
+ key, value = line["CONTENT"].split(":") if line["CONTENT"]
102
+ key.strip! if key
103
+ value.strip! if value
104
+ domain.analyze(line["STATUS"], line, key, value, @main_domain_ary, @main_domain_hash)
105
+ when AccountLines::SEPARATOR
106
+ key, value = line["CONTENT"].split(":") if line["CONTENT"]
107
+
108
+ @main_sep_ary << key
109
+ @main_sep_hash[key] = { "TITLE" => key }
110
+ else
111
+ puts("Error")
112
+ puts(line["CONTENT"])
113
+ end
114
+ end
115
+ @main_host_item_ary |= host.item_ary
116
+ end
117
+
118
+ def sort_by(other)
119
+ diff_main_host_ary = @main_host_ary - other.main_host_ary
120
+ diff_main_domain_ary = @main_domain_ary - other.main_domain_ary
121
+
122
+ n_main_sep_ary = []
123
+ n_main_sep_hash = {}
124
+ n_main_host_ary = []
125
+ n_main_host_hash = {}
126
+ n_main_domain_ary = []
127
+ n_main_domain_hash = {}
128
+
129
+ if other.main_sep_ary.size > 0
130
+ @main_sep_ary |= other.main_sep_ary
131
+ @main_sep_hash |= n_main_sep_hash
132
+ end
133
+
134
+ main_host_item_ary = other.main_host_item_ary | @main_host_item_ary
135
+
136
+ other.main_host_ary.each do |it|
137
+ sort_by_sub(it, @main_host_hash, main_host_item_ary, n_main_host_hash, n_main_host_ary)
138
+ end
139
+
140
+ diff_main_host_ary.each do |it|
141
+ sort_by_sub(it, @main_host_hash, main_host_item_ary, n_main_host_hash, n_main_host_ary)
142
+ end
143
+
144
+ other.main_domain_ary.each do |it|
145
+ sort_by_sub_for_array(it, @main_domain_hash, n_main_domain_hash, n_main_domain_ary)
146
+ end
147
+
148
+ diff_main_domain_ary.each do |it|
149
+ sort_by_sub_for_array(it, @main_domain_hash, n_main_domain_hash, n_main_domain_ary)
150
+ end
151
+
152
+ @main_host_ary = n_main_host_ary
153
+ @main_host_hash = n_main_host_hash
154
+ @main_domain_ary = n_main_domain_ary
155
+ @main_domain_hash = n_main_domain_hash
156
+ end
157
+
158
+ def sort_by_sub(it, item_hash, item_ary, n_hash, n_ary)
159
+ h = item_hash[it]
160
+ if h
161
+ n_ary << it
162
+ title = h["TITLE"]
163
+ hash = h["CONTENT"]
164
+ ary = []
165
+ if hash
166
+ item_ary.each do |ai|
167
+ v = hash[ai]
168
+ ary << ai if v
169
+ end
170
+ end
171
+ n_hash[it] = { "TITLE" => title, "META" => ary, "CONTENT" => hash }
172
+ end
173
+ end
174
+
175
+ def sort_by_sub_for_array(it, item_hash, n_hash, n_ary)
176
+ h = item_hash[it]
177
+ if h
178
+ n_ary << it
179
+ title = h["TITLE"]
180
+ ary = h["META"]
181
+ hash = h["CONTENT"]
182
+
183
+ n_hash[it] = { "TITLE" => title, "META" => ary, "CONTENT" => hash }
184
+ end
185
+ end
186
+
187
+ def dump(file = nil)
188
+ dump_ary = []
189
+ dump_ary += dump_sub(@main_sep_ary, @main_sep_hash)
190
+ dump_ary += dump_sub(@main_host_ary, @main_host_hash)
191
+ dump_ary += dump_sub_for_array(@main_domain_ary, @main_domain_hash)
192
+
193
+ str = dump_ary.join("\n")
194
+
195
+ if file
196
+ begin
197
+ file.write(str)
198
+ file.write("\n")
199
+ rescue StandardError => e
200
+ pp e
201
+ pp e.backtrace
202
+ @valid = false
203
+ end
204
+ end
205
+ str
206
+ end
207
+
208
+ def dump_sub(ary, hash)
209
+ dump_ary = []
210
+ ary.each do |it|
211
+ h = hash[it]
212
+ next unless h
213
+
214
+ item_ary = h["META"]
215
+ title = h["TITLE"]
216
+ item_hash = h["CONTENT"]
217
+ dump_ary << title
218
+
219
+ next unless item_ary
220
+
221
+ item_ary.each do |subit|
222
+ dump_ary << item_hash[subit] if item_hash[subit]
223
+ end
224
+ end
225
+ dump_ary
226
+ end
227
+
228
+ def dump_sub_for_array(ary, hash)
229
+ dump_ary = []
230
+ ary.each do |it|
231
+ dump_ary += hash[it]["CONTENT"]
232
+ end
233
+ dump_ary
234
+ end
235
+ end
236
+
237
+ class StructuredTextForAccountOneLayer
238
+ attr_accessor :item_ary
239
+
240
+ def initialize
241
+ @item_ary = []
242
+ @ary = []
243
+ @hash = {}
244
+ @title = ""
245
+ end
246
+ end
247
+
248
+ class StructuredTextForAccountOneLayerDomain < StructuredTextForAccountOneLayer
249
+ def initialize
250
+ super
251
+ @line_ary = []
252
+ end
253
+
254
+ def analyze(status, line, key, _value, main_line_ary, main_line_hash)
255
+ case status
256
+ when AccountLines::DOMAIN_ACCOUNT_START
257
+ key.sub!(/^==/, "")
258
+ @title = line["CONTENT"]
259
+ @line_ary << line["CONTENT"]
260
+ when AccountLines::DOMAIN_ACCOUNT
261
+ @line_ary << line["CONTENT"]
262
+ when AccountLines::DOMAIN_ACCOUNT_END
263
+ main_line_ary << @title
264
+ main_line_hash[@title] = { "TITLE" => @title, "CONTENT" => @line_ary }
265
+ @title = ""
266
+ @line_ary = []
267
+ end
268
+ end
269
+ end
270
+
271
+ class StructuredTextForAccountOneLayerHost < StructuredTextForAccountOneLayer
272
+ def initialize
273
+ super
274
+ end
275
+
276
+ def analyze(status, line, key, _value, main_ary, main_hash)
277
+ case status
278
+ when AccountLines::HOST_ACCOUNT_START
279
+ @hash[key] = line["CONTENT"]
280
+ @title = line["CONTENT"]
281
+ when AccountLines::HOST_ACCOUNT
282
+ @ary << key
283
+ @hash[key] = line["CONTENT"]
284
+ when AccountLines::HOST_ACCOUNT_END
285
+ main_ary << @title
286
+ main_hash[@title] = { "TITLE" => @title, "CONTENT" => @hash, "META" => @ary }
287
+
288
+ @item_ary |= @ary
289
+ @ary = []
290
+ @hash = {}
291
+ @title = ""
292
+ end
293
+ end
294
+ end
295
+
296
+ class StructuredTextForSimple < StructuredText
297
+ class Item
298
+ attr_reader :name, :contents
299
+
300
+ def initialize(name)
301
+ @name = name
302
+ @contents = []
303
+ end
304
+
305
+ def add(content)
306
+ @contents << content
307
+ end
308
+ end
309
+
310
+ def initialize
311
+ super()
312
+ @item_ary = {}
313
+ end
314
+
315
+ def get_event
316
+ value = nil
317
+ line = @line_ary.shift
318
+
319
+ if line
320
+ content = line.strip
321
+ if content.length == 0
322
+ ret = :EMPTY_LINE
323
+ value = ""
324
+ elsif content =~ /^-(.*)/
325
+ title = Regexp.last_match(1)
326
+ if title.length > 0
327
+ ret = :TITLE_LINE
328
+ value = title
329
+ else
330
+ ret = :EMPTY_TITLE_LINE
331
+ value = ""
332
+ end
333
+ else
334
+ ret = :NON_EMPTY_LINE
335
+ value = content
336
+ end
337
+ else
338
+ ret = :EOF
339
+ end
340
+
341
+ [ret, value]
342
+ end
343
+
344
+ def make_next_state_table
345
+ @next_state = {}
346
+ @next_state[:NONE] = {}
347
+ @next_state[:NONE][:EMPTY_LINE] = :NONE
348
+ @next_state[:NONE][:TITLE_LINE] = :ITEM
349
+ @next_state[:NONE][:EMPTY_TITLE_LINE] = :END
350
+ @next_state[:NONE][:NON_EMPTY_LINE] = :BAD
351
+ @next_state[:ITEM] = {}
352
+ @next_state[:ITEM][:EMPTY_LINE] = :ITEM
353
+ @next_state[:ITEM][:TITLE_LINE] = :ITEM
354
+ @next_state[:ITEM][:EMPTY_TITLE_LINE] = :END
355
+ @next_state[:ITEM][:NON_EMPTY_LINE] = :ITEM
356
+ @next_state[:END] = {}
357
+ @next_state[:END][:EMPTY_LINE] = :END
358
+ @next_state[:END][:TITLE_LINE] = :BAD
359
+ @next_state[:END][:EMPTY_TITLE_LINE] = :BAD
360
+ @next_state[:END][:NON_EMPTY_LINE] = :END
361
+ end
362
+
363
+ def get_next_state(state, event)
364
+ if state == :BAD
365
+ :BAD
366
+ else
367
+ @next_state[state][event]
368
+ end
369
+ end
370
+
371
+ def analyze(line_ary, _subfname = nil)
372
+ @line_ary = line_ary
373
+ make_next_state_table
374
+ # :NONE
375
+ # :ITEM
376
+ # :END
377
+ # :BAD
378
+
379
+ # :EMPTY_LINE
380
+ # :TITLE_LINE
381
+ # :EMPTY_TITLE_LINE
382
+ # :NON_EMPTY_LINE
383
+ state = :NONE
384
+ event = get_event
385
+
386
+ @item_ary = []
387
+ item = nil
388
+ while state != :BAD and event[0] != :EOF
389
+ case state
390
+ when :NONE
391
+ case event[0]
392
+ when :EMPTY_LINE
393
+ when :TITLE_LINE
394
+ @item_ary << (item = Item.new(event[1]))
395
+ when :EMPTY_TITLE_LINE
396
+ @item_ary << (item = Item.new(""))
397
+ when :NON_EMPTY_LINE
398
+ item.add(event[1])
399
+ end
400
+ when :ITEM
401
+ case event[0]
402
+ when :EMPTY_LINE
403
+ when :TITLE_LINE
404
+ @item_ary << (item = Item.new(event[1]))
405
+ when :EMPTY_TITLE_LINE
406
+ @item_ary << (item = Item.new(""))
407
+ when :NON_EMPTY_LINE
408
+ item.add(event[1])
409
+ end
410
+ else
411
+ case event[0]
412
+ when :EMPTY_LINE
413
+ when :TITLE_LINE
414
+ when :EMPTY_TITLE_LINE
415
+ when :NON_EMPTY_LINE
416
+ item.add(event[1])
417
+ end
418
+ end
419
+
420
+ state = get_next_state(state, event[0])
421
+ event = get_event
422
+ end
423
+
424
+ @item_ary
425
+ end
426
+ end
427
+ end