6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
52
53
54
55
56
57
58
59
60
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/table_analysis.rb', line 6
def self.generator(doc_table_html, , *selected_cols)
selected_cols = selected_cols.flatten
doc = Nokogiri::HTML(doc_table_html, nil, 'utf-8')
table = doc.xpath('//table')[0]
return false if table.nil?
= []
body_content_tds = []
body_tr_size = 0
header_body_content_tds = []
tr_rows = 1
= []
= 0
select_table_tr = table.xpath('./thead/tr|./tbody/tr')
if select_table_tr.empty?
select_table_tr = table.xpath('./tr')
end
select_table_tr.each_with_index do |tr, tr_index|
if tr_index < .to_i - 1
+= 1
tr.xpath('./td|./th').each_with_index do |td, td_index|
rowspan = td.attribute('rowspan')&.value
colspan = td.attribute('colspan')&.value
<< [rowspan, colspan]
end
elsif tr_index == .to_i - 1
tr.xpath('./td|./th').each do |td|
colspan = td.attribute('colspan')&.value
rowspan = td.attribute('rowspan')&.value
<< [rowspan, colspan]
header_body_content_tds << [rowspan, colspan]
tr_rows = rowspan.to_i.dup if !rowspan.nil? && rowspan.to_i > 1 && tr_rows < rowspan.to_i
end
elsif tr_index > .to_i - 1 && tr_index < .to_i - 1 + tr_rows
tr.xpath('./td|./th').each do |td|
rowspan = td.attribute('rowspan')&.value
colspan = td.attribute('colspan')&.value
header_body_content_tds << [rowspan, colspan]
end
elsif tr_index >= .to_i - 1 + tr_rows
body_tr_size += 1
tr.xpath('./td|./th').each_with_index do |td, td_index|
rowspan = td.attribute('rowspan')&.value
colspan = td.attribute('colspan')&.value
body_content_tds << [rowspan, colspan]
end
end
end
= .map do ||
TableAnalysis::HeaderTd.config([0], [1])
end
= TableAnalysis::Header.config(selected_cols, )
body_tr_table = TableAnalysis::Table.config(body_tr_size, )
body_tds = body_content_tds.map do |body_td|
TableAnalysis::BodyTd.config(body_td[0], body_td[1])
end
content_maps = TableAnalysis::Core.new(, body_tr_table, body_tds).entrance
= TableAnalysis::Table.config(, )
= .map do |body_td|
TableAnalysis::BodyTd.config(body_td[0], body_td[1])
end
= nil
= TableAnalysis::Core.new(, , ).entrance unless .empty?
= TableAnalysis::Table.config(tr_rows, )
header_body_tds = header_body_content_tds.map do |body_td|
TableAnalysis::BodyTd.config(body_td[0], body_td[1])
end
= TableAnalysis::Core.new(, , header_body_tds).entrance
table_maps =
if .nil?
+ content_maps
else
+ + content_maps
end
table_maps
end
|