uc3-dmp-citation 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/uc3-dmp-citation/version.rb +1 -1
- data/lib/uc3-dmp-citation.rb +69 -66
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68d3163d0ad27c7912aac4c6cd55b1ac698a30eb5d6da8596ad456b3895878eb
|
4
|
+
data.tar.gz: 91b1daad359992246dffcedc8d05c061a0251df0893f49d2b3f35c061ec044d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5db74525a5425672a53c2b40091c93a98bd675fd28639ce8bb8b471c40018155fe670a5ddef0d82a658d672a8f581db33f0375e06308a8564561de92daa212a0
|
7
|
+
data.tar.gz: 34c7659bae12077d1691584a5d579c146b9632f48ecb691bd23dc389ac1eddb4ead4559c5cec807af6253b0751e9f25b58b4b0044561c49ef8137c5d73d9e09a
|
data/lib/uc3-dmp-citation.rb
CHANGED
@@ -20,84 +20,87 @@ 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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
+
headers = { Accept: 'application/x-bibtex' }
|
29
|
+
logger.debug(message: "Fetching BibTeX from: #{uri}") if logger.respond_to?(:debug)
|
30
|
+
resp = Uc3DmpExternalApi::Client.call(url: uri, method: :get, additional_headers: headers, logger: logger)
|
31
|
+
return nil if resp.nil? || resp.to_s.strip.empty?
|
32
|
+
|
33
|
+
bibtex = BibTeX.parse(_cleanse_bibtex(text: resp))
|
34
|
+
work_type = identifier['work_type'].nil? ? determine_work_type(bibtex: bibtex) : identifier['work_type']
|
35
|
+
_bibtex_to_citation(uri: uri, work_type: work_type, bibtex: bibtex)
|
36
|
+
end
|
35
37
|
|
36
|
-
|
38
|
+
private
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
# Will convert 'doi:10.1234/abcdefg' to 'http://doi.org/10.1234/abcdefg'
|
41
|
+
def _doi_to_uri(doi:)
|
42
|
+
val = doi.match(DOI_REGEX).to_s
|
43
|
+
return nil if val.nil? || val.strip == ''
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
+
doi.start_with?('http') ? doi : "#{DEFAULT_DOI_URL}/#{doi.gsub('doi:', '')}"
|
46
|
+
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
-
|
48
|
+
# If no :work_type was specified we can try to derive it from the BibTeX metadata
|
49
|
+
def _determine_work_type(bibtex:)
|
50
|
+
return '' if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
|
49
51
|
|
50
|
-
|
52
|
+
return 'article' unless bibtex.data.first.journal.nil?
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
+
''
|
55
|
+
end
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
57
|
+
def _cleanse_bibtex(text:)
|
58
|
+
return nil if text.nil? || text.to_s.strip == ''
|
59
|
+
|
60
|
+
# Make sure we're working with UTF8
|
61
|
+
utf8 = text.force_encoding('UTF-8')
|
62
|
+
|
63
|
+
# Remove any encoded HTML (e.g. "Regular text $\\lt$strong$\\gt$Bold text$\\lt$/strong$\\gt$")
|
64
|
+
utf8 = utf8.gsub(%r{\$?\\\$?(less|lt|Lt)\$/?[a-zA-Z]+\$?\\\$?(greater|gt|Gt)\$}, '')
|
65
|
+
# Replace any special dash, semicolon and quote characters with a minus sign or single/double quote
|
66
|
+
utf8 = utf8.gsub(%r{\$?\\(T|t)ext[a-zA-Z]+dash\$?}, '-').gsub(%r{\{(T|t)ext[a-zA-Z]+dash\}}, '-')
|
67
|
+
.gsub(%r{\$?\\(M|m)athsemicolon\$?}, ':').gsub(%r{\{(M|m)semicolon\}}, ':')
|
68
|
+
.gsub(%r{\$?\\(T|t)extquotesingle\$?}, "'").gsub(%r{\{(T|t)extquotesingle\}}, "'")
|
69
|
+
.gsub(%r{\$?\\(T|t)extquotedouble\$?}, '"').gsub(%r{\{(T|t)extquotedouble\}}, '"')
|
70
|
+
# Remove any remaining `\v` entries which attempt to construct an accented character
|
71
|
+
utf8.gsub(%r{\\v}, '')
|
72
|
+
end
|
71
73
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
74
|
+
# Convert the BibTeX item to a citation
|
75
|
+
def _bibtex_to_citation(uri:, work_type: DEFAULT_WORK_TYPE, bibtex:, style: DEFAULT_CITATION_STYLE)
|
76
|
+
return nil unless uri.is_a?(String) && uri.strip != ''
|
77
|
+
return nil if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
cp = CiteProc::Processor.new(style: style, format: 'html')
|
80
|
+
cp.import(bibtex.to_citeproc)
|
81
|
+
citation = cp.render(:bibliography, id: bibtex.data.first.id)
|
82
|
+
return nil unless citation.is_a?(Array) && citation.any?
|
81
83
|
|
82
|
-
|
83
|
-
|
84
|
-
|
84
|
+
# The CiteProc renderer has trouble with some things so fix them here
|
85
|
+
# - For some reason words in all caps in the title get wrapped in curl brackets
|
86
|
+
citation = citation.first.gsub('{', '').gsub('}', '')
|
85
87
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
unless work_type.nil? || work_type.strip == ''
|
89
|
+
# This supports the :apa and :chicago-author-date styles
|
90
|
+
citation = citation.gsub(/\.”\s+/, "\.” [#{work_type.gsub('_', ' ').capitalize}]. ")
|
91
|
+
.gsub(/<\/i>\.\s+/, "<\/i>\. [#{work_type.gsub('_', ' ').capitalize}]. ")
|
92
|
+
end
|
91
93
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
94
|
+
# Convert the URL into a link. Ensure that the trailing period is not a part of
|
95
|
+
# the link!
|
96
|
+
citation.gsub(URI.regexp) do |url|
|
97
|
+
if url.start_with?('http')
|
98
|
+
'<a href="%{url}" target="_blank">%{url}</a>.' % {
|
99
|
+
url: url.end_with?('.') ? uri : "#{uri}."
|
100
|
+
}
|
101
|
+
else
|
102
|
+
url
|
103
|
+
end
|
101
104
|
end
|
102
105
|
end
|
103
106
|
end
|