Class: TableAnalysis::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/table_analysis.rb

Class Method Summary collapse

Class Method Details

.generator(doc_table_html, header_start_row, *selected_cols) ⇒ Object



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, header_start_row, *selected_cols)
  selected_cols = selected_cols.flatten 
  doc = Nokogiri::HTML(doc_table_html, nil, 'utf-8')
  # 多个table,仅处理第一个
  table = doc.xpath('//table')[0]
  return false if table.nil?
  header_content_tds = []

  body_content_tds = []
  body_tr_size = 0

  header_body_content_tds = []
  tr_rows = 1

  upheader_content_tds = []
  upheader_tr_size = 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 < header_start_row.to_i - 1
      upheader_tr_size += 1
      tr.xpath('./td|./th').each_with_index do |td, td_index|
        rowspan = td.attribute('rowspan')&.value 
        colspan = td.attribute('colspan')&.value
        upheader_content_tds << [rowspan, colspan]
      end
    elsif tr_index == header_start_row.to_i - 1
      tr.xpath('./td|./th').each do |td|
        colspan = td.attribute('colspan')&.value
        rowspan = td.attribute('rowspan')&.value
        header_content_tds << [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 > header_start_row.to_i - 1 && tr_index < header_start_row.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 >= header_start_row.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

  header_tds = header_content_tds.map do |header_content_td|
    TableAnalysis::HeaderTd.config(header_content_td[0], header_content_td[1])
  end
  header = TableAnalysis::Header.config(selected_cols, header_tds)

  body_tr_table = TableAnalysis::Table.config(body_tr_size, header)
  body_tds = body_content_tds.map do |body_td|
    TableAnalysis::BodyTd.config(body_td[0], body_td[1])
  end
  content_maps = TableAnalysis::Core.new(header, body_tr_table, body_tds).entrance

  upheader_table = TableAnalysis::Table.config(upheader_tr_size, header)
  upheader_tds = upheader_content_tds.map do |body_td|
    TableAnalysis::BodyTd.config(body_td[0], body_td[1])
  end
  upheader_maps = nil
  upheader_maps = TableAnalysis::Core.new(header, upheader_table, upheader_tds).entrance unless upheader_table.empty?

  header_table = TableAnalysis::Table.config(tr_rows, header)
  header_body_tds = header_body_content_tds.map do |body_td|
    TableAnalysis::BodyTd.config(body_td[0], body_td[1])
  end
  header_maps = TableAnalysis::Core.new(header, header_table, header_body_tds).entrance

  table_maps = 
    if upheader_maps.nil?
      header_maps + content_maps
    else 
      upheader_maps + header_maps + content_maps
    end

  table_maps
end