scinote-web/app/helpers/protocols_io_helper.rb

522 lines
17 KiB
Ruby
Raw Normal View History

module ProtocolsIoHelper
#=============================================================================
# Protocols.io limits
#=============================================================================
TEXT_MAX_LENGTH = Constants::TEXT_MAX_LENGTH
2017-11-22 02:20:15 +08:00
PIO_ELEMENT_RESERVED_LENGTH_BIG = TEXT_MAX_LENGTH * 0.015
PIO_ELEMENT_RESERVED_LENGTH_MEDIUM = TEXT_MAX_LENGTH * 0.01
PIO_ELEMENT_RESERVED_LENGTH_SMALL = TEXT_MAX_LENGTH * 0.005
2017-11-22 02:20:15 +08:00
# PROTOCOLS.IO PROTOCOL ATTRIBUTES
2017-11-21 06:19:34 +08:00
PIO_P_AVAILABLE_LENGTH =
TEXT_MAX_LENGTH -
2017-11-22 02:20:15 +08:00
(PIO_ELEMENT_RESERVED_LENGTH_SMALL * 2 +
PIO_ELEMENT_RESERVED_LENGTH_MEDIUM * 8 +
2017-11-24 18:49:57 +08:00
PIO_ELEMENT_RESERVED_LENGTH_BIG * 2)
2017-11-23 06:38:22 +08:00
# -- 2 small = created at , publish date PROTOCOL ATTRIBUTES
# -- 8 medium = description,tags,before_start,warning,guidelines,
# manuscript_citation,keywords,vendor_name PROTOCOL ATTRIBUTES
# -- 2 big = vendor_link, link PROTOCOL ATTRIBUTES
2017-11-21 06:19:34 +08:00
# PROTOCOLS.IO STEP ATTRIBUTES
2017-11-21 06:19:34 +08:00
PIO_S_AVAILABLE_LENGTH =
TEXT_MAX_LENGTH -
2017-11-24 18:49:57 +08:00
(PIO_ELEMENT_RESERVED_LENGTH_SMALL * 20)
# -- 20 small = description,expected_result,safety_information
# software_package version, software_package os_name,
# software_package os_version,software_package link,
# software_package repository,software_package developer,software_package name
# commands os_version,commands os_name, commands name,commands description,
# sub protocol full name (author), sub protocol name, sub protocol link,
# dataset link,dataset name, safety_information link,
# -- 0 medium =
# -- 0 big =
2017-11-21 06:19:34 +08:00
PIO_TITLE_TOOLONG_LEN =
I18n.t('protocols.protocols_io_import.title_too_long').length + 5
2017-11-21 06:19:34 +08:00
PIO_STEP_TOOLONG_LEN =
I18n.t('protocols.protocols_io_import.too_long').length
# The + 2 above (in title) is there because if the length was at the limit,
# the cutter method had issues, this gives it some space
2018-03-05 23:09:06 +08:00
# below are default min table settings (minimum 5x5)
PIO_TABLE_MIN_WIDTH = 5
PIO_TABLE_MIN_HEIGHT = 5
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 = {}
description_string.gsub! '<th>', '<td>'
description_string.gsub! '</th>', '</td>'
table_strings = description_string.scan(table_regex)
table_strings.each_with_index do |table, table_counter|
tables[table_counter.to_s] = {}
tr_number = table[0].scan(tr_regex).count
2018-03-05 23:09:06 +08:00
diff = PIO_TABLE_MIN_HEIGHT - tr_number # always tables have atleast 5 row
2018-03-06 00:02:37 +08:00
table_fix_str = table[0]
table_fix_str += '<tr></tr>' * diff if tr_number < PIO_TABLE_MIN_HEIGHT
2018-02-07 20:04:02 +08:00
tr_strings = table_fix_str.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_counter = td_strings.count
2018-03-05 23:09:06 +08:00
diff = PIO_TABLE_MIN_WIDTH - td_counter
td_strings.each do |td|
td_stripped = ActionController::Base.helpers.strip_tags(td[0])
contents['data'][tr_counter].push(td_stripped)
end
2018-03-06 00:02:37 +08:00
next if td_counter >= PIO_TABLE_MIN_WIDTH
2018-03-03 06:02:35 +08:00
diff.times { contents['data'][tr_counter].push(' ') }
end
tables[table_counter.to_s]['contents'] = Base64.encode64(
contents.to_s.sub('=>', ':')
)
tables[table_counter.to_s]['name'] = ' '
end
return tables, string_without_tables
end
2018-11-24 00:37:26 +08:00
def string_html_table_remove(description_string)
description_string.remove!("\n", "\t", "\r", "\f")
table_whole_regex = %r{(<table\b[^>]*>.*?<\/table>)}m
2018-11-24 00:37:26 +08:00
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
2018-11-24 00:37:26 +08:00
string_without_tables
end
def pio_eval_prot_desc(text, attribute_name)
case attribute_name
2018-11-24 00:37:26 +08:00
when 'published_on'
2017-11-22 02:20:15 +08:00
pio_eval_len(text, ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL)
2017-11-23 06:38:22 +08:00
when 'vendor_link', 'link'
2017-11-22 02:20:15 +08:00
pio_eval_len(text, ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_BIG)
else
2017-11-22 04:27:19 +08:00
pio_eval_len(text, ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_MEDIUM)
end
end
2017-11-21 06:19:34 +08:00
def pio_eval_title_len(text)
if text
text += ' ' if text.length < Constants::NAME_MIN_LENGTH
if text.length > Constants::NAME_MAX_LENGTH
text =
text[0..(Constants::NAME_MAX_LENGTH - PIO_TITLE_TOOLONG_LEN)] +
t('protocols.protocols_io_import.title_too_long')
@toolong = true
end
text
2018-02-16 03:01:36 +08:00
else
''
end
end
2017-11-21 06:19:34 +08:00
def pio_eval_len(text, reserved)
if text
text_end = reserved + @remaining - PIO_STEP_TOOLONG_LEN
text_end = 2 if text_end < 2
# Since steps have very low reserved values now (below 100),
# the above sets their index to 1 if its negative
# (length of toolong text is about 90 chars, and if remaining is 0,
# then the negative index just gets set to 1. this is a workaround
# it would also be possible to not count the length of the "too long" text
# or setting the import reserved value to 95,but then available characters
# will be like before (around 7600)
2017-11-21 06:19:34 +08:00
if text.length - reserved > @remaining
text =
2017-11-23 06:38:22 +08:00
close_open_html_tags(
text[0..text_end] + t('protocols.protocols_io_import.too_long')
)
2017-11-21 06:19:34 +08:00
@toolong = true
2017-11-22 02:20:15 +08:00
@remaining = 0
elsif (text.length - reserved) > 0
@remaining -= text.length - reserved
2017-11-21 06:19:34 +08:00
end
2017-11-21 18:42:42 +08:00
text
2018-02-16 03:01:36 +08:00
else
''
2017-11-21 06:19:34 +08:00
end
end
2018-11-23 23:30:29 +08:00
def pio_eval_authors(text)
2018-11-26 20:52:01 +08:00
# Extract authors names from the JSON
text.map { |auth| auth['name'] }.join(', ')
rescue StandardError
[]
2018-11-23 23:30:29 +08:00
end
def eval_last_modified(steps)
2018-11-26 20:52:01 +08:00
timestamps = steps.map do |step|
step['modified_on'] if step['modified_on'].present?
end
I18n.l(Time.at(timestamps.max), format: :full)
2018-11-26 20:52:01 +08:00
rescue StandardError
I18n.l(Time.at(0), format: :full)
end
2017-11-21 06:19:34 +08:00
# Checks so that null values are returned as zero length strings
# Did this so views arent as ugly (i avoid using if present statements)
def not_null(attribute)
if attribute
attribute
else
''
end
end
2017-11-21 18:15:09 +08:00
2017-11-22 02:20:15 +08:00
def close_open_html_tags(text)
2017-11-21 18:15:09 +08:00
Nokogiri::HTML::DocumentFragment.parse(text).to_html
end
2017-12-02 07:29:47 +08:00
2018-02-21 23:17:53 +08:00
def step_hash_null?(step_json)
2018-03-14 00:42:26 +08:00
step_json.dig(
2018-11-23 23:30:29 +08:00
0, 'components', 0, 'type_id'
2018-03-14 00:42:26 +08:00
).nil? && step_json.dig(
2018-11-23 23:30:29 +08:00
0, 'components', '0', 'type_id'
2018-03-11 05:48:25 +08:00
).nil?
2018-02-16 03:01:36 +08:00
end
# Images are allowed in:
# Step: description, expected result
# Protocol description : description before_start warning
# guidelines manuscript_citation
2018-02-16 03:01:36 +08:00
def prepare_for_view(
attribute_text1, size, table = 'no_table', image_allowed = false
)
2018-01-27 21:33:01 +08:00
image_tag = image_allowed ? Array('img') : Array(nil)
image_tag.push('br')
2017-12-16 03:39:15 +08:00
if table == 'no_table'
attribute_text = sanitize_input(not_null(attribute_text1), image_tag)
2017-12-16 03:39:15 +08:00
elsif table == 'table'
attribute_text = sanitize_input(
string_html_table_remove(not_null(attribute_text1)), image_tag
2017-12-03 08:17:49 +08:00
)
end
2017-12-16 03:39:15 +08:00
pio_eval_len(
attribute_text,
size
)
2017-12-03 08:17:49 +08:00
end
2017-12-16 03:39:15 +08:00
def fill_attributes(attribute_name, attribute_text, step_component)
2017-12-03 08:17:49 +08:00
output_string = ''
2017-12-16 03:39:15 +08:00
trans_string = step_component
2017-12-03 08:17:49 +08:00
trans_string +=
if attribute_name != 'os_name' && attribute_name != 'os_version'
attribute_name
else
'os'
end
output_string +=
if attribute_name != 'os_version'
t(trans_string)
else
' , '
end
if attribute_name == 'protocol_name'
output_string += pio_eval_title_len(attribute_text)
else
output_string += prepare_for_view(
attribute_text, ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL
)
end
2017-12-03 08:17:49 +08:00
output_string
2017-12-02 07:29:47 +08:00
end
# pio_stp_x means protocols io step (id of component) parser
# protocols io description parser
def pio_stp_1(iterating_key)
2017-12-02 07:29:47 +08:00
br = '<br>'
append =
if iterating_key.present?
br +
2017-12-16 03:39:15 +08:00
prepare_for_view(
iterating_key,
ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL,
'table',
true
2017-12-16 03:39:15 +08:00
) +
2017-12-02 07:29:47 +08:00
br
else
t('protocols.protocols_io_import.comp_append.missing_desc')
end
append
end
def pio_stp_6(iterating_key)
2018-01-26 16:56:59 +08:00
if iterating_key.present?
# protocols io section(title) parser
2018-01-26 16:56:59 +08:00
return pio_eval_title_len(CGI.unescapeHTML(sanitize_input(iterating_key)))
end
2017-12-02 07:29:47 +08:00
t('protocols.protocols_io_import.comp_append.missing_step')
end
def pio_stp_17(iterating_key)
# protocols io expected result parser
2017-12-02 07:29:47 +08:00
if iterating_key.present?
append =
t('protocols.protocols_io_import.comp_append.expected_result') +
2017-12-16 03:39:15 +08:00
prepare_for_view(
iterating_key,
ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL,
'table',
true
2017-12-16 03:39:15 +08:00
) +
2017-12-02 07:29:47 +08:00
'<br>'
return append
end
''
end
# protocols io software package,dataset,commands,
# sub_protocol and safety_information parser
2017-12-02 07:29:47 +08:00
2017-12-24 09:06:14 +08:00
def pio_stp(iterating_key, parse_elements_array, en_local_text)
2017-12-16 03:39:15 +08:00
append = ''
parse_elements_array.each do |element|
next unless iterating_key[element]
append += fill_attributes(
element,
iterating_key[element],
en_local_text
)
2017-12-02 07:29:47 +08:00
end
2017-12-16 03:39:15 +08:00
append
2017-12-02 07:29:47 +08:00
end
def protocols_io_fill_desc(json_hash)
unshortened_string_for_tables = ''
2017-12-02 07:29:47 +08:00
description_array = %w[
2018-11-24 00:37:26 +08:00
( before_start warning guidelines manuscript_citation published_on
2017-12-02 07:29:47 +08:00
vendor_name vendor_link keywords tags link created_on )
]
allowed_image_attributes = %w[
( before_start warning guidelines manuscript_citation )
]
if json_hash['description'].present?
unshortened_string_for_tables += json_hash['description']
description_string =
'<strong>' +
t('protocols.protocols_io_import.preview.description') +
'</strong>' +
prepare_for_view(
json_hash['description'],
ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_MEDIUM,
'table',
true
).html_safe
else
description_string =
'<strong>' +
t('protocols.protocols_io_import.preview.description') +
'</strong>' +
t('protocols.protocols_io_import.comp_append.missing_desc')
end
2017-12-02 07:29:47 +08:00
description_string += '<br>'
description_array.each do |e|
if e == 'created_on' && json_hash[e].present?
new_e = '<strong>' + e.humanize + '</strong>'
description_string +=
2017-12-03 08:17:49 +08:00
new_e.to_s + ': ' +
prepare_for_view(
2017-12-16 03:39:15 +08:00
params['protocol']['created_at'].to_s,
ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL
) +
2017-12-03 08:17:49 +08:00
+ '<br>'
2018-11-24 01:54:37 +08:00
elsif e == 'tags' && json_hash[e].present? \
&& json_hash[e].any? && json_hash[e] != ''
2017-12-02 07:29:47 +08:00
new_e = '<strong>' + e.humanize + '</strong>'
description_string +=
new_e.to_s + ': '
tags_length_checker = ''
json_hash[e].each do |tag|
tags_length_checker +=
sanitize_input(tag['tag_name']) + ' , '
end
2017-12-03 08:17:49 +08:00
description_string += prepare_for_view(
2017-12-16 03:39:15 +08:00
tags_length_checker,
ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_MEDIUM
2017-12-02 07:29:47 +08:00
)
description_string += '<br>'
elsif json_hash[e].present?
data =
if e == 'published_on'
Time.at(json_hash[e]).utc.to_datetime.to_s
else
json_hash[e]
end
unshortened_string_for_tables += data
2017-12-02 07:29:47 +08:00
new_e = '<strong>' + e.humanize + '</strong>'
2018-01-27 21:33:01 +08:00
image_tag = allowed_image_attributes.include?(e) ? Array('img') : Array(nil)
2017-12-02 07:29:47 +08:00
description_string +=
new_e.to_s + ': ' + # intercept tables here, before cut
2017-12-02 07:29:47 +08:00
pio_eval_prot_desc(
2018-11-24 01:54:37 +08:00
sanitize_input(data, image_tag),
2017-12-02 07:29:47 +08:00
e
).html_safe + '<br>'
end
end
return description_string, unshortened_string_for_tables
2017-12-02 07:29:47 +08:00
end
def protocols_io_guid_reorder_step_json(unordered_step_json)
2018-02-16 06:40:09 +08:00
return '' if unordered_step_json.blank?
base_step = unordered_step_json.find { |step| step['previous_guid'].nil? }
return unordered_step_json if base_step.nil?
number_of_steps = unordered_step_json.size
2018-02-16 03:01:36 +08:00
return unordered_step_json if number_of_steps == 1
step_order = []
step_counter = 0
step_order[step_counter] = base_step
step_counter += 1
2018-01-26 18:05:19 +08:00
while step_order.length != number_of_steps
step_order[step_counter] =
2018-01-26 16:56:59 +08:00
unordered_step_json.find do |step|
step['previous_guid'] == base_step['guid']
end
base_step = step_order[step_counter]
step_counter += 1
end
step_order
end
2017-12-02 07:29:47 +08:00
def protocols_io_fill_step(original_json, newj)
# newj = new json
# (simple to map) id 1= step description, id 6= section (title),
# id 17= expected result
# (complex mapping with nested hashes) id 8 = software package,
# id 9 = dataset, id 15 = command, id 18 = attached sub protocol
# id 19= safety information ,
# id 20= regents (materials, like scinote samples kind of)
2018-01-26 16:56:59 +08:00
original_json['steps'] = protocols_io_guid_reorder_step_json(
original_json['steps']
)
2017-12-02 07:29:47 +08:00
newj['0'] = {}
newj['0']['position'] = 0
newj['0']['name'] = 'Protocol info'
@remaining = ProtocolsIoHelper::PIO_P_AVAILABLE_LENGTH
shortened_string, unshortened_tables_string = protocols_io_fill_desc(
original_json
)
newj['0']['tables'] = protocolsio_string_to_table_element(
sanitize_input(unshortened_tables_string).html_safe
)[0]
table_str = protocolsio_string_to_table_element(
2018-02-07 16:53:49 +08:00
sanitize_input(shortened_string, Array('img')).html_safe
)[1]
2017-12-02 07:29:47 +08:00
newj['0']['description'] = table_str
original_json['steps'].each_with_index do |step, pos_orig| # loop over steps
i = pos_orig + 1
@remaining = ProtocolsIoHelper::PIO_S_AVAILABLE_LENGTH
# position of step (first, second.... etc),
newj[i.to_s] = {} # the json we will insert into db
newj[i.to_s]['position'] = i
newj[i.to_s]['description'] = '' unless newj[i.to_s].key?('description')
newj[i.to_s]['name'] = '' unless newj[i.to_s].key?('name')
unshortened_step_table_string = ''
2017-12-02 07:29:47 +08:00
step['components'].each do |key, value|
# sometimes there are random index values as keys
# instead of hashes, this is a workaround to that buggy json format
key = value if value.class == Hash
# append is the string that we append values into for description
# pio_stp_x means protocols io step (id of component) parser
2018-11-24 01:54:37 +08:00
case key['type_id']
# intercept tables in all of below before cutting
2018-11-24 01:54:37 +08:00
when 1
unshortened_step_table_string += key['source']['description']
newj[i.to_s]['description'] += pio_stp_1(key['source']['description'])
when 6
newj[i.to_s]['name'] = pio_stp_6(key['source']['title'])
when 17
unshortened_step_table_string += key['source']['body']
newj[i.to_s]['description'] += pio_stp_17(key['source']['body'])
when 8
2017-12-24 08:49:04 +08:00
pe_array = %w(
name developer version link repository os_name os_version
)
trans_text = 'protocols.protocols_io_import.comp_append.soft_packg.'
2017-12-24 09:06:14 +08:00
newj[i.to_s]['description'] += pio_stp(
key['source'], pe_array, trans_text
2017-12-24 08:49:04 +08:00
)
2018-11-24 01:54:37 +08:00
when 9
2017-12-24 08:49:04 +08:00
pe_array = %w(
name link
)
trans_text = 'protocols.protocols_io_import.comp_append.dataset.'
2017-12-24 09:06:14 +08:00
newj[i.to_s]['description'] += pio_stp(
key['source'], pe_array, trans_text
2017-12-24 08:49:04 +08:00
)
2018-11-24 01:54:37 +08:00
when 15
2017-12-24 08:49:04 +08:00
pe_array = %w(
name description os_name os_version
)
2018-11-24 03:13:34 +08:00
key['source']['name'] =
2018-02-07 20:51:22 +08:00
'<pre><code>' +
2018-11-24 03:13:34 +08:00
not_null(key['source']['name'].gsub(/\n/, '<br>')) +
2018-02-07 20:51:22 +08:00
'</code></pre>'
2017-12-24 08:49:04 +08:00
trans_text = 'protocols.protocols_io_import.comp_append.command.'
2017-12-24 09:06:14 +08:00
newj[i.to_s]['description'] += pio_stp(
2018-11-24 03:13:34 +08:00
key['source'], pe_array, trans_text
2017-12-24 08:49:04 +08:00
)
2018-11-24 01:54:37 +08:00
when 18
2017-12-24 08:49:04 +08:00
pe_array = %w(
2018-11-24 03:13:34 +08:00
title title_html uri
2017-12-24 08:49:04 +08:00
)
trans_text = 'protocols.protocols_io_import.comp_append.sub_protocol.'
2017-12-24 09:06:14 +08:00
newj[i.to_s]['description'] += pio_stp(
2018-11-24 03:13:34 +08:00
key['source'], pe_array, trans_text
2017-12-24 08:49:04 +08:00
)
2018-11-24 01:54:37 +08:00
when 19
2017-12-24 08:49:04 +08:00
pe_array = %w(
body link
)
trans_text = 'protocols.protocols_io_import.comp_append.safety_infor.'
2017-12-24 09:06:14 +08:00
newj[i.to_s]['description'] += pio_stp(
2018-11-24 03:13:34 +08:00
key['source'], pe_array, trans_text
2017-12-24 08:49:04 +08:00
)
2017-12-02 07:29:47 +08:00
end # case end
end # finished looping over step components
table_str = protocolsio_string_to_table_element(
2017-12-02 07:29:47 +08:00
newj[i.to_s]['description']
)[1]
2017-12-02 07:29:47 +08:00
newj[i.to_s]['description'] = table_str
newj[i.to_s]['tables'] = protocolsio_string_to_table_element(
sanitize_input(unshortened_step_table_string).html_safe
)[0]
2017-12-02 07:29:47 +08:00
end # steps
newj
end
def get_steps(json)
# Get steps of the given json_object
if json.key?('steps') && json['steps'].respond_to?('each')
json['steps']
else
[]
end
end
def get_components(step_json)
# Get components of given step_json
if step_json.key?('components') &&
step_json['components'].respond_to?('each')
step_json['components']
else
[]
end
end
end