mods_display 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f3f4f8efe36102bd0479b7180b22af1a6484d1278d2f8e756ac91e78a410460
4
- data.tar.gz: 2136ef6e06de8e11d7f28904614eb85b7cce2a512a1be2ce38a67c8da59f5f00
3
+ metadata.gz: ab657c57e41e5c014b54751efb2a1e7e2a3de69449215a040446ba56e5012cc3
4
+ data.tar.gz: 19b3ffeda4c16b78a66cbe644f8c1894cdce91ca1eac9902d52a89676357432b
5
5
  SHA512:
6
- metadata.gz: dbb695d25d8750a0e94fd8ca0ccd6acb76f15e27c8847c96ab79d38cd3a8bed3ca0babad1580861493367bbda894bb1540be705f4fe6b224996b55f39f1322fb
7
- data.tar.gz: ad5f4caf6e21a7be8c0d614329519438d017dd67297f8696180a4401f1ce3dd5fa5030684f1da4647c7f8a37d3711cb0be7509fe1658081df67662f725fec91c
6
+ metadata.gz: df1c227cf9f7a8dc03ff406f3e6e18f5697324216621a609bd0c8e137a8ce3fc8b7cd2e1d11bb6354d40bc8d404790c9a2d6f6512b14338f4d903c9ea3c75eeb
7
+ data.tar.gz: 70063931db7fae164481f2be8d7e8ab0b155ed1231934deea4fb630092711a69c51b17ab499575f29ac02ee6af9036a10efbf9e2d1c8dded8bd0de11cd17d3e4
data/README.md CHANGED
@@ -22,69 +22,6 @@ Or install it yourself as:
22
22
 
23
23
  $ gem install mods_display
24
24
 
25
- Include the `ModelExtension` into your model.
26
-
27
- class MyClass
28
- include ModsDisplay::ModelExtension
29
- end
30
-
31
- Configure the source of the MODS XML in your model. You can pass a string of XML to the mods_xml_source method, however it will also accept a block where you can call methods on self (so if the MODS XML string is held in MyClass#mods):
32
-
33
- class MyClass
34
- ....
35
-
36
- mods_xml_source do |model|
37
- model.mods
38
- end
39
-
40
- end
41
-
42
- Include the `ControllerExtension` into your rails controller (or another class if not using rails).
43
-
44
- class MyController
45
- include ModsDisplay::ControllerExtension
46
- end
47
-
48
- Optionally configure the mods display gem (more on configuration later).
49
-
50
- class MyController
51
- ....
52
- configure_mods_display do
53
- ....
54
- end
55
- end
56
-
57
- ## Usage
58
-
59
- Once installed, the class that included the `ControllerExtension` (`MyController`) will have the `render_mods_display` method available. This method takes one argument which is an instance of the class that included the `ModelExtension` (`MyClass`).
60
-
61
- render_mods_display(@model) # where @model.is_a?(MyClass)
62
-
63
- The basic render call will return the top-level ModsDisplay::HTML class object. Any String method (e.g. #html_safe) you call on this top-level object will be sent down to the #to_html method which will return the HTML for all the metadata in the MODS document.
64
-
65
- render_mods_display(@model).to_html
66
-
67
- You can abstract the main (first) title by calling #title on the top-level HTML method
68
-
69
- render_mods_display(@model).title
70
-
71
- When getting JUST the main (first) title out of the metadata, it will be useful to get the rest of the metadata without the main title. You can accomplish this by calling #body on the top-level HTML object.
72
-
73
- render_mods_display(@model).body
74
-
75
- ## Advanced Usage
76
-
77
- You can also access the array of ModsDisplay::Values objects for a given class directly by calling the name of the class. The class names are not always intuitive for public consumption so you may want to check the code the particular method to call.
78
-
79
- render_mods_display(@model).abstract
80
- => [#<ModsDisplay::Values @label="Abstract:", @values=["Hey. I'm an abstract."]>]
81
-
82
- Given that this semantics that we're concerned with here are more about titles and data construction rather than XML it may be required that you find something by the label. A common example of this is the imprint class. The imprint class can return other publication data that is not the imprint statement. You'll want to select (using your favorite enumerable method) the element in the array that is an imprint.
83
-
84
- imprint = render_mods_display(@model).imprint.find do |data|
85
- data.label == "Imprint:"
86
- end.values
87
-
88
25
  ## Release/Upgrade Notes
89
26
 
90
27
  #### v0.5.0
@@ -71,7 +71,7 @@ module ModsDisplay
71
71
 
72
72
  # rubocop:disable Layout/LineLength
73
73
  # @private, but used in PURL currently
74
- def link_urls_and_email(val, tags: %w[a dl dd dt i b em strong br])
74
+ def link_urls_and_email(val, tags: %w[a dl dd dt i b em strong cite br])
75
75
  val = val.gsub(%r{<[^/> ]+}) do |possible_tag|
76
76
  # Allow potentially valid HTML tags through to the sanitizer step, and HTML escape the rest
77
77
  if tags.include? possible_tag[1..]
@@ -91,6 +91,8 @@ module ModsDisplay
91
91
  if match =~ email
92
92
  val.gsub!(match, "<a href='mailto:#{match}'>#{match}</a>")
93
93
  else
94
+ match = match.delete_suffix('&gt')
95
+
94
96
  val.gsub!(match, "<a href='#{match}'>#{match}</a>")
95
97
  end
96
98
  end
@@ -73,18 +73,18 @@ module ModsDisplay
73
73
  when 'license'
74
74
  license_statement(element)
75
75
  else
76
- element.text
76
+ element_text(element)
77
77
  end
78
78
  end
79
79
 
80
80
  def copyright_statement(element)
81
- element.text.gsub(/\(c\) copyright/i, '&copy;').gsub(/\(c\)/i, '&copy;')
81
+ element_text(element).gsub(/\(c\) copyright/i, '&copy;').gsub(/\(c\)/i, '&copy;')
82
82
  end
83
83
 
84
84
  def license_statement(element)
85
- matches = element.text.match(/^(?<code>.*) (?<type>.*):(?<description>.*)$/)
85
+ matches = element_text(element).match(/^(?<code>.*) (?<type>.*):(?<description>.*)$/)
86
86
 
87
- return element.text unless matches
87
+ return element_text(element) unless matches
88
88
 
89
89
  code = matches[:code].downcase
90
90
  type = matches[:type].downcase
@@ -10,9 +10,9 @@ module ModsDisplay
10
10
  next unless value.respond_to?(:cartographics)
11
11
 
12
12
  value.cartographics.each do |field|
13
- scale = field.scale.empty? ? 'Scale not given' : field.scale.text
14
- projection = field.projection.empty? ? nil : field.projection.text
15
- coordinates = field.coordinates.empty? ? nil : field.coordinates.text
13
+ scale = field.scale.empty? ? 'Scale not given' : element_text(field.scale)
14
+ projection = field.projection.empty? ? nil : element_text(field.projection)
15
+ coordinates = field.coordinates.empty? ? nil : element_text(field.coordinates)
16
16
  post_scale = if [projection,
17
17
  coordinates].compact.length.positive?
18
18
  [projection, coordinates].compact.join(' ')
@@ -16,7 +16,7 @@ module ModsDisplay
16
16
 
17
17
  return_fields << ModsDisplay::Values.new(
18
18
  label: collection_label(value),
19
- values: [value.titleInfo.text.strip]
19
+ values: [element_text(value.titleInfo)]
20
20
  )
21
21
  end
22
22
  collapse_fields(return_fields)
@@ -4,7 +4,10 @@ module ModsDisplay
4
4
  class Contact < Field
5
5
  def fields
6
6
  return_fields = contact_fields.map do |value|
7
- ModsDisplay::Values.new(label: displayLabel(value) || I18n.t('mods_display.contact'), values: [value.text])
7
+ ModsDisplay::Values.new(
8
+ label: displayLabel(value) || I18n.t('mods_display.contact'),
9
+ values: [element_text(value)]
10
+ )
8
11
  end
9
12
  collapse_fields(return_fields)
10
13
  end
@@ -4,7 +4,7 @@ module ModsDisplay
4
4
  class Description < Field
5
5
  def fields
6
6
  return_fields = description_fields.map do |value|
7
- ModsDisplay::Values.new(label: description_label(value), values: [value.text])
7
+ ModsDisplay::Values.new(label: description_label(value), values: [element_text(value)])
8
8
  end
9
9
  collapse_fields(return_fields)
10
10
  end
@@ -8,7 +8,7 @@ module ModsDisplay
8
8
  [
9
9
  ModsDisplay::Values.new(
10
10
  label: I18n.t('mods_display.extent'),
11
- values: extent_fields.map(&:text)
11
+ values: extent_fields.map { |x| element_text(x) }
12
12
  )
13
13
  ]
14
14
  end
@@ -10,7 +10,7 @@ module ModsDisplay
10
10
  return_fields = @values.map do |value|
11
11
  ModsDisplay::Values.new(
12
12
  label: displayLabel(value) || label,
13
- values: [value.text]
13
+ values: [element_text(value)]
14
14
  )
15
15
  end
16
16
  collapse_fields(return_fields)
@@ -52,5 +52,9 @@ module ModsDisplay
52
52
  ModsDisplay::Values.new(label: group.first.label, values: group.map(&:values).flatten(1))
53
53
  end
54
54
  end
55
+
56
+ def element_text(element)
57
+ element.xpath('.//text()').to_html.strip
58
+ end
55
59
  end
56
60
  end
@@ -8,7 +8,7 @@ module ModsDisplay
8
8
  [
9
9
  ModsDisplay::Values.new(
10
10
  label: I18n.t('mods_display.form'),
11
- values: form_fields.map(&:text).uniq { |x| x.downcase.gsub(/\s/, '').gsub(/[[:punct:]]/, '') }
11
+ values: form_fields.map { |x| element_text(x) }.uniq { |x| x.downcase.gsub(/\s/, '').gsub(/[[:punct:]]/, '') }
12
12
  )
13
13
  ]
14
14
  end
@@ -4,7 +4,7 @@ module ModsDisplay
4
4
  class Genre < Field
5
5
  def fields
6
6
  return_fields = @values.map do |value|
7
- ModsDisplay::Values.new(label: displayLabel(value) || label, values: [value.text.strip.capitalize].flatten)
7
+ ModsDisplay::Values.new(label: displayLabel(value) || label, values: [element_text(value).capitalize].flatten)
8
8
  end
9
9
  collapse_fields(return_fields)
10
10
  end
@@ -4,7 +4,7 @@ module ModsDisplay
4
4
  class Identifier < Field
5
5
  def fields
6
6
  return_fields = @values.map do |value|
7
- ModsDisplay::Values.new(label: displayLabel(value) || identifier_label(value), values: [value.text])
7
+ ModsDisplay::Values.new(label: displayLabel(value) || identifier_label(value), values: [element_text(value)])
8
8
  end
9
9
  collapse_fields(return_fields)
10
10
  end
@@ -31,7 +31,7 @@ module ModsDisplay
31
31
  other_pub_info(value).each do |pub_info|
32
32
  return_fields << ModsDisplay::Values.new(
33
33
  label: displayLabel(value) || pub_info_labels[pub_info.name.to_sym],
34
- values: [pub_info.text.strip]
34
+ values: [element_text(pub_info)]
35
35
  )
36
36
  end
37
37
 
@@ -11,7 +11,7 @@ module ModsDisplay
11
11
 
12
12
  ModsDisplay::Values.new(
13
13
  label: displayLabel(value) || displayLabel(term) || I18n.t('mods_display.language'),
14
- values: [language_codes[term.text]]
14
+ values: [language_codes[element_text(term)]]
15
15
  )
16
16
  end.flatten.compact
17
17
  end.flatten.compact
@@ -10,10 +10,10 @@ module ModsDisplay
10
10
 
11
11
  if child.name.to_sym == :url
12
12
  loc_label = displayLabel(location) || I18n.t('mods_display.location')
13
- value = "<a href='#{child.text}'>#{(displayLabel(child) || child.text).gsub(/:$/, '')}</a>"
13
+ value = "<a href='#{element_text(child)}'>#{(displayLabel(child) || element_text(child)).gsub(/:$/, '')}</a>"
14
14
  else
15
15
  loc_label = location_label(child) || displayLabel(location) || I18n.t('mods_display.location')
16
- value = child.text
16
+ value = element_text(child)
17
17
  end
18
18
  return_fields << ModsDisplay::Values.new(
19
19
  label: loc_label || displayLabel(location) || I18n.t('mods_display.location'),
@@ -6,7 +6,7 @@ module ModsDisplay
6
6
  def fields
7
7
  return_fields = @values.map do |value|
8
8
  person = if value.displayForm.length.positive?
9
- ModsDisplay::Name::Person.new(name: value.displayForm.text)
9
+ ModsDisplay::Name::Person.new(name: element_text(value.displayForm))
10
10
  elsif !name_parts(value).empty?
11
11
  ModsDisplay::Name::Person.new(name: name_parts(value))
12
12
  end
@@ -58,7 +58,7 @@ module ModsDisplay
58
58
  end
59
59
 
60
60
  def format_role(element)
61
- element.text.capitalize.sub(/[.,:;]+$/, '')
61
+ element_text(element).capitalize.sub(/[.,:;]+$/, '')
62
62
  end
63
63
 
64
64
  def role?(element)
@@ -87,7 +87,7 @@ module ModsDisplay
87
87
 
88
88
  def unqualified_name_parts(element)
89
89
  element.namePart.map do |part|
90
- part.text unless part.attributes['type']
90
+ element_text(part) unless part.attributes['type']
91
91
  end.compact
92
92
  end
93
93
 
@@ -95,7 +95,7 @@ module ModsDisplay
95
95
  element.namePart.map do |part|
96
96
  if part.attributes['type'].respond_to?(:value) &&
97
97
  part.attributes['type'].value == type
98
- part.text
98
+ element_text(part)
99
99
  end
100
100
  end.compact
101
101
  end
@@ -121,7 +121,7 @@ module ModsDisplay
121
121
  end
122
122
  end.compact
123
123
  end
124
- roles.map { |t| t.text.strip }
124
+ roles.map { |t| element_text(t) }
125
125
  end
126
126
 
127
127
  def unencoded_role_term?(element)
@@ -4,7 +4,7 @@ module ModsDisplay
4
4
  class Note < Field
5
5
  def fields
6
6
  return_fields = note_fields.map do |value|
7
- ModsDisplay::Values.new(label: displayLabel(value) || note_label(value), values: [value.text])
7
+ ModsDisplay::Values.new(label: displayLabel(value) || note_label(value), values: [element_text(value)])
8
8
  end
9
9
  collapse_fields(return_fields)
10
10
  end
@@ -9,13 +9,11 @@ module ModsDisplay
9
9
  next if related_item_is_a_collection?(value)
10
10
  next if render_nested_related_item?(value)
11
11
 
12
- if related_item_is_a_location?(value)
13
- process_location value
14
- elsif related_item_is_a_reference?(value)
15
- process_reference value
16
- else
17
- process_related_item(value)
18
- end
12
+ text = related_item_value(value)
13
+
14
+ next if text.nil? || text.empty?
15
+
16
+ ModsDisplay::Values.new(label: related_item_label(value), values: [text])
19
17
  end.compact
20
18
  collapse_fields(return_fields)
21
19
  end
@@ -23,28 +21,32 @@ module ModsDisplay
23
21
  private
24
22
 
25
23
  def delimiter
26
- '<br />'
27
- end
28
-
29
- def process_location(item)
30
- ModsDisplay::Values.new(label: related_item_label(item), values: [item.location.text.strip])
24
+ '<br />'.html_safe
31
25
  end
32
26
 
33
- def process_reference(item)
34
- ModsDisplay::Values.new(label: related_item_label(item), values: [reference_title(item)])
35
- end
27
+ def related_item_value(item)
28
+ if related_item_is_a_location?(item)
29
+ element_text(item.location)
30
+ elsif related_item_is_a_reference?(item)
31
+ reference_title(item)
32
+ elsif item.titleInfo.any?
33
+ title = element_text(item.titleInfo)
34
+ location = nil
35
+ location = element_text(item.location.url) if item.location.length.positive? &&
36
+ item.location.url.length.positive?
36
37
 
37
- def process_related_item(item)
38
- return unless item.titleInfo.length.positive?
38
+ return if title.empty?
39
39
 
40
- title = item.titleInfo.text.strip
41
- return_text = title
42
- location = nil
43
- location = item.location.url.text if item.location.length.positive? &&
44
- item.location.url.length.positive?
45
- return_text = "<a href='#{location}'>#{title}</a>" if location && !title.empty?
40
+ if location
41
+ "<a href='#{location}'>#{title}</a>".html_safe
42
+ else
43
+ title
44
+ end
45
+ elsif item.note.any?
46
+ citation = item.note.find { |note| note['type'] == 'preferred citation' }
46
47
 
47
- ModsDisplay::Values.new(label: related_item_label(item), values: [return_text]) unless return_text.empty?
48
+ element_text(citation) if citation
49
+ end
48
50
  end
49
51
 
50
52
  def reference_title(item)
@@ -4,7 +4,7 @@ module ModsDisplay
4
4
  class ResourceType < Field
5
5
  def fields
6
6
  return_fields = @values.map do |value|
7
- ModsDisplay::Values.new(label: displayLabel(value) || label, values: [value.text.strip])
7
+ ModsDisplay::Values.new(label: displayLabel(value) || label, values: [element_text(value)])
8
8
  end
9
9
  collapse_fields(return_fields)
10
10
  end
@@ -12,10 +12,10 @@ module ModsDisplay
12
12
  if respond_to?(:"process_#{child.name}")
13
13
  method_send = send(:"process_#{child.name}", child)
14
14
  return_text << method_send unless method_send.to_s.empty?
15
- elsif child.text.include?('--')
16
- return_text << child.text.split('--').map(&:strip)
15
+ elsif element_text(child).include?('--')
16
+ return_text << element_text(child).split('--').map(&:strip)
17
17
  else
18
- return_text << child.text unless child.text.empty?
18
+ return_text << element_text(child) unless element_text(child).empty?
19
19
  end
20
20
  end
21
21
  return_values << return_text.flatten unless return_text.empty?
@@ -54,10 +54,10 @@ module ModsDisplay
54
54
  def values_from_subjects(element)
55
55
  return_values = []
56
56
  selected_subjects(element).each do |child|
57
- return_values << if child.text.include?('--')
58
- child.text.split('--').map(&:strip)
57
+ return_values << if element_text(child).include?('--')
58
+ element_text(child).split('--').map(&:strip)
59
59
  else
60
- child.text.strip
60
+ element_text(child)
61
61
  end
62
62
  end
63
63
  return_values
@@ -16,7 +16,7 @@ module ModsDisplay
16
16
  private
17
17
 
18
18
  def delimiter
19
- '<br />'
19
+ '<br />'.html_safe
20
20
  end
21
21
 
22
22
  def assemble_title(element)
@@ -24,7 +24,7 @@ module ModsDisplay
24
24
  previous_element = nil
25
25
 
26
26
  element.children.select { |value| title_parts.include? value.name }.each do |value|
27
- str = value.text.strip
27
+ str = element_text(value)
28
28
  next if str.empty?
29
29
 
30
30
  delimiter = if title.empty? || title.end_with?(' ')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ModsDisplay
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mods_display
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jessie Keck
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-08 00:00:00.000000000 Z
11
+ date: 2022-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: stanford-mods