2019-05-30 14:35:16 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ProtocolImporters
|
|
|
|
class TablesBuilder
|
2019-06-18 01:05:32 +08:00
|
|
|
def self.extract_tables_from_html_string(description_string, remove_first_column_row = false)
|
2019-05-30 14:35:16 +08:00
|
|
|
tables = []
|
|
|
|
|
|
|
|
doc = Nokogiri::HTML(description_string)
|
|
|
|
tables_nodeset = doc.css('table')
|
|
|
|
|
|
|
|
tables_nodeset.each do |table_node|
|
|
|
|
rows_nodeset = table_node.css('tr')
|
|
|
|
|
|
|
|
two_d_array = Array.new(rows_nodeset.count) { [] }
|
|
|
|
|
|
|
|
rows_nodeset.each_with_index do |row, i|
|
|
|
|
row.css('td').each_with_index do |cell, j|
|
2019-07-09 17:35:15 +08:00
|
|
|
two_d_array[i][j] = ActionController::Base.helpers.strip_tags(cell.inner_html)
|
2019-05-30 14:35:16 +08:00
|
|
|
end
|
2019-06-18 01:05:32 +08:00
|
|
|
two_d_array[i].shift if remove_first_column_row
|
2019-05-30 14:35:16 +08:00
|
|
|
end
|
2019-06-18 01:05:32 +08:00
|
|
|
two_d_array.shift if remove_first_column_row
|
2019-05-30 14:35:16 +08:00
|
|
|
|
|
|
|
tables << Table.new(contents: { data: two_d_array }.to_json)
|
|
|
|
end
|
|
|
|
tables
|
|
|
|
end
|
2019-06-18 23:47:08 +08:00
|
|
|
|
|
|
|
def self.remove_tables_from_html(description_string)
|
2019-07-03 22:36:26 +08:00
|
|
|
return '' if description_string.blank?
|
|
|
|
|
2019-06-18 23:47:08 +08:00
|
|
|
doc = Nokogiri::HTML(description_string)
|
|
|
|
doc.search('table').each do |t|
|
2019-07-08 22:20:16 +08:00
|
|
|
t.swap('<br/><br/><p><i>There was a table here, it was moved to tables section.</i></p><br/><br/>')
|
2019-06-18 23:47:08 +08:00
|
|
|
end
|
|
|
|
doc.css('body').first.inner_html
|
|
|
|
end
|
2019-05-30 14:35:16 +08:00
|
|
|
end
|
|
|
|
end
|