Module: ModsDisplay::RecordHelper

Defined in:
app/helpers/mods_display/record_helper.rb

Instance Method Summary collapse

Instance Method Details

#format_mods_html(val, tags: %w[a dl dd dt i b em strong cite br],, field: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/mods_display/record_helper.rb', line 72

def format_mods_html(val, tags: %w[a dl dd dt i b em strong cite br], field: nil)
  val = val.gsub(%r{<[^/> ]+}) do |possible_tag|
    # Allow potentially valid HTML tags through to the sanitizer step, and HTML escape the rest
    if tags.include?(possible_tag[1..])
      possible_tag
    else
      "&lt;#{possible_tag[1..]}"
    end
  end

  val = val.gsub(%r{</[^> ]+}) do |possible_tag|
    # Allow potentially valid HTML tags through to the sanitizer step, and HTML escape the rest
    if tags.include?(possible_tag[2..])
      possible_tag
    else
      "&lt;#{possible_tag[1..]}"
    end
  end

  # http://daringfireball.net/2010/07/improved_regex_for_matching_urls
  url = %r{(?i)\b(?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\([^\s()<>]+|\([^\s()<>]+\)*\))+(?:\([^\s()<>]+|\([^\s()<>]+\)*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])}i
  # http://www.regular-expressions.info/email.html
  email = %r{[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b}i
  matches = [val.scan(url), val.scan(email)].flatten.uniq
  unless val =~ /<a/ # we'll assume that linking has alraedy occured and we don't want to double link
    matches.each do |match|
      if match =~ email
        val.gsub!(match, "<a href='mailto:#{match}'>#{match}</a>")
      else
        match = match.delete_suffix('&gt')

        val.gsub!(match, "<a href='#{match}'>#{match}</a>")
      end
    end
  end

  formatted_val = sanitize val, tags: tags, attributes: %w[href]

  # Martin Wong data has significant linebreaks in abstracts and notes that we want
  # to preserve and display in HTML.
  #
  # See https://github.com/sul-dlss/mods_display/issues/78
  simple_formatted_fields = [ModsDisplay::Abstract, ModsDisplay::Contents, ModsDisplay::Note]
  if simple_formatted_fields.any? { |klass| field&.field.is_a? klass } && formatted_val.include?("\n")
    simple_format(formatted_val, {}, sanitize: false)
  else
    formatted_val
  end
end


48
49
50
51
# File 'app/helpers/mods_display/record_helper.rb', line 48

def link_mods_genres(genre, &block)
  link_buffer = []
  link_to_mods_subject(genre, link_buffer, &block)
end


54
55
56
57
58
59
60
61
# File 'app/helpers/mods_display/record_helper.rb', line 54

def link_mods_subjects(subjects, &block)
  link_buffer = []
  linked_subjects = []
  subjects.each do |subject|
    linked_subjects << link_to_mods_subject(subject, link_buffer, &block) if subject.present?
  end
  linked_subjects
end


64
65
66
67
68
69
70
# File 'app/helpers/mods_display/record_helper.rb', line 64

def link_to_mods_subject(subject, buffer = [])
  subject_text = subject.respond_to?(:name) ? subject.name : subject
  link = block_given? ? capture { yield(subject_text, buffer) } : subject_text
  buffer << subject_text.strip
  link << " (#{subject.roles.join(', ')})" if subject.respond_to?(:roles) && subject.roles.present?
  link
end

rubocop:disable Layout/LineLength @private, but used in PURL currently



124
125
126
# File 'app/helpers/mods_display/record_helper.rb', line 124

def link_urls_and_email(val, tags: %w[a dl dd dt i b em strong cite br])
  format_mods_html(val, tags: tags)
end

#mods_display_content(values, delimiter = nil) ⇒ Object



10
11
12
# File 'app/helpers/mods_display/record_helper.rb', line 10

def mods_display_content(values, delimiter = nil)
  mods_record_field ModsDisplay::Values.new(values: Array(values)), delimiter
end

#mods_display_label(label) ⇒ Object



5
6
7
# File 'app/helpers/mods_display/record_helper.rb', line 5

def mods_display_label(label)
  (:dt, label.delete(':')) + "\n".html_safe
end

#mods_display_name(values, &block) ⇒ Object



24
25
26
# File 'app/helpers/mods_display/record_helper.rb', line 24

def mods_display_name(values, &block)
  mods_name_field(ModsDisplay::Values.new(values: Array(values)), &block)
end

#mods_genre_field(field, &block) ⇒ Object



41
42
43
44
45
# File 'app/helpers/mods_display/record_helper.rb', line 41

def mods_genre_field(field, &block)
  mods_record_field(field) do |genre_line|
    link_to_mods_subject(genre_line, &block)
  end
end

#mods_name_field(field) ⇒ Object



18
19
20
21
22
# File 'app/helpers/mods_display/record_helper.rb', line 18

def mods_name_field(field)
  mods_record_field(field) do |name|
    block_given? ? capture { yield(name.name) } : name.name
  end
end

#mods_record_field(field, delimiter = nil, component: ModsDisplay::FieldComponent, &block) ⇒ Object



14
15
16
# File 'app/helpers/mods_display/record_helper.rb', line 14

def mods_record_field(field, delimiter = nil, component: ModsDisplay::FieldComponent, &block)
  render component.new(field: field, delimiter: delimiter, value_transformer: block)
end

#mods_subject_field(field, &block) ⇒ Object



35
36
37
38
39
# File 'app/helpers/mods_display/record_helper.rb', line 35

def mods_subject_field(field, &block)
  mods_record_field(field) do |subject_line|
    safe_join(link_mods_subjects(subject_line, &block), ' > ')
  end
end

#sanitize_mods_name_label(label) ⇒ Object

We need this to remove the ending “:” from the role labels only in data from mods_display



31
32
33
# File 'app/helpers/mods_display/record_helper.rb', line 31

def sanitize_mods_name_label(label)
  label.sub(/:$/, '')
end