maketable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,229 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "date"
5
+ require "pp"
6
+
7
+ module Maketable
8
+ class Maketable
9
+ def initialize(yamlfile, year)
10
+ @yamlfile = yamlfile
11
+ @yaml = YAML.load_file(@yamlfile)
12
+ raise InvalidYearError if year.nil?
13
+ raise InvalidYearError unless year.instance_of?(Integer)
14
+
15
+ @year = year
16
+ @year_str = @year.to_s
17
+
18
+ @hs = {}
19
+ @hs_items = {}
20
+ @hs_table = {}
21
+ @columns = []
22
+
23
+ @num_of_columns = @yaml.keys.size
24
+ @label_width = 2
25
+ @start_month = 4
26
+ @month_range_group = 1
27
+ @day_range_group = 3
28
+ @month_range = Order.get_month_range(@start_month, @month_range_group)
29
+ @day_range_array = Order.get_day_range_array(@day_range_group)
30
+ @month_range_x_index = 0
31
+ end
32
+
33
+ def show
34
+ pp @yaml
35
+ end
36
+
37
+ def analyze
38
+ analyze_level1
39
+ # item_sort
40
+ analyze_level2
41
+ @hs
42
+ end
43
+
44
+ def output_any(level, item)
45
+ indent = " " * level
46
+ if item.instance_of?(String)
47
+ puts "#{indent}#{item}"
48
+ elsif item.instance_of?(Array)
49
+ item.each do |x|
50
+ output_any(level + 1, x)
51
+ end
52
+ elsif item.instance_of?(Hash)
53
+ output_hash( level + 1, item)
54
+ else
55
+ item.output(level)
56
+ end
57
+ end
58
+
59
+ def output_hash(level, hash)
60
+ indent_k = " " * level
61
+ level_v = level + 1
62
+ indent_v = " " * level_v
63
+
64
+ hash.keys.each do |k|
65
+ puts "#{indent_k}#{k}"
66
+ output_any(level_v, hash[k])
67
+ end
68
+ end
69
+
70
+ def xhs_items_0
71
+ output_hash(0, @hs_items)
72
+ end
73
+
74
+ def next_dir( dir )
75
+ if dir == :v
76
+ :h
77
+ else
78
+ :v
79
+ end
80
+ end
81
+
82
+ def xhs_items_sub_v(v, h, output_hs, list)
83
+ puts "xhs_items_sub_v|v=#{v}|h=#{h}"
84
+ @v = v
85
+ @h = h
86
+ output_hs[@v] ||= {}
87
+
88
+ if list.instance_of?(String)
89
+ puts "xhs_items_sub_v|String"
90
+ output_hs[@v][@h] = list
91
+ global_position_update(@v, @h, :h)
92
+ elsif list.instance_of?(Array)
93
+ puts "xhs_items_sub_v|Array"
94
+ list.each do |item|
95
+ xhs_items_sub_h(@v, @h, output_hs, item)
96
+ global_position_update(v, h, :h)
97
+ end
98
+ elsif list == nil
99
+ puts "xhs_items_sub_v|Nil"
100
+ output_hs[@v][@h] = nil
101
+ global_position_update(@v, @h, :h)
102
+ else
103
+ puts "xhs_items_sub_v|Hash"
104
+ # Hashとみなす
105
+ list.keys.each do |key|
106
+ output_hs[@v][@h] = key
107
+ xhs_items_sub_h(@v, @h, output_hs, list[key] )
108
+ global_position_update(@v, @h, :h)
109
+ end
110
+ end
111
+ end
112
+
113
+ def xhs_items_sub_h(v, h, output_hs, list)
114
+ @v = v
115
+ @h = h
116
+ output_hs[@v] ||= {}
117
+
118
+ if list.instance_of?(String)
119
+ output_hs[@v][@h] = list
120
+ global_position_update(v, h, :v)
121
+ elsif list.instance_of?(Array)
122
+ list.each do |item|
123
+ xhs_items_sub_v(@v, @h, output_hs, item)
124
+ global_position_update(v, h, :v)
125
+ end
126
+ elsif list == nil
127
+ output_hs[@v][@h] = nil
128
+ global_position_update(v, h, :v)
129
+ else
130
+ # Hashとみなす
131
+ list.keys.each do |key|
132
+ output_hs[@v][@h] = nil
133
+ xhs_items_sub_h(@v, @h, output_hs, list[key] )
134
+ global_position_update(v, h, :v)
135
+ end
136
+ end
137
+ end
138
+
139
+ def global_position_update(v, h, dir)
140
+ if dir == :h
141
+ @h += 1
142
+ @v = v
143
+ else
144
+ @h = h
145
+ @v += 1
146
+ end
147
+ end
148
+
149
+ def xhs_items
150
+ v = 0
151
+ h = 0
152
+ @hs = {}
153
+ dir = :v
154
+ @v = v
155
+ @h = h
156
+ next_dir = next_dir(dir)
157
+
158
+ @yaml.keys.each do |key|
159
+ puts "xhs_items|key=#{key}"
160
+ @hs[@v] ||= {}
161
+ @hs[@v][@h] = key
162
+ puts "xhs_items|@hs[#{@v}][#{@h}]=#{ @hs[@v][@h] }"
163
+ if dir == :v
164
+ xhs_items_sub_v(@v, @h, @hs, @yaml[key] )
165
+ else
166
+ xhs_items_sub_h(@v, @h, @hs, @yaml[key] )
167
+ end
168
+ global_position_update(v, h, dir)
169
+ end
170
+
171
+ pp @hs
172
+ end
173
+
174
+ def analyze_2
175
+ puts "analyze_2"
176
+ @yaml.each do |k, v|
177
+ unless v
178
+ @hs[k] = []
179
+ @hs_items[k] = []
180
+ next
181
+ end
182
+ v.each do |str|
183
+ item = Item.new(str, @year_str)
184
+ #item.analyze
185
+ @hs_items[k] ||= []
186
+ @hs_items[k] << item
187
+ end
188
+ #@hs_items[k] = @hs_items[k].sort_by(&:date_head)
189
+ end
190
+ end
191
+
192
+ def analyze_level1
193
+ @yaml.each do |k, v|
194
+ unless v
195
+ @hs[k] = []
196
+ @hs_items[k] = []
197
+ next
198
+ end
199
+ v.each do |str|
200
+ item = Item.new(str, @year_str)
201
+ item.analyze
202
+ @hs_items[k] ||= []
203
+ @hs_items[k] << item
204
+ end
205
+ @hs_items[k] = @hs_items[k].sort_by(&:date_head)
206
+ end
207
+ end
208
+
209
+ def analyze_level2
210
+ @hs_items.each do |column, v|
211
+ if v.empty?
212
+ @hs_table[column] = {}
213
+ @hs_table[column][@month_range_x_index] = {}
214
+ else
215
+ v.each do |item|
216
+ Order.order(@month_range, @day_range_array, column, item, @hs_table)
217
+ end
218
+ end
219
+ end
220
+ @hs_table
221
+ end
222
+
223
+ def output(table_format = :trac_wiki)
224
+ ot = OutputTable.new(@month_range, @hs_table, @month_range_x_index, @year, @day_range_group)
225
+ max_row = ot.make_table
226
+ ot.output(max_row, table_format)
227
+ end
228
+ end
229
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maketable
4
+ class Order
5
+ @order_month = {
6
+ 1 => {
7
+ 1 => [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
8
+ 2 => [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]],
9
+ 3 => [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],
10
+ 4 => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
11
+ },
12
+ 4 => {
13
+ 1 => [[4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]],
14
+ 2 => [[4, 5, 6, 7, 8, 9], [10, 11, 12, 1, 2, 3]],
15
+ 3 => [[4, 5, 6, 7], [8, 9, 10, 11], [12, 1, 2, 3]],
16
+ 4 => [[4, 5, 6], [7, 8, 9], [10, 11, 12], [1, 2, 3]]
17
+ }
18
+ }
19
+ @order_month_1 = {
20
+ 1 => [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
21
+ 2 => [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]],
22
+ 3 => [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],
23
+ 4 => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
24
+ }
25
+ @order_month_4 = {
26
+ 1 => [[4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]],
27
+ 2 => [[4, 5, 6, 7, 8, 9], [10, 11, 12, 1, 2, 3]],
28
+ 3 => [[4, 5, 6, 7], [8, 9, 10, 11], [12, 1, 2, 3]],
29
+ 4 => [[4, 5, 6], [7, 8, 9], [10, 11, 12], [1, 2, 3]]
30
+ }
31
+
32
+ @order_month_a = {}
33
+
34
+ @order_day = {
35
+ 1 => [[1, 31]],
36
+ 2 => [[1, 15], [16, 31]],
37
+ 3 => [[1, 10], [11, 20], [21, 31]],
38
+ 4 => [[1, 8], [9, 16], [17, 24], [25, 31]],
39
+ 5 => [[1, 6], [7, 12], [13, 18], [19, 24], [25, 31]]
40
+ }
41
+
42
+ @day_range_label = {
43
+ 1 => { 0 => "" },
44
+ 2 => { 0 => "上", 1 => "下" },
45
+ 3 => { 0 => "上", 1 => "中", 2 => "下" },
46
+ 4 => { 0 => "1", 1 => "2", 2 => "3", 3 => "4" },
47
+ 5 => { 0 => "1", 1 => "2", 2 => "3", 3 => "4", 4 => "5" }
48
+ }
49
+
50
+ class << self
51
+ def make_month_order(year, start_month, max)
52
+ # datetime_hash = Util.make_datetime_hash(year, start_month, max)
53
+ date_hash = Util.make_date_hash(year, start_month, max)
54
+ # Util.make_month_order(@order_month, datetime_hash)
55
+ Util.make_month_order(@order_month, date_hash)
56
+ end
57
+
58
+ def make_month_order_1(year, max)
59
+ start_month = 1
60
+ # datetime_hash = Util.make_datetime_hash(year, start_month, max)
61
+ date_hash = Util.make_date_hash(year, start_month, max)
62
+ # Util.make_month_order(@order_month_1, datetime_hash)
63
+ Util.make_month_order(@order_month_1, date_hash)
64
+ end
65
+
66
+ def make_month_order_4(year, max)
67
+ start_month = 4
68
+ # datetime_hash = Util.make_datetime_hash(year, start_month, max)
69
+ date_hash = Util.make_date_hash(year, start_month, max)
70
+ # Util.make_month_order(@order_month_4, datetime_hash)
71
+ Util.make_month_order(@order_month_4, date_hash)
72
+ end
73
+
74
+ def make_month_order_a(year, max)
75
+ @order_month_a[1] = make_month_order_1(year, max)
76
+ @order_month_a[4] = make_month_order_4(year, max)
77
+ @order_month_a
78
+ end
79
+
80
+ def get_day_range_label(day_range, index)
81
+ @day_range_label[day_range][index]
82
+ end
83
+
84
+ def month_range_index(range_array, target)
85
+ range_array.index { |v| v.include?(target) }
86
+ end
87
+
88
+ def month_range_index_a(range_array, target)
89
+ range_array.index { |v| v.include?(target) }
90
+ end
91
+
92
+ def day_range_index(range_array, target)
93
+ range_array.index { |v| v.first <= target && v.last >= target }
94
+ end
95
+
96
+ def get_month_range(start_month, month_range_group)
97
+ @order_month[start_month][month_range_group]
98
+ end
99
+
100
+ def get_day_range_array(day_range_group)
101
+ @order_day[day_range_group]
102
+ end
103
+
104
+ def order(month_range, day_range_array, column, item, hs_table)
105
+ # month = item.datetime_head.month
106
+ month = item.date_head.month
107
+ # day = item.datetime_head.day
108
+ day = item.date_head.day
109
+
110
+ m_r_index = month_range_index(month_range, month)
111
+ d_r_index = day_range_index(day_range_array, day)
112
+
113
+ hs_table[column] ||= {}
114
+ hs_table[column][m_r_index] ||= {}
115
+ hs_table[column][m_r_index][month] ||= {}
116
+ hs_table[column][m_r_index][month][d_r_index] ||= []
117
+ hs_table[column][m_r_index][month][d_r_index] << item
118
+ end
119
+
120
+ def make_table_row_label(start_month)
121
+ start_day = "#{start_month}/1"
122
+ @order_month[1][1][0].each_with_object([]) do |_x, list|
123
+ list << if list.empty?
124
+ item = Item.new(start_day, @year)
125
+ item.analyze
126
+ item
127
+ else
128
+ list.last.make_next_month_item
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maketable
4
+ class OutputTable
5
+ def initialize(month_range, hs_table, month_range_x_index, year, day_range_group)
6
+ @row_index = 0
7
+ @col_index = 0
8
+ @max_row_index = 0
9
+
10
+ @month_range = month_range
11
+ @hs_table = hs_table
12
+ @tc_ary = []
13
+ @month_range_x_index = month_range_x_index
14
+ @year = year
15
+ @day_range_group = day_range_group
16
+
17
+ @label_width = 2
18
+ @width = @hs_table.keys.size + @label_width
19
+ @output_table = Array.new(@width) { [] }
20
+
21
+ @hs_table.keys.each_with_index do |column, index|
22
+ @tc_ary << TableColumn.new(index, column, @hs_table[column][@month_range_x_index], @month_range)
23
+ end
24
+ end
25
+
26
+ def make_table
27
+ @months_of_year = 12
28
+ @month_index_conv = { 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 1,
29
+ 14 => 2, 15 => 3 }
30
+
31
+ output_pad = 2
32
+
33
+ @month_first = 4
34
+ @month_last = 3
35
+ @day_range_first = 0
36
+ @day_range_last = 2
37
+
38
+ @tc_first = 0
39
+ @tc_last = (@tc_ary.size - 1)
40
+
41
+ @label_item_list = Order.make_table_row_label(@month_first)
42
+ start_row = 0
43
+ make_output_table_header(output_pad, start_row)
44
+ start_row = 2
45
+ make_output_table(output_pad, start_row)
46
+ end
47
+
48
+ def make_output_table_header(output_pad, start_row)
49
+ # byebug
50
+ max_row = 0
51
+ @tc_first.upto(@tc_last) do |tc_index|
52
+ output_column_index = tc_index + output_pad
53
+ next_row = start_row
54
+ next_row = @tc_ary[tc_index].output_key(@output_table[output_column_index], next_row)
55
+ max_row = next_row if max_row < next_row
56
+ end
57
+ max_row
58
+ end
59
+
60
+ def make_table_row_label(start_row, month_index, day_range_index)
61
+ index = month_index - @month_first
62
+ item = @label_item_list[index]
63
+ date = item.date_head
64
+ content = if day_range_index == 0
65
+ %(#{date.year}-#{date.month})
66
+ else
67
+ ""
68
+ end
69
+ @output_table[0][start_row] = content
70
+ end
71
+
72
+ def make_output_table(output_pad, start_row)
73
+ max_row = 0
74
+ tc_start_row = start_row
75
+
76
+ @month_first.upto(@month_first + @months_of_year - 1) do |m_index|
77
+ month_index = @month_index_conv[m_index]
78
+ @day_range_first.upto(@day_range_last) do |day_range_index|
79
+ make_table_row_label(tc_start_row, m_index, day_range_index)
80
+ @tc_first.upto(@tc_last) do |tc_index|
81
+ output_column_index = tc_index + output_pad
82
+ next_row = tc_start_row
83
+ tc = @tc_ary[tc_index]
84
+ next_row = tc.output(@output_table[output_column_index], next_row, month_index, day_range_index)
85
+ max_row = next_row if max_row < next_row
86
+ end
87
+ tc_start_row = max_row
88
+ end
89
+ end
90
+ max_row
91
+ end
92
+
93
+ def make_table_format(head_flag:, data:, format: :trac_wiki)
94
+ lines = []
95
+ case format
96
+ when :trac_wiki
97
+ lines << %(|| #{data.join(" || ")} ||)
98
+ when :markdown
99
+ lines << %(| #{data.join(" | ")} |)
100
+ if head_flag
101
+ data.size
102
+ lines << %(|#{Array.new(data.size) { "-" }.join("|")}|)
103
+ end
104
+ else
105
+ raise InvalidFormatError
106
+ end
107
+ lines
108
+ end
109
+
110
+ def output(max_row, table_format)
111
+ lineno = 0
112
+ lines = []
113
+ 0.upto(max_row - 1) do |j|
114
+ ary = []
115
+ 0.upto(@width - 1) do |i|
116
+ ary << if @output_table[i]
117
+ @output_table[i][j] || ""
118
+ else
119
+ ""
120
+ end
121
+ end
122
+ lines = make_table_format(head_flag: (lineno == 0), data: ary, format: table_format)
123
+ lines.map { |l| puts l }
124
+ lineno += 1
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,9 @@
1
+ module Maketable
2
+ class Table
3
+ def initialize(start_dir)
4
+ @axi = {}
5
+ @start_dir = start_dir
6
+ @cur_dir = @start_dir
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maketable
4
+ class TableColumn
5
+ def initialize(column_index, key, data_hs, month_range)
6
+ @column_index = column_index
7
+ @key = key
8
+ @data_hs = data_hs
9
+ @month_range = month_range
10
+ end
11
+
12
+ def show
13
+ pp @month_range
14
+ pp @column_index
15
+ pp @key
16
+ pp @data_hs
17
+ end
18
+
19
+ def output_key(output_hs, output_row)
20
+ lh, sep, rh = @key.partition("//")
21
+ if sep == ""
22
+ output_hs[output_row] = @key
23
+ else
24
+ output_hs[output_row] = lh
25
+ output_row += 1
26
+ output_hs[output_row] = rh
27
+ end
28
+ output_row += 1
29
+ output_row
30
+ end
31
+
32
+ def output(output_hs, start_row, month_index, day_range_index)
33
+ output_row = start_row
34
+ if @data_hs
35
+ m_info = @data_hs[month_index]
36
+ if m_info && m_info[day_range_index] && !m_info[day_range_index].empty?
37
+ m_info[day_range_index].each do |item|
38
+ output_hs[output_row] = item.desc_str
39
+ output_row += 1
40
+ end
41
+ end
42
+ end
43
+ if output_row == start_row
44
+ output_hs[output_row] = ""
45
+ output_row += 1
46
+ end
47
+ output_row
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Maketable
6
+ class Util
7
+ class << self
8
+ def make_date_list(year, month, max = 12)
9
+ @dt_list = []
10
+ dt = nil
11
+ month.upto(month + max - 1) do |x|
12
+ dt = if dt
13
+ dt.next_month
14
+ else
15
+ Date.new(year, x)
16
+ end
17
+ @dt_list << dt
18
+ end
19
+ @dt_list
20
+ end
21
+
22
+ def convert_index(month)
23
+ month -= 12 if month > 12
24
+ month
25
+ end
26
+
27
+ def make_date_hash(year, month, max = 12)
28
+ @dt_hash = {}
29
+ dt = nil
30
+ month.upto(month + max - 1) do |x|
31
+ dt = if dt
32
+ dt.next_month
33
+ else
34
+ Date.new(year, x)
35
+ end
36
+ y = convert_index(x)
37
+ @dt_hash[y] = dt
38
+ end
39
+ @dt_hash
40
+ end
41
+
42
+ def make_month_order(src, date_hash)
43
+ if src.instance_of?(Hash)
44
+ dest_hash = {}
45
+ src.each do |k, v|
46
+ dest_hash[k] = make_month_order(v, date_hash)
47
+ end
48
+ dest_hash
49
+ elsif src.instance_of?(Array)
50
+ dest_array = []
51
+ src.each do |v|
52
+ dest_array << make_month_order(v, date_hash)
53
+ end
54
+ dest_array
55
+ elsif src.instance_of?(Integer)
56
+ date_hash[src]
57
+ else
58
+ raise InvalidObjectError
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,19 @@
1
+ module Maketable
2
+ class Utilx
3
+ class << self
4
+ def make_table_format(data:, format: :trac_wiki)
5
+ lines = []
6
+ case format
7
+ when :trac_wiki
8
+ line = %(|| #{data.join(" || ")} ||)
9
+ lines << line
10
+ when :markdown
11
+ lines << %(| #{data.join(" | ")} |)
12
+ else
13
+ raise InvalidFormatError
14
+ end
15
+ lines
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Maketable
4
+ VERSION = "0.1.0"
5
+ end
data/lib/maketable.rb ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "maketable/version"
4
+ require_relative "maketable/item"
5
+ require_relative "maketable/order"
6
+ require_relative "maketable/maketable"
7
+ require_relative "maketable/outputtable"
8
+ require_relative "maketable/tablecolumn"
9
+ require_relative "maketable/util"
10
+ require_relative "maketable/limitedmarkdown"
11
+ require_relative "maketable/utilx"
12
+ #require_relative "maketable/itemx"
13
+
14
+ require "pathname"
15
+ require "byebug"
16
+ module Maketable
17
+ TEST_DATA_DIR = Pathname.new(__dir__).parent + "test_data"
18
+
19
+ class Error < StandardError; end
20
+ class InvalidYearError < Error; end
21
+ class InvalidObjectError < Error; end
22
+ class InvalidFormatError < Error; end
23
+ class InvalidLevelChangeError < Error; end
24
+ class NoParentItemxError < Error; end
25
+ class InvalidParentLevelError < Error; end
26
+ # Your code goes here...
27
+ end
data/sig/maketable.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Maketable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end