mods_display 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +16 -0
- data/.rubocop_todo.yml +60 -0
- data/.travis.yml +2 -2
- data/Rakefile +10 -2
- data/lib/mods_display.rb +42 -42
- data/lib/mods_display/configuration.rb +69 -67
- data/lib/mods_display/configuration/access_condition.rb +17 -13
- data/lib/mods_display/configuration/base.rb +27 -24
- data/lib/mods_display/configuration/format.rb +8 -4
- data/lib/mods_display/configuration/genre.rb +8 -4
- data/lib/mods_display/configuration/imprint.rb +12 -7
- data/lib/mods_display/configuration/name.rb +8 -4
- data/lib/mods_display/configuration/note.rb +8 -4
- data/lib/mods_display/configuration/related_item.rb +8 -4
- data/lib/mods_display/configuration/subject.rb +11 -9
- data/lib/mods_display/configuration/title.rb +8 -4
- data/lib/mods_display/controller_extension.rb +24 -24
- data/lib/mods_display/country_codes.rb +385 -384
- data/lib/mods_display/fields/abstract.rb +7 -6
- data/lib/mods_display/fields/access_condition.rb +55 -55
- data/lib/mods_display/fields/audience.rb +7 -6
- data/lib/mods_display/fields/cartographics.rb +15 -14
- data/lib/mods_display/fields/collection.rb +32 -17
- data/lib/mods_display/fields/contact.rb +15 -13
- data/lib/mods_display/fields/contents.rb +7 -6
- data/lib/mods_display/fields/description.rb +21 -21
- data/lib/mods_display/fields/field.rb +164 -109
- data/lib/mods_display/fields/format.rb +36 -33
- data/lib/mods_display/fields/genre.rb +12 -11
- data/lib/mods_display/fields/identifier.rb +34 -34
- data/lib/mods_display/fields/imprint.rb +258 -214
- data/lib/mods_display/fields/language.rb +18 -16
- data/lib/mods_display/fields/location.rb +27 -26
- data/lib/mods_display/fields/name.rb +113 -118
- data/lib/mods_display/fields/note.rb +33 -34
- data/lib/mods_display/fields/related_item.rb +64 -66
- data/lib/mods_display/fields/resource_type.rb +13 -11
- data/lib/mods_display/fields/sub_title.rb +6 -4
- data/lib/mods_display/fields/subject.rb +92 -90
- data/lib/mods_display/fields/title.rb +54 -49
- data/lib/mods_display/fields/values.rb +7 -8
- data/lib/mods_display/html.rb +117 -96
- data/lib/mods_display/model_extension.rb +17 -16
- data/lib/mods_display/relator_codes.rb +269 -267
- data/lib/mods_display/version.rb +1 -1
- data/mods_display.gemspec +13 -12
- data/spec/configuration/access_condition_spec.rb +5 -5
- data/spec/configuration/base_spec.rb +24 -24
- data/spec/fields/abstract_spec.rb +24 -14
- data/spec/fields/access_condition_spec.rb +80 -55
- data/spec/fields/audience_spec.rb +15 -12
- data/spec/fields/cartographics_spec.rb +17 -18
- data/spec/fields/collection_spec.rb +65 -19
- data/spec/fields/contact_spec.rb +11 -9
- data/spec/fields/contents_spec.rb +15 -12
- data/spec/fields/description_spec.rb +40 -22
- data/spec/fields/format_spec.rb +28 -18
- data/spec/fields/genre_spec.rb +18 -16
- data/spec/fields/identifier_spec.rb +46 -34
- data/spec/fields/imprint_spec.rb +151 -125
- data/spec/fields/language_spec.rb +36 -20
- data/spec/fields/location_spec.rb +43 -27
- data/spec/fields/name_spec.rb +92 -92
- data/spec/fields/note_spec.rb +53 -40
- data/spec/fields/related_item_spec.rb +42 -40
- data/spec/fields/resource_type_spec.rb +20 -14
- data/spec/fields/sub_title_spec.rb +11 -9
- data/spec/fields/subject_spec.rb +61 -56
- data/spec/fields/title_spec.rb +53 -40
- data/spec/fixtures/access_condition_fixtures.rb +50 -0
- data/spec/fixtures/cartographics_fixtures.rb +1 -1
- data/spec/fixtures/imprint_fixtures.rb +49 -1
- data/spec/fixtures/name_fixtures.rb +195 -2
- data/spec/fixtures/related_item_fixtures.rb +107 -0
- data/spec/fixtures/subjects_fixtures.rb +15 -2
- data/spec/fixtures/title_fixtures.rb +101 -0
- data/spec/integration/configuration_spec.rb +23 -20
- data/spec/integration/html_spec.rb +40 -29
- data/spec/integration/installation_spec.rb +14 -14
- data/spec/spec_helper.rb +5 -8
- metadata +25 -3
@@ -1,8 +1,9 @@
|
|
1
|
-
|
1
|
+
module ModsDisplay
|
2
|
+
class Abstract < Field
|
3
|
+
private
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
5
|
+
def displayLabel(element)
|
6
|
+
super(element) || I18n.t('mods_display.abstract')
|
7
|
+
end
|
6
8
|
end
|
7
|
-
|
8
|
-
end
|
9
|
+
end
|
@@ -1,73 +1,73 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module ModsDisplay
|
2
|
+
class AccessCondition < Field
|
3
|
+
def fields
|
4
|
+
return_fields = @values.map do |value|
|
5
|
+
ModsDisplay::Values.new(
|
6
|
+
label: displayLabel(value) || access_label(value),
|
7
|
+
values: [process_access_statement(value)]
|
8
|
+
)
|
9
|
+
end
|
10
|
+
collapse_fields(return_fields)
|
6
11
|
end
|
7
|
-
collapse_fields(return_fields)
|
8
|
-
end
|
9
12
|
|
10
|
-
|
13
|
+
private
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
def process_access_statement(element)
|
16
|
+
case normalize_type(element)
|
17
|
+
when 'copyright'
|
18
|
+
copyright_statement(element)
|
19
|
+
when 'license'
|
20
|
+
license_statement(element)
|
21
|
+
else
|
22
|
+
element.text
|
23
|
+
end
|
20
24
|
end
|
21
|
-
end
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
+
def copyright_statement(element)
|
27
|
+
element.text.gsub(/\(c\) copyright/i, '©').gsub(/\(c\)/i, '©')
|
28
|
+
end
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
if license_link(
|
31
|
-
|
30
|
+
def license_statement(element)
|
31
|
+
element.text[/^(.*) (.*):(.*)$/]
|
32
|
+
output = "<div class='#{[Regexp.last_match(1), Regexp.last_match(2)].join('-').downcase}'>"
|
33
|
+
if license_link(Regexp.last_match(1), Regexp.last_match(2))
|
34
|
+
link = "<a href='#{license_link(Regexp.last_match(1), Regexp.last_match(2))}'>#{Regexp.last_match(3).strip}</a>"
|
35
|
+
output << link
|
32
36
|
else
|
33
|
-
|
37
|
+
output << Regexp.last_match(3).strip
|
34
38
|
end
|
35
|
-
|
36
|
-
|
39
|
+
output << '</div>'
|
40
|
+
end
|
37
41
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
def license_code_urls
|
43
|
+
{ 'cc' => 'http://creativecommons.org/licenses/',
|
44
|
+
'odc' => 'http://opendatacommons.org/licenses/' }
|
45
|
+
end
|
42
46
|
|
43
|
-
|
44
|
-
|
45
|
-
|
47
|
+
def license_link(code, type)
|
48
|
+
code = code.downcase
|
49
|
+
return unless license_code_urls.key?(code)
|
46
50
|
"#{license_code_urls[code]}#{type.downcase}#{"/#{@config.cc_license_version}/" if code == 'cc'}"
|
47
51
|
end
|
48
|
-
end
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
53
|
+
def access_label(element)
|
54
|
+
type = normalize_type(element)
|
55
|
+
return access_labels[type] if access_labels.key?(type)
|
56
|
+
I18n.t('mods_display.access_condition')
|
54
57
|
end
|
55
|
-
I18n.t('mods_display.access_condition')
|
56
|
-
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
59
|
+
def normalize_type(element)
|
60
|
+
type = element.attributes['type']
|
61
|
+
return type.value.strip.gsub(/\s*/, '').downcase if type.respond_to?(:value)
|
62
|
+
''
|
62
63
|
end
|
63
|
-
""
|
64
|
-
end
|
65
64
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
65
|
+
def access_labels
|
66
|
+
{ 'useandreproduction' => I18n.t('mods_display.use_and_reproduction'),
|
67
|
+
'restrictiononaccess' => I18n.t('mods_display.restriction_on_access'),
|
68
|
+
'copyright' => I18n.t('mods_display.copyright'),
|
69
|
+
'license' => I18n.t('mods_display.license')
|
70
|
+
}
|
71
|
+
end
|
72
72
|
end
|
73
|
-
end
|
73
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
|
-
|
1
|
+
module ModsDisplay
|
2
|
+
class Audience < Field
|
3
|
+
private
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
5
|
+
def displayLabel(element)
|
6
|
+
super(element) || I18n.t('mods_display.target_audience')
|
7
|
+
end
|
6
8
|
end
|
7
|
-
|
8
|
-
end
|
9
|
+
end
|
@@ -1,21 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module ModsDisplay
|
2
|
+
class Cartographics < Field
|
3
|
+
def fields
|
4
|
+
return nil if @values.nil?
|
5
|
+
return_fields = []
|
6
|
+
@values.each do |value|
|
7
|
+
next unless value.respond_to?(:cartographics)
|
8
8
|
value.cartographics.each do |field|
|
9
|
-
scale = field.scale.empty? ?
|
9
|
+
scale = field.scale.empty? ? 'Scale not given' : field.scale.text
|
10
10
|
projection = field.projection.empty? ? nil : field.projection.text
|
11
11
|
coordinates = field.coordinates.empty? ? nil : field.coordinates.text
|
12
|
-
post_scale = [projection, coordinates].compact.length > 0 ? [projection, coordinates].compact.join(
|
13
|
-
return_fields << ModsDisplay::Values.new(
|
14
|
-
|
12
|
+
post_scale = [projection, coordinates].compact.length > 0 ? [projection, coordinates].compact.join(' ') : nil
|
13
|
+
return_fields << ModsDisplay::Values.new(
|
14
|
+
label: (displayLabel(field) || label || I18n.t('mods_display.map_data')),
|
15
|
+
values: [[scale, post_scale].compact.join(' ; ')]
|
16
|
+
)
|
15
17
|
end
|
16
18
|
end
|
19
|
+
collapse_fields(return_fields)
|
17
20
|
end
|
18
|
-
collapse_fields(return_fields)
|
19
21
|
end
|
20
|
-
|
21
|
-
end
|
22
|
+
end
|
@@ -1,21 +1,36 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module ModsDisplay
|
2
|
+
###
|
3
|
+
# Collection class to parse collection data out of Mods relatedItem fields
|
4
|
+
###
|
5
|
+
class Collection < Field
|
6
|
+
def collection_label(value)
|
7
|
+
displayLabel(value) || I18n.t('mods_display.collection')
|
8
|
+
end
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
return_fields << ModsDisplay::Values.new(:label => label, :values => [value.titleInfo.text.strip])
|
10
|
+
def fields
|
11
|
+
return_fields = []
|
12
|
+
@values.each do |value|
|
13
|
+
next unless related_item_is_a_collection?(value)
|
14
|
+
return_fields << ModsDisplay::Values.new(
|
15
|
+
label: collection_label(value),
|
16
|
+
values: [value.titleInfo.text.strip]
|
17
|
+
)
|
16
18
|
end
|
19
|
+
collapse_fields(return_fields)
|
17
20
|
end
|
18
|
-
collapse_fields(return_fields)
|
19
|
-
end
|
20
21
|
|
21
|
-
|
22
|
+
private
|
23
|
+
|
24
|
+
def related_item_is_a_collection?(value)
|
25
|
+
value.respond_to?(:titleInfo) && resource_type_is_collection?(value)
|
26
|
+
end
|
27
|
+
|
28
|
+
def resource_type_is_collection?(value)
|
29
|
+
return unless value.respond_to?(:typeOfResource)
|
30
|
+
return unless value.typeOfResource.attributes.length > 0
|
31
|
+
value.typeOfResource.attributes.length > 0 &&
|
32
|
+
value.typeOfResource.attributes.first.key?('collection') &&
|
33
|
+
value.typeOfResource.attributes.first['collection'].value == 'yes'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,17 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module ModsDisplay
|
2
|
+
class Contact < Field
|
3
|
+
def fields
|
4
|
+
return_fields = contact_fields.map do |value|
|
5
|
+
ModsDisplay::Values.new(label: displayLabel(value) || I18n.t('mods_display.contact'), values: [value.text])
|
6
|
+
end
|
7
|
+
collapse_fields(return_fields)
|
5
8
|
end
|
6
|
-
collapse_fields(return_fields)
|
7
|
-
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
value.attributes[
|
10
|
+
private
|
11
|
+
|
12
|
+
def contact_fields
|
13
|
+
@values.select do |value|
|
14
|
+
value.attributes['type'].respond_to?(:value) &&
|
15
|
+
value.attributes['type'].value.downcase == 'contact'
|
16
|
+
end
|
14
17
|
end
|
15
18
|
end
|
16
|
-
|
17
|
-
end
|
19
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
|
-
|
1
|
+
module ModsDisplay
|
2
|
+
class Contents < Field
|
3
|
+
private
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
5
|
+
def displayLabel(element)
|
6
|
+
super(element) || I18n.t('mods_display.table_of_contents')
|
7
|
+
end
|
6
8
|
end
|
7
|
-
|
8
|
-
end
|
9
|
+
end
|
@@ -1,28 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module ModsDisplay
|
2
|
+
class Description < Field
|
3
|
+
def fields
|
4
|
+
return_fields = description_fields.map do |value|
|
5
|
+
ModsDisplay::Values.new(label: description_label(value), values: [value.text])
|
6
|
+
end
|
7
|
+
collapse_fields(return_fields)
|
6
8
|
end
|
7
|
-
collapse_fields(return_fields)
|
8
|
-
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def description_label(element)
|
11
|
+
label || displayLabel(element) || labels[element.name.to_sym] || I18n.t('mods_display.physical_description')
|
12
|
+
end
|
13
13
|
|
14
|
-
|
14
|
+
private
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def description_fields
|
17
|
+
@values.children.select do |child|
|
18
|
+
labels.keys.include?(child.name.to_sym)
|
19
|
+
end
|
19
20
|
end
|
20
|
-
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def labels
|
23
|
+
{ digitalOrigin: I18n.t('mods_display.digital_origin'),
|
24
|
+
note: I18n.t('mods_display.note')
|
25
|
+
}
|
26
|
+
end
|
26
27
|
end
|
27
|
-
|
28
|
-
end
|
28
|
+
end
|
@@ -1,140 +1,195 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
module ModsDisplay
|
3
|
+
class Field
|
4
|
+
def initialize(values, config, klass)
|
5
|
+
@values = values
|
6
|
+
@config = config
|
7
|
+
@klass = klass
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def fields
|
11
|
+
return_fields = @values.map do |value|
|
12
|
+
ModsDisplay::Values.new(
|
13
|
+
label: displayLabel(value) || label,
|
14
|
+
values: [displayForm(@values) || value.text].flatten
|
15
|
+
)
|
16
|
+
end
|
17
|
+
collapse_fields(return_fields)
|
12
18
|
end
|
13
|
-
collapse_fields(return_fields)
|
14
|
-
end
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
+
def label
|
21
|
+
return nil if @values.nil?
|
22
|
+
displayLabel(@values.first)
|
23
|
+
end
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
def to_html
|
26
|
+
return nil if fields.empty? || @config.ignore?
|
27
|
+
output = ''
|
28
|
+
fields.each do |field|
|
29
|
+
next unless field.values.any? { |f| f && !f.empty? }
|
26
30
|
output << "<dt#{label_class} #{sanitized_field_title(field.label)}>#{field.label}</dt>"
|
27
31
|
output << "<dd#{value_class}>"
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
output <<
|
32
|
+
output << field.values.map do |val|
|
33
|
+
@config.link ? link_to_value(val.to_s) : link_urls_and_email(val.to_s)
|
34
|
+
end.join(@config.delimiter)
|
35
|
+
output << '</dd>'
|
32
36
|
end
|
37
|
+
output
|
33
38
|
end
|
34
|
-
output
|
35
|
-
end
|
36
39
|
|
37
|
-
|
40
|
+
private
|
38
41
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
def compact_and_join_with_delimiter(values, delimiter)
|
43
|
+
compact_values = values.compact.reject { |v| v.strip.empty? }
|
44
|
+
return compact_values.join(delimiter) if compact_values.length == 1 ||
|
45
|
+
!ends_in_terminating_punctuation?(delimiter)
|
46
|
+
compact_values.each_with_index.map do |value, i|
|
47
|
+
if (compact_values.length - 1) == i || # last item?
|
48
|
+
ends_in_terminating_punctuation?(value)
|
49
|
+
value << ' '
|
50
|
+
else
|
51
|
+
value << delimiter
|
52
|
+
end
|
53
|
+
end.join.strip
|
54
|
+
end
|
42
55
|
|
43
|
-
|
44
|
-
|
45
|
-
|
56
|
+
def process_bc_ad_dates(date_fields)
|
57
|
+
date_fields.map do |date_field|
|
58
|
+
case
|
59
|
+
when date_is_bc_edtf?(date_field)
|
60
|
+
year = date_field.text.strip.gsub(/^-0*/, '').to_i - 1
|
61
|
+
date_field.content = "#{year} B.C."
|
62
|
+
when date_is_ad?(date_field)
|
63
|
+
date_field.content = "#{date_field.text.strip.gsub(/^0*/, '')} A.D."
|
64
|
+
end
|
65
|
+
date_field
|
66
|
+
end
|
67
|
+
end
|
46
68
|
|
47
|
-
|
48
|
-
|
49
|
-
|
69
|
+
def date_is_bc_edtf?(date_field)
|
70
|
+
date_field.text.strip.start_with?('-') && date_is_edtf?(date_field)
|
71
|
+
end
|
50
72
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
return display.text if display
|
55
|
-
end
|
73
|
+
def date_is_ad?(date_field)
|
74
|
+
date_field.text.strip.gsub(/^0*/, '').length < 4
|
75
|
+
end
|
56
76
|
|
57
|
-
|
58
|
-
|
59
|
-
element.attributes["displayLabel"].respond_to?(:value))
|
60
|
-
"#{element.attributes["displayLabel"].value}:"
|
77
|
+
def date_is_edtf?(date_field)
|
78
|
+
field_is_encoded?(date_field, 'edtf')
|
61
79
|
end
|
62
|
-
end
|
63
80
|
|
64
|
-
|
65
|
-
|
66
|
-
|
81
|
+
def field_is_encoded?(field, encoding)
|
82
|
+
field.attributes['encoding'] &&
|
83
|
+
field.attributes['encoding'].respond_to?(:value) &&
|
84
|
+
field.attributes['encoding'].value.downcase == encoding
|
85
|
+
end
|
67
86
|
|
68
|
-
|
69
|
-
|
70
|
-
if object.is_a?(Hash)
|
71
|
-
object.each do |k,v|
|
72
|
-
object[k] = replace_token(v, value)
|
73
|
-
end
|
74
|
-
elsif object.is_a?(String)
|
75
|
-
object = replace_token(object, value)
|
87
|
+
def ends_in_terminating_punctuation?(value)
|
88
|
+
value.strip.end_with?('.', ',', ':', ';')
|
76
89
|
end
|
77
|
-
object
|
78
|
-
end
|
79
90
|
|
80
|
-
|
81
|
-
|
82
|
-
tokens.each do |token|
|
83
|
-
string.gsub!(token, value)
|
91
|
+
def label_class
|
92
|
+
" class='#{@config.label_class}'" unless @config.label_class == ''
|
84
93
|
end
|
85
|
-
string
|
86
|
-
end
|
87
94
|
|
88
|
-
|
89
|
-
|
90
|
-
|
95
|
+
def value_class
|
96
|
+
" class='#{@config.value_class}'" unless @config.value_class == ''
|
97
|
+
end
|
91
98
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
99
|
+
def link_to_value(link_text, link_href = nil)
|
100
|
+
href_or_text = link_href || link_text
|
101
|
+
"<a href='#{@klass.send(@config.link[0], replace_tokens(@config.link[1], href_or_text))}'>#{link_text}</a>"
|
102
|
+
end
|
103
|
+
|
104
|
+
def displayForm(element)
|
105
|
+
return element unless element # basically return nil
|
106
|
+
display = element.children.find { |c| c.name == 'displayForm' }
|
107
|
+
return display.text if display
|
108
|
+
end
|
109
|
+
|
110
|
+
def displayLabel(element)
|
111
|
+
return unless element.respond_to?(:attributes) &&
|
112
|
+
element.attributes['displayLabel'].respond_to?(:value)
|
113
|
+
"#{element.attributes['displayLabel'].value}:"
|
114
|
+
end
|
115
|
+
|
116
|
+
def sanitized_field_title(label)
|
117
|
+
"title='#{label.gsub(/:$/, '').strip}'"
|
118
|
+
end
|
119
|
+
|
120
|
+
def replace_tokens(object, value)
|
121
|
+
object = object.dup
|
122
|
+
if object.is_a?(Hash)
|
123
|
+
object.each do |k, v|
|
124
|
+
object[k] = replace_token(v, value)
|
105
125
|
end
|
126
|
+
elsif object.is_a?(String)
|
127
|
+
object = replace_token(object, value)
|
106
128
|
end
|
129
|
+
object
|
107
130
|
end
|
108
|
-
val
|
109
|
-
end
|
110
131
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
132
|
+
def replace_token(string, value)
|
133
|
+
string = string.dup
|
134
|
+
tokens.each do |token|
|
135
|
+
string.gsub!(token, value)
|
136
|
+
end
|
137
|
+
string
|
138
|
+
end
|
139
|
+
|
140
|
+
def tokens
|
141
|
+
['%value%']
|
142
|
+
end
|
143
|
+
|
144
|
+
# rubocop:disable Metrics/LineLength
|
145
|
+
# Disabling line length due to necessarily long regular expressions
|
146
|
+
def link_urls_and_email(val)
|
147
|
+
val = val.dup
|
148
|
+
# http://daringfireball.net/2010/07/improved_regex_for_matching_urls
|
149
|
+
url = %r{(?i)\b(?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\([^\s()<>]+|\([^\s()<>]+\)*\))+(?:\([^\s()<>]+|\([^\s()<>]+\)*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])}i
|
150
|
+
# http://www.regular-expressions.info/email.html
|
151
|
+
email = /[A-Z0-9_\.%\+\-\']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)/i
|
152
|
+
matches = [val.scan(url), val.scan(email)].flatten
|
153
|
+
unless val =~ /<a/ # we'll assume that linking has alraedy occured and we don't want to double link
|
154
|
+
matches.each do |match|
|
155
|
+
if match =~ email
|
156
|
+
val = val.gsub(match, "<a href='mailto:#{match}'>#{match}</a>")
|
157
|
+
else
|
158
|
+
val = val.gsub(match, "<a href='#{match}'>#{match}</a>")
|
159
|
+
end
|
129
160
|
end
|
130
|
-
elsif prev_label and (current_label != prev_label)
|
131
|
-
return_values << ModsDisplay::Values.new(:label => prev_label, :values => buffer.flatten)
|
132
|
-
buffer = []
|
133
161
|
end
|
134
|
-
|
135
|
-
|
162
|
+
val
|
163
|
+
end
|
164
|
+
# rubocop:enable Metrics/LineLength
|
165
|
+
|
166
|
+
def collapse_fields(display_fields)
|
167
|
+
return_values = []
|
168
|
+
current_label = nil
|
169
|
+
prev_label = nil
|
170
|
+
buffer = []
|
171
|
+
display_fields.each_with_index do |field, index|
|
172
|
+
current_label = field.label
|
173
|
+
current_values = field.values
|
174
|
+
if display_fields.length == 1
|
175
|
+
return_values << ModsDisplay::Values.new(label: current_label, values: current_values)
|
176
|
+
elsif index == (display_fields.length - 1)
|
177
|
+
# need to deal w/ when we have a last element but we have separate labels in the buffer.
|
178
|
+
if current_label != prev_label
|
179
|
+
return_values << ModsDisplay::Values.new(label: prev_label, values: buffer.flatten)
|
180
|
+
return_values << ModsDisplay::Values.new(label: current_label, values: current_values)
|
181
|
+
else
|
182
|
+
buffer.concat(current_values)
|
183
|
+
return_values << ModsDisplay::Values.new(label: current_label, values: buffer.flatten)
|
184
|
+
end
|
185
|
+
elsif prev_label && (current_label != prev_label)
|
186
|
+
return_values << ModsDisplay::Values.new(label: prev_label, values: buffer.flatten)
|
187
|
+
buffer = []
|
188
|
+
end
|
189
|
+
buffer.concat(current_values)
|
190
|
+
prev_label = current_label
|
191
|
+
end
|
192
|
+
return_values
|
136
193
|
end
|
137
|
-
return_values
|
138
194
|
end
|
139
|
-
|
140
|
-
end
|
195
|
+
end
|