scinote-web/app/helpers/protocols_io_table_helper.rb
2017-11-12 15:41:50 +01:00

45 lines
1.6 KiB
Ruby

module ProtocolsIoTableHelper
def protocolsio_string_to_table_element(description_string)
string_without_tables = string_html_table_remove(description_string)
table_regex = %r{<table\b[^>]*>(.*?)<\/table>}m
tr_regex = %r{<tr\b[^>]*>(.*?)<\/tr>}m
td_regex = %r{<td\b[^>]*>(.*?)<\/td>}m
tables = {}
table_strings = description_string.scan(table_regex)
table_strings.each_with_index do |table, table_counter|
tables[table_counter.to_s] = {}
tr_strings = table[0].scan(tr_regex)
contents = {}
contents['data'] = []
tr_strings.each_with_index do |tr, tr_counter|
td_strings = tr[0].scan(td_regex)
contents['data'][tr_counter] = []
td_strings.each do |td|
td_stripped = ActionController::Base.helpers.strip_tags(td[0])
contents['data'][tr_counter].push(td_stripped)
end
end
tables[table_counter.to_s]['contents'] = Base64.encode64(
contents.to_s.sub('=>', ':')
)
tables[table_counter.to_s]['name'] = nil
end
# return string_without_tables, tables
return tables, string_without_tables
end
def string_html_table_remove(description_string)
description_string.remove!("\n", "\t", "\r", "\f")
table_whole_regex = %r{(<table\b[^>]*>.*?<\/table>)}m
table_pattern_array = description_string.scan(table_whole_regex)
string_without_tables = description_string
table_pattern_array.each do |table_pattern|
string_without_tables = string_without_tables.gsub(
table_pattern[0],
t('protocols.protocols_io_import.comp_append.table_moved').html_safe
)
end
string_without_tables
end
end