uc3-dmp-citation 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f168bafa37250b52f74453f1e9f044fd5ba07eab51632b650207514d7c9decd
4
- data.tar.gz: b1f37ab91666d6356f29b6d5da6242947310df4fe5f7e337d75310b6ea78ec7c
3
+ metadata.gz: f294fbee4a95b3148dab60d1c48b56a67ee71dfdb3d103b11814c68802c1d522
4
+ data.tar.gz: f61d50ddf7597ba1300b373450d306d7ff982b133197e40e87dddb5c3e1404bf
5
5
  SHA512:
6
- metadata.gz: d6fbdb819e93454d608a3f281e0be0839e2b7785b8072035b3a36192c5ae2be8fe79a6433174f37bdbaec8f99b836bce0a993d8189e966bd78f40a1a22f174dc
7
- data.tar.gz: c56061f1ce9e204c97d05ddd03d385b0dcc4044e4f3bfc26e3f4ed436e26550059e8d44c0ba5b64e14ad941f4ffae74fb61d6bdaab6aaaa35cddbfc3a974b8c2
6
+ metadata.gz: b395c33034a154b1052aefaf7f02d5fd6fa50e35bdc0dfe143ae1a33c7d5cddd3fdcc5e7964f930441c1354341cf72fb75e92a18db602419ff2e4bea0a7fa431
7
+ data.tar.gz: 755a385577fd2ef925df4a4671d26e6925751462ec24995726cefe483adfe512f5c048c48f8419af4d6c9af595664f46a60ae54ac15704fe5d950345e36d761f
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uc3DmpCitation
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
@@ -20,84 +20,86 @@ module Uc3DmpCitation
20
20
  MSG_BIBTEX_FAILURE = 'Unable to fetch Bibtex for the specified DOI.'
21
21
  MSG_UNABLE_TO_UPDATE = 'Unable to update the citations on the DMP ID.'
22
22
 
23
- def fetch_citation(doi:, logger: nil)
24
- uri = _doi_to_uri(doi: doi)
25
- return nil if uri.nil? || uri.blank?
26
-
27
- logger.debug(message: "Fetching BibTeX from: #{uri}") if logger.respond_to?(:debug)
28
- resp = Uc3DmpExternalApi::Client.call(url: uri, method: :get, additional_headers: headers, logger: logger)
29
- return nil if resp.nil? || resp.to_s.strip.empty?
30
-
31
- bibtex = BibTeX.parse(_cleanse_bibtex(text: resp))
32
- work_type = identifier['work_type'].nil? ? determine_work_type(bibtex: bibtex) : identifier['work_type']
33
- _bibtex_to_citation(uri: uri, work_type: work_type, bibtex: bibtex)
34
- end
23
+ class << self
24
+ def fetch_citation(doi:, logger: nil)
25
+ uri = _doi_to_uri(doi: doi)
26
+ return nil if uri.nil? || uri.blank?
27
+
28
+ logger.debug(message: "Fetching BibTeX from: #{uri}") if logger.respond_to?(:debug)
29
+ resp = Uc3DmpExternalApi::Client.call(url: uri, method: :get, additional_headers: headers, logger: logger)
30
+ return nil if resp.nil? || resp.to_s.strip.empty?
31
+
32
+ bibtex = BibTeX.parse(_cleanse_bibtex(text: resp))
33
+ work_type = identifier['work_type'].nil? ? determine_work_type(bibtex: bibtex) : identifier['work_type']
34
+ _bibtex_to_citation(uri: uri, work_type: work_type, bibtex: bibtex)
35
+ end
35
36
 
36
- private
37
+ private
37
38
 
38
- # Will convert 'doi:10.1234/abcdefg' to 'http://doi.org/10.1234/abcdefg'
39
- def _doi_to_uri(doi:)
40
- val = doi.match(DOI_REGEX).to_s
41
- return nil if val.nil? || val.strip == ''
39
+ # Will convert 'doi:10.1234/abcdefg' to 'http://doi.org/10.1234/abcdefg'
40
+ def _doi_to_uri(doi:)
41
+ val = doi.match(DOI_REGEX).to_s
42
+ return nil if val.nil? || val.strip == ''
42
43
 
43
- doi.start_with?('http') ? doi : "#{DEFAULT_DOI_URL}/#{doi.gsub('doi:', '')}"
44
- end
44
+ doi.start_with?('http') ? doi : "#{DEFAULT_DOI_URL}/#{doi.gsub('doi:', '')}"
45
+ end
45
46
 
46
- # If no :work_type was specified we can try to derive it from the BibTeX metadata
47
- def _determine_work_type(bibtex:)
48
- return '' if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
47
+ # If no :work_type was specified we can try to derive it from the BibTeX metadata
48
+ def _determine_work_type(bibtex:)
49
+ return '' if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
49
50
 
50
- return 'article' unless bibtex.data.first.journal.nil?
51
+ return 'article' unless bibtex.data.first.journal.nil?
51
52
 
52
- ''
53
- end
53
+ ''
54
+ end
54
55
 
55
- def _cleanse_bibtex(text:)
56
- return nil if text.nil? || text.to_s.strip == ''
57
-
58
- # Make sure we're working with UTF8
59
- utf8 = text.force_encoding('UTF-8')
60
-
61
- # Remove any encoded HTML (e.g. "Regular text $\\lt$strong$\\gt$Bold text$\\lt$/strong$\\gt$")
62
- utf8 = utf8.gsub(%r{\$?\\\$?(less|lt|Lt)\$/?[a-zA-Z]+\$?\\\$?(greater|gt|Gt)\$}, '')
63
- # Replace any special dash, semicolon and quote characters with a minus sign or single/double quote
64
- utf8 = utf8.gsub(%r{\$?\\(T|t)ext[a-zA-Z]+dash\$?}, '-').gsub(%r{\{(T|t)ext[a-zA-Z]+dash\}}, '-')
65
- .gsub(%r{\$?\\(M|m)athsemicolon\$?}, ':').gsub(%r{\{(M|m)semicolon\}}, ':')
66
- .gsub(%r{\$?\\(T|t)extquotesingle\$?}, "'").gsub(%r{\{(T|t)extquotesingle\}}, "'")
67
- .gsub(%r{\$?\\(T|t)extquotedouble\$?}, '"').gsub(%r{\{(T|t)extquotedouble\}}, '"')
68
- # Remove any remaining `\v` entries which attempt to construct an accented character
69
- utf8.gsub(%r{\\v}, '')
70
- end
56
+ def _cleanse_bibtex(text:)
57
+ return nil if text.nil? || text.to_s.strip == ''
58
+
59
+ # Make sure we're working with UTF8
60
+ utf8 = text.force_encoding('UTF-8')
61
+
62
+ # Remove any encoded HTML (e.g. "Regular text $\\lt$strong$\\gt$Bold text$\\lt$/strong$\\gt$")
63
+ utf8 = utf8.gsub(%r{\$?\\\$?(less|lt|Lt)\$/?[a-zA-Z]+\$?\\\$?(greater|gt|Gt)\$}, '')
64
+ # Replace any special dash, semicolon and quote characters with a minus sign or single/double quote
65
+ utf8 = utf8.gsub(%r{\$?\\(T|t)ext[a-zA-Z]+dash\$?}, '-').gsub(%r{\{(T|t)ext[a-zA-Z]+dash\}}, '-')
66
+ .gsub(%r{\$?\\(M|m)athsemicolon\$?}, ':').gsub(%r{\{(M|m)semicolon\}}, ':')
67
+ .gsub(%r{\$?\\(T|t)extquotesingle\$?}, "'").gsub(%r{\{(T|t)extquotesingle\}}, "'")
68
+ .gsub(%r{\$?\\(T|t)extquotedouble\$?}, '"').gsub(%r{\{(T|t)extquotedouble\}}, '"')
69
+ # Remove any remaining `\v` entries which attempt to construct an accented character
70
+ utf8.gsub(%r{\\v}, '')
71
+ end
71
72
 
72
- # Convert the BibTeX item to a citation
73
- def _bibtex_to_citation(uri:, work_type: DEFAULT_WORK_TYPE, bibtex:, style: DEFAULT_CITATION_STYLE)
74
- return nil unless uri.is_a?(String) && uri.strip != ''
75
- return nil if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
73
+ # Convert the BibTeX item to a citation
74
+ def _bibtex_to_citation(uri:, work_type: DEFAULT_WORK_TYPE, bibtex:, style: DEFAULT_CITATION_STYLE)
75
+ return nil unless uri.is_a?(String) && uri.strip != ''
76
+ return nil if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
76
77
 
77
- cp = CiteProc::Processor.new(style: style, format: 'html')
78
- cp.import(bibtex.to_citeproc)
79
- citation = cp.render(:bibliography, id: bibtex.data.first.id)
80
- return nil unless citation.is_a?(Array) && citation.any?
78
+ cp = CiteProc::Processor.new(style: style, format: 'html')
79
+ cp.import(bibtex.to_citeproc)
80
+ citation = cp.render(:bibliography, id: bibtex.data.first.id)
81
+ return nil unless citation.is_a?(Array) && citation.any?
81
82
 
82
- # The CiteProc renderer has trouble with some things so fix them here
83
- # - For some reason words in all caps in the title get wrapped in curl brackets
84
- citation = citation.first.gsub('{', '').gsub('}', '')
83
+ # The CiteProc renderer has trouble with some things so fix them here
84
+ # - For some reason words in all caps in the title get wrapped in curl brackets
85
+ citation = citation.first.gsub('{', '').gsub('}', '')
85
86
 
86
- unless work_type.nil? || work_type.strip == ''
87
- # This supports the :apa and :chicago-author-date styles
88
- citation = citation.gsub(/\.”\s+/, "\.” [#{work_type.gsub('_', ' ').capitalize}]. ")
89
- .gsub(/<\/i>\.\s+/, "<\/i>\. [#{work_type.gsub('_', ' ').capitalize}]. ")
90
- end
87
+ unless work_type.nil? || work_type.strip == ''
88
+ # This supports the :apa and :chicago-author-date styles
89
+ citation = citation.gsub(/\.”\s+/, "\.” [#{work_type.gsub('_', ' ').capitalize}]. ")
90
+ .gsub(/<\/i>\.\s+/, "<\/i>\. [#{work_type.gsub('_', ' ').capitalize}]. ")
91
+ end
91
92
 
92
- # Convert the URL into a link. Ensure that the trailing period is not a part of
93
- # the link!
94
- citation.gsub(URI.regexp) do |url|
95
- if url.start_with?('http')
96
- '<a href="%{url}" target="_blank">%{url}</a>.' % {
97
- url: url.end_with?('.') ? uri : "#{uri}."
98
- }
99
- else
100
- url
93
+ # Convert the URL into a link. Ensure that the trailing period is not a part of
94
+ # the link!
95
+ citation.gsub(URI.regexp) do |url|
96
+ if url.start_with?('http')
97
+ '<a href="%{url}" target="_blank">%{url}</a>.' % {
98
+ url: url.end_with?('.') ? uri : "#{uri}."
99
+ }
100
+ else
101
+ url
102
+ end
101
103
  end
102
104
  end
103
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uc3-dmp-citation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Riley