mods_display 0.0.1.beta4 → 0.0.1.beta5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,32 +1,7 @@
1
- # encoding: utf-8
2
1
  class ModsDisplay::Abstract < ModsDisplay::Field
3
2
 
4
3
  def label
5
4
  super || "Abstract"
6
5
  end
7
6
 
8
- def text
9
- return link_value(super) unless super.nil?
10
- link_value(@value.text)
11
- end
12
-
13
- private
14
-
15
- def link_value(val)
16
- val = val.dup
17
- # http://daringfireball.net/2010/07/improved_regex_for_matching_urls
18
- url = /(?i)\b(?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\([^\s()<>]+|\([^\s()<>]+\)*\))+(?:\([^\s()<>]+|\([^\s()<>]+\)*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])/i
19
- # http://www.regular-expressions.info/email.html
20
- email = /[A-Z0-9_\.%\+\-\']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)/i
21
- matches = [val.scan(url), val.scan(email)].flatten
22
- matches.each do |match|
23
- if match =~ email
24
- val = val.gsub(match, "<a href='mailto:#{match}'>#{match}</a>")
25
- else
26
- val = val.gsub(match, "<a href='#{match}'>#{match}</a>")
27
- end
28
- end
29
- val
30
- end
31
-
32
7
  end
@@ -1,14 +1,35 @@
1
1
  class ModsDisplay::Description < ModsDisplay::Field
2
2
 
3
3
  def fields
4
- description_fields.map do |description|
5
- ModsDisplay::Values.new({:label => (label || description_label(description) || labels[description.name.to_sym] || "Physical Description"),
6
- :values => [description.text]})
4
+ return_values = []
5
+ current_label = nil
6
+ prev_label = nil
7
+ buffer = []
8
+ description_fields.each_with_index do |val, index|
9
+ current_label = description_label(val)
10
+ if description_fields.length == 1
11
+ return_values << ModsDisplay::Values.new(:label => current_label, :values => [val.text])
12
+ elsif index == (description_fields.length-1)
13
+ # need to deal w/ when we have a last element but we have separate labels in the buffer.
14
+ if current_label != prev_label
15
+ return_values << ModsDisplay::Values.new(:label => prev_label, :values => buffer.flatten)
16
+ return_values << ModsDisplay::Values.new(:label => current_label, :values => [val.text])
17
+ else
18
+ buffer << val.text
19
+ return_values << ModsDisplay::Values.new(:label => current_label, :values => buffer.flatten)
20
+ end
21
+ elsif prev_label and (current_label != prev_label)
22
+ return_values << ModsDisplay::Values.new(:label => prev_label, :values => buffer.flatten)
23
+ buffer = []
24
+ end
25
+ buffer << val.text
26
+ prev_label = current_label
7
27
  end
28
+ return_values
8
29
  end
9
30
 
10
31
  def description_label(element)
11
- element.attributes["displayLabel"].value if element.attributes["displayLabel"].respond_to?(:value)
32
+ label || displayLabel(element) || labels[element.name.to_sym] || "Physical Description"
12
33
  end
13
34
 
14
35
  private
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  class ModsDisplay::Field
2
3
  def initialize(value, config, klass)
3
4
  @value = value
@@ -57,7 +58,7 @@ class ModsDisplay::Field
57
58
  output << "<dt#{label_class} title='#{field.label}'>#{field.label}:</dt>"
58
59
  output << "<dd#{value_class}>"
59
60
  output << field.values.map do |val|
60
- @config.link ? link_to_value(val.to_s) : val.to_s
61
+ @config.link ? link_to_value(val.to_s) : link_urls_and_email(val.to_s)
61
62
  end.join(@config.delimiter)
62
63
  output << "</dd>"
63
64
  end
@@ -114,4 +115,23 @@ class ModsDisplay::Field
114
115
  ["%value%"]
115
116
  end
116
117
 
118
+ def link_urls_and_email(val)
119
+ val = val.dup
120
+ # http://daringfireball.net/2010/07/improved_regex_for_matching_urls
121
+ url = /(?i)\b(?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\([^\s()<>]+|\([^\s()<>]+\)*\))+(?:\([^\s()<>]+|\([^\s()<>]+\)*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])/i
122
+ # http://www.regular-expressions.info/email.html
123
+ email = /[A-Z0-9_\.%\+\-\']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)/i
124
+ matches = [val.scan(url), val.scan(email)].flatten
125
+ unless val =~ /<a/ # we'll assume that linking has alraedy occured and we don't want to double link
126
+ matches.each do |match|
127
+ if match =~ email
128
+ val = val.gsub(match, "<a href='mailto:#{match}'>#{match}</a>")
129
+ else
130
+ val = val.gsub(match, "<a href='#{match}'>#{match}</a>")
131
+ end
132
+ end
133
+ end
134
+ val
135
+ end
136
+
117
137
  end
@@ -67,10 +67,22 @@ class ModsDisplay::Name < ModsDisplay::Field
67
67
  private
68
68
 
69
69
  def name_label(element)
70
- if element.attributes.has_key?("type") && name_labels.has_key?(element.attributes["type"].value)
71
- return name_labels[element.attributes["type"].value]
70
+ if name_is_main_author?(element)
71
+ if element.attributes.has_key?("type") && name_labels.has_key?(element.attributes["type"].value)
72
+ return name_labels[element.attributes["type"].value]
73
+ end
74
+ "Author/Creator"
75
+ else
76
+ "Contributor"
77
+ end
78
+ end
79
+
80
+ def name_is_main_author?(element)
81
+ begin
82
+ ["author", "aut", "creator", "cre", ""].include?(element.role.roleTerm.text.downcase)
83
+ rescue
84
+ false
72
85
  end
73
- "Creator/Contributor"
74
86
  end
75
87
 
76
88
  def name_labels
@@ -1,3 +1,3 @@
1
1
  module ModsDisplay
2
- VERSION = "0.0.1.beta4"
2
+ VERSION = "0.0.1.beta5"
3
3
  end
@@ -12,10 +12,10 @@ describe ModsDisplay::Abstract do
12
12
 
13
13
  describe "links" do
14
14
  it "should turn URLs into links" do
15
- mods_display_abstract(@link).fields.first.values.first.should match(/\(<a href='http:\/\/library.stanford.edu'>http:\/\/library.stanford.edu<\/a>\)/)
15
+ mods_display_abstract(@link).to_html.should match(/A link to the library \(<a href='http:\/\/library.stanford.edu'>http:\/\/library.stanford.edu<\/a>\) should appear here/)
16
16
  end
17
17
  it "should turn email addresses into mailto links" do
18
- mods_display_abstract(@email).fields.first.values.first.should match(/<a href='mailto:jdoe@example.com'>jdoe@example.com<\/a>/)
18
+ mods_display_abstract(@email).to_html.should match(/A link to an email address <a href='mailto:jdoe@example.com'>jdoe@example.com<\/a> should appear here/)
19
19
  end
20
20
  end
21
21
  end
@@ -18,18 +18,22 @@ describe ModsDisplay::Language do
18
18
  @name = Stanford::Mods::Record.new.from_str("<mods><name><namePart>John Doe</namePart></name></mods>", false).plain_name
19
19
  @blank_name = Stanford::Mods::Record.new.from_str("<mods><name><namePart/><role><roleTerm></roleTerm></role></name></mods>", false).plain_name
20
20
  @conf_name = Stanford::Mods::Record.new.from_str("<mods><name type='conference'><namePart>John Doe</namePart></name></mods>", false).plain_name
21
+ @contributor = Stanford::Mods::Record.new.from_str("<mods><name><namePart>John Doe</namePart><role><roleTerm>lithographer</roleTerm></role></name></mods>", false).plain_name
21
22
  @display_form = Stanford::Mods::Record.new.from_str("<mods><name><namePart>John Doe</namePart><displayForm>Mr. John Doe</displayForm></name></mods>", false).plain_name
22
23
  @collapse_label = Stanford::Mods::Record.new.from_str("<mods><name><namePart>John Doe</namePart></name><name><namePart>Jane Doe</namePart></name></mods>", false).plain_name
23
24
  @complex_labels = Stanford::Mods::Record.new.from_str("<mods><name><namePart>John Doe</namePart></name><name><namePart>Jane Doe</namePart></name><name type='conference'><namePart>John Dough</namePart></name><name><namePart>Jane Dough</namePart></name></mods>", false).plain_name
24
25
  @name_with_role = Stanford::Mods::Record.new.from_str("<mods><name><namePart>John Doe</namePart><role><roleTerm type='text'>Depicted</roleTerm></role></name></mods>", false).plain_name
25
26
  end
26
27
  describe "label" do
27
- it "should default Creator/Contributor when none is available" do
28
- mods_display_name(@name).fields.first.label.should == "Creator/Contributor"
28
+ it "should default Author/Creator when none is available" do
29
+ mods_display_name(@name).fields.first.label.should == "Author/Creator"
29
30
  end
30
31
  it "should derive the name from the type attribute if one is available" do
31
32
  mods_display_name(@conf_name).fields.first.label.should == "Meeting"
32
33
  end
34
+ it "should apply contributor labeling to all non blank/author/creator roles" do
35
+ mods_display_name(@contributor).fields.first.label.should == "Contributor"
36
+ end
33
37
  end
34
38
 
35
39
  describe "fields" do
@@ -53,7 +57,7 @@ describe ModsDisplay::Language do
53
57
  it "should collapse adjacent matching labels" do
54
58
  fields = mods_display_name(@collapse_label).fields
55
59
  fields.length.should == 1
56
- fields.first.label.should == "Creator/Contributor"
60
+ fields.first.label.should == "Author/Creator"
57
61
  fields.first.values.each do |val|
58
62
  ["John Doe", "Jane Doe"].should include val.to_s
59
63
  end
@@ -62,7 +66,7 @@ describe ModsDisplay::Language do
62
66
  fields = mods_display_name(@complex_labels).fields
63
67
 
64
68
  fields.length.should == 3
65
- fields.first.label.should == "Creator/Contributor"
69
+ fields.first.label.should == "Author/Creator"
66
70
  fields.first.values.length.should == 2
67
71
  fields.first.values.each do |val|
68
72
  ["John Doe", "Jane Doe"].should include val.to_s
@@ -72,7 +76,7 @@ describe ModsDisplay::Language do
72
76
  fields[1].values.length.should == 1
73
77
  fields[1].values.first.to_s.should == "John Dough"
74
78
 
75
- fields.last.label.should == "Creator/Contributor"
79
+ fields.last.label.should == "Author/Creator"
76
80
  fields.last.values.length.should == 1
77
81
  fields.last.values.first.to_s.should == "Jane Dough"
78
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mods_display
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta4
4
+ version: 0.0.1.beta5
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-01 00:00:00.000000000 Z
12
+ date: 2013-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stanford-mods
@@ -139,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
139
  version: '0'
140
140
  segments:
141
141
  - 0
142
- hash: -2208754655843204973
142
+ hash: -354569883239195069
143
143
  required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  none: false
145
145
  requirements: